/[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.84 by wakaba, Thu Mar 6 15:23:17 2008 UTC revision 1.138 by wakaba, Sun May 18 04:15:48 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335      my $self = shift;
336      my $charset_name = shift;
337      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
338      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
339    } # parse_byte_string
340    
341    sub parse_byte_stream ($$$$;$) {
342    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
343    my $charset = shift;    my $charset_name = shift;
344    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;  
   }  
345    
346    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
347      my $self = shift;      my (%opt) = @_;
348      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
349      ## TODO: if $charset is supported    };
350      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
351    
352      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
353      require Message::Charset::Info;
354      ## Step 1        my $charset;
355      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
356        $charset = 'utf-8';    my ($char_stream, $e_status);
357    
358      SNIFFING: {
359    
360        ## Step 1
361        if (defined $charset_name) {
362          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
363    
364          ## ISSUE: Unsupported encoding is not ignored according to the spec.
365          ($char_stream, $e_status) = $charset->get_decode_handle
366              ($byte_stream, allow_error_reporting => 1,
367               allow_fallback => 1);
368          if ($char_stream) {
369            $self->{confident} = 1;
370            last SNIFFING;
371          } else {
372            ## TODO: unsupported error
373          }
374      }      }
375    
376      ## Step 2      ## Step 2
377      if (defined $self->{input_encoding} and      my $byte_buffer = '';
378          $self->{input_encoding} eq $charset) {      for (1..1024) {
379          my $char = $byte_stream->getc;
380          last unless defined $char;
381          $byte_buffer .= $char;
382        } ## TODO: timeout
383    
384        ## Step 3
385        if ($byte_buffer =~ /^\xFE\xFF/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390        $self->{confident} = 1;        $self->{confident} = 1;
391        return;        last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
400          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
401          ($char_stream, $e_status) = $charset->get_decode_handle
402              ($byte_stream, allow_error_reporting => 1,
403               allow_fallback => 1, byte_buffer => \$byte_buffer);
404          $self->{confident} = 1;
405          last SNIFFING;
406      }      }
407    
408      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
409          ':'.$charset, level => 'w');      ## TODO: <meta charset>
410    
411      ## Step 3      ## Step 5
412      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
413    
414      ## Step 4      ## Step 6
415      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
416        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
417            ($byte_buffer);
418        if (defined $charset_name) {
419          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
420    
421          ## ISSUE: Unsupported encoding is not ignored according to the spec.
422          require Whatpm::Charset::DecodeHandle;
423          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
424              ($byte_stream);
425          ($char_stream, $e_status) = $charset->get_decode_handle
426              ($buffer, allow_error_reporting => 1,
427               allow_fallback => 1, byte_buffer => \$byte_buffer);
428          if ($char_stream) {
429            $buffer->{buffer} = $byte_buffer;
430            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
431                            value => $charset_name,
432                            level => $self->{info_level},
433                            line => 1, column => 1);
434            $self->{confident} = 0;
435            last SNIFFING;
436          }
437        }
438    
439        ## Step 7: default
440        ## TODO: Make this configurable.
441        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
442            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
443            ## detectable in the step 6.
444        require Whatpm::Charset::DecodeHandle;
445        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
446            ($byte_stream);
447        ($char_stream, $e_status)
448            = $charset->get_decode_handle ($buffer,
449                                           allow_error_reporting => 1,
450                                           allow_fallback => 1,
451                                           byte_buffer => \$byte_buffer);
452        $buffer->{buffer} = $byte_buffer;
453        !!!parse-error (type => 'sniffing:default', ## TODO: type name
454                        value => 'windows-1252',
455                        level => $self->{info_level},
456                        line => 1, column => 1);
457        $self->{confident} = 0;
458      } # SNIFFING
459    
460      $self->{input_encoding} = $charset->get_iana_name;
461      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
462        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
463                        value => $self->{input_encoding},
464                        level => $self->{unsupported_level},
465                        line => 1, column => 1);
466      } elsif (not ($e_status &
467                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
468        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
469                        value => $self->{input_encoding},
470                        level => $self->{unsupported_level},
471                        line => 1, column => 1);
472      }
473    
474      $self->{change_encoding} = sub {
475        my $self = shift;
476        $charset_name = shift;
477        my $token = shift;
478    
479        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
480        ($char_stream, $e_status) = $charset->get_decode_handle
481            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
482             byte_buffer => \ $buffer->{buffer});
483        
484        if ($char_stream) { # if supported
485          ## "Change the encoding" algorithm:
486    
487          ## Step 1    
488          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
489            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
490            ($char_stream, $e_status) = $charset->get_decode_handle
491                ($byte_stream,
492                 byte_buffer => \ $buffer->{buffer});
493          }
494          $charset_name = $charset->get_iana_name;
495          
496          ## Step 2
497          if (defined $self->{input_encoding} and
498              $self->{input_encoding} eq $charset_name) {
499            !!!parse-error (type => 'charset label:matching', ## TODO: type
500                            value => $charset_name,
501                            level => $self->{info_level});
502            $self->{confident} = 1;
503            return;
504          }
505    
506          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
507              ':'.$charset_name, level => 'w', token => $token);
508          
509          ## Step 3
510          # if (can) {
511            ## change the encoding on the fly.
512            #$self->{confident} = 1;
513            #return;
514          # }
515          
516          ## Step 4
517          throw Whatpm::HTML::RestartParser ();
518        }
519    }; # $self->{change_encoding}    }; # $self->{change_encoding}
520    
521      my $char_onerror = sub {
522        my (undef, $type, %opt) = @_;
523        !!!parse-error (%opt, type => $type,
524                        line => $self->{line}, column => $self->{column} + 1);
525        if ($opt{octets}) {
526          ${$opt{octets}} = "\x{FFFD}"; # relacement character
527        }
528      };
529      $char_stream->onerror ($char_onerror);
530    
531    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
532    my $return;    my $return;
533    try {    try {
534      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
535    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
536      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
537      $s = \ (Encode::decode ($charset, $$bytes_s));      
538      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
539        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
540          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
541                          value => $self->{input_encoding},
542                          level => $self->{unsupported_level},
543                          line => 1, column => 1);
544        } elsif (not ($e_status &
545                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
546          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
547                          value => $self->{input_encoding},
548                          level => $self->{unsupported_level},
549                          line => 1, column => 1);
550        }
551      $self->{confident} = 1;      $self->{confident} = 1;
552      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
553        $return = $self->parse_char_stream ($char_stream, @args);
554    };    };
555    return $return;    return $return;
556  } # parse_byte_string  } # parse_byte_stream
557    
558  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
559  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 564  sub parse_byte_string ($$$$;$) {
564  ## 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
565  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
566    
567  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
568      my $self = shift;
569      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
570      return $self->parse_char_stream ($input, @_[1..$#_]);
571    } # parse_char_string
572    *parse_string = \&parse_char_string;
573    
574  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
575    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
576    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
577    $self->{document} = $_[1];    $self->{document} = $_[1];
578    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
579    
# Line 177  sub parse_string ($$$;$) { Line 584  sub parse_string ($$$;$) {
584        if defined $self->{input_encoding};        if defined $self->{input_encoding};
585    
586    my $i = 0;    my $i = 0;
587    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
588    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
589    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
590      my $self = shift;      my $self = shift;
591    
592      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
593      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
594    
595      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = $input->getc;
596      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
597      $column++;      $self->{next_char} = ord $char;
598    
599        ($self->{line_prev}, $self->{column_prev})
600            = ($self->{line}, $self->{column});
601        $self->{column}++;
602            
603      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
604        $line++;        !!!cp ('j1');
605        $column = 0;        $self->{line}++;
606          $self->{column} = 0;
607      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
608        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
609          my $next = $input->getc;
610          if ($next ne "\x0A") {
611            $input->ungetc ($next);
612          }
613        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
614        $line++;        $self->{line}++;
615        $column = 0;        $self->{column} = 0;
616      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
617          !!!cp ('j3');
618        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
619      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
620          !!!cp ('j4');
621        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
622        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
623        } elsif ($self->{next_char} <= 0x0008 or
624                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
625                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
626                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
627                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
628                 {
629                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
630                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
631                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
632                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
633                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
634                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
635                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
636                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
637                  0x10FFFE => 1, 0x10FFFF => 1,
638                 }->{$self->{next_char}}) {
639          !!!cp ('j5');
640          !!!parse-error (type => 'control char', level => $self->{must_level});
641    ## TODO: error type documentation
642      }      }
643    };    };
644    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 646  sub parse_string ($$$;$) {
646    
647    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
648      my (%opt) = @_;      my (%opt) = @_;
649      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
650        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
651        warn "Parse error ($opt{type}) at line $line column $column\n";
652    };    };
653    $self->{parse_error} = sub {    $self->{parse_error} = sub {
654      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
655    };    };
656    
657    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 659  sub parse_string ($$$;$) {
659    $self->_construct_tree;    $self->_construct_tree;
660    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
661    
662      delete $self->{parse_error}; # remove loop
663    
664    return $self->{document};    return $self->{document};
665  } # parse_string  } # parse_char_stream
666    
667  sub new ($) {  sub new ($) {
668    my $class = shift;    my $class = shift;
669    my $self = bless {}, $class;    my $self = bless {
670        must_level => 'm',
671        should_level => 's',
672        good_level => 'w',
673        warn_level => 'w',
674        info_level => 'i',
675        unsupported_level => 'u',
676      }, $class;
677    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
678      $self->{next_char} = -1;      $self->{next_char} = -1;
679    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 735  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
735  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
736  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
737  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
738    sub SELF_CLOSING_START_TAG_STATE () { 34 }
739    sub CDATA_BLOCK_STATE () { 35 }
740    
741  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
742  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 753  sub TABLE_IMS ()      { 0b1000000 }
753  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
754  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
755  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
756    sub SELECT_IMS ()     { 0b10000000000 }
757    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
758        ## NOTE: "in foreign content" insertion mode is special; it is combined
759        ## with the secondary insertion mode.  In this parser, they are stored
760        ## together in the bit-or'ed form.
761    
762  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
763    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 780  sub IN_TABLE_IM () { TABLE_IMS }
780  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
781  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
782  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
783  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
784    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
785  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
786    
787  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 794  sub _initialize_tokenizer ($) {
794    undef $self->{current_attribute};    undef $self->{current_attribute};
795    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
796    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
797      delete $self->{self_closing};
798    $self->{char} = [];    $self->{char} = [];
799    # $self->{next_char}    # $self->{next_char}
800    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 815  sub _initialize_tokenizer ($) {
815  ##        ->{value}  ##        ->{value}
816  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
817  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
818    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
819    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
820    ##     while the token is pushed back to the stack.
821    
822    ## ISSUE: "When a DOCTYPE token is created, its
823    ## <i>self-closing flag</i> must be unset (its other state is that it
824    ## be set), and its attributes list must be empty.": Wrong subject?
825    
826  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
827    
# Line 384  sub _initialize_tokenizer ($) { Line 848  sub _initialize_tokenizer ($) {
848    
849  sub _get_next_token ($) {  sub _get_next_token ($) {
850    my $self = shift;    my $self = shift;
851    
852      if ($self->{self_closing}) {
853        !!!parse-error (type => 'nestc', token => $self->{current_token});
854        ## NOTE: The |self_closing| flag is only set by start tag token.
855        ## In addition, when a start tag token is emitted, it is always set to
856        ## |current_token|.
857        delete $self->{self_closing};
858      }
859    
860    if (@{$self->{token}}) {    if (@{$self->{token}}) {
861        $self->{self_closing} = $self->{token}->[0]->{self_closing};
862      return shift @{$self->{token}};      return shift @{$self->{token}};
863    }    }
864    
# Line 447  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921          #          #
922        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
923          !!!cp (11);          !!!cp (11);
924          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
925                      line => $self->{line}, column => $self->{column}});
926          last A; ## TODO: ok?          last A; ## TODO: ok?
927        } else {        } else {
928          !!!cp (12);          !!!cp (12);
929        }        }
930        # Anything else        # Anything else
931        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
932                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
933                       line => $self->{line}, column => $self->{column},
934                      };
935        ## Stay in the data state        ## Stay in the data state
936        !!!next-input-character;        !!!next-input-character;
937    
# Line 463  sub _get_next_token ($) { Line 940  sub _get_next_token ($) {
940        redo A;        redo A;
941      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
942        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
943    
944          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
945                
946        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
947    
# Line 471  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950    
951        unless (defined $token) {        unless (defined $token) {
952          !!!cp (13);          !!!cp (13);
953          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
954                      line => $l, column => $c,
955                     });
956        } else {        } else {
957          !!!cp (14);          !!!cp (14);
958          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 971  sub _get_next_token ($) {
971            ## reconsume            ## reconsume
972            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
973    
974            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
975                        line => $self->{line_prev},
976                        column => $self->{column_prev},
977                       });
978    
979            redo A;            redo A;
980          }          }
# Line 510  sub _get_next_token ($) { Line 994  sub _get_next_token ($) {
994            !!!cp (19);            !!!cp (19);
995            $self->{current_token}            $self->{current_token}
996              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
997                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
998                   line => $self->{line_prev},
999                   column => $self->{column_prev}};
1000            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1001            !!!next-input-character;            !!!next-input-character;
1002            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1004  sub _get_next_token ($) {
1004                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1005            !!!cp (20);            !!!cp (20);
1006            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1007                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1008                                        line => $self->{line_prev},
1009                                        column => $self->{column_prev}};
1010            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1014            !!!cp (21);            !!!cp (21);
1015            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1016                              line => $self->{line_prev},
1017                              column => $self->{column_prev});
1018            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1019            !!!next-input-character;            !!!next-input-character;
1020    
1021            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1022                        line => $self->{line_prev},
1023                        column => $self->{column_prev},
1024                       });
1025    
1026            redo A;            redo A;
1027          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1028            !!!cp (22);            !!!cp (22);
1029            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1030                              line => $self->{line_prev},
1031                              column => $self->{column_prev});
1032            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1033              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1034                                        line => $self->{line_prev},
1035                                        column => $self->{column_prev},
1036                                       };
1037            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1038            redo A;            redo A;
1039          } else {          } else {
1040            !!!cp (23);            !!!cp (23);
1041            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1042                              line => $self->{line_prev},
1043                              column => $self->{column_prev});
1044            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1045            ## reconsume            ## reconsume
1046    
1047            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1048                        line => $self->{line_prev},
1049                        column => $self->{column_prev},
1050                       });
1051    
1052            redo A;            redo A;
1053          }          }
# Line 551  sub _get_next_token ($) { Line 1055  sub _get_next_token ($) {
1055          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1056        }        }
1057      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1058          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1059        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1060          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1061    
1062            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1063            my @next_char;            my @next_char;
1064            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 1075  sub _get_next_token ($) {
1075                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1076                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1077    
1078                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1079                            line => $l, column => $c,
1080                           });
1081        
1082                redo A;                redo A;
1083              }              }
# Line 588  sub _get_next_token ($) { Line 1096  sub _get_next_token ($) {
1096              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1097              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1098              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1099              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1100                          line => $l, column => $c,
1101                         });
1102              redo A;              redo A;
1103            } else {            } else {
1104              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111            !!!cp (28);            !!!cp (28);
1112            # next-input-character is already done            # next-input-character is already done
1113            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1114            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1115                        line => $l, column => $c,
1116                       });
1117            redo A;            redo A;
1118          }          }
1119        }        }
# Line 609  sub _get_next_token ($) { Line 1121  sub _get_next_token ($) {
1121        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1122            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1123          !!!cp (29);          !!!cp (29);
1124          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1125                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1126                   tag_name => chr ($self->{next_char} + 0x0020),
1127                   line => $l, column => $c};
1128          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1129          !!!next-input-character;          !!!next-input-character;
1130          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1132  sub _get_next_token ($) {
1132                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1133          !!!cp (30);          !!!cp (30);
1134          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1135                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1136                                      line => $l, column => $c};
1137          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1138          !!!next-input-character;          !!!next-input-character;
1139          redo A;          redo A;
1140        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1141          !!!cp (31);          !!!cp (31);
1142          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1143                            line => $self->{line_prev}, ## "<" in "</>"
1144                            column => $self->{column_prev} - 1);
1145          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1146          !!!next-input-character;          !!!next-input-character;
1147          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1152          # reconsume          # reconsume
1153    
1154          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1155                      line => $l, column => $c,
1156                     });
1157    
1158          redo A;          redo A;
1159        } else {        } else {
1160          !!!cp (33);          !!!cp (33);
1161          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1162          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1163            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1164                                      line => $self->{line_prev}, # "<" of "</"
1165                                      column => $self->{column_prev} - 1,
1166                                     };
1167          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1168          redo A;          redo A;
1169        }        }
# Line 657  sub _get_next_token ($) { Line 1180  sub _get_next_token ($) {
1180        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1181          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1182            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1183            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1184          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1185            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1211  sub _get_next_token ($) {
1211          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1212          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1213            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1214            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1215          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1216            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1231  sub _get_next_token ($) {
1231    
1232          redo A;          redo A;
1233        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1234            !!!cp (42);
1235            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1236          !!!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  
1237          redo A;          redo A;
1238        } else {        } else {
1239          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1256  sub _get_next_token ($) {
1256        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1257          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1258            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1259            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1260          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1261            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1277  sub _get_next_token ($) {
1277        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1278                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1279          !!!cp (49);          !!!cp (49);
1280          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1281                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1282                   value => '',
1283                   line => $self->{line}, column => $self->{column}};
1284          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1285          !!!next-input-character;          !!!next-input-character;
1286          redo A;          redo A;
1287        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1288            !!!cp (50);
1289            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1290          !!!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  
1291          redo A;          redo A;
1292        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1293          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1294          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1295            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1296            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1297          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1298            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          } else {          } else {
1323            !!!cp (56);            !!!cp (56);
1324          }          }
1325          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1326                                value => ''};              = {name => chr ($self->{next_char}),
1327                   value => '',
1328                   line => $self->{line}, column => $self->{column}};
1329          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1330          !!!next-input-character;          !!!next-input-character;
1331          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1335  sub _get_next_token ($) {
1335          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1336              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1337            !!!cp (57);            !!!cp (57);
1338            !!!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});
1339            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1340          } else {          } else {
1341            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1364  sub _get_next_token ($) {
1364          $before_leave->();          $before_leave->();
1365          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1366            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1367            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1368          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1369            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1388  sub _get_next_token ($) {
1388          !!!next-input-character;          !!!next-input-character;
1389          redo A;          redo A;
1390        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1391            !!!cp (64);
1392          $before_leave->();          $before_leave->();
1393            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1394          !!!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  
1395          redo A;          redo A;
1396        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1397          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1448  sub _get_next_token ($) {
1448        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1449          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1470  sub _get_next_token ($) {
1470        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1471                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1472          !!!cp (76);          !!!cp (76);
1473          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1474                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1475                   value => '',
1476                   line => $self->{line}, column => $self->{column}};
1477          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1478          !!!next-input-character;          !!!next-input-character;
1479          redo A;          redo A;
1480        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1481            !!!cp (77);
1482            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1483          !!!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  
1484          redo A;          redo A;
1485        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1486          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1487          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1488            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1489            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1490          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1491            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1507  sub _get_next_token ($) {
1507          redo A;          redo A;
1508        } else {        } else {
1509          !!!cp (82);          !!!cp (82);
1510          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1511                                value => ''};              = {name => chr ($self->{next_char}),
1512                   value => '',
1513                   line => $self->{line}, column => $self->{column}};
1514          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1515          !!!next-input-character;          !!!next-input-character;
1516          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1543  sub _get_next_token ($) {
1543        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1544          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1545            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1546            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1547          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1548            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1567          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1613  sub _get_next_token ($) {
1613          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1614          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1655  sub _get_next_token ($) {
1655          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1656          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1657            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1658            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1659          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1660            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1700  sub _get_next_token ($) {
1700        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1701          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1702            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1703            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1704          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1705            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1723  sub _get_next_token ($) {
1723          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1724          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1725            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1726            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1727          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1728            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1793  sub _get_next_token ($) {
1793        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1794          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1813  sub _get_next_token ($) {
1813    
1814          redo A;          redo A;
1815        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1816            !!!cp (122);
1817            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1818          !!!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 (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1819          redo A;          redo A;
1820        } else {        } else {
1821          !!!cp (124);          !!!cp ('124.1');
1822          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1823          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1824          ## reconsume          ## reconsume
1825          redo A;          redo A;
1826        }        }
1827        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1828          if ($self->{next_char} == 0x003E) { # >
1829            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1830              !!!cp ('124.2');
1831              !!!parse-error (type => 'nestc', token => $self->{current_token});
1832              ## TODO: Different type than slash in start tag
1833              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1834              if ($self->{current_token}->{attributes}) {
1835                !!!cp ('124.4');
1836                !!!parse-error (type => 'end tag attribute');
1837              } else {
1838                !!!cp ('124.5');
1839              }
1840              ## TODO: Test |<title></title/>|
1841            } else {
1842              !!!cp ('124.3');
1843              $self->{self_closing} = 1;
1844            }
1845    
1846            $self->{state} = DATA_STATE;
1847            !!!next-input-character;
1848    
1849            !!!emit ($self->{current_token}); # start tag or end tag
1850    
1851            redo A;
1852          } else {
1853            !!!cp ('124.4');
1854            !!!parse-error (type => 'nestc');
1855            ## TODO: This error type is wrong.
1856            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1857            ## Reconsume.
1858            redo A;
1859          }
1860      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1861        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1862                
1863        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1864          #my $token = {type => COMMENT_TOKEN, data => ''};
1865    
1866        BC: {        BC: {
1867          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1869  sub _get_next_token ($) {
1869            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1870            !!!next-input-character;            !!!next-input-character;
1871    
1872            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1873    
1874            redo A;            redo A;
1875          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1877  sub _get_next_token ($) {
1877            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1878            ## reconsume            ## reconsume
1879    
1880            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1881    
1882            redo A;            redo A;
1883          } else {          } else {
1884            !!!cp (126);            !!!cp (126);
1885            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1886            !!!next-input-character;            !!!next-input-character;
1887            redo BC;            redo BC;
1888          }          }
# Line 1408  sub _get_next_token ($) { Line 1892  sub _get_next_token ($) {
1892      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1893        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1894    
1895          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1896    
1897        my @next_char;        my @next_char;
1898        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1899                
# Line 1416  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1903          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1904            !!!cp (127);            !!!cp (127);
1905            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1906                                        line => $l, column => $c,
1907                                       };
1908            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1909            !!!next-input-character;            !!!next-input-character;
1910            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1940  sub _get_next_token ($) {
1940                      !!!cp (129);                      !!!cp (129);
1941                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1942                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1943                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1944                                                  quirks => 1,
1945                                                  line => $l, column => $c,
1946                                                 };
1947                      !!!next-input-character;                      !!!next-input-character;
1948                      redo A;                      redo A;
1949                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1964  sub _get_next_token ($) {
1964          } else {          } else {
1965            !!!cp (135);            !!!cp (135);
1966          }          }
1967          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1968                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1969                   $self->{next_char} == 0x005B) { # [
1970            !!!next-input-character;
1971            push @next_char, $self->{next_char};
1972            if ($self->{next_char} == 0x0043) { # C
1973              !!!next-input-character;
1974              push @next_char, $self->{next_char};
1975              if ($self->{next_char} == 0x0044) { # D
1976                !!!next-input-character;
1977                push @next_char, $self->{next_char};
1978                if ($self->{next_char} == 0x0041) { # A
1979                  !!!next-input-character;
1980                  push @next_char, $self->{next_char};
1981                  if ($self->{next_char} == 0x0054) { # T
1982                    !!!next-input-character;
1983                    push @next_char, $self->{next_char};
1984                    if ($self->{next_char} == 0x0041) { # A
1985                      !!!next-input-character;
1986                      push @next_char, $self->{next_char};
1987                      if ($self->{next_char} == 0x005B) { # [
1988                        !!!cp (135.1);
1989                        $self->{state} = CDATA_BLOCK_STATE;
1990                        !!!next-input-character;
1991                        redo A;
1992                      } else {
1993                        !!!cp (135.2);
1994                      }
1995                    } else {
1996                      !!!cp (135.3);
1997                    }
1998                  } else {
1999                    !!!cp (135.4);                
2000                  }
2001                } else {
2002                  !!!cp (135.5);
2003                }
2004              } else {
2005                !!!cp (135.6);
2006              }
2007            } else {
2008              !!!cp (135.7);
2009            }
2010        } else {        } else {
2011          !!!cp (136);          !!!cp (136);
2012        }        }
# Line 1480  sub _get_next_token ($) { Line 2015  sub _get_next_token ($) {
2015        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2016        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2017        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2018          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2019                                    line => $l, column => $c,
2020                                   };
2021        redo A;        redo A;
2022                
2023        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2141  sub _get_next_token ($) {
2141          redo A;          redo A;
2142        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2143          !!!cp (152);          !!!cp (152);
2144          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2145                            line => $self->{line_prev},
2146                            column => $self->{column_prev});
2147          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2148          ## Stay in the state          ## Stay in the state
2149          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2159  sub _get_next_token ($) {
2159          redo A;          redo A;
2160        } else {        } else {
2161          !!!cp (154);          !!!cp (154);
2162          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2163                            line => $self->{line_prev},
2164                            column => $self->{column_prev});
2165          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2166          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2167          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2200  sub _get_next_token ($) {
2200          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2201          !!!next-input-character;          !!!next-input-character;
2202    
2203          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2204    
2205          redo A;          redo A;
2206        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2209  sub _get_next_token ($) {
2209          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2210          ## reconsume          ## reconsume
2211    
2212          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2213    
2214          redo A;          redo A;
2215        } else {        } else {
2216          !!!cp (160);          !!!cp (160);
2217          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2218              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2219  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2220          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2221          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2731  sub _get_next_token ($) {
2731          !!!next-input-character;          !!!next-input-character;
2732          redo A;          redo A;
2733        }        }
2734        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2735          my $s = '';
2736          
2737          my ($l, $c) = ($self->{line}, $self->{column});
2738    
2739          CS: while ($self->{next_char} != -1) {
2740            if ($self->{next_char} == 0x005D) { # ]
2741              !!!next-input-character;
2742              if ($self->{next_char} == 0x005D) { # ]
2743                !!!next-input-character;
2744                MDC: {
2745                  if ($self->{next_char} == 0x003E) { # >
2746                    !!!cp (221.1);
2747                    !!!next-input-character;
2748                    last CS;
2749                  } elsif ($self->{next_char} == 0x005D) { # ]
2750                    !!!cp (221.2);
2751                    $s .= ']';
2752                    !!!next-input-character;
2753                    redo MDC;
2754                  } else {
2755                    !!!cp (221.3);
2756                    $s .= ']]';
2757                    #
2758                  }
2759                } # MDC
2760              } else {
2761                !!!cp (221.4);
2762                $s .= ']';
2763                #
2764              }
2765            } else {
2766              !!!cp (221.5);
2767              #
2768            }
2769            $s .= chr $self->{next_char};
2770            !!!next-input-character;
2771          } # CS
2772    
2773          $self->{state} = DATA_STATE;
2774          ## next-input-character done or EOF, which is reconsumed.
2775    
2776          if (length $s) {
2777            !!!cp (221.6);
2778            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2779                      line => $l, column => $c});
2780          } else {
2781            !!!cp (221.7);
2782          }
2783    
2784          redo A;
2785    
2786          ## ISSUE: "text tokens" in spec.
2787          ## TODO: Streaming support
2788      } else {      } else {
2789        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2790      }      }
# Line 2203  sub _get_next_token ($) { Line 2796  sub _get_next_token ($) {
2796  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2797    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2798    
2799      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2800    
2801    if ({    if ({
2802         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2803         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2838  sub _tokenize_attempt_to_consume_an_enti
2838            redo X;            redo X;
2839          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2840            !!!cp (1005);            !!!cp (1005);
2841            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2842            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2843            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2844            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2847  sub _tokenize_attempt_to_consume_an_enti
2847            !!!next-input-character;            !!!next-input-character;
2848          } else {          } else {
2849            !!!cp (1007);            !!!cp (1007);
2850            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2851          }          }
2852    
2853          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2854            !!!cp (1008);            !!!cp (1008);
2855            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2856            $code = 0xFFFD;            $code = 0xFFFD;
2857          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2858            !!!cp (1009);            !!!cp (1009);
2859            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2860            $code = 0xFFFD;            $code = 0xFFFD;
2861          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2862            !!!cp (1010);            !!!cp (1010);
2863            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2864            $code = 0x000A;            $code = 0x000A;
2865          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2866            !!!cp (1011);            !!!cp (1011);
2867            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2868            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2869          }          }
2870    
2871          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2872                  has_reference => 1};                  has_reference => 1,
2873                    line => $l, column => $c,
2874                   };
2875        } # X        } # X
2876      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2877               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2892  sub _tokenize_attempt_to_consume_an_enti
2892          !!!next-input-character;          !!!next-input-character;
2893        } else {        } else {
2894          !!!cp (1014);          !!!cp (1014);
2895          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2896        }        }
2897    
2898        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2899          !!!cp (1015);          !!!cp (1015);
2900          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2901          $code = 0xFFFD;          $code = 0xFFFD;
2902        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2903          !!!cp (1016);          !!!cp (1016);
2904          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2905          $code = 0xFFFD;          $code = 0xFFFD;
2906        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2907          !!!cp (1017);          !!!cp (1017);
2908          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2909          $code = 0x000A;          $code = 0x000A;
2910        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2911          !!!cp (1018);          !!!cp (1018);
2912          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2913          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2914        }        }
2915                
2916        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2917                  line => $l, column => $c,
2918                 };
2919      } else {      } else {
2920        !!!cp (1019);        !!!cp (1019);
2921        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2922        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2923        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2924        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2935  sub _tokenize_attempt_to_consume_an_enti
2935      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2936      our $EntityChar;      our $EntityChar;
2937    
2938      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2939             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2940             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2941               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2968  sub _tokenize_attempt_to_consume_an_enti
2968            
2969      if ($match > 0) {      if ($match > 0) {
2970        !!!cp (1023);        !!!cp (1023);
2971        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2972                  line => $l, column => $c,
2973                 };
2974      } elsif ($match < 0) {      } elsif ($match < 0) {
2975        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2976        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2977          !!!cp (1024);          !!!cp (1024);
2978          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2979                    line => $l, column => $c,
2980                   };
2981        } else {        } else {
2982          !!!cp (1025);          !!!cp (1025);
2983          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2984                    line => $l, column => $c,
2985                   };
2986        }        }
2987      } else {      } else {
2988        !!!cp (1026);        !!!cp (1026);
2989        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2990        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2991        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2992                  line => $l, column => $c,
2993                 };
2994      }      }
2995    } else {    } else {
2996      !!!cp (1027);      !!!cp (1027);
2997      ## no characters are consumed      ## no characters are consumed
2998      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2999      return undef;      return undef;
3000    }    }
3001  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 3066  sub _tree_construction_initial ($) {
3066            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3067            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3068          !!!cp ('t1');          !!!cp ('t1');
3069          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3070        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3071          !!!cp ('t2');          !!!cp ('t2');
3072          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3073          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3074        } else {        } else {
3075          !!!cp ('t3');          !!!cp ('t3');
3076        }        }
3077                
3078        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3079          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3080          ## NOTE: Default value for both |public_id| and |system_id| attributes
3081          ## are empty strings, so that we don't set any value in missing cases.
3082        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3083            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3084        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3211  sub _tree_construction_initial ($) {
3211                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3212               }->{$token->{type}}) {               }->{$token->{type}}) {
3213        !!!cp ('t14');        !!!cp ('t14');
3214        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3215        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3216        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3217        ## reprocess        ## reprocess
3218          !!!ack-later;
3219        return;        return;
3220      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3221        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3233  sub _tree_construction_initial ($) {
3233          !!!cp ('t17');          !!!cp ('t17');
3234        }        }
3235    
3236        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3237        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3238        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3239        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3262  sub _tree_construction_root_element ($)
3262    B: {    B: {
3263        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3264          !!!cp ('t19');          !!!cp ('t19');
3265          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3266          ## Ignore the token          ## Ignore the token
3267          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3268          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3296  sub _tree_construction_root_element ($)
3296        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3297          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3298            my $root_element;            my $root_element;
3299            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3300            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3301            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3302                  [$root_element, $el_category->{html}];
3303    
3304            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3305              !!!cp ('t24');              !!!cp ('t24');
3306              $self->{application_cache_selection}              $self->{application_cache_selection}
3307                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3308              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3309                ## According to Hixie (#whatwg 2008-03-19), it should be
3310                ## resolved against the base URI of the document in HTML
3311                ## or xml:base of the element in XHTML.
3312            } else {            } else {
3313              !!!cp ('t25');              !!!cp ('t25');
3314              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3315            }            }
3316    
3317              !!!nack ('t25c');
3318    
3319            !!!next-token;            !!!next-token;
3320            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3321          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3332  sub _tree_construction_root_element ($)
3332          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3333        }        }
3334    
3335      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3336        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3337      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3338      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3339    
3340      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3341    
3342      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3343        !!!ack-later;
3344      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3345    
3346      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3361  sub _reset_insertion_mode ($) {
3361            
3362      ## Step 3      ## Step 3
3363      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3364        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3365          $last = 1;          $last = 1;
3366          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3367            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3368              !!!cp ('t27');              !!!cp ('t27');
3369              #              #
3370            } else {            } else {
# Line 2762  sub _reset_insertion_mode ($) { Line 3374  sub _reset_insertion_mode ($) {
3374          }          }
3375        }        }
3376            
3377        ## Step 4..13      ## Step 4..14
3378        my $new_mode = {      my $new_mode;
3379        if ($node->[1] & FOREIGN_EL) {
3380          ## NOTE: Strictly spaking, the line below only applies to MathML and
3381          ## SVG elements.  Currently the HTML syntax supports only MathML and
3382          ## SVG elements as foreigners.
3383          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3384          ## ISSUE: What is set as the secondary insertion mode?
3385        } else {
3386          $new_mode = {
3387                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3388                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3389                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2779  sub _reset_insertion_mode ($) { Line 3399  sub _reset_insertion_mode ($) {
3399                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3400                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3401                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3402                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3403        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3404        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3405                
3406        ## Step 14        ## Step 15
3407        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3408          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3409            !!!cp ('t29');            !!!cp ('t29');
3410            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3418  sub _reset_insertion_mode ($) {
3418          !!!cp ('t31');          !!!cp ('t31');
3419        }        }
3420                
3421        ## Step 15        ## Step 16
3422        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3423                
3424        ## Step 16        ## Step 17
3425        $i--;        $i--;
3426        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3427                
3428        ## Step 17        ## Step 18
3429        redo S3;        redo S3;
3430      } # S3      } # S3
3431    
# Line 2908  sub _tree_construction_main ($) { Line 3529  sub _tree_construction_main ($) {
3529      !!!cp ('t39');      !!!cp ('t39');
3530    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3531    
3532    my $parse_rcdata = sub ($$) {    my $insert;
3533      my ($content_model_flag, $insert) = @_;  
3534      my $parse_rcdata = sub ($) {
3535        my ($content_model_flag) = @_;
3536    
3537      ## Step 1      ## Step 1
3538      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3539      my $el;      my $el;
3540      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3541    
3542      ## Step 2      ## Step 2
3543      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3544    
3545      ## Step 3      ## Step 3
3546      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3548  sub _tree_construction_main ($) {
3548    
3549      ## Step 4      ## Step 4
3550      my $text = '';      my $text = '';
3551        !!!nack ('t40.1');
3552      !!!next-token;      !!!next-token;
3553      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3554        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3571  sub _tree_construction_main ($) {
3571          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3572        !!!cp ('t42');        !!!cp ('t42');
3573        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3574      } else {      } else {
3575        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3576          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3577            !!!cp ('t43');
3578            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3579          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3580            !!!cp ('t44');
3581            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3582          } else {
3583            die "$0: $content_model_flag in parse_rcdata";
3584          }
3585      }      }
3586      !!!next-token;      !!!next-token;
3587    }; # $parse_rcdata    }; # $parse_rcdata
3588    
3589    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3590      my $script_el;      my $script_el;
3591      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3592      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3593    
3594      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3595      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3596            
3597      my $text = '';      my $text = '';
3598        !!!nack ('t45.1');
3599      !!!next-token;      !!!next-token;
3600      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3601        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3615  sub _tree_construction_main ($) {
3615        ## Ignore the token        ## Ignore the token
3616      } else {      } else {
3617        !!!cp ('t48');        !!!cp ('t48');
3618        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3619        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3620        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3621      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3638  sub _tree_construction_main ($) {
3638      !!!next-token;      !!!next-token;
3639    }; # $script_start_tag    }; # $script_start_tag
3640    
3641      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3642      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3643      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3644    
3645    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3646      my $tag_name = shift;      my $end_tag_token = shift;
3647        my $tag_name = $end_tag_token->{tag_name};
3648    
3649        ## NOTE: The adoption agency algorithm (AAA).
3650    
3651      FET: {      FET: {
3652        ## Step 1        ## Step 1
3653        my $formatting_element;        my $formatting_element;
3654        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3655        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3656          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3657              !!!cp ('t52');
3658              last AFE;
3659            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3660                         eq $tag_name) {
3661            !!!cp ('t51');            !!!cp ('t51');
3662            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3663            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3664            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3665          }          }
3666        } # AFE        } # AFE
3667        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3668          !!!cp ('t53');          !!!cp ('t53');
3669          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3670          ## Ignore the token          ## Ignore the token
3671          !!!next-token;          !!!next-token;
3672          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3683  sub _tree_construction_main ($) {
3683              last INSCOPE;              last INSCOPE;
3684            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3685              !!!cp ('t55');              !!!cp ('t55');
3686              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3687                                token => $end_tag_token);
3688              ## Ignore the token              ## Ignore the token
3689              !!!next-token;              !!!next-token;
3690              return;              return;
3691            }            }
3692          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3693            !!!cp ('t56');            !!!cp ('t56');
3694            $in_scope = 0;            $in_scope = 0;
3695          }          }
3696        } # INSCOPE        } # INSCOPE
3697        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3698          !!!cp ('t57');          !!!cp ('t57');
3699          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3700                            token => $end_tag_token);
3701          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3702          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3703          return;          return;
3704        }        }
3705        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3706          !!!cp ('t58');          !!!cp ('t58');
3707          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3708                            value => $self->{open_elements}->[-1]->[0]
3709                                ->manakai_local_name,
3710                            token => $end_tag_token);
3711        }        }
3712                
3713        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3715  sub _tree_construction_main ($) {
3715        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3716        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3717          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3718          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3719              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3720              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3721               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3722            !!!cp ('t59');            !!!cp ('t59');
3723            $furthest_block = $node;            $furthest_block = $node;
3724            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3804  sub _tree_construction_main ($) {
3804        } # S7          } # S7  
3805                
3806        ## Step 8        ## Step 8
3807        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3808            my $foster_parent_element;
3809            my $next_sibling;
3810            OE: for (reverse 0..$#{$self->{open_elements}}) {
3811              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3812                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3813                                 if (defined $parent and $parent->node_type == 1) {
3814                                   !!!cp ('t65.1');
3815                                   $foster_parent_element = $parent;
3816                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3817                                 } else {
3818                                   !!!cp ('t65.2');
3819                                   $foster_parent_element
3820                                     = $self->{open_elements}->[$_ - 1]->[0];
3821                                 }
3822                                 last OE;
3823                               }
3824                             } # OE
3825                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3826                               unless defined $foster_parent_element;
3827            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3828            $open_tables->[-1]->[1] = 1; # tainted
3829          } else {
3830            !!!cp ('t65.3');
3831            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3832          }
3833                
3834        ## Step 9        ## Step 9
3835        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875      } # FET      } # FET
3876    }; # $formatting_end_tag    }; # $formatting_end_tag
3877    
3878    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3879      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3880    }; # $insert_to_current    }; # $insert_to_current
3881    
3882    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3883                         my $child = shift;      my $child = shift;
3884                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3885                              table => 1, tbody => 1, tfoot => 1,        # MUST
3886                              thead => 1, tr => 1,        my $foster_parent_element;
3887                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3888                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3889                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3890                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3891                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3892                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3904  sub _tree_construction_main ($) {
3904                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3905                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3906                             ($child, $next_sibling);                             ($child, $next_sibling);
3907                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3908                           !!!cp ('t72');      } else {
3909                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3910                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3911        }
3912    }; # $insert_to_foster    }; # $insert_to_foster
3913    
3914    my $insert;    B: while (1) {
   
   B: {  
3915      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3916        !!!cp ('t73');        !!!cp ('t73');
3917        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3918        ## Ignore the token        ## Ignore the token
3919        ## Stay in the phase        ## Stay in the phase
3920        !!!next-token;        !!!next-token;
3921        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3922      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3923               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3924        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3925          !!!cp ('t79');          !!!cp ('t79');
3926          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3927          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3928        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3929          !!!cp ('t80');          !!!cp ('t80');
3930          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3931          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3932        } else {        } else {
3933          !!!cp ('t81');          !!!cp ('t81');
3934        }        }
3935    
3936        !!!cp ('t82');        !!!cp ('t82');
3937        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3938        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3939        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3940          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3321  sub _tree_construction_main ($) { Line 3944  sub _tree_construction_main ($) {
3944               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3945          }          }
3946        }        }
3947          !!!nack ('t84.1');
3948        !!!next-token;        !!!next-token;
3949        redo B;        next B;
3950      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3951        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3952        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3336  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3961        }        }
3962        !!!next-token;        !!!next-token;
3963        redo B;        next B;
3964      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3965          if ($token->{type} == CHARACTER_TOKEN) {
3966            !!!cp ('t87.1');
3967            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3968            !!!next-token;
3969            next B;
3970          } elsif ($token->{type} == START_TAG_TOKEN) {
3971            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3972                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3973                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3974                ($token->{tag_name} eq 'svg' and
3975                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3976              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3977              !!!cp ('t87.2');
3978              #
3979            } elsif ({
3980                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3981                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3982                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3983                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3984                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3985                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3986                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3987                      var => 1,
3988                     }->{$token->{tag_name}}) {
3989              !!!cp ('t87.2');
3990              !!!parse-error (type => 'not closed',
3991                              value => $self->{open_elements}->[-1]->[0]
3992                                  ->manakai_local_name,
3993                              token => $token);
3994    
3995              pop @{$self->{open_elements}}
3996                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3997    
3998              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3999              ## Reprocess.
4000              next B;
4001            } else {
4002              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4003              my $tag_name = $token->{tag_name};
4004              if ($nsuri eq $SVG_NS) {
4005                $tag_name = {
4006                   altglyph => 'altGlyph',
4007                   altglyphdef => 'altGlyphDef',
4008                   altglyphitem => 'altGlyphItem',
4009                   animatecolor => 'animateColor',
4010                   animatemotion => 'animateMotion',
4011                   animatetransform => 'animateTransform',
4012                   clippath => 'clipPath',
4013                   feblend => 'feBlend',
4014                   fecolormatrix => 'feColorMatrix',
4015                   fecomponenttransfer => 'feComponentTransfer',
4016                   fecomposite => 'feComposite',
4017                   feconvolvematrix => 'feConvolveMatrix',
4018                   fediffuselighting => 'feDiffuseLighting',
4019                   fedisplacementmap => 'feDisplacementMap',
4020                   fedistantlight => 'feDistantLight',
4021                   feflood => 'feFlood',
4022                   fefunca => 'feFuncA',
4023                   fefuncb => 'feFuncB',
4024                   fefuncg => 'feFuncG',
4025                   fefuncr => 'feFuncR',
4026                   fegaussianblur => 'feGaussianBlur',
4027                   feimage => 'feImage',
4028                   femerge => 'feMerge',
4029                   femergenode => 'feMergeNode',
4030                   femorphology => 'feMorphology',
4031                   feoffset => 'feOffset',
4032                   fepointlight => 'fePointLight',
4033                   fespecularlighting => 'feSpecularLighting',
4034                   fespotlight => 'feSpotLight',
4035                   fetile => 'feTile',
4036                   feturbulence => 'feTurbulence',
4037                   foreignobject => 'foreignObject',
4038                   glyphref => 'glyphRef',
4039                   lineargradient => 'linearGradient',
4040                   radialgradient => 'radialGradient',
4041                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4042                   textpath => 'textPath',  
4043                }->{$tag_name} || $tag_name;
4044              }
4045    
4046              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4047    
4048              ## "adjust foreign attributes" - done in insert-element-f
4049    
4050              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4051    
4052              if ($self->{self_closing}) {
4053                pop @{$self->{open_elements}};
4054                !!!ack ('t87.3');
4055              } else {
4056                !!!cp ('t87.4');
4057              }
4058    
4059              !!!next-token;
4060              next B;
4061            }
4062          } elsif ($token->{type} == END_TAG_TOKEN) {
4063            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4064            !!!cp ('t87.5');
4065            #
4066          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4067            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4068            !!!cp ('t87.6');
4069            #
4070            ## TODO: ...
4071          } else {
4072            die "$0: $token->{type}: Unknown token type";        
4073          }
4074        }
4075    
4076        if ($self->{insertion_mode} & HEAD_IMS) {
4077        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4078          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4079            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4080                !!!cp ('t88.2');
4081                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4082              } else {
4083                !!!cp ('t88.1');
4084                ## Ignore the token.
4085                !!!next-token;
4086                next B;
4087              }
4088            unless (length $token->{data}) {            unless (length $token->{data}) {
4089              !!!cp ('t88');              !!!cp ('t88');
4090              !!!next-token;              !!!next-token;
4091              redo B;              next B;
4092            }            }
4093          }          }
4094    
4095          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4096            !!!cp ('t89');            !!!cp ('t89');
4097            ## As if <head>            ## As if <head>
4098            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4099            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4100            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4101                  [$self->{head_element}, $el_category->{head}];
4102    
4103            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4104            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3363  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108            !!!cp ('t90');            !!!cp ('t90');
4109            ## As if </noscript>            ## As if </noscript>
4110            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4111            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4112                        
4113            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4114            ## As if </head>            ## As if </head>
# Line 3379  sub _tree_construction_main ($) { Line 4124  sub _tree_construction_main ($) {
4124            !!!cp ('t92');            !!!cp ('t92');
4125          }          }
4126    
4127              ## "after head" insertion mode          ## "after head" insertion mode
4128              ## As if <body>          ## As if <body>
4129              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4130              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4131              ## reprocess          ## reprocess
4132              redo B;          next B;
4133            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4134              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4135                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4136                  !!!cp ('t93');              !!!cp ('t93');
4137                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4138                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4139                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4140                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4141                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4142                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4143                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4144                  !!!cp ('t94');              !!!next-token;
4145                  #              next B;
4146                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4147                  !!!cp ('t95');              !!!cp ('t94');
4148                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
4149                  ## Ignore the token            } else {
4150                  !!!next-token;              !!!cp ('t95');
4151                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4152                }              ## Ignore the token
4153              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4154                !!!cp ('t96');              !!!next-token;
4155                ## As if <head>              next B;
4156                !!!create-element ($self->{head_element}, 'head');            }
4157                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4158                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4159              ## As if <head>
4160              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4161              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4162              push @{$self->{open_elements}},
4163                  [$self->{head_element}, $el_category->{head}];
4164    
4165                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4166                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4167              } else {          } else {
4168                !!!cp ('t97');            !!!cp ('t97');
4169              }          }
4170    
4171              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4172                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4173                  !!!cp ('t98');                  !!!cp ('t98');
4174                  ## As if </noscript>                  ## As if </noscript>
4175                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4176                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4177                                
4178                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4179                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3434  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4185                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4186                  !!!cp ('t100');                  !!!cp ('t100');
4187                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4188                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4189                        [$self->{head_element}, $el_category->{head}];
4190                } else {                } else {
4191                  !!!cp ('t101');                  !!!cp ('t101');
4192                }                }
4193                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4194                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4195                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4196                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4197                  !!!nack ('t101.1');
4198                !!!next-token;                !!!next-token;
4199                redo B;                next B;
4200              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4201                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4202                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4203                  !!!cp ('t102');                  !!!cp ('t102');
4204                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4205                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4206                        [$self->{head_element}, $el_category->{head}];
4207                } else {                } else {
4208                  !!!cp ('t103');                  !!!cp ('t103');
4209                }                }
4210                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4211                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4212                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4213                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4214                  !!!ack ('t103.1');
4215                !!!next-token;                !!!next-token;
4216                redo B;                next B;
4217              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4218                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4219                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4220                  !!!cp ('t104');                  !!!cp ('t104');
4221                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4222                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4223                        [$self->{head_element}, $el_category->{head}];
4224                } else {                } else {
4225                  !!!cp ('t105');                  !!!cp ('t105');
4226                }                }
4227                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4228                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.
4229    
4230                unless ($self->{confident}) {                unless ($self->{confident}) {
4231                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4232                    !!!cp ('t106');                    !!!cp ('t106');
4233                      ## NOTE: Whether the encoding is supported or not is handled
4234                      ## in the {change_encoding} callback.
4235                    $self->{change_encoding}                    $self->{change_encoding}
4236                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4237                             $token);
4238                                        
4239                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4240                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4241                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4242                                                 ->{has_reference});                                                 ->{has_reference});
4243                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4244                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4245                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4246                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4247                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4248                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4249                      !!!cp ('t107');                      !!!cp ('t107');
4250                        ## NOTE: Whether the encoding is supported or not is handled
4251                        ## in the {change_encoding} callback.
4252                      $self->{change_encoding}                      $self->{change_encoding}
4253                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4254                               $token);
4255                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4256                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4257                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3517  sub _tree_construction_main ($) { Line 4277  sub _tree_construction_main ($) {
4277                  }                  }
4278                }                }
4279    
4280                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4281                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4282                  !!!ack ('t110.1');
4283                !!!next-token;                !!!next-token;
4284                redo B;                next B;
4285              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4286                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4287                  !!!cp ('t111');                  !!!cp ('t111');
4288                  ## As if </noscript>                  ## As if </noscript>
4289                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4290                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4291                                
4292                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4293                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4294                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4295                  !!!cp ('t112');                  !!!cp ('t112');
4296                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4297                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4298                        [$self->{head_element}, $el_category->{head}];
4299                } else {                } else {
4300                  !!!cp ('t113');                  !!!cp ('t113');
4301                }                }
# Line 3541  sub _tree_construction_main ($) { Line 4303  sub _tree_construction_main ($) {
4303                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4304                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4305                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4306                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4307                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4308                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4309                redo B;                next B;
4310              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4311                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4312                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4313                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4314                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4315                  !!!cp ('t114');                  !!!cp ('t114');
4316                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4317                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4318                        [$self->{head_element}, $el_category->{head}];
4319                } else {                } else {
4320                  !!!cp ('t115');                  !!!cp ('t115');
4321                }                }
4322                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4323                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4324                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4325                redo B;                next B;
4326              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4327                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4328                  !!!cp ('t116');                  !!!cp ('t116');
4329                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4330                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4331                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4332                    !!!nack ('t116.1');
4333                  !!!next-token;                  !!!next-token;
4334                  redo B;                  next B;
4335                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4336                  !!!cp ('t117');                  !!!cp ('t117');
4337                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4338                  ## Ignore the token                  ## Ignore the token
4339                    !!!nack ('t117.1');
4340                  !!!next-token;                  !!!next-token;
4341                  redo B;                  next B;
4342                } else {                } else {
4343                  !!!cp ('t118');                  !!!cp ('t118');
4344                  #                  #
# Line 3584  sub _tree_construction_main ($) { Line 4348  sub _tree_construction_main ($) {
4348                  !!!cp ('t119');                  !!!cp ('t119');
4349                  ## As if </noscript>                  ## As if </noscript>
4350                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4351                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4352                                
4353                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4354                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4355                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4356                  !!!cp ('t120');                  !!!cp ('t120');
4357                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4358                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4359                        [$self->{head_element}, $el_category->{head}];
4360                } else {                } else {
4361                  !!!cp ('t121');                  !!!cp ('t121');
4362                }                }
4363    
4364                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4365                $script_start_tag->($insert_to_current);                $script_start_tag->();
4366                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4367                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4368                redo B;                next B;
4369              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4370                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4371                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4372                  !!!cp ('t122');                  !!!cp ('t122');
4373                  ## As if </noscript>                  ## As if </noscript>
4374                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4375                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4376                                    
4377                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4378                  ## As if </head>                  ## As if </head>
# Line 3624  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
4389                }                }
4390    
4391                ## "after head" insertion mode                ## "after head" insertion mode
4392                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4393                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4394                  !!!cp ('t126');                  !!!cp ('t126');
4395                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3634  sub _tree_construction_main ($) { Line 4399  sub _tree_construction_main ($) {
4399                } else {                } else {
4400                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4401                }                }
4402                  !!!nack ('t127.1');
4403                !!!next-token;                !!!next-token;
4404                redo B;                next B;
4405              } else {              } else {
4406                !!!cp ('t128');                !!!cp ('t128');
4407                #                #
# Line 3645  sub _tree_construction_main ($) { Line 4411  sub _tree_construction_main ($) {
4411                !!!cp ('t129');                !!!cp ('t129');
4412                ## As if </noscript>                ## As if </noscript>
4413                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4414                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4415                                
4416                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4417                ## As if </head>                ## As if </head>
# Line 3664  sub _tree_construction_main ($) { Line 4430  sub _tree_construction_main ($) {
4430    
4431              ## "after head" insertion mode              ## "after head" insertion mode
4432              ## As if <body>              ## As if <body>
4433              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4434              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4435              ## reprocess              ## reprocess
4436              redo B;              !!!ack-later;
4437                next B;
4438            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4439              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4440                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4441                  !!!cp ('t132');                  !!!cp ('t132');
4442                  ## As if <head>                  ## As if <head>
4443                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4444                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4445                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4446                        [$self->{head_element}, $el_category->{head}];
4447    
4448                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4449                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4450                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4451                  !!!next-token;                  !!!next-token;
4452                  redo B;                  next B;
4453                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4454                  !!!cp ('t133');                  !!!cp ('t133');
4455                  ## As if </noscript>                  ## As if </noscript>
4456                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4457                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4458                                    
4459                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4460                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4461                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4462                  !!!next-token;                  !!!next-token;
4463                  redo B;                  next B;
4464                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4465                  !!!cp ('t134');                  !!!cp ('t134');
4466                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4467                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4468                  !!!next-token;                  !!!next-token;
4469                  redo B;                  next B;
4470                } else {                } else {
4471                  !!!cp ('t135');                  !!!cp ('t135');
4472                  #                  #
# Line 3709  sub _tree_construction_main ($) { Line 4477  sub _tree_construction_main ($) {
4477                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4478                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4479                  !!!next-token;                  !!!next-token;
4480                  redo B;                  next B;
4481                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4482                  !!!cp ('t137');                  !!!cp ('t137');
4483                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4484                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } else {                } else {
4488                  !!!cp ('t138');                  !!!cp ('t138');
4489                  #                  #
# Line 3726  sub _tree_construction_main ($) { Line 4494  sub _tree_construction_main ($) {
4494                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4495                  !!!cp ('t139');                  !!!cp ('t139');
4496                  ## As if <head>                  ## As if <head>
4497                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4498                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4499                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4500                        [$self->{head_element}, $el_category->{head}];
4501    
4502                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4503                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4504                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4505                  !!!cp ('t140');                  !!!cp ('t140');
4506                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4507                  ## Ignore the token                  ## Ignore the token
4508                  !!!next-token;                  !!!next-token;
4509                  redo B;                  next B;
4510                } else {                } else {
4511                  !!!cp ('t141');                  !!!cp ('t141');
4512                }                }
# Line 3749  sub _tree_construction_main ($) { Line 4518  sub _tree_construction_main ($) {
4518                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4519                  !!!cp ('t142');                  !!!cp ('t142');
4520                  ## As if <head>                  ## As if <head>
4521                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4522                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4523                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4524                        [$self->{head_element}, $el_category->{head}];
4525    
4526                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4527                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3766  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                  #                  #
4537                } else {                } else {
4538                  !!!cp ('t145');                  !!!cp ('t145');
4539                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4540                  ## Ignore the token                  ## Ignore the token
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                }                }
4544              }              }
4545    
# Line 3777  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547                !!!cp ('t146');                !!!cp ('t146');
4548                ## As if </noscript>                ## As if </noscript>
4549                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4550                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4551                                
4552                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4553                ## As if </head>                ## As if </head>
# Line 3793  sub _tree_construction_main ($) { Line 4563  sub _tree_construction_main ($) {
4563              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4564  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4565                !!!cp ('t148');                !!!cp ('t148');
4566                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4567                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4568                !!!next-token;                !!!next-token;
4569                redo B;                next B;
4570              } else {              } else {
4571                !!!cp ('t149');                !!!cp ('t149');
4572              }              }
4573    
4574              ## "after head" insertion mode              ## "after head" insertion mode
4575              ## As if <body>              ## As if <body>
4576              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4577              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4578              ## reprocess              ## reprocess
4579              redo B;              next B;
4580            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4581              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4582            }            !!!cp ('t149.1');
4583    
4584              ## NOTE: As if <head>
4585              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4586              $self->{open_elements}->[-1]->[0]->append_child
4587                  ($self->{head_element});
4588              #push @{$self->{open_elements}},
4589              #    [$self->{head_element}, $el_category->{head}];
4590              #$self->{insertion_mode} = IN_HEAD_IM;
4591              ## NOTE: Reprocess.
4592    
4593              ## NOTE: As if </head>
4594              #pop @{$self->{open_elements}};
4595              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4596              ## NOTE: Reprocess.
4597              
4598              #
4599            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4600              !!!cp ('t149.2');
4601    
4602              ## NOTE: As if </head>
4603              pop @{$self->{open_elements}};
4604              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4605              ## NOTE: Reprocess.
4606    
4607              #
4608            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4609              !!!cp ('t149.3');
4610    
4611              !!!parse-error (type => 'in noscript:#eof', token => $token);
4612    
4613              ## As if </noscript>
4614              pop @{$self->{open_elements}};
4615              #$self->{insertion_mode} = IN_HEAD_IM;
4616              ## NOTE: Reprocess.
4617    
4618              ## NOTE: As if </head>
4619              pop @{$self->{open_elements}};
4620              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4621              ## NOTE: Reprocess.
4622    
4623              #
4624            } else {
4625              !!!cp ('t149.4');
4626              #
4627            }
4628    
4629            ## NOTE: As if <body>
4630            !!!insert-element ('body',, $token);
4631            $self->{insertion_mode} = IN_BODY_IM;
4632            ## NOTE: Reprocess.
4633            next B;
4634          } else {
4635            die "$0: $token->{type}: Unknown token type";
4636          }
4637    
4638            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4639      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3821  sub _tree_construction_main ($) { Line 4645  sub _tree_construction_main ($) {
4645              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4646    
4647              !!!next-token;              !!!next-token;
4648              redo B;              next B;
4649            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4650              if ({              if ({
4651                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3829  sub _tree_construction_main ($) { Line 4653  sub _tree_construction_main ($) {
4653                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4654                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4655                  ## have an element in table scope                  ## have an element in table scope
4656                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4657                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4658                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4659                      !!!cp ('t151');                      !!!cp ('t151');
4660                      $tn = $node->[1];  
4661                      last INSCOPE;                      ## Close the cell
4662                    } elsif ({                      !!!back-token; # <x>
4663                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4664                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4665                                  line => $token->{line},
4666                                  column => $token->{column}};
4667                        next B;
4668                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4669                      !!!cp ('t152');                      !!!cp ('t152');
4670                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4671                        last;
4672                    }                    }
4673                  } # INSCOPE                  }
4674                    unless (defined $tn) {  
4675                      !!!cp ('t153');                  !!!cp ('t153');
4676  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4677                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4678                      ## Ignore the token                  ## Ignore the token
4679                      !!!next-token;                  !!!nack ('t153.1');
4680                      redo B;                  !!!next-token;
4681                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4682                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4683                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4684                                    
4685                  ## As if </caption>                  ## NOTE: As if </caption>.
4686                  ## have a table element in table scope                  ## have a table element in table scope
4687                  my $i;                  my $i;
4688                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4689                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4690                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4691                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4692                      $i = $_;                        !!!cp ('t155');
4693                      last INSCOPE;                        $i = $_;
4694                    } elsif ({                        last INSCOPE;
4695                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4696                             }->{$node->[1]}) {                        !!!cp ('t156');
4697                      !!!cp ('t156');                        last;
4698                      last INSCOPE;                      }
4699                    }                    }
4700    
4701                      !!!cp ('t157');
4702                      !!!parse-error (type => 'start tag not allowed',
4703                                      value => $token->{tag_name}, token => $token);
4704                      ## Ignore the token
4705                      !!!nack ('t157.1');
4706                      !!!next-token;
4707                      next B;
4708                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4709                                    
4710                  ## generate implied end tags                  ## generate implied end tags
4711                  if ({                  while ($self->{open_elements}->[-1]->[1]
4712                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: Maybe the following elements never appear here.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4713                    !!!cp ('t158');                    !!!cp ('t158');
4714                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4715                  }                  }
4716    
4717                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4718                    !!!cp ('t159');                    !!!cp ('t159');
4719                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4720                                      value => $self->{open_elements}->[-1]->[0]
4721                                          ->manakai_local_name,
4722                                      token => $token);
4723                  } else {                  } else {
4724                    !!!cp ('t160');                    !!!cp ('t160');
4725                  }                  }
# Line 3916  sub _tree_construction_main ($) { Line 4731  sub _tree_construction_main ($) {
4731                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4732                                    
4733                  ## reprocess                  ## reprocess
4734                  redo B;                  !!!ack-later;
4735                    next B;
4736                } else {                } else {
4737                  !!!cp ('t161');                  !!!cp ('t161');
4738                  #                  #
# Line 3932  sub _tree_construction_main ($) { Line 4748  sub _tree_construction_main ($) {
4748                  my $i;                  my $i;
4749                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4750                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4751                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4752                      !!!cp ('t163');                      !!!cp ('t163');
4753                      $i = $_;                      $i = $_;
4754                      last INSCOPE;                      last INSCOPE;
4755                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4756                      !!!cp ('t164');                      !!!cp ('t164');
4757                      last INSCOPE;                      last INSCOPE;
4758                    }                    }
4759                  } # INSCOPE                  } # INSCOPE
4760                    unless (defined $i) {                    unless (defined $i) {
4761                      !!!cp ('t165');                      !!!cp ('t165');
4762                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4763                      ## Ignore the token                      ## Ignore the token
4764                      !!!next-token;                      !!!next-token;
4765                      redo B;                      next B;
4766                    }                    }
4767                                    
4768                  ## generate implied end tags                  ## generate implied end tags
4769                  if ({                  while ($self->{open_elements}->[-1]->[1]
4770                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
   
                      ## NOTE: Maybe the following elements never appear here.  
                      tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4771                    !!!cp ('t166');                    !!!cp ('t166');
4772                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4773                  }                  }
4774                    
4775                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4776                            ne $token->{tag_name}) {
4777                    !!!cp ('t167');                    !!!cp ('t167');
4778                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4779                                      value => $self->{open_elements}->[-1]->[0]
4780                                          ->manakai_local_name,
4781                                      token => $token);
4782                  } else {                  } else {
4783                    !!!cp ('t168');                    !!!cp ('t168');
4784                  }                  }
# Line 3982  sub _tree_construction_main ($) { Line 4790  sub _tree_construction_main ($) {
4790                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4791                                    
4792                  !!!next-token;                  !!!next-token;
4793                  redo B;                  next B;
4794                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4795                  !!!cp ('t169');                  !!!cp ('t169');
4796                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4797                  ## Ignore the token                  ## Ignore the token
4798                  !!!next-token;                  !!!next-token;
4799                  redo B;                  next B;
4800                } else {                } else {
4801                  !!!cp ('t170');                  !!!cp ('t170');
4802                  #                  #
# Line 3997  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4806                  ## have a table element in table scope                  ## have a table element in table scope
4807                  my $i;                  my $i;
4808                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4809                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4810                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4811                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4812                      $i = $_;                        !!!cp ('t171');
4813                      last INSCOPE;                        $i = $_;
4814                    } elsif ({                        last INSCOPE;
4815                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4816                             }->{$node->[1]}) {                        !!!cp ('t172');
4817                      !!!cp ('t172');                        last;
4818                      last INSCOPE;                      }
4819                    }                    }
4820    
4821                      !!!cp ('t173');
4822                      !!!parse-error (type => 'unmatched end tag',
4823                                      value => $token->{tag_name}, token => $token);
4824                      ## Ignore the token
4825                      !!!next-token;
4826                      next B;
4827                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4828                                    
4829                  ## generate implied end tags                  ## generate implied end tags
4830                  if ({                  while ($self->{open_elements}->[-1]->[1]
4831                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: The following elements never appear here, maybe.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4832                    !!!cp ('t174');                    !!!cp ('t174');
4833                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4834                  }                  }
4835                                    
4836                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4837                    !!!cp ('t175');                    !!!cp ('t175');
4838                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4839                                      value => $self->{open_elements}->[-1]->[0]
4840                                          ->manakai_local_name,
4841                                      token => $token);
4842                  } else {                  } else {
4843                    !!!cp ('t176');                    !!!cp ('t176');
4844                  }                  }
# Line 4047  sub _tree_construction_main ($) { Line 4850  sub _tree_construction_main ($) {
4850                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4851                                    
4852                  !!!next-token;                  !!!next-token;
4853                  redo B;                  next B;
4854                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4855                  !!!cp ('t177');                  !!!cp ('t177');
4856                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4857                  ## Ignore the token                  ## Ignore the token
4858                  !!!next-token;                  !!!next-token;
4859                  redo B;                  next B;
4860                } else {                } else {
4861                  !!!cp ('t178');                  !!!cp ('t178');
4862                  #                  #
# Line 4066  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869                ## have an element in table scope                ## have an element in table scope
4870                my $i;                my $i;
4871                my $tn;                my $tn;
4872                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4873                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4874                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4875                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4876                    $i = $_;                      !!!cp ('t179');
4877                    last INSCOPE;                      $i = $_;
4878                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4879                    !!!cp ('t180');                      ## Close the cell
4880                    $tn = $node->[1];                      !!!back-token; # </x>
4881                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4882                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4883                  } elsif ({                                column => $token->{column}};
4884                            table => 1, html => 1,                      next B;
4885                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4886                    !!!cp ('t181');                      !!!cp ('t180');
4887                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4888                        ## NOTE: There is exactly one |td| or |th| element
4889                        ## in scope in the stack of open elements by definition.
4890                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4891                        ## ISSUE: Can this be reached?
4892                        !!!cp ('t181');
4893                        last;
4894                      }
4895                  }                  }
4896                } # INSCOPE  
               unless (defined $i) {  
4897                  !!!cp ('t182');                  !!!cp ('t182');
4898                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4899                        value => $token->{tag_name}, token => $token);
4900                  ## Ignore the token                  ## Ignore the token
4901                  !!!next-token;                  !!!next-token;
4902                  redo B;                  next B;
4903                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4904              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4905                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4906                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4907    
4908                ## As if </caption>                ## As if </caption>
4909                ## have a table element in table scope                ## have a table element in table scope
4910                my $i;                my $i;
4911                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4912                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4913                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4914                    !!!cp ('t184');                    !!!cp ('t184');
4915                    $i = $_;                    $i = $_;
4916                    last INSCOPE;                    last INSCOPE;
4917                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4918                    !!!cp ('t185');                    !!!cp ('t185');
4919                    last INSCOPE;                    last INSCOPE;
4920                  }                  }
4921                } # INSCOPE                } # INSCOPE
4922                unless (defined $i) {                unless (defined $i) {
4923                  !!!cp ('t186');                  !!!cp ('t186');
4924                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4925                  ## Ignore the token                  ## Ignore the token
4926                  !!!next-token;                  !!!next-token;
4927                  redo B;                  next B;
4928                }                }
4929                                
4930                ## generate implied end tags                ## generate implied end tags
4931                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
   
                    ## NOTE: The following elements never appear, maybe.  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4932                  !!!cp ('t187');                  !!!cp ('t187');
4933                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4934                }                }
4935    
4936                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4937                  !!!cp ('t188');                  !!!cp ('t188');
4938                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4939                                    value => $self->{open_elements}->[-1]->[0]
4940                                        ->manakai_local_name,
4941                                    token => $token);
4942                } else {                } else {
4943                  !!!cp ('t189');                  !!!cp ('t189');
4944                }                }
# Line 4157  sub _tree_construction_main ($) { Line 4950  sub _tree_construction_main ($) {
4950                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4951    
4952                ## reprocess                ## reprocess
4953                redo B;                next B;
4954              } elsif ({              } elsif ({
4955                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4957                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4958                  !!!cp ('t190');                  !!!cp ('t190');
4959                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  redo B;                  next B;
4963                } else {                } else {
4964                  !!!cp ('t191');                  !!!cp ('t191');
4965                  #                  #
# Line 4177  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4971                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4972                !!!cp ('t192');                !!!cp ('t192');
4973                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4974                ## Ignore the token                ## Ignore the token
4975                !!!next-token;                !!!next-token;
4976                redo B;                next B;
4977              } else {              } else {
4978                !!!cp ('t193');                !!!cp ('t193');
4979                #                #
4980              }              }
4981          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4982            for my $entry (@{$self->{open_elements}}) {
4983              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4984                !!!cp ('t75');
4985                !!!parse-error (type => 'in body:#eof', token => $token);
4986                last;
4987              }
4988            }
4989    
4990            ## Stop parsing.
4991            last B;
4992        } else {        } else {
4993          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4994        }        }
# Line 4193  sub _tree_construction_main ($) { Line 4997  sub _tree_construction_main ($) {
4997        #        #
4998      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4999        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5000              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5001                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5002              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5003                                
5004                unless (length $token->{data}) {            unless (length $token->{data}) {
5005                  !!!cp ('t194');              !!!cp ('t194');
5006                  !!!next-token;              !!!next-token;
5007                  redo B;              next B;
5008                } else {            } else {
5009                  !!!cp ('t195');              !!!cp ('t195');
5010                }            }
5011              }          }
5012    
5013              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5014    
5015              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5016              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018              ## result in a new Text node.              ## result in a new Text node.
5019              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5020                            
5021              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]}) {  
5022                # MUST                # MUST
5023                my $foster_parent_element;                my $foster_parent_element;
5024                my $next_sibling;                my $next_sibling;
5025                my $prev_sibling;                my $prev_sibling;
5026                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5027                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5028                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5029                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5030                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4250  sub _tree_construction_main ($) { Line 5052  sub _tree_construction_main ($) {
5052                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5053                     $next_sibling);                     $next_sibling);
5054                }                }
5055              } else {            $open_tables->[-1]->[1] = 1; # tainted
5056                !!!cp ('t200');          } else {
5057                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5058              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5059            }
5060                            
5061              !!!next-token;          !!!next-token;
5062              redo B;          next B;
5063        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5064              if ({              if ({
5065                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4264  sub _tree_construction_main ($) { Line 5067  sub _tree_construction_main ($) {
5067                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5068                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5069                  ## Clear back to table context                  ## Clear back to table context
5070                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5071                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5072                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5073                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5074                  }                  }
5075                                    
5076                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5077                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5078                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5079                }                }
# Line 4279  sub _tree_construction_main ($) { Line 5081  sub _tree_construction_main ($) {
5081                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5082                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5083                    !!!cp ('t202');                    !!!cp ('t202');
5084                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5085                  }                  }
5086                                    
5087                  ## Clear back to table body context                  ## Clear back to table body context
5088                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5089                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5090                    !!!cp ('t203');                    !!!cp ('t203');
5091                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5092                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5093                  }                  }
5094                                    
5095                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5096                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5097                    !!!cp ('t204');                    !!!cp ('t204');
5098                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5099                      !!!nack ('t204');
5100                    !!!next-token;                    !!!next-token;
5101                    redo B;                    next B;
5102                  } else {                  } else {
5103                    !!!cp ('t205');                    !!!cp ('t205');
5104                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5105                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5106                  }                  }
5107                } else {                } else {
# Line 4308  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109                }                }
5110    
5111                ## Clear back to table row context                ## Clear back to table row context
5112                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5113                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5114                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5115                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5116                }                }
5117                                
5118                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5119                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5120    
5121                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5122                                
5123                  !!!nack ('t207.1');
5124                !!!next-token;                !!!next-token;
5125                redo B;                next B;
5126              } elsif ({              } elsif ({
5127                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5128                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4334  sub _tree_construction_main ($) { Line 5134  sub _tree_construction_main ($) {
5134                  my $i;                  my $i;
5135                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5136                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5137                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5138                      !!!cp ('t208');                      !!!cp ('t208');
5139                      $i = $_;                      $i = $_;
5140                      last INSCOPE;                      last INSCOPE;
5141                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5142                      !!!cp ('t209');                      !!!cp ('t209');
5143                      last INSCOPE;                      last INSCOPE;
5144                    }                    }
5145                  } # INSCOPE                  } # INSCOPE
5146                  unless (defined $i) {                  unless (defined $i) {
5147                   !!!cp ('t210');                    !!!cp ('t210');
5148  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5149                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5150                    ## Ignore the token                    ## Ignore the token
5151                      !!!nack ('t210.1');
5152                    !!!next-token;                    !!!next-token;
5153                    redo B;                    next B;
5154                  }                  }
5155                                    
5156                  ## Clear back to table row context                  ## Clear back to table row context
5157                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5158                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5159                    !!!cp ('t211');                    !!!cp ('t211');
5160                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5161                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5162                  }                  }
5163                                    
# Line 4372  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5167                    !!!cp ('t212');                    !!!cp ('t212');
5168                    ## reprocess                    ## reprocess
5169                    redo B;                    !!!ack-later;
5170                      next B;
5171                  } else {                  } else {
5172                    !!!cp ('t213');                    !!!cp ('t213');
5173                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                  my $i;                  my $i;
5180                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5181                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5182                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5183                      !!!cp ('t214');                      !!!cp ('t214');
5184                      $i = $_;                      $i = $_;
5185                      last INSCOPE;                      last INSCOPE;
5186                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5187                      !!!cp ('t215');                      !!!cp ('t215');
5188                      last INSCOPE;                      last INSCOPE;
5189                    }                    }
# Line 4400  sub _tree_construction_main ($) { Line 5191  sub _tree_construction_main ($) {
5191                  unless (defined $i) {                  unless (defined $i) {
5192                    !!!cp ('t216');                    !!!cp ('t216');
5193  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5194                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5195                    ## Ignore the token                    ## Ignore the token
5196                      !!!nack ('t216.1');
5197                    !!!next-token;                    !!!next-token;
5198                    redo B;                    next B;
5199                  }                  }
5200    
5201                  ## Clear back to table body context                  ## Clear back to table body context
5202                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5203                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5204                    !!!cp ('t217');                    !!!cp ('t217');
5205                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5206                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5207                  }                  }
5208                                    
# Line 4432  sub _tree_construction_main ($) { Line 5222  sub _tree_construction_main ($) {
5222    
5223                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5224                  ## Clear back to table context                  ## Clear back to table context
5225                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5226                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5227                    !!!cp ('t219');                    !!!cp ('t219');
5228                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5229                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5230                  }                  }
5231                                    
5232                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5233                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5234                  ## reprocess                  ## reprocess
5235                  redo B;                  !!!ack-later;
5236                    next B;
5237                } elsif ({                } elsif ({
5238                          caption => 1,                          caption => 1,
5239                          colgroup => 1,                          colgroup => 1,
5240                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5241                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5242                  ## Clear back to table context                  ## Clear back to table context
5243                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5244                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5245                    !!!cp ('t220');                    !!!cp ('t220');
5246                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5247                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5248                  }                  }
5249                                    
5250                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5251                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5252                                    
5253                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5254                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5255                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5256                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4470  sub _tree_construction_main ($) { Line 5259  sub _tree_construction_main ($) {
5259                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5260                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5261                  !!!next-token;                  !!!next-token;
5262                  redo B;                  !!!nack ('t220.1');
5263                    next B;
5264                } else {                } else {
5265                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5266                }                }
5267              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5268                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5269                                  value => $self->{open_elements}->[-1]->[0]
5270                                      ->manakai_local_name,
5271                                  token => $token);
5272    
5273                ## As if </table>                ## As if </table>
5274                ## have a table element in table scope                ## have a table element in table scope
5275                my $i;                my $i;
5276                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5277                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5278                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5279                    !!!cp ('t221');                    !!!cp ('t221');
5280                    $i = $_;                    $i = $_;
5281                    last INSCOPE;                    last INSCOPE;
5282                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5283                    !!!cp ('t222');                    !!!cp ('t222');
5284                    last INSCOPE;                    last INSCOPE;
5285                  }                  }
# Line 4497  sub _tree_construction_main ($) { Line 5287  sub _tree_construction_main ($) {
5287                unless (defined $i) {                unless (defined $i) {
5288                  !!!cp ('t223');                  !!!cp ('t223');
5289  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5290                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5291                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5292                    !!!nack ('t223.1');
5293                  !!!next-token;                  !!!next-token;
5294                  redo B;                  next B;
5295                }                }
5296                                
5297    ## TODO: Followings are removed from the latest spec.
5298                ## generate implied end tags                ## generate implied end tags
5299                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5300                  !!!cp ('t224');                  !!!cp ('t224');
5301                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5302                }                }
5303    
5304                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5305                  !!!cp ('t225');                  !!!cp ('t225');
5306  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5307                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5308                                    value => $self->{open_elements}->[-1]->[0]
5309                                        ->manakai_local_name,
5310                                    token => $token);
5311                } else {                } else {
5312                  !!!cp ('t226');                  !!!cp ('t226');
5313                }                }
5314    
5315                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5316                  pop @{$open_tables};
5317    
5318                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5319    
5320                ## reprocess            ## reprocess
5321                redo B;            !!!ack-later;
5322              next B;
5323            } elsif ($token->{tag_name} eq 'style') {
5324              if (not $open_tables->[-1]->[1]) { # tainted
5325                !!!cp ('t227.8');
5326                ## NOTE: This is a "as if in head" code clone.
5327                $parse_rcdata->(CDATA_CONTENT_MODEL);
5328                next B;
5329              } else {
5330                !!!cp ('t227.7');
5331                #
5332              }
5333            } elsif ($token->{tag_name} eq 'script') {
5334              if (not $open_tables->[-1]->[1]) { # tainted
5335                !!!cp ('t227.6');
5336                ## NOTE: This is a "as if in head" code clone.
5337                $script_start_tag->();
5338                next B;
5339              } else {
5340                !!!cp ('t227.5');
5341                #
5342              }
5343            } elsif ($token->{tag_name} eq 'input') {
5344              if (not $open_tables->[-1]->[1]) { # tainted
5345                if ($token->{attributes}->{type}) { ## TODO: case
5346                  my $type = lc $token->{attributes}->{type}->{value};
5347                  if ($type eq 'hidden') {
5348                    !!!cp ('t227.3');
5349                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5350    
5351                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5352    
5353                    ## TODO: form element pointer
5354    
5355                    pop @{$self->{open_elements}};
5356    
5357                    !!!next-token;
5358                    !!!ack ('t227.2.1');
5359                    next B;
5360                  } else {
5361                    !!!cp ('t227.2');
5362                    #
5363                  }
5364                } else {
5365                  !!!cp ('t227.1');
5366                  #
5367                }
5368              } else {
5369                !!!cp ('t227.4');
5370                #
5371              }
5372          } else {          } else {
5373            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5374            #            #
5375          }          }
5376    
5377            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5378    
5379            $insert = $insert_to_foster;
5380            #
5381        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5382              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5383                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4546  sub _tree_construction_main ($) { Line 5385  sub _tree_construction_main ($) {
5385                my $i;                my $i;
5386                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5387                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5388                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5389                    !!!cp ('t228');                    !!!cp ('t228');
5390                    $i = $_;                    $i = $_;
5391                    last INSCOPE;                    last INSCOPE;
5392                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5393                    !!!cp ('t229');                    !!!cp ('t229');
5394                    last INSCOPE;                    last INSCOPE;
5395                  }                  }
5396                } # INSCOPE                } # INSCOPE
5397                unless (defined $i) {                unless (defined $i) {
5398                  !!!cp ('t230');                  !!!cp ('t230');
5399                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5400                  ## Ignore the token                  ## Ignore the token
5401                    !!!nack ('t230.1');
5402                  !!!next-token;                  !!!next-token;
5403                  redo B;                  next B;
5404                } else {                } else {
5405                  !!!cp ('t232');                  !!!cp ('t232');
5406                }                }
5407    
5408                ## Clear back to table row context                ## Clear back to table row context
5409                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5410                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5411                  !!!cp ('t231');                  !!!cp ('t231');
5412  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5413                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5414                }                }
5415    
5416                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5417                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5418                !!!next-token;                !!!next-token;
5419                redo B;                !!!nack ('t231.1');
5420                  next B;
5421              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5422                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5423                  ## As if </tr>                  ## As if </tr>
# Line 4588  sub _tree_construction_main ($) { Line 5425  sub _tree_construction_main ($) {
5425                  my $i;                  my $i;
5426                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5427                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5428                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5429                      !!!cp ('t233');                      !!!cp ('t233');
5430                      $i = $_;                      $i = $_;
5431                      last INSCOPE;                      last INSCOPE;
5432                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5433                      !!!cp ('t234');                      !!!cp ('t234');
5434                      last INSCOPE;                      last INSCOPE;
5435                    }                    }
# Line 4602  sub _tree_construction_main ($) { Line 5437  sub _tree_construction_main ($) {
5437                  unless (defined $i) {                  unless (defined $i) {
5438                    !!!cp ('t235');                    !!!cp ('t235');
5439  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5440                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5441                    ## Ignore the token                    ## Ignore the token
5442                      !!!nack ('t236.1');
5443                    !!!next-token;                    !!!next-token;
5444                    redo B;                    next B;
5445                  }                  }
5446                                    
5447                  ## Clear back to table row context                  ## Clear back to table row context
5448                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5449                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5450                    !!!cp ('t236');                    !!!cp ('t236');
5451  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5452                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5453                  }                  }
5454                                    
# Line 4628  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 ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5466                      !!!cp ('t237');                      !!!cp ('t237');
5467                      $i = $_;                      $i = $_;
5468                      last INSCOPE;                      last INSCOPE;
5469                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5470                      !!!cp ('t238');                      !!!cp ('t238');
5471                      last INSCOPE;                      last INSCOPE;
5472                    }                    }
5473                  } # INSCOPE                  } # INSCOPE
5474                  unless (defined $i) {                  unless (defined $i) {
5475                    !!!cp ('t239');                    !!!cp ('t239');
5476                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                    ## Ignore the token                    ## Ignore the token
5478                      !!!nack ('t239.1');
5479                    !!!next-token;                    !!!next-token;
5480                    redo B;                    next B;
5481                  }                  }
5482                                    
5483                  ## Clear back to table body context                  ## Clear back to table body context
5484                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5485                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5486                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5487                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5488                  }                  }
5489                                    
# Line 4670  sub _tree_construction_main ($) { Line 5499  sub _tree_construction_main ($) {
5499                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5500                }                }
5501    
5502                  ## NOTE: </table> in the "in table" insertion mode.
5503                  ## When you edit the code fragment below, please ensure that
5504                  ## the code for <table> in the "in table" insertion mode
5505                  ## is synced with it.
5506    
5507                ## have a table element in table scope                ## have a table element in table scope
5508                my $i;                my $i;
5509                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5511                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5512                    !!!cp ('t241');                    !!!cp ('t241');
5513                    $i = $_;                    $i = $_;
5514                    last INSCOPE;                    last INSCOPE;
5515                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5516                    !!!cp ('t242');                    !!!cp ('t242');
5517                    last INSCOPE;                    last INSCOPE;
5518                  }                  }
5519                } # INSCOPE                } # INSCOPE
5520                unless (defined $i) {                unless (defined $i) {
5521                  !!!cp ('t243');                  !!!cp ('t243');
5522                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                  ## Ignore the token                  ## Ignore the token
5524                    !!!nack ('t243.1');
5525                  !!!next-token;                  !!!next-token;
5526                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5527                }                }
5528                                    
5529                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5530                  pop @{$open_tables};
5531                                
5532                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5533                                
5534                !!!next-token;                !!!next-token;
5535                redo B;                next B;
5536              } elsif ({              } elsif ({
5537                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5538                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4729  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542                  my $i;                  my $i;
5543                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5544                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5545                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5546                      !!!cp ('t247');                      !!!cp ('t247');
5547                      $i = $_;                      $i = $_;
5548                      last INSCOPE;                      last INSCOPE;
5549                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5550                      !!!cp ('t248');                      !!!cp ('t248');
5551                      last INSCOPE;                      last INSCOPE;
5552                    }                    }
5553                  } # INSCOPE                  } # INSCOPE
5554                    unless (defined $i) {                    unless (defined $i) {
5555                      !!!cp ('t249');                      !!!cp ('t249');
5556                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5557                      ## Ignore the token                      ## Ignore the token
5558                        !!!nack ('t249.1');
5559                      !!!next-token;                      !!!next-token;
5560                      redo B;                      next B;
5561                    }                    }
5562                                    
5563                  ## As if </tr>                  ## As if </tr>
# Line 4753  sub _tree_construction_main ($) { Line 5565  sub _tree_construction_main ($) {
5565                  my $i;                  my $i;
5566                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5567                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5568                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5569                      !!!cp ('t250');                      !!!cp ('t250');
5570                      $i = $_;                      $i = $_;
5571                      last INSCOPE;                      last INSCOPE;
5572                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5573                      !!!cp ('t251');                      !!!cp ('t251');
5574                      last INSCOPE;                      last INSCOPE;
5575                    }                    }
5576                  } # INSCOPE                  } # INSCOPE
5577                    unless (defined $i) {                    unless (defined $i) {
5578                      !!!cp ('t252');                      !!!cp ('t252');
5579                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5580                      ## Ignore the token                      ## Ignore the token
5581                        !!!nack ('t252.1');
5582                      !!!next-token;                      !!!next-token;
5583                      redo B;                      next B;
5584                    }                    }
5585                                    
5586                  ## Clear back to table row context                  ## Clear back to table row context
5587                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5588                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5589                    !!!cp ('t253');                    !!!cp ('t253');
5590  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5591                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5592                  }                  }
5593                                    
# Line 4791  sub _tree_construction_main ($) { Line 5600  sub _tree_construction_main ($) {
5600                my $i;                my $i;
5601                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5602                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5603                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5604                    !!!cp ('t254');                    !!!cp ('t254');
5605                    $i = $_;                    $i = $_;
5606                    last INSCOPE;                    last INSCOPE;
5607                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5608                    !!!cp ('t255');                    !!!cp ('t255');
5609                    last INSCOPE;                    last INSCOPE;
5610                  }                  }
5611                } # INSCOPE                } # INSCOPE
5612                unless (defined $i) {                unless (defined $i) {
5613                  !!!cp ('t256');                  !!!cp ('t256');
5614                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5615                  ## Ignore the token                  ## Ignore the token
5616                    !!!nack ('t256.1');
5617                  !!!next-token;                  !!!next-token;
5618                  redo B;                  next B;
5619                }                }
5620    
5621                ## Clear back to table body context                ## Clear back to table body context
5622                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5623                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5624                  !!!cp ('t257');                  !!!cp ('t257');
5625  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5626                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5627                }                }
5628    
5629                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5630                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5631                  !!!nack ('t257.1');
5632                !!!next-token;                !!!next-token;
5633                redo B;                next B;
5634              } elsif ({              } elsif ({
5635                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5636                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5637                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5638                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5639                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5640                !!!cp ('t258');            !!!cp ('t258');
5641                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5642                ## Ignore the token            ## Ignore the token
5643                !!!next-token;            !!!nack ('t258.1');
5644                redo B;             !!!next-token;
5645              next B;
5646          } else {          } else {
5647            !!!cp ('t259');            !!!cp ('t259');
5648            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5649    
5650            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5651            #            #
5652          }          }
5653          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5654            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5655                    @{$self->{open_elements}} == 1) { # redundant, maybe
5656              !!!parse-error (type => 'in body:#eof', token => $token);
5657              !!!cp ('t259.1');
5658              #
5659            } else {
5660              !!!cp ('t259.2');
5661              #
5662            }
5663    
5664            ## Stop parsing
5665            last B;
5666        } else {        } else {
5667          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5668        }        }
# Line 4852  sub _tree_construction_main ($) { Line 5673  sub _tree_construction_main ($) {
5673                unless (length $token->{data}) {                unless (length $token->{data}) {
5674                  !!!cp ('t260');                  !!!cp ('t260');
5675                  !!!next-token;                  !!!next-token;
5676                  redo B;                  next B;
5677                }                }
5678              }              }
5679                            
# Line 4861  sub _tree_construction_main ($) { Line 5682  sub _tree_construction_main ($) {
5682            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5683              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5684                !!!cp ('t262');                !!!cp ('t262');
5685                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5686                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5687                  !!!ack ('t262.1');
5688                !!!next-token;                !!!next-token;
5689                redo B;                next B;
5690              } else {              } else {
5691                !!!cp ('t263');                !!!cp ('t263');
5692                #                #
5693              }              }
5694            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5695              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5696                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5697                  !!!cp ('t264');                  !!!cp ('t264');
5698                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5699                  ## Ignore the token                  ## Ignore the token
5700                  !!!next-token;                  !!!next-token;
5701                  redo B;                  next B;
5702                } else {                } else {
5703                  !!!cp ('t265');                  !!!cp ('t265');
5704                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5705                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5706                  !!!next-token;                  !!!next-token;
5707                  redo B;                              next B;            
5708                }                }
5709              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5710                !!!cp ('t266');                !!!cp ('t266');
5711                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5712                ## Ignore the token                ## Ignore the token
5713                !!!next-token;                !!!next-token;
5714                redo B;                next B;
5715              } else {              } else {
5716                !!!cp ('t267');                !!!cp ('t267');
5717                #                #
5718              }              }
5719            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5720              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5721            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5722              !!!cp ('t270.2');
5723              ## Stop parsing.
5724              last B;
5725            } else {
5726              ## NOTE: As if </colgroup>.
5727              !!!cp ('t270.1');
5728              pop @{$self->{open_elements}}; # colgroup
5729              $self->{insertion_mode} = IN_TABLE_IM;
5730              ## Reprocess.
5731              next B;
5732            }
5733          } else {
5734            die "$0: $token->{type}: Unknown token type";
5735          }
5736    
5737            ## As if </colgroup>            ## As if </colgroup>
5738            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5739              !!!cp ('t269');              !!!cp ('t269');
5740              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5741                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5742              ## Ignore the token              ## Ignore the token
5743                !!!nack ('t269.1');
5744              !!!next-token;              !!!next-token;
5745              redo B;              next B;
5746            } else {            } else {
5747              !!!cp ('t270');              !!!cp ('t270');
5748              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5749              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5750                !!!ack-later;
5751              ## reprocess              ## reprocess
5752              redo B;              next B;
5753            }            }
5754      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5755        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5756          !!!cp ('t271');          !!!cp ('t271');
5757          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5758          !!!next-token;          !!!next-token;
5759          redo B;          next B;
5760        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5761              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5762                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5763                  !!!cp ('t272');              !!!cp ('t272');
5764                  ## As if </option>              ## As if </option>
5765                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5766                } else {            } else {
5767                  !!!cp ('t273');              !!!cp ('t273');
5768                }            }
5769    
5770                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5771                !!!next-token;            !!!nack ('t273.1');
5772                redo B;            !!!next-token;
5773              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5774                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5775                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5776                  ## As if </option>              !!!cp ('t274');
5777                  pop @{$self->{open_elements}};              ## As if </option>
5778                } else {              pop @{$self->{open_elements}};
5779                  !!!cp ('t275');            } else {
5780                }              !!!cp ('t275');
5781              }
5782    
5783                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5784                  !!!cp ('t276');              !!!cp ('t276');
5785                  ## As if </optgroup>              ## As if </optgroup>
5786                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5787                } else {            } else {
5788                  !!!cp ('t277');              !!!cp ('t277');
5789                }            }
5790    
5791                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5792                !!!next-token;            !!!nack ('t277.1');
5793                redo B;            !!!next-token;
5794              } elsif ($token->{tag_name} eq 'select') {            next B;
5795  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5796                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5797                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5798                ## have an element in table scope                    {
5799                my $i;                     caption => 1, table => 1,
5800                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5801                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5802                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5803                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5804                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5805                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5806                  } elsif ({            ## as if there were </select> (otherwise).
5807                            table => 1, html => 1,            ## have an element in table scope
5808                           }->{$node->[1]}) {            my $i;
5809                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5810                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5811                  }              if ($node->[1] & SELECT_EL) {
5812                } # INSCOPE                !!!cp ('t278');
5813                unless (defined $i) {                $i = $_;
5814                  !!!cp ('t280');                last INSCOPE;
5815                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5816                  ## Ignore the token                !!!cp ('t279');
5817                  !!!next-token;                last INSCOPE;
5818                  redo B;              }
5819                }            } # INSCOPE
5820              unless (defined $i) {
5821                !!!cp ('t280');
5822                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5823                ## Ignore the token
5824                !!!nack ('t280.1');
5825                !!!next-token;
5826                next B;
5827              }
5828                                
5829                !!!cp ('t281');            !!!cp ('t281');
5830                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5831    
5832                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5833    
5834                !!!next-token;            if ($token->{tag_name} eq 'select') {
5835                redo B;              !!!nack ('t281.2');
5836                !!!next-token;
5837                next B;
5838              } else {
5839                !!!cp ('t281.1');
5840                !!!ack-later;
5841                ## Reprocess the token.
5842                next B;
5843              }
5844          } else {          } else {
5845            !!!cp ('t282');            !!!cp ('t282');
5846            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5847            ## Ignore the token            ## Ignore the token
5848              !!!nack ('t282.1');
5849            !!!next-token;            !!!next-token;
5850            redo B;            next B;
5851          }          }
5852        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5853              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5854                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5855                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5856                  !!!cp ('t283');              !!!cp ('t283');
5857                  ## As if </option>              ## As if </option>
5858                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5859                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5860                  !!!cp ('t284');              !!!cp ('t284');
5861                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5862                } else {            } else {
5863                  !!!cp ('t285');              !!!cp ('t285');
5864                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5865                  ## Ignore the token              ## Ignore the token
5866                }            }
5867                !!!next-token;            !!!nack ('t285.1');
5868                redo B;            !!!next-token;
5869              } elsif ($token->{tag_name} eq 'option') {            next B;
5870                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5871                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5872                  pop @{$self->{open_elements}};              !!!cp ('t286');
5873                } else {              pop @{$self->{open_elements}};
5874                  !!!cp ('t287');            } else {
5875                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5876                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5877                }              ## Ignore the token
5878                !!!next-token;            }
5879                redo B;            !!!nack ('t287.1');
5880              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5881                ## have an element in table scope            next B;
5882                my $i;          } elsif ($token->{tag_name} eq 'select') {
5883                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5884                  my $node = $self->{open_elements}->[$_];            my $i;
5885                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5887                    $i = $_;              if ($node->[1] & SELECT_EL) {
5888                    last INSCOPE;                !!!cp ('t288');
5889                  } elsif ({                $i = $_;
5890                            table => 1, html => 1,                last INSCOPE;
5891                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892                    !!!cp ('t289');                !!!cp ('t289');
5893                    last INSCOPE;                last INSCOPE;
5894                  }              }
5895                } # INSCOPE            } # INSCOPE
5896                unless (defined $i) {            unless (defined $i) {
5897                  !!!cp ('t290');              !!!cp ('t290');
5898                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5899                  ## Ignore the token              ## Ignore the token
5900                  !!!next-token;              !!!nack ('t290.1');
5901                  redo B;              !!!next-token;
5902                }              next B;
5903              }
5904                                
5905                !!!cp ('t291');            !!!cp ('t291');
5906                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5907    
5908                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5909    
5910                !!!next-token;            !!!nack ('t291.1');
5911                redo B;            !!!next-token;
5912              } elsif ({            next B;
5913                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5914                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5915                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5916                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5917                     }->{$token->{tag_name}}) {
5918  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5919                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5920                                
5921                ## have an element in table scope            ## have an element in table scope
5922                my $i;            my $i;
5923                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5924                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5925                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5926                    !!!cp ('t292');                !!!cp ('t292');
5927                    $i = $_;                $i = $_;
5928                    last INSCOPE;                last INSCOPE;
5929                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5930                            table => 1, html => 1,                !!!cp ('t293');
5931                           }->{$node->[1]}) {                last INSCOPE;
5932                    !!!cp ('t293');              }
5933                    last INSCOPE;            } # INSCOPE
5934                  }            unless (defined $i) {
5935                } # INSCOPE              !!!cp ('t294');
5936                unless (defined $i) {              ## Ignore the token
5937                  !!!cp ('t294');              !!!nack ('t294.1');
5938                  ## Ignore the token              !!!next-token;
5939                  !!!next-token;              next B;
5940                  redo B;            }
               }  
5941                                
5942                ## As if </select>            ## As if </select>
5943                ## have an element in table scope            ## have an element in table scope
5944                undef $i;            undef $i;
5945                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5946                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5947                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5948                    !!!cp ('t295');                !!!cp ('t295');
5949                    $i = $_;                $i = $_;
5950                    last INSCOPE;                last INSCOPE;
5951                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5952  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5953                    !!!cp ('t296');                !!!cp ('t296');
5954                    last INSCOPE;                last INSCOPE;
5955                  }              }
5956                } # INSCOPE            } # INSCOPE
5957                unless (defined $i) {            unless (defined $i) {
5958                  !!!cp ('t297');              !!!cp ('t297');
5959  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5960                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5961                  ## Ignore the </select> token              ## Ignore the </select> token
5962                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5963                  redo B;              !!!next-token; ## TODO: ok?
5964                }              next B;
5965              }
5966                                
5967                !!!cp ('t298');            !!!cp ('t298');
5968                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5969    
5970                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5971    
5972                ## reprocess            !!!ack-later;
5973                redo B;            ## reprocess
5974              next B;
5975          } else {          } else {
5976            !!!cp ('t299');            !!!cp ('t299');
5977            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5978            ## Ignore the token            ## Ignore the token
5979              !!!nack ('t299.3');
5980            !!!next-token;            !!!next-token;
5981            redo B;            next B;
5982          }          }
5983          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5984            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5985                    @{$self->{open_elements}} == 1) { # redundant, maybe
5986              !!!cp ('t299.1');
5987              !!!parse-error (type => 'in body:#eof', token => $token);
5988            } else {
5989              !!!cp ('t299.2');
5990            }
5991    
5992            ## Stop parsing.
5993            last B;
5994        } else {        } else {
5995          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5996        }        }
# Line 5135  sub _tree_construction_main ($) { Line 6006  sub _tree_construction_main ($) {
6006            unless (length $token->{data}) {            unless (length $token->{data}) {
6007              !!!cp ('t300');              !!!cp ('t300');
6008              !!!next-token;              !!!next-token;
6009              redo B;              next B;
6010            }            }
6011          }          }
6012                    
6013          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6014            !!!cp ('t301');            !!!cp ('t301');
6015            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6016    
6017            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6018          } else {          } else {
# Line 5149  sub _tree_construction_main ($) { Line 6020  sub _tree_construction_main ($) {
6020          }          }
6021                    
6022          ## "after body" insertion mode          ## "after body" insertion mode
6023          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6024    
6025          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6026          ## reprocess          ## reprocess
6027          redo B;          next B;
6028        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6029          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6030            !!!cp ('t303');            !!!cp ('t303');
6031            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6032                        
6033            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6034          } else {          } else {
# Line 5165  sub _tree_construction_main ($) { Line 6036  sub _tree_construction_main ($) {
6036          }          }
6037    
6038          ## "after body" insertion mode          ## "after body" insertion mode
6039          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6040    
6041          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6042            !!!ack-later;
6043          ## reprocess          ## reprocess
6044          redo B;          next B;
6045        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6046          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6047            !!!cp ('t305');            !!!cp ('t305');
6048            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6049                        
6050            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6051            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5185  sub _tree_construction_main ($) { Line 6057  sub _tree_construction_main ($) {
6057          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6058            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6059              !!!cp ('t307');              !!!cp ('t307');
6060              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6061              ## Ignore the token              ## Ignore the token
6062              !!!next-token;              !!!next-token;
6063              redo B;              next B;
6064            } else {            } else {
6065              !!!cp ('t308');              !!!cp ('t308');
6066              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6067              !!!next-token;              !!!next-token;
6068              redo B;              next B;
6069            }            }
6070          } else {          } else {
6071            !!!cp ('t309');            !!!cp ('t309');
6072            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6073    
6074            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6075            ## reprocess            ## reprocess
6076            redo B;            next B;
6077          }          }
6078          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6079            !!!cp ('t309.2');
6080            ## Stop parsing
6081            last B;
6082        } else {        } else {
6083          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6084        }        }
# Line 5214  sub _tree_construction_main ($) { Line 6090  sub _tree_construction_main ($) {
6090            unless (length $token->{data}) {            unless (length $token->{data}) {
6091              !!!cp ('t310');              !!!cp ('t310');
6092              !!!next-token;              !!!next-token;
6093              redo B;              next B;
6094            }            }
6095          }          }
6096                    
6097          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6098            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6099              !!!cp ('t311');              !!!cp ('t311');
6100              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6101            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6102              !!!cp ('t312');              !!!cp ('t312');
6103              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6104            } else { # "after html frameset"            } else { # "after html frameset"
6105              !!!cp ('t313');              !!!cp ('t313');
6106              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6107    
6108              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6109              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6110              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6111            }            }
6112                        
6113            ## Ignore the token.            ## Ignore the token.
# Line 5242  sub _tree_construction_main ($) { Line 6118  sub _tree_construction_main ($) {
6118              !!!cp ('t315');              !!!cp ('t315');
6119              !!!next-token;              !!!next-token;
6120            }            }
6121            redo B;            next B;
6122          }          }
6123                    
6124          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6125        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6126          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6127            !!!cp ('t316');            !!!cp ('t316');
6128            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6129    
6130            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6131            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5260  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6137              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6138            !!!cp ('t318');            !!!cp ('t318');
6139            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6140              !!!nack ('t318.1');
6141            !!!next-token;            !!!next-token;
6142            redo B;            next B;
6143          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6144                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6145            !!!cp ('t319');            !!!cp ('t319');
6146            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6147            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6148              !!!ack ('t319.1');
6149            !!!next-token;            !!!next-token;
6150            redo B;            next B;
6151          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6152            !!!cp ('t320');            !!!cp ('t320');
6153            ## NOTE: As if in body.            ## NOTE: As if in body.
6154            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6155            redo B;            next B;
6156          } else {          } else {
6157            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6158              !!!cp ('t321');              !!!cp ('t321');
6159              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6160            } else {            } else {
6161              !!!cp ('t322');              !!!cp ('t322');
6162              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6163            }            }
6164            ## Ignore the token            ## Ignore the token
6165              !!!nack ('t322.1');
6166            !!!next-token;            !!!next-token;
6167            redo B;            next B;
6168          }          }
6169        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6170          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6171            !!!cp ('t323');            !!!cp ('t323');
6172            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6173    
6174            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6175            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5300  sub _tree_construction_main ($) { Line 6179  sub _tree_construction_main ($) {
6179    
6180          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6181              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6182            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6183                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6184              !!!cp ('t325');              !!!cp ('t325');
6185              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6186              ## Ignore the token              ## Ignore the token
6187              !!!next-token;              !!!next-token;
6188            } else {            } else {
# Line 5313  sub _tree_construction_main ($) { Line 6192  sub _tree_construction_main ($) {
6192            }            }
6193    
6194            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6195                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6196              !!!cp ('t327');              !!!cp ('t327');
6197              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6198            } else {            } else {
6199              !!!cp ('t328');              !!!cp ('t328');
6200            }            }
6201            redo B;            next B;
6202          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6203                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6204            !!!cp ('t329');            !!!cp ('t329');
6205            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6206            !!!next-token;            !!!next-token;
6207            redo B;            next B;
6208          } else {          } else {
6209            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6210              !!!cp ('t330');              !!!cp ('t330');
6211              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6212            } else {            } else {
6213              !!!cp ('t331');              !!!cp ('t331');
6214              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6215            }            }
6216            ## Ignore the token            ## Ignore the token
6217            !!!next-token;            !!!next-token;
6218            redo B;            next B;
6219            }
6220          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6221            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6222                    @{$self->{open_elements}} == 1) { # redundant, maybe
6223              !!!cp ('t331.1');
6224              !!!parse-error (type => 'in body:#eof', token => $token);
6225            } else {
6226              !!!cp ('t331.2');
6227          }          }
6228            
6229            ## Stop parsing
6230            last B;
6231        } else {        } else {
6232          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6233        }        }
# Line 5352  sub _tree_construction_main ($) { Line 6242  sub _tree_construction_main ($) {
6242        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6243          !!!cp ('t332');          !!!cp ('t332');
6244          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6245          $script_start_tag->($insert);          $script_start_tag->();
6246          redo B;          next B;
6247        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6248          !!!cp ('t333');          !!!cp ('t333');
6249          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6250          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6251          redo B;          next B;
6252        } elsif ({        } elsif ({
6253                  base => 1, link => 1,                  base => 1, link => 1,
6254                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6255          !!!cp ('t334');          !!!cp ('t334');
6256          ## 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
6257          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6258          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6259            !!!ack ('t334.1');
6260          !!!next-token;          !!!next-token;
6261          redo B;          next B;
6262        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6263          ## 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
6264          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6265          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.
6266    
6267          unless ($self->{confident}) {          unless ($self->{confident}) {
6268            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6269              !!!cp ('t335');              !!!cp ('t335');
6270                ## NOTE: Whether the encoding is supported or not is handled
6271                ## in the {change_encoding} callback.
6272              $self->{change_encoding}              $self->{change_encoding}
6273                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6274                            
6275              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6276                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6277                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6278                                           ->{has_reference});                                           ->{has_reference});
6279            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6280              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6281                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6282                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6283                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6284                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6285                !!!cp ('t336');                !!!cp ('t336');
6286                  ## NOTE: Whether the encoding is supported or not is handled
6287                  ## in the {change_encoding} callback.
6288                $self->{change_encoding}                $self->{change_encoding}
6289                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6290                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6291                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6292                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5416  sub _tree_construction_main ($) { Line 6310  sub _tree_construction_main ($) {
6310            }            }
6311          }          }
6312    
6313            !!!ack ('t338.1');
6314          !!!next-token;          !!!next-token;
6315          redo B;          next B;
6316        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6317          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6318          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6319          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6320            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6321        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6322          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6323                                
6324          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6325              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6326            !!!cp ('t342');            !!!cp ('t342');
6327            ## Ignore the token            ## Ignore the token
6328          } else {          } else {
# Line 5450  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336              }              }
6337            }            }
6338          }          }
6339            !!!nack ('t343.1');
6340          !!!next-token;          !!!next-token;
6341          redo B;          next B;
6342        } elsif ({        } elsif ({
6343                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6344                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6345                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6346                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6347                  pre => 1,                  pre => 1, listing => 1,
6348                    form => 1,
6349                    table => 1,
6350                    hr => 1,
6351                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6352            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6353              !!!cp ('t350');
6354              !!!parse-error (type => 'in form:form', token => $token);
6355              ## Ignore the token
6356              !!!nack ('t350.1');
6357              !!!next-token;
6358              next B;
6359            }
6360    
6361          ## has a p element in scope          ## has a p element in scope
6362          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6363            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6364              !!!cp ('t344');              !!!cp ('t344');
6365              !!!back-token;              !!!back-token; # <form>
6366              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6367              redo B;                        line => $token->{line}, column => $token->{column}};
6368            } elsif ({              next B;
6369                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6370              !!!cp ('t345');              !!!cp ('t345');
6371              last INSCOPE;              last INSCOPE;
6372            }            }
6373          } # INSCOPE          } # INSCOPE
6374                        
6375          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6376          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6377              !!!nack ('t346.1');
6378            !!!next-token;            !!!next-token;
6379            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6380              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5488  sub _tree_construction_main ($) { Line 6387  sub _tree_construction_main ($) {
6387            } else {            } else {
6388              !!!cp ('t348');              !!!cp ('t348');
6389            }            }
6390          } else {          } elsif ($token->{tag_name} eq 'form') {
6391            !!!cp ('t347');            !!!cp ('t347.1');
6392              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6393    
6394              !!!nack ('t347.2');
6395            !!!next-token;            !!!next-token;
6396          }          } elsif ($token->{tag_name} eq 'table') {
6397          redo B;            !!!cp ('t382');
6398        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6399          if (defined $self->{form_element}) {            
6400            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6401            !!!parse-error (type => 'in form:form');  
6402            ## Ignore the token            !!!nack ('t382.1');
6403              !!!next-token;
6404            } elsif ($token->{tag_name} eq 'hr') {
6405              !!!cp ('t386');
6406              pop @{$self->{open_elements}};
6407            
6408              !!!nack ('t386.1');
6409            !!!next-token;            !!!next-token;
           redo B;  
6410          } else {          } else {
6411            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6412            !!!next-token;            !!!next-token;
           redo B;  
6413          }          }
6414        } elsif ($token->{tag_name} eq 'li') {          next B;
6415          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6416          ## has a p element in scope          ## has a p element in scope
6417          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6418            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6419              !!!cp ('t353');              !!!cp ('t353');
6420              !!!back-token;              !!!back-token; # <x>
6421              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6422              redo B;                        line => $token->{line}, column => $token->{column}};
6423            } elsif ({              next B;
6424                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6425              !!!cp ('t354');              !!!cp ('t354');
6426              last INSCOPE;              last INSCOPE;
6427            }            }
# Line 5542  sub _tree_construction_main ($) { Line 6430  sub _tree_construction_main ($) {
6430          ## Step 1          ## Step 1
6431          my $i = -1;          my $i = -1;
6432          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6433            my $li_or_dtdd = {li => {li => 1},
6434                              dt => {dt => 1, dd => 1},
6435                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6436          LI: {          LI: {
6437            ## Step 2            ## Step 2
6438            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6439              if ($i != -1) {              if ($i != -1) {
6440                !!!cp ('t355');                !!!cp ('t355');
6441                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6442                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6443                                      ->manakai_local_name,
6444                                  token => $token);
6445              } else {              } else {
6446                !!!cp ('t356');                !!!cp ('t356');
6447              }              }
# Line 5559  sub _tree_construction_main ($) { Line 6452  sub _tree_construction_main ($) {
6452            }            }
6453                        
6454            ## Step 3            ## Step 3
6455            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6456                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6457                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6458                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6459                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6460                  not ($node->[1] & DIV_EL)) {
6461              !!!cp ('t358');              !!!cp ('t358');
6462              last LI;              last LI;
6463            }            }
# Line 5575  sub _tree_construction_main ($) { Line 6469  sub _tree_construction_main ($) {
6469            redo LI;            redo LI;
6470          } # LI          } # LI
6471                        
6472          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6473            !!!nack ('t359.1');
6474          !!!next-token;          !!!next-token;
6475          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6476        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6477          ## has a p element in scope          ## has a p element in scope
6478          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6479            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6480              !!!cp ('t367');              !!!cp ('t367');
6481              !!!back-token;              !!!back-token; # <plaintext>
6482              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6483              redo B;                        line => $token->{line}, column => $token->{column}};
6484            } elsif ({              next B;
6485                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6486              !!!cp ('t368');              !!!cp ('t368');
6487              last INSCOPE;              last INSCOPE;
6488            }            }
6489          } # INSCOPE          } # INSCOPE
6490                        
6491          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6492                        
6493          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6494                        
6495            !!!nack ('t368.1');
6496          !!!next-token;          !!!next-token;
6497          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6498        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6499          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6500            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6501            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6502              !!!cp ('t371');              !!!cp ('t371');
6503              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6504                            
6505              !!!back-token;              !!!back-token; # <a>
6506              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6507              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6508                $formatting_end_tag->($token);
6509                            
6510              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6511                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5738  sub _tree_construction_main ($) { Line 6530  sub _tree_construction_main ($) {
6530                        
6531          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6532    
6533          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6534          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6535    
6536            !!!nack ('t374.1');
6537          !!!next-token;          !!!next-token;
6538          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6539        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6540          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6541    
6542          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6543          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6544            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6545            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6546              !!!cp ('t376');              !!!cp ('t376');
6547              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6548              !!!back-token;              !!!back-token; # <nobr>
6549              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6550              redo B;                        line => $token->{line}, column => $token->{column}};
6551            } elsif ({              next B;
6552                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6553              !!!cp ('t377');              !!!cp ('t377');
6554              last INSCOPE;              last INSCOPE;
6555            }            }
6556          } # INSCOPE          } # INSCOPE
6557                    
6558          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6559          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6560                    
6561            !!!nack ('t377.1');
6562          !!!next-token;          !!!next-token;
6563          redo B;          next B;
6564        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6565          ## has a button element in scope          ## has a button element in scope
6566          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6567            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6568            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6569              !!!cp ('t378');              !!!cp ('t378');
6570              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6571              !!!back-token;              !!!back-token; # <button>
6572              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6573              redo B;                        line => $token->{line}, column => $token->{column}};
6574            } elsif ({              next B;
6575                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6576              !!!cp ('t379');              !!!cp ('t379');
6577              last INSCOPE;              last INSCOPE;
6578            }            }
# Line 5803  sub _tree_construction_main ($) { Line 6580  sub _tree_construction_main ($) {
6580                        
6581          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6582                        
6583          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6584          push @$active_formatting_elements, ['#marker', ''];  
6585            ## TODO: associate with $self->{form_element} if defined
6586    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6587          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6588            
6589          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6590          !!!next-token;          !!!next-token;
6591          redo B;          next B;
6592        } elsif ({        } elsif ({
6593                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6594                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6595                  image => 1,                  noembed => 1,
6596                    noframes => 1,
6597                    noscript => 0, ## TODO: 1 if scripting is enabled
6598                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6599          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6600            !!!cp ('t384');            !!!cp ('t381');
6601            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6602          } else {          } else {
6603            !!!cp ('t385');            !!!cp ('t399');
6604          }          }
6605            ## NOTE: There is an "as if in body" code clone.
6606          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6607          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6608        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6609          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6610                    
6611          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6612            !!!cp ('t389');            !!!cp ('t389');
6613            ## Ignore the token            ## Ignore the token
6614              !!!nack ('t389'); ## NOTE: Not acknowledged.
6615            !!!next-token;            !!!next-token;
6616            redo B;            next B;
6617          } else {          } else {
6618            my $at = $token->{attributes};            my $at = $token->{attributes};
6619            my $form_attrs;            my $form_attrs;
# Line 5917  sub _tree_construction_main ($) { Line 6624  sub _tree_construction_main ($) {
6624            delete $at->{prompt};            delete $at->{prompt};
6625            my @tokens = (            my @tokens = (
6626                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6627                           attributes => $form_attrs},                           attributes => $form_attrs,
6628                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6629                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6630                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6631                            {type => START_TAG_TOKEN, tag_name => 'p',
6632                             line => $token->{line}, column => $token->{column}},
6633                            {type => START_TAG_TOKEN, tag_name => 'label',
6634                             line => $token->{line}, column => $token->{column}},
6635                         );                         );
6636            if ($prompt_attr) {            if ($prompt_attr) {
6637              !!!cp ('t390');              !!!cp ('t390');
6638              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6639                               #line => $token->{line}, column => $token->{column},
6640                              };
6641            } else {            } else {
6642              !!!cp ('t391');              !!!cp ('t391');
6643              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6644                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6645                               #line => $token->{line}, column => $token->{column},
6646                              }; # SHOULD
6647              ## TODO: make this configurable              ## TODO: make this configurable
6648            }            }
6649            push @tokens,            push @tokens,
6650                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6651                             line => $token->{line}, column => $token->{column}},
6652                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6653                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6654                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6655                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6656                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6657            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6658                             line => $token->{line}, column => $token->{column}},
6659                            {type => END_TAG_TOKEN, tag_name => 'form',
6660                             line => $token->{line}, column => $token->{column}};
6661              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6662            !!!back-token (@tokens);            !!!back-token (@tokens);
6663            redo B;            !!!next-token;
6664              next B;
6665          }          }
6666        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6667          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6668          my $el;          my $el;
6669          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6670                    
6671          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6672          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5954  sub _tree_construction_main ($) { Line 6675  sub _tree_construction_main ($) {
6675          $insert->($el);          $insert->($el);
6676                    
6677          my $text = '';          my $text = '';
6678            !!!nack ('t392.1');
6679          !!!next-token;          !!!next-token;
6680          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6681            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5984  sub _tree_construction_main ($) { Line 6706  sub _tree_construction_main ($) {
6706            ## Ignore the token            ## Ignore the token
6707          } else {          } else {
6708            !!!cp ('t398');            !!!cp ('t398');
6709            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6710          }          }
6711          !!!next-token;          !!!next-token;
6712          redo B;          next B;
6713        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6714                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6715          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6716    
6717            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6718    
6719            ## "adjust foreign attributes" - done in insert-element-f
6720                    
6721          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6722                    
6723          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6724              pop @{$self->{open_elements}};
6725              !!!ack ('t398.1');
6726            } else {
6727              !!!cp ('t398.2');
6728              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6729              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6730              ## mode, "in body" (not "in foreign content") secondary insertion
6731              ## mode, maybe.
6732            }
6733    
6734          !!!next-token;          !!!next-token;
6735          redo B;          next B;
6736        } elsif ({        } elsif ({
6737                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6738                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6014  sub _tree_construction_main ($) { Line 6740  sub _tree_construction_main ($) {
6740                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6741                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6742          !!!cp ('t401');          !!!cp ('t401');
6743          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6744          ## Ignore the token          ## Ignore the token
6745            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6746          !!!next-token;          !!!next-token;
6747          redo B;          next B;
6748                    
6749          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6750        } else {        } else {
6751          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6752              !!!cp ('t384');
6753              !!!parse-error (type => 'image', token => $token);
6754              $token->{tag_name} = 'img';
6755            } else {
6756              !!!cp ('t385');
6757            }
6758    
6759            ## NOTE: There is an "as if <br>" code clone.
6760          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6761                    
6762          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6763    
6764            if ({
6765                 applet => 1, marquee => 1, object => 1,
6766                }->{$token->{tag_name}}) {
6767              !!!cp ('t380');
6768              push @$active_formatting_elements, ['#marker', ''];
6769              !!!nack ('t380.1');
6770            } elsif ({
6771                      b => 1, big => 1, em => 1, font => 1, i => 1,
6772                      s => 1, small => 1, strile => 1,
6773                      strong => 1, tt => 1, u => 1,
6774                     }->{$token->{tag_name}}) {
6775              !!!cp ('t375');
6776              push @$active_formatting_elements, $self->{open_elements}->[-1];
6777              !!!nack ('t375.1');
6778            } elsif ($token->{tag_name} eq 'input') {
6779              !!!cp ('t388');
6780              ## TODO: associate with $self->{form_element} if defined
6781              pop @{$self->{open_elements}};
6782              !!!ack ('t388.2');
6783            } elsif ({
6784                      area => 1, basefont => 1, bgsound => 1, br => 1,
6785                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6786                      #image => 1,
6787                     }->{$token->{tag_name}}) {
6788              !!!cp ('t388.1');
6789              pop @{$self->{open_elements}};
6790              !!!ack ('t388.3');
6791            } elsif ($token->{tag_name} eq 'select') {
6792              ## TODO: associate with $self->{form_element} if defined
6793            
6794              if ($self->{insertion_mode} & TABLE_IMS or
6795                  $self->{insertion_mode} & BODY_TABLE_IMS or
6796                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6797                !!!cp ('t400.1');
6798                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6799              } else {
6800                !!!cp ('t400.2');
6801                $self->{insertion_mode} = IN_SELECT_IM;
6802              }
6803              !!!nack ('t400.3');
6804            } else {
6805              !!!nack ('t402');
6806            }
6807                    
6808          !!!next-token;          !!!next-token;
6809          redo B;          next B;
6810        }        }
6811      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6812        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6813          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6814              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6815            for (@{$self->{open_elements}}) {          INSCOPE: {
6816              unless ({            for (reverse @{$self->{open_elements}}) {
6817                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6818                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6819                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6820                      }->{$_->[1]}) {                last INSCOPE;
6821                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6822                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6823              } else {                last;
               !!!cp ('t404');  
6824              }              }
6825            }            }
6826    
6827            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6828                              value => $token->{tag_name}, token => $token);
6829              ## NOTE: Ignore the token.
6830            !!!next-token;            !!!next-token;
6831            redo B;            next B;
6832          } else {          } # INSCOPE
6833            !!!cp ('t405');  
6834            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
6835            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6836            !!!next-token;              !!!cp ('t403');
6837            redo B;              !!!parse-error (type => 'not closed',
6838                                value => $_->[0]->manakai_local_name,
6839                                token => $token);
6840                last;
6841              } else {
6842                !!!cp ('t404');
6843              }
6844          }          }
6845    
6846            $self->{insertion_mode} = AFTER_BODY_IM;
6847            !!!next-token;
6848            next B;
6849        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6850          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
6851            ## up-to-date, though it has same effect as speced.
6852            if (@{$self->{open_elements}} > 1 and
6853                $self->{open_elements}->[1]->[1] & BODY_EL) {
6854            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6855            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6856              !!!cp ('t406');              !!!cp ('t406');
6857              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6858                                value => $self->{open_elements}->[1]->[0]
6859                                    ->manakai_local_name,
6860                                token => $token);
6861            } else {            } else {
6862              !!!cp ('t407');              !!!cp ('t407');
6863            }            }
6864            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6865            ## reprocess            ## reprocess
6866            redo B;            next B;
6867          } else {          } else {
6868            !!!cp ('t408');            !!!cp ('t408');
6869            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6870            ## Ignore the token            ## Ignore the token
6871            !!!next-token;            !!!next-token;
6872            redo B;            next B;
6873          }          }
6874        } elsif ({        } elsif ({
6875                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6876                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6877                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6878                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6879                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6880                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6881          ## has an element in scope          ## has an element in scope
6882          my $i;          my $i;
6883          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6884            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6885            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6886              !!!cp ('t410');              !!!cp ('t410');
6887              $i = $_;              $i = $_;
6888              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6889            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6890              !!!cp ('t411');              !!!cp ('t411');
6891              last INSCOPE;              last INSCOPE;
6892            }            }
6893          } # INSCOPE          } # INSCOPE
6894            
6895          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6896            if (defined $i) {            !!!cp ('t413');
6897              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6898            } else {
6899              ## Step 1. generate implied end tags
6900              while ({
6901                      dd => ($token->{tag_name} ne 'dd'),
6902                      dt => ($token->{tag_name} ne 'dt'),
6903                      li => ($token->{tag_name} ne 'li'),
6904                      p => 1,
6905                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6906                !!!cp ('t409');
6907                pop @{$self->{open_elements}};
6908              }
6909    
6910              ## Step 2.
6911              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6912                      ne $token->{tag_name}) {
6913              !!!cp ('t412');              !!!cp ('t412');
6914              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6915                                value => $self->{open_elements}->[-1]->[0]
6916                                    ->manakai_local_name,
6917                                token => $token);
6918            } else {            } else {
6919              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6920            }            }
6921          }  
6922                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6923            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6924          } elsif ($token->{tag_name} eq 'p') {  
6925            !!!cp ('t415');            ## Step 4.
6926            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6927            my $el;                if {
6928            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6929            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6930          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6931          !!!next-token;          !!!next-token;
6932          redo B;          next B;
6933        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6934            undef $self->{form_element};
6935    
6936          ## has an element in scope          ## has an element in scope
6937            my $i;
6938          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6939            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6940            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
   
                  ## NOTE: The following elements never appear here, maybe.  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6941              !!!cp ('t418');              !!!cp ('t418');
6942                $i = $_;
6943              last INSCOPE;              last INSCOPE;
6944            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6945              !!!cp ('t419');              !!!cp ('t419');
6946              last INSCOPE;              last INSCOPE;
6947            }            }
6948          } # INSCOPE          } # INSCOPE
6949            
6950          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6951            !!!cp ('t421');            !!!cp ('t421');
6952            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6953            } else {
6954              ## Step 1. generate implied end tags
6955              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6956                !!!cp ('t417');
6957                pop @{$self->{open_elements}};
6958              }
6959              
6960              ## Step 2.
6961              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6962                      ne $token->{tag_name}) {
6963                !!!cp ('t417.1');
6964                !!!parse-error (type => 'not closed',
6965                                value => $self->{open_elements}->[-1]->[0]
6966                                    ->manakai_local_name,
6967                                token => $token);
6968              } else {
6969                !!!cp ('t420');
6970              }  
6971              
6972              ## Step 3.
6973              splice @{$self->{open_elements}}, $i;
6974          }          }
6975    
         undef $self->{form_element};  
6976          !!!next-token;          !!!next-token;
6977          redo B;          next B;
6978        } elsif ({        } elsif ({
6979                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6980                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6193  sub _tree_construction_main ($) { Line 6982  sub _tree_construction_main ($) {
6982          my $i;          my $i;
6983          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6984            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6985            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
6986              !!!cp ('t423');              !!!cp ('t423');
6987              $i = $_;              $i = $_;
6988              last INSCOPE;              last INSCOPE;
6989            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6990              !!!cp ('t424');              !!!cp ('t424');
6991              last INSCOPE;              last INSCOPE;
6992            }            }
6993          } # INSCOPE          } # INSCOPE
6994    
6995            unless (defined $i) { # has an element in scope
6996              !!!cp ('t425.1');
6997              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6998            } else {
6999              ## Step 1. generate implied end tags
7000              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7001                !!!cp ('t422');
7002                pop @{$self->{open_elements}};
7003              }
7004              
7005              ## Step 2.
7006              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7007                      ne $token->{tag_name}) {
7008                !!!cp ('t425');
7009                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7010              } else {
7011                !!!cp ('t426');
7012              }
7013    
7014              ## Step 3.
7015              splice @{$self->{open_elements}}, $i;
7016            }
7017                    
7018          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7019            !!!cp ('t425');          next B;
7020            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7021            ## has an element in scope
7022            my $i;
7023            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7024              my $node = $self->{open_elements}->[$_];
7025              if ($node->[1] & P_EL) {
7026                !!!cp ('t410.1');
7027                $i = $_;
7028                last INSCOPE;
7029              } elsif ($node->[1] & SCOPING_EL) {
7030                !!!cp ('t411.1');
7031                last INSCOPE;
7032              }
7033            } # INSCOPE
7034    
7035            if (defined $i) {
7036              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7037                      ne $token->{tag_name}) {
7038                !!!cp ('t412.1');
7039                !!!parse-error (type => 'not closed',
7040                                value => $self->{open_elements}->[-1]->[0]
7041                                    ->manakai_local_name,
7042                                token => $token);
7043              } else {
7044                !!!cp ('t414.1');
7045              }
7046    
7047              splice @{$self->{open_elements}}, $i;
7048          } else {          } else {
7049            !!!cp ('t426');            !!!cp ('t413.1');
7050              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7051    
7052              !!!cp ('t415.1');
7053              ## As if <p>, then reprocess the current token
7054              my $el;
7055              !!!create-element ($el, $HTML_NS, 'p',, $token);
7056              $insert->($el);
7057              ## NOTE: Not inserted into |$self->{open_elements}|.
7058          }          }
7059            
         splice @{$self->{open_elements}}, $i if defined $i;  
7060          !!!next-token;          !!!next-token;
7061          redo B;          next B;
7062        } elsif ({        } elsif ({
7063                  a => 1,                  a => 1,
7064                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6238  sub _tree_construction_main ($) { Line 7066  sub _tree_construction_main ($) {
7066                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7067                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7068          !!!cp ('t427');          !!!cp ('t427');
7069          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7070          redo B;          next B;
7071        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7072          !!!cp ('t428');          !!!cp ('t428');
7073          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7074    
7075          ## As if <br>          ## As if <br>
7076          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7077                    
7078          my $el;          my $el;
7079          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7080          $insert->($el);          $insert->($el);
7081                    
7082          ## Ignore the token.          ## Ignore the token.
7083          !!!next-token;          !!!next-token;
7084          redo B;          next B;
7085        } elsif ({        } elsif ({
7086                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7087                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6267  sub _tree_construction_main ($) { Line 7095  sub _tree_construction_main ($) {
7095                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7096                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7097          !!!cp ('t429');          !!!cp ('t429');
7098          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7099          ## Ignore the token          ## Ignore the token
7100          !!!next-token;          !!!next-token;
7101          redo B;          next B;
7102                    
7103          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7104                    
# Line 6281  sub _tree_construction_main ($) { Line 7109  sub _tree_construction_main ($) {
7109    
7110          ## Step 2          ## Step 2
7111          S2: {          S2: {
7112            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7113              ## Step 1              ## Step 1
7114              ## generate implied end tags              ## generate implied end tags
7115              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7116                !!!cp ('t430');                !!!cp ('t430');
7117                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7118                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7119              }              }
7120                    
7121              ## Step 2              ## Step 2
7122              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7123                        ne $token->{tag_name}) {
7124                !!!cp ('t431');                !!!cp ('t431');
7125                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7126                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7127                                  value => $self->{open_elements}->[-1]->[0]
7128                                      ->manakai_local_name,
7129                                  token => $token);
7130              } else {              } else {
7131                !!!cp ('t432');                !!!cp ('t432');
7132              }              }
# Line 6313  sub _tree_construction_main ($) { Line 7138  sub _tree_construction_main ($) {
7138              last S2;              last S2;
7139            } else {            } else {
7140              ## Step 3              ## Step 3
7141              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7142                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7143                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7144                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7145                !!!cp ('t433');                !!!cp ('t433');
7146                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7147                ## Ignore the token                ## Ignore the token
7148                !!!next-token;                !!!next-token;
7149                last S2;                last S2;
# Line 6334  sub _tree_construction_main ($) { Line 7159  sub _tree_construction_main ($) {
7159            ## Step 5;            ## Step 5;
7160            redo S2;            redo S2;
7161          } # S2          } # S2
7162          redo B;          next B;
7163        }        }
7164      }      }
7165      redo B;      next B;
7166      } continue { # B
7167        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7168          ## NOTE: The code below is executed in cases where it does not have
7169          ## to be, but it it is harmless even in those cases.
7170          ## has an element in scope
7171          INSCOPE: {
7172            for (reverse 0..$#{$self->{open_elements}}) {
7173              my $node = $self->{open_elements}->[$_];
7174              if ($node->[1] & FOREIGN_EL) {
7175                last INSCOPE;
7176              } elsif ($node->[1] & SCOPING_EL) {
7177                last;
7178              }
7179            }
7180            
7181            ## NOTE: No foreign element in scope.
7182            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7183          } # INSCOPE
7184        }
7185    } # B    } # B
7186    
7187    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6383  sub set_inner_html ($$$) { Line 7227  sub set_inner_html ($$$) {
7227    
7228      ## Step 8 # MUST      ## Step 8 # MUST
7229      my $i = 0;      my $i = 0;
7230      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7231      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7232      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7233        my $self = shift;        my $self = shift;
7234    
# Line 6393  sub set_inner_html ($$$) { Line 7237  sub set_inner_html ($$$) {
7237    
7238        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7239        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7240        $column++;  
7241          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7242          $p->{column}++;
7243    
7244        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7245          $line++;          $p->{line}++;
7246          $column = 0;          $p->{column} = 0;
7247          !!!cp ('i1');          !!!cp ('i1');
7248        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7249          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7250          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7251          $line++;          $p->{line}++;
7252          $column = 0;          $p->{column} = 0;
7253          !!!cp ('i2');          !!!cp ('i2');
7254        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7255          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6412  sub set_inner_html ($$$) { Line 7258  sub set_inner_html ($$$) {
7258          !!!cp ('i4');          !!!cp ('i4');
7259          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7260          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7261          } elsif ($self->{next_char} <= 0x0008 or
7262                   (0x000E <= $self->{next_char} and
7263                    $self->{next_char} <= 0x001F) or
7264                   (0x007F <= $self->{next_char} and
7265                    $self->{next_char} <= 0x009F) or
7266                   (0xD800 <= $self->{next_char} and
7267                    $self->{next_char} <= 0xDFFF) or
7268                   (0xFDD0 <= $self->{next_char} and
7269                    $self->{next_char} <= 0xFDDF) or
7270                   {
7271                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7272                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7273                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7274                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7275                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7276                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7277                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7278                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7279                    0x10FFFE => 1, 0x10FFFF => 1,
7280                   }->{$self->{next_char}}) {
7281            !!!cp ('i4.1');
7282            !!!parse-error (type => 'control char', level => $self->{must_level});
7283    ## TODO: error type documentation
7284        }        }
7285      };      };
7286      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6419  sub set_inner_html ($$$) { Line 7288  sub set_inner_html ($$$) {
7288            
7289      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7290        my (%opt) = @_;        my (%opt) = @_;
7291        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7292          my $column = $opt{column};
7293          if (defined $opt{token} and defined $opt{token}->{line}) {
7294            $line = $opt{token}->{line};
7295            $column = $opt{token}->{column};
7296          }
7297          warn "Parse error ($opt{type}) at line $line column $column\n";
7298      };      };
7299      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7300        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7301      };      };
7302            
7303      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6446  sub set_inner_html ($$$) { Line 7321  sub set_inner_html ($$$) {
7321          unless defined $p->{content_model};          unless defined $p->{content_model};
7322          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7323    
7324      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7325          ## TODO: Foreign element OK?
7326    
7327      ## Step 3      ## Step 3
7328      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6456  sub set_inner_html ($$$) { Line 7332  sub set_inner_html ($$$) {
7332      $doc->append_child ($root);      $doc->append_child ($root);
7333    
7334      ## Step 5 # MUST      ## Step 5 # MUST
7335      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7336    
7337      undef $p->{head_element};      undef $p->{head_element};
7338    
# Line 6502  sub set_inner_html ($$$) { Line 7378  sub set_inner_html ($$$) {
7378      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7379    
7380      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7381    
7382        delete $p->{parse_error}; # delete loop
7383    } else {    } else {
7384      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";
7385    }    }

Legend:
Removed from v.1.84  
changed lines
  Added in v.1.138

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24