/[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.78 by wakaba, Mon Mar 3 11:56:18 2008 UTC revision 1.139 by wakaba, Sat May 24 04:26:27 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  require IO::Handle;
15    base => 1,  
16    link => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17    meta => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    hr => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    br => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    img => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    embed => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    param => 1,  
23    area => 1,  sub A_EL () { 0b1 }
24    col => 1,  sub ADDRESS_EL () { 0b10 }
25    input => 1,  sub BODY_EL () { 0b100 }
26    sub BUTTON_EL () { 0b1000 }
27    sub CAPTION_EL () { 0b10000 }
28    sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216    my $svg_attr_name = {
217      attributetype => 'attributeType',
218      basefrequency => 'baseFrequency',
219      baseprofile => 'baseProfile',
220      calcmode => 'calcMode',
221      clippathunits => 'clipPathUnits',
222      contentscripttype => 'contentScriptType',
223      contentstyletype => 'contentStyleType',
224      diffuseconstant => 'diffuseConstant',
225      edgemode => 'edgeMode',
226      externalresourcesrequired => 'externalResourcesRequired',
227      fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282  };  };
283    
284    my $foreign_attr_xname = {
285      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292      'xml:base' => [$XML_NS, ['xml', 'base']],
293      'xml:lang' => [$XML_NS, ['xml', 'lang']],
294      'xml:space' => [$XML_NS, ['xml', 'space']],
295      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297    };
298    
299    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
300    
301  my $c1_entity_char = {  my $c1_entity_char = {
302    0x80 => 0x20AC,    0x80 => 0x20AC,
303    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 333  my $c1_entity_char = {
333    0x9F => 0x0178,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
336  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
337      my $self = shift;
338      my $charset_name = shift;
339      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    } # parse_byte_string
342    
343    sub parse_byte_stream ($$$$;$) {
344    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
345    my $charset = shift;    my $charset_name = shift;
346    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
347    
348    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
349      my $self = shift;      my (%opt) = @_;
350      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
351      ## TODO: if $charset is supported    };
352      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
353    
354      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      ## Step 1        my $charset;
357      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
358        $charset = 'utf-8';    my ($char_stream, $e_status);
359    
360      SNIFFING: {
361    
362        ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376      }      }
377    
378      ## Step 2      ## Step 2
379      if (defined $self->{input_encoding} and      my $byte_buffer = '';
380          $self->{input_encoding} eq $charset) {      for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392        $self->{confident} = 1;        $self->{confident} = 1;
393        return;        last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408      }      }
409    
410      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
411          ':'.$charset, level => 'w');      ## TODO: <meta charset>
412    
413      ## Step 3      ## Step 5
414      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
415    
416      ## Step 4      ## Step 6
417      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521    }; # $self->{change_encoding}    }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
534    my $return;    my $return;
535    try {    try {
536      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
537    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
538      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
539      $s = \ (Encode::decode ($charset, $$bytes_s));      
540      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553      $self->{confident} = 1;      $self->{confident} = 1;
554      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556    };    };
557    return $return;    return $return;
558  } # parse_byte_string  } # parse_byte_stream
559    
560  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 566  sub parse_byte_string ($$$$;$) {
566  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
567  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
568    
569  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
579    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
580    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
583    
# Line 177  sub parse_string ($$$;$) { Line 588  sub parse_string ($$$;$) {
588        if defined $self->{input_encoding};        if defined $self->{input_encoding};
589    
590    my $i = 0;    my $i = 0;
591    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
592    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
593    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
594      my $self = shift;      my $self = shift;
595    
596      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
597      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
600      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
601      $column++;        $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609        ($self->{line_prev}, $self->{column_prev})
610            = ($self->{line}, $self->{column});
611        $self->{column}++;
612            
613      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614        $line++;        !!!cp ('j1');
615        $column = 0;        $self->{line}++;
616          $self->{column} = 0;
617      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
618        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
619          my $next = $input->getc;
620          if (defined $next and $next ne "\x0A") {
621            $self->{next_next_char} = $next;
622          }
623        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
624        $line++;        $self->{line}++;
625        $column = 0;        $self->{column} = 0;
626      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
627          !!!cp ('j3');
628        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
630          !!!cp ('j4');
631        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
632        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 656  sub parse_string ($$$;$) {
656    
657    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
658      my (%opt) = @_;      my (%opt) = @_;
659      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
660        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
661        warn "Parse error ($opt{type}) at line $line column $column\n";
662    };    };
663    $self->{parse_error} = sub {    $self->{parse_error} = sub {
664      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
665    };    };
666    
667    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    $self->_construct_tree;    $self->_construct_tree;
670    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
671    
672      delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680        must_level => 'm',
681        should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
688      $self->{next_char} = -1;      $self->{next_char} = -1;
689    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 745  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
745  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
747  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
752  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 763  sub TABLE_IMS ()      { 0b1000000 }
763  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
764  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
765  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
766    sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772    ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
774    ## NOTE: "after after body" insertion mode.
775  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
776    
777    ## NOTE: "after after frameset" insertion mode.
778  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
779    
780  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
781  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
782  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 790  sub IN_TABLE_IM () { TABLE_IMS }
790  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
791  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
792  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
793  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
794    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
795  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
796    
797  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 332  sub _initialize_tokenizer ($) { Line 804  sub _initialize_tokenizer ($) {
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_char}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
# Line 352  sub _initialize_tokenizer ($) { Line 825  sub _initialize_tokenizer ($) {
825  ##        ->{value}  ##        ->{value}
826  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
827  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 378  sub _initialize_tokenizer ($) { Line 858  sub _initialize_tokenizer ($) {
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
# Line 441  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          #          #
932        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
933          !!!cp (11);          !!!cp (11);
934          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
935                      line => $self->{line}, column => $self->{column}});
936          last A; ## TODO: ok?          last A; ## TODO: ok?
937        } else {        } else {
938          !!!cp (12);          !!!cp (12);
939        }        }
940        # Anything else        # Anything else
941        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
943                       line => $self->{line}, column => $self->{column},
944                      };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
947    
# Line 457  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950        redo A;        redo A;
951      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
# Line 465  sub _get_next_token ($) { Line 960  sub _get_next_token ($) {
960    
961        unless (defined $token) {        unless (defined $token) {
962          !!!cp (13);          !!!cp (13);
963          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                      line => $l, column => $c,
965                     });
966        } else {        } else {
967          !!!cp (14);          !!!cp (14);
968          !!!emit ($token);          !!!emit ($token);
# Line 484  sub _get_next_token ($) { Line 981  sub _get_next_token ($) {
981            ## reconsume            ## reconsume
982            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                        line => $self->{line_prev},
986                        column => $self->{column_prev},
987                       });
988    
989            redo A;            redo A;
990          }          }
# Line 504  sub _get_next_token ($) { Line 1004  sub _get_next_token ($) {
1004            !!!cp (19);            !!!cp (19);
1005            $self->{current_token}            $self->{current_token}
1006              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1007                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1008                   line => $self->{line_prev},
1009                   column => $self->{column_prev}};
1010            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
# Line 512  sub _get_next_token ($) { Line 1014  sub _get_next_token ($) {
1014                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1015            !!!cp (20);            !!!cp (20);
1016            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1017                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1018                                        line => $self->{line_prev},
1019                                        column => $self->{column_prev}};
1020            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1021            !!!next-input-character;            !!!next-input-character;
1022            redo A;            redo A;
1023          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1024            !!!cp (21);            !!!cp (21);
1025            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1026                              line => $self->{line_prev},
1027                              column => $self->{column_prev});
1028            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                        line => $self->{line_prev},
1033                        column => $self->{column_prev},
1034                       });
1035    
1036            redo A;            redo A;
1037          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1038            !!!cp (22);            !!!cp (22);
1039            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1040                              line => $self->{line_prev},
1041                              column => $self->{column_prev});
1042            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1043              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                        line => $self->{line_prev},
1045                                        column => $self->{column_prev},
1046                                       };
1047            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!cp (23);            !!!cp (23);
1051            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                        line => $self->{line_prev},
1059                        column => $self->{column_prev},
1060                       });
1061    
1062            redo A;            redo A;
1063          }          }
# Line 545  sub _get_next_token ($) { Line 1065  sub _get_next_token ($) {
1065          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1066        }        }
1067      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1068          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1069        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1070          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1071    
1072            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1073            my @next_char;            my @next_char;
1074            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 563  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1086                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1087    
1088                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                            line => $l, column => $c,
1090                           });
1091        
1092                redo A;                redo A;
1093              }              }
# Line 582  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1109              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110                          line => $l, column => $c,
1111                         });
1112              redo A;              redo A;
1113            } else {            } else {
1114              !!!cp (27);              !!!cp (27);
# Line 595  sub _get_next_token ($) { Line 1121  sub _get_next_token ($) {
1121            !!!cp (28);            !!!cp (28);
1122            # next-input-character is already done            # next-input-character is already done
1123            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1124            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                        line => $l, column => $c,
1126                       });
1127            redo A;            redo A;
1128          }          }
1129        }        }
# Line 603  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1132            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1133          !!!cp (29);          !!!cp (29);
1134          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1135                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1136                   tag_name => chr ($self->{next_char} + 0x0020),
1137                   line => $l, column => $c};
1138          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
# Line 612  sub _get_next_token ($) { Line 1142  sub _get_next_token ($) {
1142                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1143          !!!cp (30);          !!!cp (30);
1144          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1145                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1146                                      line => $l, column => $c};
1147          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1148          !!!next-input-character;          !!!next-input-character;
1149          redo A;          redo A;
1150        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1151          !!!cp (31);          !!!cp (31);
1152          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1153                            line => $self->{line_prev}, ## "<" in "</>"
1154                            column => $self->{column_prev} - 1);
1155          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1156          !!!next-input-character;          !!!next-input-character;
1157          redo A;          redo A;
# Line 628  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                      line => $l, column => $c,
1166                     });
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170          !!!cp (33);          !!!cp (33);
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1173            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174                                      line => $self->{line_prev}, # "<" of "</"
1175                                      column => $self->{column_prev} - 1,
1176                                     };
1177          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1178          redo A;          redo A;
1179        }        }
# Line 651  sub _get_next_token ($) { Line 1190  sub _get_next_token ($) {
1190        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1191          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1192            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1193            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1194          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1195            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 684  sub _get_next_token ($) { Line 1221  sub _get_next_token ($) {
1221          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1222          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1223            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 706  sub _get_next_token ($) { Line 1241  sub _get_next_token ($) {
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1247          redo A;          redo A;
1248        } else {        } else {
1249          !!!cp (44);          !!!cp (44);
# Line 741  sub _get_next_token ($) { Line 1266  sub _get_next_token ($) {
1266        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1267          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1268            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1269            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1270          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1271            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 764  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          !!!cp (49);          !!!cp (49);
1290          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1291                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1301          redo A;          redo A;
1302        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 819  sub _get_next_token ($) { Line 1332  sub _get_next_token ($) {
1332          } else {          } else {
1333            !!!cp (56);            !!!cp (56);
1334          }          }
1335          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1336                                value => ''};              = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
# Line 830  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!cp (57);            !!!cp (57);
1348            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351            !!!cp (58);            !!!cp (58);
# Line 859  sub _get_next_token ($) { Line 1374  sub _get_next_token ($) {
1374          $before_leave->();          $before_leave->();
1375          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            !!!cp (62);            !!!cp (62);
# Line 885  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1405          redo A;          redo A;
1406        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1408          $before_leave->();          $before_leave->();
1409          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1410            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1411            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1412          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1413            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 957  sub _get_next_token ($) { Line 1458  sub _get_next_token ($) {
1458        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1459          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1460            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1461            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1462          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1463            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 981  sub _get_next_token ($) { Line 1480  sub _get_next_token ($) {
1480        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          !!!cp (76);          !!!cp (76);
1483          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1484                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1485                   value => '',
1486                   line => $self->{line}, column => $self->{column}};
1487          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1494          redo A;          redo A;
1495        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1029  sub _get_next_token ($) { Line 1517  sub _get_next_token ($) {
1517          redo A;          redo A;
1518        } else {        } else {
1519          !!!cp (82);          !!!cp (82);
1520          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1521                                value => ''};              = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
# Line 1063  sub _get_next_token ($) { Line 1553  sub _get_next_token ($) {
1553        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1554          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1088  sub _get_next_token ($) { Line 1576  sub _get_next_token ($) {
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1577          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1624          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1666          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1228  sub _get_next_token ($) { Line 1710  sub _get_next_token ($) {
1710        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1711          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1253  sub _get_next_token ($) { Line 1733  sub _get_next_token ($) {
1733          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1734          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1735            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1736            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1737          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1738            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1325  sub _get_next_token ($) { Line 1803  sub _get_next_token ($) {
1803        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1804          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1805            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1806            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1807          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1808            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1347  sub _get_next_token ($) { Line 1823  sub _get_next_token ($) {
1823    
1824          redo A;          redo A;
1825        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828          !!!next-input-character;          !!!next-input-character;
         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  
1829          redo A;          redo A;
1830        } else {        } else {
1831          !!!cp (124);          !!!cp ('124.1');
1832          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1833          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1834          ## reconsume          ## reconsume
1835          redo A;          redo A;
1836        }        }
1837        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1838          if ($self->{next_char} == 0x003E) { # >
1839            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1840              !!!cp ('124.2');
1841              !!!parse-error (type => 'nestc', token => $self->{current_token});
1842              ## TODO: Different type than slash in start tag
1843              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1844              if ($self->{current_token}->{attributes}) {
1845                !!!cp ('124.4');
1846                !!!parse-error (type => 'end tag attribute');
1847              } else {
1848                !!!cp ('124.5');
1849              }
1850              ## TODO: Test |<title></title/>|
1851            } else {
1852              !!!cp ('124.3');
1853              $self->{self_closing} = 1;
1854            }
1855    
1856            $self->{state} = DATA_STATE;
1857            !!!next-input-character;
1858    
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860    
1861            redo A;
1862          } else {
1863            !!!cp ('124.4');
1864            !!!parse-error (type => 'nestc');
1865            ## TODO: This error type is wrong.
1866            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1867            ## Reconsume.
1868            redo A;
1869          }
1870      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1871        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1872                
1873        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1874          #my $token = {type => COMMENT_TOKEN, data => ''};
1875    
1876        BC: {        BC: {
1877          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1379  sub _get_next_token ($) { Line 1879  sub _get_next_token ($) {
1879            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1880            !!!next-input-character;            !!!next-input-character;
1881    
1882            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1883    
1884            redo A;            redo A;
1885          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1387  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1888            ## reconsume            ## reconsume
1889    
1890            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1891    
1892            redo A;            redo A;
1893          } else {          } else {
1894            !!!cp (126);            !!!cp (126);
1895            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1896            !!!next-input-character;            !!!next-input-character;
1897            redo BC;            redo BC;
1898          }          }
# Line 1402  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1903        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1904    
1905          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1906    
1907        my @next_char;        my @next_char;
1908        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1909                
# Line 1410  sub _get_next_token ($) { Line 1912  sub _get_next_token ($) {
1912          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1913          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1914            !!!cp (127);            !!!cp (127);
1915            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1916                                        line => $l, column => $c,
1917                                       };
1918            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1919            !!!next-input-character;            !!!next-input-character;
1920            redo A;            redo A;
# Line 1446  sub _get_next_token ($) { Line 1950  sub _get_next_token ($) {
1950                      !!!cp (129);                      !!!cp (129);
1951                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1952                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1953                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1954                                                  quirks => 1,
1955                                                  line => $l, column => $c,
1956                                                 };
1957                      !!!next-input-character;                      !!!next-input-character;
1958                      redo A;                      redo A;
1959                    } else {                    } else {
# Line 1466  sub _get_next_token ($) { Line 1974  sub _get_next_token ($) {
1974          } else {          } else {
1975            !!!cp (135);            !!!cp (135);
1976          }          }
1977          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1978                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1979                   $self->{next_char} == 0x005B) { # [
1980            !!!next-input-character;
1981            push @next_char, $self->{next_char};
1982            if ($self->{next_char} == 0x0043) { # C
1983              !!!next-input-character;
1984              push @next_char, $self->{next_char};
1985              if ($self->{next_char} == 0x0044) { # D
1986                !!!next-input-character;
1987                push @next_char, $self->{next_char};
1988                if ($self->{next_char} == 0x0041) { # A
1989                  !!!next-input-character;
1990                  push @next_char, $self->{next_char};
1991                  if ($self->{next_char} == 0x0054) { # T
1992                    !!!next-input-character;
1993                    push @next_char, $self->{next_char};
1994                    if ($self->{next_char} == 0x0041) { # A
1995                      !!!next-input-character;
1996                      push @next_char, $self->{next_char};
1997                      if ($self->{next_char} == 0x005B) { # [
1998                        !!!cp (135.1);
1999                        $self->{state} = CDATA_BLOCK_STATE;
2000                        !!!next-input-character;
2001                        redo A;
2002                      } else {
2003                        !!!cp (135.2);
2004                      }
2005                    } else {
2006                      !!!cp (135.3);
2007                    }
2008                  } else {
2009                    !!!cp (135.4);                
2010                  }
2011                } else {
2012                  !!!cp (135.5);
2013                }
2014              } else {
2015                !!!cp (135.6);
2016              }
2017            } else {
2018              !!!cp (135.7);
2019            }
2020        } else {        } else {
2021          !!!cp (136);          !!!cp (136);
2022        }        }
# Line 1474  sub _get_next_token ($) { Line 2025  sub _get_next_token ($) {
2025        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2026        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2027        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2028          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2029                                    line => $l, column => $c,
2030                                   };
2031        redo A;        redo A;
2032                
2033        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1597  sub _get_next_token ($) { Line 2151  sub _get_next_token ($) {
2151          redo A;          redo A;
2152        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2153          !!!cp (152);          !!!cp (152);
2154          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2155                            line => $self->{line_prev},
2156                            column => $self->{column_prev});
2157          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2158          ## Stay in the state          ## Stay in the state
2159          !!!next-input-character;          !!!next-input-character;
# Line 1613  sub _get_next_token ($) { Line 2169  sub _get_next_token ($) {
2169          redo A;          redo A;
2170        } else {        } else {
2171          !!!cp (154);          !!!cp (154);
2172          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2173                            line => $self->{line_prev},
2174                            column => $self->{column_prev});
2175          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2176          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2177          !!!next-input-character;          !!!next-input-character;
# Line 1652  sub _get_next_token ($) { Line 2210  sub _get_next_token ($) {
2210          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212    
2213          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1661  sub _get_next_token ($) { Line 2219  sub _get_next_token ($) {
2219          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2220          ## reconsume          ## reconsume
2221    
2222          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2223    
2224          redo A;          redo A;
2225        } else {        } else {
2226          !!!cp (160);          !!!cp (160);
2227          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2228              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2229  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2230          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2231          !!!next-input-character;          !!!next-input-character;
# Line 2186  sub _get_next_token ($) { Line 2741  sub _get_next_token ($) {
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        }        }
2744        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2745          my $s = '';
2746          
2747          my ($l, $c) = ($self->{line}, $self->{column});
2748    
2749          CS: while ($self->{next_char} != -1) {
2750            if ($self->{next_char} == 0x005D) { # ]
2751              !!!next-input-character;
2752              if ($self->{next_char} == 0x005D) { # ]
2753                !!!next-input-character;
2754                MDC: {
2755                  if ($self->{next_char} == 0x003E) { # >
2756                    !!!cp (221.1);
2757                    !!!next-input-character;
2758                    last CS;
2759                  } elsif ($self->{next_char} == 0x005D) { # ]
2760                    !!!cp (221.2);
2761                    $s .= ']';
2762                    !!!next-input-character;
2763                    redo MDC;
2764                  } else {
2765                    !!!cp (221.3);
2766                    $s .= ']]';
2767                    #
2768                  }
2769                } # MDC
2770              } else {
2771                !!!cp (221.4);
2772                $s .= ']';
2773                #
2774              }
2775            } else {
2776              !!!cp (221.5);
2777              #
2778            }
2779            $s .= chr $self->{next_char};
2780            !!!next-input-character;
2781          } # CS
2782    
2783          $self->{state} = DATA_STATE;
2784          ## next-input-character done or EOF, which is reconsumed.
2785    
2786          if (length $s) {
2787            !!!cp (221.6);
2788            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2789                      line => $l, column => $c});
2790          } else {
2791            !!!cp (221.7);
2792          }
2793    
2794          redo A;
2795    
2796          ## ISSUE: "text tokens" in spec.
2797          ## TODO: Streaming support
2798      } else {      } else {
2799        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2800      }      }
# Line 2197  sub _get_next_token ($) { Line 2806  sub _get_next_token ($) {
2806  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2807    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2808    
2809      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2810    
2811    if ({    if ({
2812         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2813         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2237  sub _tokenize_attempt_to_consume_an_enti Line 2848  sub _tokenize_attempt_to_consume_an_enti
2848            redo X;            redo X;
2849          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2850            !!!cp (1005);            !!!cp (1005);
2851            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2852            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2853            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2854            return undef;            return undef;
# Line 2246  sub _tokenize_attempt_to_consume_an_enti Line 2857  sub _tokenize_attempt_to_consume_an_enti
2857            !!!next-input-character;            !!!next-input-character;
2858          } else {          } else {
2859            !!!cp (1007);            !!!cp (1007);
2860            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2861          }          }
2862    
2863          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2864            !!!cp (1008);            !!!cp (1008);
2865            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2866            $code = 0xFFFD;            $code = 0xFFFD;
2867          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2868            !!!cp (1009);            !!!cp (1009);
2869            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2870            $code = 0xFFFD;            $code = 0xFFFD;
2871          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2872            !!!cp (1010);            !!!cp (1010);
2873            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2874            $code = 0x000A;            $code = 0x000A;
2875          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2876            !!!cp (1011);            !!!cp (1011);
2877            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2878            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2879          }          }
2880    
2881          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2882                  has_reference => 1};                  has_reference => 1,
2883                    line => $l, column => $c,
2884                   };
2885        } # X        } # X
2886      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2887               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2289  sub _tokenize_attempt_to_consume_an_enti Line 2902  sub _tokenize_attempt_to_consume_an_enti
2902          !!!next-input-character;          !!!next-input-character;
2903        } else {        } else {
2904          !!!cp (1014);          !!!cp (1014);
2905          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2906        }        }
2907    
2908        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2909          !!!cp (1015);          !!!cp (1015);
2910          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2911          $code = 0xFFFD;          $code = 0xFFFD;
2912        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2913          !!!cp (1016);          !!!cp (1016);
2914          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2915          $code = 0xFFFD;          $code = 0xFFFD;
2916        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2917          !!!cp (1017);          !!!cp (1017);
2918          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2919          $code = 0x000A;          $code = 0x000A;
2920        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2921          !!!cp (1018);          !!!cp (1018);
2922          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2923          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2924        }        }
2925                
2926        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2927                  line => $l, column => $c,
2928                 };
2929      } else {      } else {
2930        !!!cp (1019);        !!!cp (1019);
2931        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2932        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2933        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2934        return undef;        return undef;
# Line 2330  sub _tokenize_attempt_to_consume_an_enti Line 2945  sub _tokenize_attempt_to_consume_an_enti
2945      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2946      our $EntityChar;      our $EntityChar;
2947    
2948      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2949             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2950             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2951               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2363  sub _tokenize_attempt_to_consume_an_enti Line 2978  sub _tokenize_attempt_to_consume_an_enti
2978            
2979      if ($match > 0) {      if ($match > 0) {
2980        !!!cp (1023);        !!!cp (1023);
2981        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2982                  line => $l, column => $c,
2983                 };
2984      } elsif ($match < 0) {      } elsif ($match < 0) {
2985        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2986        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2987          !!!cp (1024);          !!!cp (1024);
2988          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2989                    line => $l, column => $c,
2990                   };
2991        } else {        } else {
2992          !!!cp (1025);          !!!cp (1025);
2993          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2994                    line => $l, column => $c,
2995                   };
2996        }        }
2997      } else {      } else {
2998        !!!cp (1026);        !!!cp (1026);
2999        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3000        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3001        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3002                  line => $l, column => $c,
3003                 };
3004      }      }
3005    } else {    } else {
3006      !!!cp (1027);      !!!cp (1027);
3007      ## no characters are consumed      ## no characters are consumed
3008      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3009      return undef;      return undef;
3010    }    }
3011  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2420  sub _construct_tree ($) { Line 3043  sub _construct_tree ($) {
3043        
3044    !!!next-token;    !!!next-token;
3045    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3046    undef $self->{form_element};    undef $self->{form_element};
3047    undef $self->{head_element};    undef $self->{head_element};
3048    $self->{open_elements} = [];    $self->{open_elements} = [];
3049    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3050    
3051      ## NOTE: The "initial" insertion mode.
3052    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3053    
3054      ## NOTE: The "before html" insertion mode.
3055    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3056      $self->{insertion_mode} = BEFORE_HEAD_IM;
3057    
3058      ## NOTE: The "before head" insertion mode and so on.
3059    $self->_tree_construction_main;    $self->_tree_construction_main;
3060  } # _construct_tree  } # _construct_tree
3061    
3062  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3063    my $self = shift;    my $self = shift;
3064    
3065      ## NOTE: "initial" insertion mode
3066    
3067    INITIAL: {    INITIAL: {
3068      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3069        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2444  sub _tree_construction_initial ($) { Line 3075  sub _tree_construction_initial ($) {
3075        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3076            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3077            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3078          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3079            !!!parse-error (type => 'not HTML5', token => $token);
3080        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3081            !!!cp ('t2');
3082          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3083          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3084          } else {
3085            !!!cp ('t3');
3086        }        }
3087                
3088        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3089          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3090          ## NOTE: Default value for both |public_id| and |system_id| attributes
3091          ## are empty strings, so that we don't set any value in missing cases.
3092        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3093            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3094        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2461  sub _tree_construction_initial ($) { Line 3098  sub _tree_construction_initial ($) {
3098        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3099                
3100        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3101            !!!cp ('t4');
3102          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3103        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3104          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2539  sub _tree_construction_initial ($) { Line 3177  sub _tree_construction_initial ($) {
3177            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3178            "HTML" => 1,            "HTML" => 1,
3179          }->{$pubid}) {          }->{$pubid}) {
3180              !!!cp ('t5');
3181            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3182          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3183                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3184            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3185                !!!cp ('t6');
3186              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3187            } else {            } else {
3188                !!!cp ('t7');
3189              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3190            }            }
3191          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3192                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3193              !!!cp ('t8');
3194            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3195            } else {
3196              !!!cp ('t9');
3197          }          }
3198          } else {
3199            !!!cp ('t10');
3200        }        }
3201        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3202          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3203          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3204          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3205              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3206            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3207              !!!cp ('t11');
3208            } else {
3209              !!!cp ('t12');
3210          }          }
3211          } else {
3212            !!!cp ('t13');
3213        }        }
3214                
3215        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3216        !!!next-token;        !!!next-token;
3217        return;        return;
3218      } elsif ({      } elsif ({
# Line 2568  sub _tree_construction_initial ($) { Line 3220  sub _tree_construction_initial ($) {
3220                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3221                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3222               }->{$token->{type}}) {               }->{$token->{type}}) {
3223        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3224          !!!parse-error (type => 'no DOCTYPE', token => $token);
3225        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3226        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3227        ## reprocess        ## reprocess
3228          !!!ack-later;
3229        return;        return;
3230      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3231        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3232          ## Ignore the token          ## Ignore the token
3233    
3234          unless (length $token->{data}) {          unless (length $token->{data}) {
3235            ## Stay in the phase            !!!cp ('t15');
3236              ## Stay in the insertion mode.
3237            !!!next-token;            !!!next-token;
3238            redo INITIAL;            redo INITIAL;
3239            } else {
3240              !!!cp ('t16');
3241          }          }
3242          } else {
3243            !!!cp ('t17');
3244        }        }
3245    
3246        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3247        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3248        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3249        ## reprocess        ## reprocess
3250        return;        return;
3251      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3252          !!!cp ('t18');
3253        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3254        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3255                
3256        ## Stay in the phase.        ## Stay in the insertion mode.
3257        !!!next-token;        !!!next-token;
3258        redo INITIAL;        redo INITIAL;
3259      } else {      } else {
3260        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3261      }      }
3262    } # INITIAL    } # INITIAL
3263    
3264      die "$0: _tree_construction_initial: This should be never reached";
3265  } # _tree_construction_initial  } # _tree_construction_initial
3266    
3267  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3268    my $self = shift;    my $self = shift;
3269    
3270      ## NOTE: "before html" insertion mode.
3271        
3272    B: {    B: {
3273        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3274          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3275            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3276          ## Ignore the token          ## Ignore the token
3277          ## Stay in the phase          ## Stay in the insertion mode.
3278          !!!next-token;          !!!next-token;
3279          redo B;          redo B;
3280        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3281            !!!cp ('t20');
3282          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3283          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3284          ## Stay in the phase          ## Stay in the insertion mode.
3285          !!!next-token;          !!!next-token;
3286          redo B;          redo B;
3287        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2623  sub _tree_construction_root_element ($) Line 3289  sub _tree_construction_root_element ($)
3289            ## Ignore the token.            ## Ignore the token.
3290    
3291            unless (length $token->{data}) {            unless (length $token->{data}) {
3292              ## Stay in the phase              !!!cp ('t21');
3293                ## Stay in the insertion mode.
3294              !!!next-token;              !!!next-token;
3295              redo B;              redo B;
3296              } else {
3297                !!!cp ('t22');
3298            }            }
3299            } else {
3300              !!!cp ('t23');
3301          }          }
3302    
3303          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3304    
3305          #          #
3306        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3307          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3308              $token->{attributes}->{manifest}) {            my $root_element;
3309            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3310                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3311            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3312                  [$root_element, $el_category->{html}];
3313    
3314              if ($token->{attributes}->{manifest}) {
3315                !!!cp ('t24');
3316                $self->{application_cache_selection}
3317                    ->($token->{attributes}->{manifest}->{value});
3318                ## ISSUE: Spec is unclear on relative references.
3319                ## According to Hixie (#whatwg 2008-03-19), it should be
3320                ## resolved against the base URI of the document in HTML
3321                ## or xml:base of the element in XHTML.
3322              } else {
3323                !!!cp ('t25');
3324                $self->{application_cache_selection}->(undef);
3325              }
3326    
3327              !!!nack ('t25c');
3328    
3329              !!!next-token;
3330              return; ## Go to the "before head" insertion mode.
3331          } else {          } else {
3332            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3333              #
3334          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3335        } elsif ({        } elsif ({
3336                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3337                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3338                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3339          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3340          #          #
3341        } else {        } else {
3342          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3343        }        }
3344    
3345        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3346        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3347        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3348        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3349        #redo B;  
3350        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3351    
3352        ## NOTE: Reprocess the token.
3353        !!!ack-later;
3354        return; ## Go to the "before head" insertion mode.
3355    
3356        ## ISSUE: There is an issue in the spec
3357    } # B    } # B
3358    
3359      die "$0: _tree_construction_root_element: This should never be reached";
3360  } # _tree_construction_root_element  } # _tree_construction_root_element
3361    
3362  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2677  sub _reset_insertion_mode ($) { Line 3371  sub _reset_insertion_mode ($) {
3371            
3372      ## Step 3      ## Step 3
3373      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"!?  
3374        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3375          $last = 1;          $last = 1;
3376          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3377            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3378                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3379              #              #
3380            } else {            } else {
3381                !!!cp ('t28');
3382              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3383            }            }
3384          }          }
3385        }        }
3386            
3387        ## Step 4..13      ## Step 4..14
3388        my $new_mode = {      my $new_mode;
3389        if ($node->[1] & FOREIGN_EL) {
3390          ## NOTE: Strictly spaking, the line below only applies to MathML and
3391          ## SVG elements.  Currently the HTML syntax supports only MathML and
3392          ## SVG elements as foreigners.
3393          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3394          ## ISSUE: What is set as the secondary insertion mode?
3395        } else {
3396          $new_mode = {
3397                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3398                          ## NOTE: |option| and |optgroup| do not set
3399                          ## insertion mode to "in select" by themselves.
3400                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3401                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3402                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2709  sub _reset_insertion_mode ($) { Line 3409  sub _reset_insertion_mode ($) {
3409                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3410                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3411                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3412                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3413        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3414        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3415                
3416        ## Step 14        ## Step 15
3417        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3418          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3419              !!!cp ('t29');
3420            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3421          } else {          } else {
3422              ## ISSUE: Can this state be reached?
3423              !!!cp ('t30');
3424            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3425          }          }
3426          return;          return;
3427          } else {
3428            !!!cp ('t31');
3429        }        }
3430                
3431        ## Step 15        ## Step 16
3432        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3433                
3434        ## Step 16        ## Step 17
3435        $i--;        $i--;
3436        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3437                
3438        ## Step 17        ## Step 18
3439        redo S3;        redo S3;
3440      } # S3      } # S3
3441    
3442      die "$0: _reset_insertion_mode: This line should never be reached";
3443  } # _reset_insertion_mode  } # _reset_insertion_mode
3444    
3445  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2753  sub _tree_construction_main ($) { Line 3461  sub _tree_construction_main ($) {
3461      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3462      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3463        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3464            !!!cp ('t32');
3465          return;          return;
3466        }        }
3467      }      }
# Line 2767  sub _tree_construction_main ($) { Line 3476  sub _tree_construction_main ($) {
3476    
3477        ## Step 6        ## Step 6
3478        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3479            !!!cp ('t33_1');
3480          #          #
3481        } else {        } else {
3482          my $in_open_elements;          my $in_open_elements;
3483          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3484            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3485                !!!cp ('t33');
3486              $in_open_elements = 1;              $in_open_elements = 1;
3487              last OE;              last OE;
3488            }            }
3489          }          }
3490          if ($in_open_elements) {          if ($in_open_elements) {
3491              !!!cp ('t34');
3492            #            #
3493          } else {          } else {
3494              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3495              !!!cp ('t35');
3496            redo S4;            redo S4;
3497          }          }
3498        }        }
# Line 2801  sub _tree_construction_main ($) { Line 3515  sub _tree_construction_main ($) {
3515    
3516        ## Step 11        ## Step 11
3517        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3518            !!!cp ('t36');
3519          ## Step 7'          ## Step 7'
3520          $i++;          $i++;
3521          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3522                    
3523          redo S7;          redo S7;
3524        }        }
3525    
3526          !!!cp ('t37');
3527      } # S7      } # S7
3528    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3529    
3530    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3531      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3532        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3533            !!!cp ('t38');
3534          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3535          return;          return;
3536        }        }
3537      }      }
3538    
3539        !!!cp ('t39');
3540    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3541    
3542    my $parse_rcdata = sub ($$) {    my $insert;
3543      my ($content_model_flag, $insert) = @_;  
3544      my $parse_rcdata = sub ($) {
3545        my ($content_model_flag) = @_;
3546    
3547      ## Step 1      ## Step 1
3548      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3549      my $el;      my $el;
3550      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3551    
3552      ## Step 2      ## Step 2
3553      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3554    
3555      ## Step 3      ## Step 3
3556      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2836  sub _tree_construction_main ($) { Line 3558  sub _tree_construction_main ($) {
3558    
3559      ## Step 4      ## Step 4
3560      my $text = '';      my $text = '';
3561        !!!nack ('t40.1');
3562      !!!next-token;      !!!next-token;
3563      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3564          !!!cp ('t40');
3565        $text .= $token->{data};        $text .= $token->{data};
3566        !!!next-token;        !!!next-token;
3567      }      }
3568    
3569      ## Step 5      ## Step 5
3570      if (length $text) {      if (length $text) {
3571          !!!cp ('t41');
3572        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3573        $el->append_child ($text);        $el->append_child ($text);
3574      }      }
# Line 2852  sub _tree_construction_main ($) { Line 3577  sub _tree_construction_main ($) {
3577      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3578    
3579      ## Step 7      ## Step 7
3580      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3581            $token->{tag_name} eq $start_tag_name) {
3582          !!!cp ('t42');
3583        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3584      } else {      } else {
3585        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3586          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3587            !!!cp ('t43');
3588            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3589          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3590            !!!cp ('t44');
3591            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3592          } else {
3593            die "$0: $content_model_flag in parse_rcdata";
3594          }
3595      }      }
3596      !!!next-token;      !!!next-token;
3597    }; # $parse_rcdata    }; # $parse_rcdata
3598    
3599    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3600      my $script_el;      my $script_el;
3601      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3602      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3603    
3604      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3605      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3606            
3607      my $text = '';      my $text = '';
3608        !!!nack ('t45.1');
3609      !!!next-token;      !!!next-token;
3610      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3611          !!!cp ('t45');
3612        $text .= $token->{data};        $text .= $token->{data};
3613        !!!next-token;        !!!next-token;
3614      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3615      if (length $text) {      if (length $text) {
3616          !!!cp ('t46');
3617        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3618      }      }
3619                                
# Line 2887  sub _tree_construction_main ($) { Line 3621  sub _tree_construction_main ($) {
3621    
3622      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3623          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3624          !!!cp ('t47');
3625        ## Ignore the token        ## Ignore the token
3626      } else {      } else {
3627        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3628          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3629        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3630        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3631      }      }
3632            
3633      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3634          !!!cp ('t49');
3635        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3636      } else {      } else {
3637          !!!cp ('t50');
3638        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3639        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3640    
# Line 2910  sub _tree_construction_main ($) { Line 3648  sub _tree_construction_main ($) {
3648      !!!next-token;      !!!next-token;
3649    }; # $script_start_tag    }; # $script_start_tag
3650    
3651      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3652      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3653      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3654    
3655    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3656      my $tag_name = shift;      my $end_tag_token = shift;
3657        my $tag_name = $end_tag_token->{tag_name};
3658    
3659        ## NOTE: The adoption agency algorithm (AAA).
3660    
3661      FET: {      FET: {
3662        ## Step 1        ## Step 1
3663        my $formatting_element;        my $formatting_element;
3664        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3665        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3666          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3667              !!!cp ('t52');
3668              last AFE;
3669            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3670                         eq $tag_name) {
3671              !!!cp ('t51');
3672            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3673            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3674            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3675          }          }
3676        } # AFE        } # AFE
3677        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3678          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3679            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3680          ## Ignore the token          ## Ignore the token
3681          !!!next-token;          !!!next-token;
3682          return;          return;
# Line 2939  sub _tree_construction_main ($) { Line 3688  sub _tree_construction_main ($) {
3688          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3689          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3690            if ($in_scope) {            if ($in_scope) {
3691                !!!cp ('t54');
3692              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3693              last INSCOPE;              last INSCOPE;
3694            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3695              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3696                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3697                                token => $end_tag_token);
3698              ## Ignore the token              ## Ignore the token
3699              !!!next-token;              !!!next-token;
3700              return;              return;
3701            }            }
3702          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3703                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3704            $in_scope = 0;            $in_scope = 0;
3705          }          }
3706        } # INSCOPE        } # INSCOPE
3707        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3708          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3709            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3710                            token => $end_tag_token);
3711          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3712          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3713          return;          return;
3714        }        }
3715        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3716          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3717            !!!parse-error (type => 'not closed',
3718                            value => $self->{open_elements}->[-1]->[0]
3719                                ->manakai_local_name,
3720                            token => $end_tag_token);
3721        }        }
3722                
3723        ## Step 2        ## Step 2
# Line 2969  sub _tree_construction_main ($) { Line 3725  sub _tree_construction_main ($) {
3725        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3726        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3727          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3728          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3729              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3730              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3731               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3732              !!!cp ('t59');
3733            $furthest_block = $node;            $furthest_block = $node;
3734            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3735          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3736              !!!cp ('t60');
3737            last OE;            last OE;
3738          }          }
3739        } # OE        } # OE
3740                
3741        ## Step 3        ## Step 3
3742        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3743            !!!cp ('t61');
3744          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3745          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3746          !!!next-token;          !!!next-token;
# Line 2994  sub _tree_construction_main ($) { Line 3753  sub _tree_construction_main ($) {
3753        ## Step 5        ## Step 5
3754        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3755        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3756            !!!cp ('t62');
3757          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3758        }        }
3759                
# Line 3016  sub _tree_construction_main ($) { Line 3776  sub _tree_construction_main ($) {
3776          S7S2: {          S7S2: {
3777            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3778              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3779                  !!!cp ('t63');
3780                $node_i_in_active = $_;                $node_i_in_active = $_;
3781                last S7S2;                last S7S2;
3782              }              }
# Line 3029  sub _tree_construction_main ($) { Line 3790  sub _tree_construction_main ($) {
3790                    
3791          ## Step 4          ## Step 4
3792          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3793              !!!cp ('t64');
3794            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3795          }          }
3796                    
3797          ## Step 5          ## Step 5
3798          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3799              !!!cp ('t65');
3800            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3801            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3802            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3051  sub _tree_construction_main ($) { Line 3814  sub _tree_construction_main ($) {
3814        } # S7          } # S7  
3815                
3816        ## Step 8        ## Step 8
3817        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3818            my $foster_parent_element;
3819            my $next_sibling;
3820            OE: for (reverse 0..$#{$self->{open_elements}}) {
3821              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3822                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3823                                 if (defined $parent and $parent->node_type == 1) {
3824                                   !!!cp ('t65.1');
3825                                   $foster_parent_element = $parent;
3826                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3827                                 } else {
3828                                   !!!cp ('t65.2');
3829                                   $foster_parent_element
3830                                     = $self->{open_elements}->[$_ - 1]->[0];
3831                                 }
3832                                 last OE;
3833                               }
3834                             } # OE
3835                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3836                               unless defined $foster_parent_element;
3837            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3838            $open_tables->[-1]->[1] = 1; # tainted
3839          } else {
3840            !!!cp ('t65.3');
3841            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3842          }
3843                
3844        ## Step 9        ## Step 9
3845        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3068  sub _tree_construction_main ($) { Line 3856  sub _tree_construction_main ($) {
3856        my $i;        my $i;
3857        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3858          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3859              !!!cp ('t66');
3860            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3861            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3862          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3863              !!!cp ('t67');
3864            $i = $_;            $i = $_;
3865          }          }
3866        } # AFE        } # AFE
# Line 3080  sub _tree_construction_main ($) { Line 3870  sub _tree_construction_main ($) {
3870        undef $i;        undef $i;
3871        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3872          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3873              !!!cp ('t68');
3874            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3875            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3876          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3877              !!!cp ('t69');
3878            $i = $_;            $i = $_;
3879          }          }
3880        } # OE        } # OE
# Line 3093  sub _tree_construction_main ($) { Line 3885  sub _tree_construction_main ($) {
3885      } # FET      } # FET
3886    }; # $formatting_end_tag    }; # $formatting_end_tag
3887    
3888    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3889      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3890    }; # $insert_to_current    }; # $insert_to_current
3891    
3892    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3893                         my $child = shift;      my $child = shift;
3894                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3895                              table => 1, tbody => 1, tfoot => 1,        # MUST
3896                              thead => 1, tr => 1,        my $foster_parent_element;
3897                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3898                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3899                           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') {  
3900                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3901                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3902                                   !!!cp ('t70');
3903                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3904                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3905                               } else {                               } else {
3906                                   !!!cp ('t71');
3907                                 $foster_parent_element                                 $foster_parent_element
3908                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3909                               }                               }
# Line 3123  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3915                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3916                             ($child, $next_sibling);                             ($child, $next_sibling);
3917                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3918                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3919                         }        !!!cp ('t72');
3920          $self->{open_elements}->[-1]->[0]->append_child ($child);
3921        }
3922    }; # $insert_to_foster    }; # $insert_to_foster
3923    
3924    my $insert;    B: while (1) {
   
   B: {  
3925      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3926        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3927          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3928        ## Ignore the token        ## Ignore the token
3929        ## Stay in the phase        ## Stay in the phase
3930        !!!next-token;        !!!next-token;
3931        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } 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]}) {  
           !!!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')) {  
           !!!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') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3932      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3933               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3934        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3935          ## Turn into the main phase          !!!cp ('t79');
3936          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3937          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3938        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3939          ## Turn into the main phase          !!!cp ('t80');
3940          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3941          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3942          } else {
3943            !!!cp ('t81');
3944        }        }
3945    
3946  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3947  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3948        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3949        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3950          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3951              !!!cp ('t84');
3952            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3953              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3954               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3955          }          }
3956        }        }
3957          !!!nack ('t84.1');
3958        !!!next-token;        !!!next-token;
3959        redo B;        next B;
3960      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3961        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3962        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3963            !!!cp ('t85');
3964          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3965        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3966            !!!cp ('t86');
3967          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3968        } else {        } else {
3969            !!!cp ('t87');
3970          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3971        }        }
3972        !!!next-token;        !!!next-token;
3973        redo B;        next B;
3974      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3975          if ($token->{type} == CHARACTER_TOKEN) {
3976            !!!cp ('t87.1');
3977            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3978            !!!next-token;
3979            next B;
3980          } elsif ($token->{type} == START_TAG_TOKEN) {
3981            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3982                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3983                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3984                ($token->{tag_name} eq 'svg' and
3985                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3986              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3987              !!!cp ('t87.2');
3988              #
3989            } elsif ({
3990                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3991                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3992                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3993                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3994                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3995                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3996                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3997                      var => 1,
3998                     }->{$token->{tag_name}}) {
3999              !!!cp ('t87.2');
4000              !!!parse-error (type => 'not closed',
4001                              value => $self->{open_elements}->[-1]->[0]
4002                                  ->manakai_local_name,
4003                              token => $token);
4004    
4005              pop @{$self->{open_elements}}
4006                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4007    
4008              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4009              ## Reprocess.
4010              next B;
4011            } else {
4012              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4013              my $tag_name = $token->{tag_name};
4014              if ($nsuri eq $SVG_NS) {
4015                $tag_name = {
4016                   altglyph => 'altGlyph',
4017                   altglyphdef => 'altGlyphDef',
4018                   altglyphitem => 'altGlyphItem',
4019                   animatecolor => 'animateColor',
4020                   animatemotion => 'animateMotion',
4021                   animatetransform => 'animateTransform',
4022                   clippath => 'clipPath',
4023                   feblend => 'feBlend',
4024                   fecolormatrix => 'feColorMatrix',
4025                   fecomponenttransfer => 'feComponentTransfer',
4026                   fecomposite => 'feComposite',
4027                   feconvolvematrix => 'feConvolveMatrix',
4028                   fediffuselighting => 'feDiffuseLighting',
4029                   fedisplacementmap => 'feDisplacementMap',
4030                   fedistantlight => 'feDistantLight',
4031                   feflood => 'feFlood',
4032                   fefunca => 'feFuncA',
4033                   fefuncb => 'feFuncB',
4034                   fefuncg => 'feFuncG',
4035                   fefuncr => 'feFuncR',
4036                   fegaussianblur => 'feGaussianBlur',
4037                   feimage => 'feImage',
4038                   femerge => 'feMerge',
4039                   femergenode => 'feMergeNode',
4040                   femorphology => 'feMorphology',
4041                   feoffset => 'feOffset',
4042                   fepointlight => 'fePointLight',
4043                   fespecularlighting => 'feSpecularLighting',
4044                   fespotlight => 'feSpotLight',
4045                   fetile => 'feTile',
4046                   feturbulence => 'feTurbulence',
4047                   foreignobject => 'foreignObject',
4048                   glyphref => 'glyphRef',
4049                   lineargradient => 'linearGradient',
4050                   radialgradient => 'radialGradient',
4051                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4052                   textpath => 'textPath',  
4053                }->{$tag_name} || $tag_name;
4054              }
4055    
4056              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4057    
4058              ## "adjust foreign attributes" - done in insert-element-f
4059    
4060              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4061    
4062              if ($self->{self_closing}) {
4063                pop @{$self->{open_elements}};
4064                !!!ack ('t87.3');
4065              } else {
4066                !!!cp ('t87.4');
4067              }
4068    
4069              !!!next-token;
4070              next B;
4071            }
4072          } elsif ($token->{type} == END_TAG_TOKEN) {
4073            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4074            !!!cp ('t87.5');
4075            #
4076          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4077            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4078            !!!cp ('t87.6');
4079            #
4080            ## TODO: ...
4081          } else {
4082            die "$0: $token->{type}: Unknown token type";        
4083          }
4084        }
4085    
4086        if ($self->{insertion_mode} & HEAD_IMS) {
4087        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4088          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4089            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4090                !!!cp ('t88.2');
4091                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4092              } else {
4093                !!!cp ('t88.1');
4094                ## Ignore the token.
4095                !!!next-token;
4096                next B;
4097              }
4098            unless (length $token->{data}) {            unless (length $token->{data}) {
4099                !!!cp ('t88');
4100              !!!next-token;              !!!next-token;
4101              redo B;              next B;
4102            }            }
4103          }          }
4104    
4105          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4106              !!!cp ('t89');
4107            ## As if <head>            ## As if <head>
4108            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4109            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4110            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4111                  [$self->{head_element}, $el_category->{head}];
4112    
4113            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4114            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4115    
4116            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4117          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4118              !!!cp ('t90');
4119            ## As if </noscript>            ## As if </noscript>
4120            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4121            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4122                        
4123            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4124            ## As if </head>            ## As if </head>
# Line 3234  sub _tree_construction_main ($) { Line 4126  sub _tree_construction_main ($) {
4126    
4127            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4128          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4129              !!!cp ('t91');
4130            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4131    
4132            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4133            } else {
4134              !!!cp ('t92');
4135          }          }
4136    
4137              ## "after head" insertion mode          ## "after head" insertion mode
4138              ## As if <body>          ## As if <body>
4139              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4140              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4141              ## reprocess          ## reprocess
4142              redo B;          next B;
4143            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4144              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4145                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4146                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4147                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4148                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4149                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4150                  !!!next-token;              push @{$self->{open_elements}},
4151                  redo B;                  [$self->{head_element}, $el_category->{head}];
4152                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4153                  #              !!!nack ('t93.1');
4154                } else {              !!!next-token;
4155                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4156                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4157                  !!!next-token;              !!!cp ('t93.2');
4158                  redo B;              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4159                }              ## Ignore the token
4160              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t93.3');
4161                ## As if <head>              !!!next-token;
4162                !!!create-element ($self->{head_element}, 'head');              next B;
4163                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            } else {
4164                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!cp ('t95');
4165                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4166                ## Ignore the token
4167                !!!nack ('t95.1');
4168                !!!next-token;
4169                next B;
4170              }
4171            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4172              !!!cp ('t96');
4173              ## As if <head>
4174              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4175              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4176              push @{$self->{open_elements}},
4177                  [$self->{head_element}, $el_category->{head}];
4178    
4179                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4180                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4181              }          } else {
4182              !!!cp ('t97');
4183            }
4184    
4185              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4186                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4187                    !!!cp ('t98');
4188                  ## As if </noscript>                  ## As if </noscript>
4189                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4190                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4191                                
4192                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4193                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4194                  } else {
4195                    !!!cp ('t99');
4196                }                }
4197    
4198                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4199                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4200                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4201                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4202                    push @{$self->{open_elements}},
4203                        [$self->{head_element}, $el_category->{head}];
4204                  } else {
4205                    !!!cp ('t101');
4206                }                }
4207                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4208                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4209                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4210                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4211                  !!!nack ('t101.1');
4212                !!!next-token;                !!!next-token;
4213                redo B;                next B;
4214              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4215                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4216                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4217                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4218                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4219                    push @{$self->{open_elements}},
4220                        [$self->{head_element}, $el_category->{head}];
4221                  } else {
4222                    !!!cp ('t103');
4223                }                }
4224                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4225                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4226                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4227                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4228                  !!!ack ('t103.1');
4229                !!!next-token;                !!!next-token;
4230                redo B;                next B;
4231              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4232                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4233                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4234                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4235                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4236                    push @{$self->{open_elements}},
4237                        [$self->{head_element}, $el_category->{head}];
4238                  } else {
4239                    !!!cp ('t105');
4240                }                }
4241                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4242                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.
4243    
4244                unless ($self->{confident}) {                unless ($self->{confident}) {
4245                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4246                      !!!cp ('t106');
4247                      ## NOTE: Whether the encoding is supported or not is handled
4248                      ## in the {change_encoding} callback.
4249                    $self->{change_encoding}                    $self->{change_encoding}
4250                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4251                             $token);
4252                                        
4253                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4254                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4255                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4256                                                 ->{has_reference});                                                 ->{has_reference});
4257                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4258                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4259                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4260                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4261                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4262                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4263                        !!!cp ('t107');
4264                        ## NOTE: Whether the encoding is supported or not is handled
4265                        ## in the {change_encoding} callback.
4266                      $self->{change_encoding}                      $self->{change_encoding}
4267                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4268                               $token);
4269                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4270                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4271                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
4272                                                     ->{has_reference});                                                     ->{has_reference});
4273                      } else {
4274                        !!!cp ('t108');
4275                    }                    }
4276                  }                  }
4277                } else {                } else {
4278                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
4279                      !!!cp ('t109');
4280                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4281                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4282                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4283                                                 ->{has_reference});                                                 ->{has_reference});
4284                  }                  }
4285                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
4286                      !!!cp ('t110');
4287                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4288                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4289                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3353  sub _tree_construction_main ($) { Line 4291  sub _tree_construction_main ($) {
4291                  }                  }
4292                }                }
4293    
4294                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4295                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4296                  !!!ack ('t110.1');
4297                !!!next-token;                !!!next-token;
4298                redo B;                next B;
4299              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4300                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4301                    !!!cp ('t111');
4302                  ## As if </noscript>                  ## As if </noscript>
4303                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4304                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4305                                
4306                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4307                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4308                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4309                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4310                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4311                    push @{$self->{open_elements}},
4312                        [$self->{head_element}, $el_category->{head}];
4313                  } else {
4314                    !!!cp ('t113');
4315                }                }
4316    
4317                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4318                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4319                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4320                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4321                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4322                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4323                redo B;                next B;
4324              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4325                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4326                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4327                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4328                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4329                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4330                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4331                    push @{$self->{open_elements}},
4332                        [$self->{head_element}, $el_category->{head}];
4333                  } else {
4334                    !!!cp ('t115');
4335                }                }
4336                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4337                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4338                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4339                redo B;                next B;
4340              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4341                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4342                    !!!cp ('t116');
4343                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4344                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4345                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4346                    !!!nack ('t116.1');
4347                  !!!next-token;                  !!!next-token;
4348                  redo B;                  next B;
4349                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4350                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4351                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4352                  ## Ignore the token                  ## Ignore the token
4353                    !!!nack ('t117.1');
4354                  !!!next-token;                  !!!next-token;
4355                  redo B;                  next B;
4356                } else {                } else {
4357                    !!!cp ('t118');
4358                  #                  #
4359                }                }
4360              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4361                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4362                    !!!cp ('t119');
4363                  ## As if </noscript>                  ## As if </noscript>
4364                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4365                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4366                                
4367                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4368                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4369                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4370                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4371                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4372                    push @{$self->{open_elements}},
4373                        [$self->{head_element}, $el_category->{head}];
4374                  } else {
4375                    !!!cp ('t121');
4376                }                }
4377    
4378                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4379                $script_start_tag->($insert_to_current);                $script_start_tag->();
4380                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4381                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4382                redo B;                next B;
4383              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4384                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4385                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                    !!!cp ('t122');
4387                  ## As if </noscript>                  ## As if </noscript>
4388                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4389                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4390                                    
4391                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4392                  ## As if </head>                  ## As if </head>
# Line 3436  sub _tree_construction_main ($) { Line 4394  sub _tree_construction_main ($) {
4394                                    
4395                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4396                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4397                    !!!cp ('t124');
4398                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4399                                    
4400                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4401                  } else {
4402                    !!!cp ('t125');
4403                }                }
4404    
4405                ## "after head" insertion mode                ## "after head" insertion mode
4406                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4407                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4408                    !!!cp ('t126');
4409                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4410                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4411                    !!!cp ('t127');
4412                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4413                } else {                } else {
4414                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4415                }                }
4416                  !!!nack ('t127.1');
4417                !!!next-token;                !!!next-token;
4418                redo B;                next B;
4419              } else {              } else {
4420                  !!!cp ('t128');
4421                #                #
4422              }              }
4423    
4424              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4425                  !!!cp ('t129');
4426                ## As if </noscript>                ## As if </noscript>
4427                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4428                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4429                                
4430                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4431                ## As if </head>                ## As if </head>
# Line 3467  sub _tree_construction_main ($) { Line 4433  sub _tree_construction_main ($) {
4433    
4434                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4435              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4436                  !!!cp ('t130');
4437                ## As if </head>                ## As if </head>
4438                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4439    
4440                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4441                } else {
4442                  !!!cp ('t131');
4443              }              }
4444    
4445              ## "after head" insertion mode              ## "after head" insertion mode
4446              ## As if <body>              ## As if <body>
4447              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4448              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4449              ## reprocess              ## reprocess
4450              redo B;              !!!ack-later;
4451                next B;
4452            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4453              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4454                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4455                    !!!cp ('t132');
4456                  ## As if <head>                  ## As if <head>
4457                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4458                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4459                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4460                        [$self->{head_element}, $el_category->{head}];
4461    
4462                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4463                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4464                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4465                  !!!next-token;                  !!!next-token;
4466                  redo B;                  next B;
4467                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4468                    !!!cp ('t133');
4469                  ## As if </noscript>                  ## As if </noscript>
4470                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4471                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4472                                    
4473                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4474                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4475                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4476                  !!!next-token;                  !!!next-token;
4477                  redo B;                  next B;
4478                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4479                    !!!cp ('t134');
4480                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4481                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4482                  !!!next-token;                  !!!next-token;
4483                  redo B;                  next B;
4484                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4485                    !!!cp ('t134.1');
4486                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4487                    ## Ignore the token
4488                    !!!next-token;
4489                    next B;
4490                } else {                } else {
4491                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4492                }                }
4493              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4494                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4495                    !!!cp ('t136');
4496                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4497                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4498                  !!!next-token;                  !!!next-token;
4499                  redo B;                  next B;
4500                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4501                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4502                    !!!cp ('t137');
4503                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4504                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4505                  !!!next-token;                  !!!next-token;
4506                  redo B;                  next B;
4507                } else {                } else {
4508                    !!!cp ('t138');
4509                  #                  #
4510                }                }
4511              } elsif ({              } elsif ({
4512                        body => 1, html => 1,                        body => 1, html => 1,
4513                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4514                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4515                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4516                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4517                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4518                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4519                    ## Ignore the token
4520                  $self->{insertion_mode} = IN_HEAD_IM;                  !!!next-token;
4521                  ## Reprocess in the "in head" insertion mode...                  next B;
4522                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4523                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140.1');
4524                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4525                  ## Ignore the token                  ## Ignore the token
4526                  !!!next-token;                  !!!next-token;
4527                  redo B;                  next B;
4528                  } else {
4529                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4530                }                }
4531                              } elsif ($token->{tag_name} eq 'p') {
4532                #                !!!cp ('t142');
4533              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4534                        p => 1, br => 1,                ## Ignore the token
4535                       }->{$token->{tag_name}}) {                !!!next-token;
4536                  next B;
4537                } elsif ($token->{tag_name} eq 'br') {
4538                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4539                  ## As if <head>                  !!!cp ('t142.2');
4540                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4541                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4542                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4543                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4544      
4545                    ## Reprocess in the "after head" insertion mode...
4546                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4547                    !!!cp ('t143.2');
4548                    ## As if </head>
4549                    pop @{$self->{open_elements}};
4550                    $self->{insertion_mode} = AFTER_HEAD_IM;
4551      
4552                    ## Reprocess in the "after head" insertion mode...
4553                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4554                    !!!cp ('t143.3');
4555                    ## ISSUE: Two parse errors for <head><noscript></br>
4556                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4557                    ## As if </noscript>
4558                    pop @{$self->{open_elements}};
4559                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4560    
4561                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4562                }                  ## As if </head>
4563                    pop @{$self->{open_elements}};
4564                    $self->{insertion_mode} = AFTER_HEAD_IM;
4565    
4566                #                  ## Reprocess in the "after head" insertion mode...
4567              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4568                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4569                  #                  #
4570                } else {                } else {
4571                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4572                }                }
4573    
4574                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4575                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4576                  ## Ignore the token
4577                  !!!next-token;
4578                  next B;
4579                } else {
4580                  !!!cp ('t145');
4581                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4582                  ## Ignore the token
4583                  !!!next-token;
4584                  next B;
4585              }              }
4586    
4587              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4588                  !!!cp ('t146');
4589                ## As if </noscript>                ## As if </noscript>
4590                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4591                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4592                                
4593                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4594                ## As if </head>                ## As if </head>
# Line 3579  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596    
4597                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4598              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4599                  !!!cp ('t147');
4600                ## As if </head>                ## As if </head>
4601                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4602    
4603                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4604              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4605                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4606                  !!!cp ('t148');
4607                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4608                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4609                !!!next-token;                !!!next-token;
4610                redo B;                next B;
4611                } else {
4612                  !!!cp ('t149');
4613              }              }
4614    
4615              ## "after head" insertion mode              ## "after head" insertion mode
4616              ## As if <body>              ## As if <body>
4617              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4618              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4619              ## reprocess              ## reprocess
4620              redo B;              next B;
4621            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4622              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4623            }            !!!cp ('t149.1');
4624    
4625              ## NOTE: As if <head>
4626              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4627              $self->{open_elements}->[-1]->[0]->append_child
4628                  ($self->{head_element});
4629              #push @{$self->{open_elements}},
4630              #    [$self->{head_element}, $el_category->{head}];
4631              #$self->{insertion_mode} = IN_HEAD_IM;
4632              ## NOTE: Reprocess.
4633    
4634              ## NOTE: As if </head>
4635              #pop @{$self->{open_elements}};
4636              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4637              ## NOTE: Reprocess.
4638              
4639              #
4640            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4641              !!!cp ('t149.2');
4642    
4643              ## NOTE: As if </head>
4644              pop @{$self->{open_elements}};
4645              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4646              ## NOTE: Reprocess.
4647    
4648              #
4649            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4650              !!!cp ('t149.3');
4651    
4652              !!!parse-error (type => 'in noscript:#eof', token => $token);
4653    
4654              ## As if </noscript>
4655              pop @{$self->{open_elements}};
4656              #$self->{insertion_mode} = IN_HEAD_IM;
4657              ## NOTE: Reprocess.
4658    
4659              ## NOTE: As if </head>
4660              pop @{$self->{open_elements}};
4661              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4662              ## NOTE: Reprocess.
4663    
4664              #
4665            } else {
4666              !!!cp ('t149.4');
4667              #
4668            }
4669    
4670            ## NOTE: As if <body>
4671            !!!insert-element ('body',, $token);
4672            $self->{insertion_mode} = IN_BODY_IM;
4673            ## NOTE: Reprocess.
4674            next B;
4675          } else {
4676            die "$0: $token->{type}: Unknown token type";
4677          }
4678    
4679            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4680      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4681            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4682                !!!cp ('t150');
4683              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4684              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4685                            
4686              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4687    
4688              !!!next-token;              !!!next-token;
4689              redo B;              next B;
4690            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4691              if ({              if ({
4692                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3617  sub _tree_construction_main ($) { Line 4694  sub _tree_construction_main ($) {
4694                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4695                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4696                  ## have an element in table scope                  ## have an element in table scope
4697                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4698                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4699                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4700                      $tn = $node->[1];                      !!!cp ('t151');
4701                      last INSCOPE;  
4702                    } elsif ({                      ## Close the cell
4703                              table => 1, html => 1,                      !!!back-token; # <x>
4704                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4705                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4706                    }                                line => $token->{line},
4707                  } # INSCOPE                                column => $token->{column}};
4708                    unless (defined $tn) {                      next B;
4709                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4710                      ## Ignore the token                      !!!cp ('t152');
4711                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4712                      redo B;                      last;
4713                    }                    }
4714                                    }
4715                  ## Close the cell  
4716                  !!!back-token; # <?>                  !!!cp ('t153');
4717                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4718                  redo B;                      value => $token->{tag_name}, token => $token);
4719                    ## Ignore the token
4720                    !!!nack ('t153.1');
4721                    !!!next-token;
4722                    next B;
4723                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4724                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4725                                    
4726                  ## As if </caption>                  ## NOTE: As if </caption>.
4727                  ## have a table element in table scope                  ## have a table element in table scope
4728                  my $i;                  my $i;
4729                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4730                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4731                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4732                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4733                      last INSCOPE;                        !!!cp ('t155');
4734                    } elsif ({                        $i = $_;
4735                              table => 1, html => 1,                        last INSCOPE;
4736                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4737                      last INSCOPE;                        !!!cp ('t156');
4738                          last;
4739                        }
4740                    }                    }
4741    
4742                      !!!cp ('t157');
4743                      !!!parse-error (type => 'start tag not allowed',
4744                                      value => $token->{tag_name}, token => $token);
4745                      ## Ignore the token
4746                      !!!nack ('t157.1');
4747                      !!!next-token;
4748                      next B;
4749                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4750                                    
4751                  ## generate implied end tags                  ## generate implied end tags
4752                  if ({                  while ($self->{open_elements}->[-1]->[1]
4753                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4754                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4755                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $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;  
4756                  }                  }
4757    
4758                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4759                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4760                      !!!parse-error (type => 'not closed',
4761                                      value => $self->{open_elements}->[-1]->[0]
4762                                          ->manakai_local_name,
4763                                      token => $token);
4764                    } else {
4765                      !!!cp ('t160');
4766                  }                  }
4767                                    
4768                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3689  sub _tree_construction_main ($) { Line 4772  sub _tree_construction_main ($) {
4772                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4773                                    
4774                  ## reprocess                  ## reprocess
4775                  redo B;                  !!!ack-later;
4776                    next B;
4777                } else {                } else {
4778                    !!!cp ('t161');
4779                  #                  #
4780                }                }
4781              } else {              } else {
4782                  !!!cp ('t162');
4783                #                #
4784              }              }
4785            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3703  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789                  my $i;                  my $i;
4790                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4791                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4792                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4793                        !!!cp ('t163');
4794                      $i = $_;                      $i = $_;
4795                      last INSCOPE;                      last INSCOPE;
4796                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4797                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4798                      last INSCOPE;                      last INSCOPE;
4799                    }                    }
4800                  } # INSCOPE                  } # INSCOPE
4801                    unless (defined $i) {                    unless (defined $i) {
4802                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4803                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4804                      ## Ignore the token                      ## Ignore the token
4805                      !!!next-token;                      !!!next-token;
4806                      redo B;                      next B;
4807                    }                    }
4808                                    
4809                  ## generate implied end tags                  ## generate implied end tags
4810                  if ({                  while ($self->{open_elements}->[-1]->[1]
4811                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4812                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4813                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4814                  }                  }
4815                    
4816                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4817                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4818                      !!!cp ('t167');
4819                      !!!parse-error (type => 'not closed',
4820                                      value => $self->{open_elements}->[-1]->[0]
4821                                          ->manakai_local_name,
4822                                      token => $token);
4823                    } else {
4824                      !!!cp ('t168');
4825                  }                  }
4826                                    
4827                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3744  sub _tree_construction_main ($) { Line 4831  sub _tree_construction_main ($) {
4831                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4832                                    
4833                  !!!next-token;                  !!!next-token;
4834                  redo B;                  next B;
4835                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4836                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4837                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4838                  ## Ignore the token                  ## Ignore the token
4839                  !!!next-token;                  !!!next-token;
4840                  redo B;                  next B;
4841                } else {                } else {
4842                    !!!cp ('t170');
4843                  #                  #
4844                }                }
4845              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4846                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4847                  ## have a table element in table scope                  ## have a table element in table scope
4848                  my $i;                  my $i;
4849                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4850                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4851                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4852                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4853                      last INSCOPE;                        !!!cp ('t171');
4854                    } elsif ({                        $i = $_;
4855                              table => 1, html => 1,                        last INSCOPE;
4856                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4857                      last INSCOPE;                        !!!cp ('t172');
4858                          last;
4859                        }
4860                    }                    }
4861    
4862                      !!!cp ('t173');
4863                      !!!parse-error (type => 'unmatched end tag',
4864                                      value => $token->{tag_name}, token => $token);
4865                      ## Ignore the token
4866                      !!!next-token;
4867                      next B;
4868                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4869                                    
4870                  ## generate implied end tags                  ## generate implied end tags
4871                  if ({                  while ($self->{open_elements}->[-1]->[1]
4872                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4873                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4874                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4875                  }                  }
4876                                    
4877                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4878                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4879                      !!!parse-error (type => 'not closed',
4880                                      value => $self->{open_elements}->[-1]->[0]
4881                                          ->manakai_local_name,
4882                                      token => $token);
4883                    } else {
4884                      !!!cp ('t176');
4885                  }                  }
4886                                    
4887                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3798  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4892                                    
4893                  !!!next-token;                  !!!next-token;
4894                  redo B;                  next B;
4895                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4896                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4897                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4898                  ## Ignore the token                  ## Ignore the token
4899                  !!!next-token;                  !!!next-token;
4900                  redo B;                  next B;
4901                } else {                } else {
4902                    !!!cp ('t178');
4903                  #                  #
4904                }                }
4905              } elsif ({              } elsif ({
# Line 3815  sub _tree_construction_main ($) { Line 4910  sub _tree_construction_main ($) {
4910                ## have an element in table scope                ## have an element in table scope
4911                my $i;                my $i;
4912                my $tn;                my $tn;
4913                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4914                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4915                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4916                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4917                    last INSCOPE;                      !!!cp ('t179');
4918                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4919                    $tn = $node->[1];  
4920                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4921                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4922                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4923                            table => 1, html => 1,                                line => $token->{line},
4924                           }->{$node->[1]}) {                                column => $token->{column}};
4925                    last INSCOPE;                      next B;
4926                      } elsif ($node->[1] & TABLE_CELL_EL) {
4927                        !!!cp ('t180');
4928                        $tn = $node->[0]->manakai_local_name;
4929                        ## NOTE: There is exactly one |td| or |th| element
4930                        ## in scope in the stack of open elements by definition.
4931                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4932                        ## ISSUE: Can this be reached?
4933                        !!!cp ('t181');
4934                        last;
4935                      }
4936                  }                  }
4937                } # INSCOPE  
4938                unless (defined $i) {                  !!!cp ('t182');
4939                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4940                        value => $token->{tag_name}, token => $token);
4941                  ## Ignore the token                  ## Ignore the token
4942                  !!!next-token;                  !!!next-token;
4943                  redo B;                  next B;
4944                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4945              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4946                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4947                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4948    
4949                ## As if </caption>                ## As if </caption>
4950                ## have a table element in table scope                ## have a table element in table scope
4951                my $i;                my $i;
4952                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4953                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4954                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4955                      !!!cp ('t184');
4956                    $i = $_;                    $i = $_;
4957                    last INSCOPE;                    last INSCOPE;
4958                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4959                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4960                    last INSCOPE;                    last INSCOPE;
4961                  }                  }
4962                } # INSCOPE                } # INSCOPE
4963                unless (defined $i) {                unless (defined $i) {
4964                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4965                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4966                  ## Ignore the token                  ## Ignore the token
4967                  !!!next-token;                  !!!next-token;
4968                  redo B;                  next B;
4969                }                }
4970                                
4971                ## generate implied end tags                ## generate implied end tags
4972                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4973                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4974                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $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;  
4975                }                }
4976    
4977                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4978                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4979                    !!!parse-error (type => 'not closed',
4980                                    value => $self->{open_elements}->[-1]->[0]
4981                                        ->manakai_local_name,
4982                                    token => $token);
4983                  } else {
4984                    !!!cp ('t189');
4985                }                }
4986    
4987                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3891  sub _tree_construction_main ($) { Line 4991  sub _tree_construction_main ($) {
4991                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4992    
4993                ## reprocess                ## reprocess
4994                redo B;                next B;
4995              } elsif ({              } elsif ({
4996                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4997                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4998                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4999                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5000                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5001                  ## Ignore the token                  ## Ignore the token
5002                  !!!next-token;                  !!!next-token;
5003                  redo B;                  next B;
5004                } else {                } else {
5005                    !!!cp ('t191');
5006                  #                  #
5007                }                }
5008              } elsif ({              } elsif ({
# Line 3908  sub _tree_construction_main ($) { Line 5010  sub _tree_construction_main ($) {
5010                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5011                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5012                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5013                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5014                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5015                ## Ignore the token                ## Ignore the token
5016                !!!next-token;                !!!next-token;
5017                redo B;                next B;
5018              } else {              } else {
5019                  !!!cp ('t193');
5020                #                #
5021              }              }
5022          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5023            for my $entry (@{$self->{open_elements}}) {
5024              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5025                !!!cp ('t75');
5026                !!!parse-error (type => 'in body:#eof', token => $token);
5027                last;
5028              }
5029            }
5030    
5031            ## Stop parsing.
5032            last B;
5033        } else {        } else {
5034          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5035        }        }
# Line 3923  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038        #        #
5039      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5040        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5041              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5042                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5043              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5044                                
5045                unless (length $token->{data}) {            unless (length $token->{data}) {
5046                  !!!next-token;              !!!cp ('t194');
5047                  redo B;              !!!next-token;
5048                }              next B;
5049              }            } else {
5050                !!!cp ('t195');
5051              }
5052            }
5053    
5054              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5055    
5056              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5057              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3940  sub _tree_construction_main ($) { Line 5059  sub _tree_construction_main ($) {
5059              ## result in a new Text node.              ## result in a new Text node.
5060              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5061                            
5062              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]}) {  
5063                # MUST                # MUST
5064                my $foster_parent_element;                my $foster_parent_element;
5065                my $next_sibling;                my $next_sibling;
5066                my $prev_sibling;                my $prev_sibling;
5067                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5068                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5069                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5070                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5071                        !!!cp ('t196');
5072                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5073                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5074                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5075                    } else {                    } else {
5076                        !!!cp ('t197');
5077                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5078                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5079                    }                    }
# Line 3967  sub _tree_construction_main ($) { Line 5085  sub _tree_construction_main ($) {
5085                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5086                if (defined $prev_sibling and                if (defined $prev_sibling and
5087                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5088                    !!!cp ('t198');
5089                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5090                } else {                } else {
5091                    !!!cp ('t199');
5092                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5093                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5094                     $next_sibling);                     $next_sibling);
5095                }                }
5096              } else {            $open_tables->[-1]->[1] = 1; # tainted
5097                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5098              }            !!!cp ('t200');
5099              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5100            }
5101                            
5102              !!!next-token;          !!!next-token;
5103              redo B;          next B;
5104        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5105              if ({              if ({
5106                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3986  sub _tree_construction_main ($) { Line 5108  sub _tree_construction_main ($) {
5108                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5109                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5110                  ## Clear back to table context                  ## Clear back to table context
5111                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5112                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5113                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5114                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5115                  }                  }
5116                                    
5117                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5118                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5119                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5120                }                }
5121    
5122                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5123                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5124                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5125                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5126                  }                  }
5127                                    
5128                  ## Clear back to table body context                  ## Clear back to table body context
5129                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5130                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5131                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5132                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5133                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5134                  }                  }
5135                                    
5136                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5137                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5138                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5139                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5140                      !!!nack ('t204');
5141                    !!!next-token;                    !!!next-token;
5142                    redo B;                    next B;
5143                  } else {                  } else {
5144                    !!!insert-element ('tr');                    !!!cp ('t205');
5145                      !!!insert-element ('tr',, $token);
5146                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5147                  }                  }
5148                  } else {
5149                    !!!cp ('t206');
5150                }                }
5151    
5152                ## Clear back to table row context                ## Clear back to table row context
5153                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5154                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5155                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5156                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5157                }                }
5158                                
5159                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5160                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5161    
5162                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5163                                
5164                  !!!nack ('t207.1');
5165                !!!next-token;                !!!next-token;
5166                redo B;                next B;
5167              } elsif ({              } elsif ({
5168                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5169                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4047  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175                  my $i;                  my $i;
5176                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5177                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5178                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5179                        !!!cp ('t208');
5180                      $i = $_;                      $i = $_;
5181                      last INSCOPE;                      last INSCOPE;
5182                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5183                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5184                      last INSCOPE;                      last INSCOPE;
5185                    }                    }
5186                  } # INSCOPE                  } # INSCOPE
5187                  unless (defined $i) {                  unless (defined $i) {
5188                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5189    ## TODO: This type is wrong.
5190                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5191                    ## Ignore the token                    ## Ignore the token
5192                      !!!nack ('t210.1');
5193                    !!!next-token;                    !!!next-token;
5194                    redo B;                    next B;
5195                  }                  }
5196                                    
5197                  ## Clear back to table row context                  ## Clear back to table row context
5198                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5199                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5200                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5201                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5202                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5203                  }                  }
5204                                    
5205                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5206                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5207                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5208                      !!!cp ('t212');
5209                    ## reprocess                    ## reprocess
5210                    redo B;                    !!!ack-later;
5211                      next B;
5212                  } else {                  } else {
5213                      !!!cp ('t213');
5214                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5215                  }                  }
5216                }                }
# Line 4086  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220                  my $i;                  my $i;
5221                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5222                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5223                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5224                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5225                      $i = $_;                      $i = $_;
5226                      last INSCOPE;                      last INSCOPE;
5227                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5228                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5229                      last INSCOPE;                      last INSCOPE;
5230                    }                    }
5231                  } # INSCOPE                  } # INSCOPE
5232                  unless (defined $i) {                  unless (defined $i) {
5233                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5234    ## TODO: This erorr type ios wrong.
5235                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5236                    ## Ignore the token                    ## Ignore the token
5237                      !!!nack ('t216.1');
5238                    !!!next-token;                    !!!next-token;
5239                    redo B;                    next B;
5240                  }                  }
5241    
5242                  ## Clear back to table body context                  ## Clear back to table body context
5243                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5244                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5245                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5246                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5247                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5248                  }                  }
5249                                    
# Line 4122  sub _tree_construction_main ($) { Line 5257  sub _tree_construction_main ($) {
5257                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5258                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5259                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5260                  } else {
5261                    !!!cp ('t218');
5262                }                }
5263    
5264                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5265                  ## Clear back to table context                  ## Clear back to table context
5266                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5267                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5268                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5269                      ## ISSUE: Can this state be reached?
5270                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5271                  }                  }
5272                                    
5273                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5274                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5275                  ## reprocess                  ## reprocess
5276                  redo B;                  !!!ack-later;
5277                    next B;
5278                } elsif ({                } elsif ({
5279                          caption => 1,                          caption => 1,
5280                          colgroup => 1,                          colgroup => 1,
5281                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5282                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5283                  ## Clear back to table context                  ## Clear back to table context
5284                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5285                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5286                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5287                      ## ISSUE: Can this state be reached?
5288                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5289                  }                  }
5290                                    
5291                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5292                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5293                                    
5294                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5295                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5296                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5297                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4160  sub _tree_construction_main ($) { Line 5300  sub _tree_construction_main ($) {
5300                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5301                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5302                  !!!next-token;                  !!!next-token;
5303                  redo B;                  !!!nack ('t220.1');
5304                    next B;
5305                } else {                } else {
5306                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5307                }                }
5308              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5309                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5310                                  value => $self->{open_elements}->[-1]->[0]
5311                                      ->manakai_local_name,
5312                                  token => $token);
5313    
5314                ## As if </table>                ## As if </table>
5315                ## have a table element in table scope                ## have a table element in table scope
5316                my $i;                my $i;
5317                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5318                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5319                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5320                      !!!cp ('t221');
5321                    $i = $_;                    $i = $_;
5322                    last INSCOPE;                    last INSCOPE;
5323                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5324                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5325                    last INSCOPE;                    last INSCOPE;
5326                  }                  }
5327                } # INSCOPE                } # INSCOPE
5328                unless (defined $i) {                unless (defined $i) {
5329                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5330    ## TODO: The following is wrong, maybe.
5331                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5332                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5333                    !!!nack ('t223.1');
5334                  !!!next-token;                  !!!next-token;
5335                  redo B;                  next B;
5336                }                }
5337                                
5338    ## TODO: Followings are removed from the latest spec.
5339                ## generate implied end tags                ## generate implied end tags
5340                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5341                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5342                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $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;  
5343                }                }
5344    
5345                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5346                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5347                    ## NOTE: |<table><tr><table>|
5348                    !!!parse-error (type => 'not closed',
5349                                    value => $self->{open_elements}->[-1]->[0]
5350                                        ->manakai_local_name,
5351                                    token => $token);
5352                  } else {
5353                    !!!cp ('t226');
5354                }                }
5355    
5356                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5357                  pop @{$open_tables};
5358    
5359                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5360    
5361                ## reprocess            ## reprocess
5362                redo B;            !!!ack-later;
5363          } else {            next B;
5364            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5365              if (not $open_tables->[-1]->[1]) { # tainted
5366                !!!cp ('t227.8');
5367                ## NOTE: This is a "as if in head" code clone.
5368                $parse_rcdata->(CDATA_CONTENT_MODEL);
5369                next B;
5370              } else {
5371                !!!cp ('t227.7');
5372                #
5373              }
5374            } elsif ($token->{tag_name} eq 'script') {
5375              if (not $open_tables->[-1]->[1]) { # tainted
5376                !!!cp ('t227.6');
5377                ## NOTE: This is a "as if in head" code clone.
5378                $script_start_tag->();
5379                next B;
5380              } else {
5381                !!!cp ('t227.5');
5382                #
5383              }
5384            } elsif ($token->{tag_name} eq 'input') {
5385              if (not $open_tables->[-1]->[1]) { # tainted
5386                if ($token->{attributes}->{type}) { ## TODO: case
5387                  my $type = lc $token->{attributes}->{type}->{value};
5388                  if ($type eq 'hidden') {
5389                    !!!cp ('t227.3');
5390                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5391    
5392            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5393    
5394                    ## TODO: form element pointer
5395    
5396                    pop @{$self->{open_elements}};
5397    
5398                    !!!next-token;
5399                    !!!ack ('t227.2.1');
5400                    next B;
5401                  } else {
5402                    !!!cp ('t227.2');
5403                    #
5404                  }
5405                } else {
5406                  !!!cp ('t227.1');
5407                  #
5408                }
5409              } else {
5410                !!!cp ('t227.4');
5411                #
5412              }
5413            } else {
5414              !!!cp ('t227');
5415            #            #
5416          }          }
5417    
5418            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5419    
5420            $insert = $insert_to_foster;
5421            #
5422        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5423              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5424                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4225  sub _tree_construction_main ($) { Line 5426  sub _tree_construction_main ($) {
5426                my $i;                my $i;
5427                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5428                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5429                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5430                      !!!cp ('t228');
5431                    $i = $_;                    $i = $_;
5432                    last INSCOPE;                    last INSCOPE;
5433                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5434                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5435                    last INSCOPE;                    last INSCOPE;
5436                  }                  }
5437                } # INSCOPE                } # INSCOPE
5438                unless (defined $i) {                unless (defined $i) {
5439                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5440                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5441                  ## Ignore the token                  ## Ignore the token
5442                    !!!nack ('t230.1');
5443                  !!!next-token;                  !!!next-token;
5444                  redo B;                  next B;
5445                  } else {
5446                    !!!cp ('t232');
5447                }                }
5448    
5449                ## Clear back to table row context                ## Clear back to table row context
5450                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5451                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5452                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5453                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5454                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5455                }                }
5456    
5457                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5458                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5459                !!!next-token;                !!!next-token;
5460                redo B;                !!!nack ('t231.1');
5461                  next B;
5462              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5463                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5464                  ## As if </tr>                  ## As if </tr>
# Line 4260  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  my $i;                  my $i;
5467                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5468                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5469                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5470                        !!!cp ('t233');
5471                      $i = $_;                      $i = $_;
5472                      last INSCOPE;                      last INSCOPE;
5473                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5474                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5475                      last INSCOPE;                      last INSCOPE;
5476                    }                    }
5477                  } # INSCOPE                  } # INSCOPE
5478                  unless (defined $i) {                  unless (defined $i) {
5479                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5480    ## TODO: The following is wrong.
5481                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5482                    ## Ignore the token                    ## Ignore the token
5483                      !!!nack ('t236.1');
5484                    !!!next-token;                    !!!next-token;
5485                    redo B;                    next B;
5486                  }                  }
5487                                    
5488                  ## Clear back to table row context                  ## Clear back to table row context
5489                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5490                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5491                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5492                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5493                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5494                  }                  }
5495                                    
# Line 4294  sub _tree_construction_main ($) { Line 5503  sub _tree_construction_main ($) {
5503                  my $i;                  my $i;
5504                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5505                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5506                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5507                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5508                      $i = $_;                      $i = $_;
5509                      last INSCOPE;                      last INSCOPE;
5510                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5511                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5512                      last INSCOPE;                      last INSCOPE;
5513                    }                    }
5514                  } # INSCOPE                  } # INSCOPE
5515                  unless (defined $i) {                  unless (defined $i) {
5516                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5517                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5518                    ## Ignore the token                    ## Ignore the token
5519                      !!!nack ('t239.1');
5520                    !!!next-token;                    !!!next-token;
5521                    redo B;                    next B;
5522                  }                  }
5523                                    
5524                  ## Clear back to table body context                  ## Clear back to table body context
5525                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5526                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5527                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5528                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5529                  }                  }
5530                                    
# Line 4332  sub _tree_construction_main ($) { Line 5540  sub _tree_construction_main ($) {
5540                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5541                }                }
5542    
5543                  ## NOTE: </table> in the "in table" insertion mode.
5544                  ## When you edit the code fragment below, please ensure that
5545                  ## the code for <table> in the "in table" insertion mode
5546                  ## is synced with it.
5547    
5548                ## have a table element in table scope                ## have a table element in table scope
5549                my $i;                my $i;
5550                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5551                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5552                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5553                      !!!cp ('t241');
5554                    $i = $_;                    $i = $_;
5555                    last INSCOPE;                    last INSCOPE;
5556                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5557                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5558                    last INSCOPE;                    last INSCOPE;
5559                  }                  }
5560                } # INSCOPE                } # INSCOPE
5561                unless (defined $i) {                unless (defined $i) {
5562                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5563                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5564                  ## Ignore the token                  ## Ignore the token
5565                    !!!nack ('t243.1');
5566                  !!!next-token;                  !!!next-token;
5567                  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]}) {  
                 !!!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') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5568                }                }
5569                                    
5570                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5571                  pop @{$open_tables};
5572                                
5573                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5574                                
5575                !!!next-token;                !!!next-token;
5576                redo B;                next B;
5577              } elsif ({              } elsif ({
5578                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5579                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4383  sub _tree_construction_main ($) { Line 5583  sub _tree_construction_main ($) {
5583                  my $i;                  my $i;
5584                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5585                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5586                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5587                        !!!cp ('t247');
5588                      $i = $_;                      $i = $_;
5589                      last INSCOPE;                      last INSCOPE;
5590                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5591                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5592                      last INSCOPE;                      last INSCOPE;
5593                    }                    }
5594                  } # INSCOPE                  } # INSCOPE
5595                    unless (defined $i) {                    unless (defined $i) {
5596                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5597                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5598                      ## Ignore the token                      ## Ignore the token
5599                        !!!nack ('t249.1');
5600                      !!!next-token;                      !!!next-token;
5601                      redo B;                      next B;
5602                    }                    }
5603                                    
5604                  ## As if </tr>                  ## As if </tr>
# Line 4404  sub _tree_construction_main ($) { Line 5606  sub _tree_construction_main ($) {
5606                  my $i;                  my $i;
5607                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5608                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5609                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5610                        !!!cp ('t250');
5611                      $i = $_;                      $i = $_;
5612                      last INSCOPE;                      last INSCOPE;
5613                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5614                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5615                      last INSCOPE;                      last INSCOPE;
5616                    }                    }
5617                  } # INSCOPE                  } # INSCOPE
5618                    unless (defined $i) {                    unless (defined $i) {
5619                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5620                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5621                      ## Ignore the token                      ## Ignore the token
5622                        !!!nack ('t252.1');
5623                      !!!next-token;                      !!!next-token;
5624                      redo B;                      next B;
5625                    }                    }
5626                                    
5627                  ## Clear back to table row context                  ## Clear back to table row context
5628                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5629                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5630                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5631                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5632                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5633                  }                  }
5634                                    
# Line 4437  sub _tree_construction_main ($) { Line 5641  sub _tree_construction_main ($) {
5641                my $i;                my $i;
5642                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5644                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5645                      !!!cp ('t254');
5646                    $i = $_;                    $i = $_;
5647                    last INSCOPE;                    last INSCOPE;
5648                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5649                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5650                    last INSCOPE;                    last INSCOPE;
5651                  }                  }
5652                } # INSCOPE                } # INSCOPE
5653                unless (defined $i) {                unless (defined $i) {
5654                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5655                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5656                  ## Ignore the token                  ## Ignore the token
5657                    !!!nack ('t256.1');
5658                  !!!next-token;                  !!!next-token;
5659                  redo B;                  next B;
5660                }                }
5661    
5662                ## Clear back to table body context                ## Clear back to table body context
5663                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5664                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5665                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5666                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5667                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5668                }                }
5669    
5670                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5671                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5672                  !!!nack ('t257.1');
5673                !!!next-token;                !!!next-token;
5674                redo B;                next B;
5675              } elsif ({              } elsif ({
5676                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5677                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5678                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5679                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5680                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5681                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5682                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5683                !!!next-token;            ## Ignore the token
5684                redo B;            !!!nack ('t258.1');
5685               !!!next-token;
5686              next B;
5687          } else {          } else {
5688            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5689              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5690    
5691            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5692            #            #
5693          }          }
5694          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5695            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5696                    @{$self->{open_elements}} == 1) { # redundant, maybe
5697              !!!parse-error (type => 'in body:#eof', token => $token);
5698              !!!cp ('t259.1');
5699              #
5700            } else {
5701              !!!cp ('t259.2');
5702              #
5703            }
5704    
5705            ## Stop parsing
5706            last B;
5707        } else {        } else {
5708          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5709        }        }
# Line 4489  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5713                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5714                unless (length $token->{data}) {                unless (length $token->{data}) {
5715                    !!!cp ('t260');
5716                  !!!next-token;                  !!!next-token;
5717                  redo B;                  next B;
5718                }                }
5719              }              }
5720                            
5721                !!!cp ('t261');
5722              #              #
5723            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5724              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5725                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5726                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5727                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5728                  !!!ack ('t262.1');
5729                !!!next-token;                !!!next-token;
5730                redo B;                next B;
5731              } else {              } else {
5732                  !!!cp ('t263');
5733                #                #
5734              }              }
5735            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5736              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5737                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5738                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5739                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5740                  ## Ignore the token                  ## Ignore the token
5741                  !!!next-token;                  !!!next-token;
5742                  redo B;                  next B;
5743                } else {                } else {
5744                    !!!cp ('t265');
5745                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5746                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5747                  !!!next-token;                  !!!next-token;
5748                  redo B;                              next B;            
5749                }                }
5750              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5751                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5752                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5753                ## Ignore the token                ## Ignore the token
5754                !!!next-token;                !!!next-token;
5755                redo B;                next B;
5756              } else {              } else {
5757                  !!!cp ('t267');
5758                #                #
5759              }              }
5760            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5761              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5762            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5763              !!!cp ('t270.2');
5764              ## Stop parsing.
5765              last B;
5766            } else {
5767              ## NOTE: As if </colgroup>.
5768              !!!cp ('t270.1');
5769              pop @{$self->{open_elements}}; # colgroup
5770              $self->{insertion_mode} = IN_TABLE_IM;
5771              ## Reprocess.
5772              next B;
5773            }
5774          } else {
5775            die "$0: $token->{type}: Unknown token type";
5776          }
5777    
5778            ## As if </colgroup>            ## As if </colgroup>
5779            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5780              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5781    ## TODO: Wrong error type?
5782                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5783              ## Ignore the token              ## Ignore the token
5784                !!!nack ('t269.1');
5785              !!!next-token;              !!!next-token;
5786              redo B;              next B;
5787            } else {            } else {
5788                !!!cp ('t270');
5789              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5790              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5791                !!!ack-later;
5792              ## reprocess              ## reprocess
5793              redo B;              next B;
5794            }            }
5795      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5796        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5797            !!!cp ('t271');
5798          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5799          !!!next-token;          !!!next-token;
5800          redo B;          next B;
5801        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5802              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5803                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5804                  ## As if </option>              !!!cp ('t272');
5805                  pop @{$self->{open_elements}};              ## As if </option>
5806                }              pop @{$self->{open_elements}};
5807              } else {
5808                !!!cp ('t273');
5809              }
5810    
5811                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5812                !!!next-token;            !!!nack ('t273.1');
5813                redo B;            !!!next-token;
5814              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5815                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5816                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5817                  pop @{$self->{open_elements}};              !!!cp ('t274');
5818                }              ## As if </option>
5819                pop @{$self->{open_elements}};
5820              } else {
5821                !!!cp ('t275');
5822              }
5823    
5824                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5825                  ## As if </optgroup>              !!!cp ('t276');
5826                  pop @{$self->{open_elements}};              ## As if </optgroup>
5827                }              pop @{$self->{open_elements}};
5828              } else {
5829                !!!cp ('t277');
5830              }
5831    
5832                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5833                !!!next-token;            !!!nack ('t277.1');
5834                redo B;            !!!next-token;
5835              } elsif ($token->{tag_name} eq 'select') {            next B;
5836                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5837                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5838                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5839                my $i;                    {
5840                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5841                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5842                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5843                    $i = $_;                    }->{$token->{tag_name}})) {
5844                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5845                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5846                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5847                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5848                    last INSCOPE;            ## have an element in table scope
5849                  }            my $i;
5850                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5851                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5852                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5853                  ## Ignore the token                !!!cp ('t278');
5854                  !!!next-token;                $i = $_;
5855                  redo B;                last INSCOPE;
5856                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5857                  !!!cp ('t279');
5858                  last INSCOPE;
5859                }
5860              } # INSCOPE
5861              unless (defined $i) {
5862                !!!cp ('t280');
5863                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5864                ## Ignore the token
5865                !!!nack ('t280.1');
5866                !!!next-token;
5867                next B;
5868              }
5869                                
5870                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5871              splice @{$self->{open_elements}}, $i;
5872    
5873                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5874    
5875                !!!next-token;            if ($token->{tag_name} eq 'select') {
5876                redo B;              !!!nack ('t281.2');
5877                !!!next-token;
5878                next B;
5879              } else {
5880                !!!cp ('t281.1');
5881                !!!ack-later;
5882                ## Reprocess the token.
5883                next B;
5884              }
5885          } else {          } else {
5886            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5887              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5888            ## Ignore the token            ## Ignore the token
5889              !!!nack ('t282.1');
5890            !!!next-token;            !!!next-token;
5891            redo B;            next B;
5892          }          }
5893        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5894              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5895                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5896                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5897                  ## As if </option>              !!!cp ('t283');
5898                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5899                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5900                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5901                } else {              !!!cp ('t284');
5902                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5903                  ## Ignore the token            } else {
5904                }              !!!cp ('t285');
5905                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5906                redo B;              ## Ignore the token
5907              } elsif ($token->{tag_name} eq 'option') {            }
5908                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5909                  pop @{$self->{open_elements}};            !!!next-token;
5910                } else {            next B;
5911                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5912                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5913                }              !!!cp ('t286');
5914                !!!next-token;              pop @{$self->{open_elements}};
5915                redo B;            } else {
5916              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5917                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5918                my $i;              ## Ignore the token
5919                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5920                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5921                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5922                    $i = $_;            next B;
5923                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5924                  } elsif ({            ## have an element in table scope
5925                            table => 1, html => 1,            my $i;
5926                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5927                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5928                  }              if ($node->[1] & SELECT_EL) {
5929                } # INSCOPE                !!!cp ('t288');
5930                unless (defined $i) {                $i = $_;
5931                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5932                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5933                  !!!next-token;                !!!cp ('t289');
5934                  redo B;                last INSCOPE;
5935                }              }
5936              } # INSCOPE
5937              unless (defined $i) {
5938                !!!cp ('t290');
5939                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5940                ## Ignore the token
5941                !!!nack ('t290.1');
5942                !!!next-token;
5943                next B;
5944              }
5945                                
5946                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5947              splice @{$self->{open_elements}}, $i;
5948    
5949                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5950    
5951                !!!next-token;            !!!nack ('t291.1');
5952                redo B;            !!!next-token;
5953              } elsif ({            next B;
5954                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5955                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5956                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5957                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5958                     }->{$token->{tag_name}}) {
5959    ## TODO: The following is wrong?
5960              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5961                                
5962                ## have an element in table scope            ## have an element in table scope
5963                my $i;            my $i;
5964                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5965                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5966                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5967                    $i = $_;                !!!cp ('t292');
5968                    last INSCOPE;                $i = $_;
5969                  } elsif ({                last INSCOPE;
5970                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5971                           }->{$node->[1]}) {                !!!cp ('t293');
5972                    last INSCOPE;                last INSCOPE;
5973                  }              }
5974                } # INSCOPE            } # INSCOPE
5975                unless (defined $i) {            unless (defined $i) {
5976                  ## Ignore the token              !!!cp ('t294');
5977                  !!!next-token;              ## Ignore the token
5978                  redo B;              !!!nack ('t294.1');
5979                }              !!!next-token;
5980                next B;
5981              }
5982                                
5983                ## As if </select>            ## As if </select>
5984                ## have an element in table scope            ## have an element in table scope
5985                undef $i;            undef $i;
5986                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5987                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5988                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5989                    $i = $_;                !!!cp ('t295');
5990                    last INSCOPE;                $i = $_;
5991                  } elsif ({                last INSCOPE;
5992                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5993                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5994                    last INSCOPE;                !!!cp ('t296');
5995                  }                last INSCOPE;
5996                } # INSCOPE              }
5997                unless (defined $i) {            } # INSCOPE
5998                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5999                  ## Ignore the </select> token              !!!cp ('t297');
6000                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6001                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6002                }              ## Ignore the </select> token
6003                !!!nack ('t297.1');
6004                !!!next-token; ## TODO: ok?
6005                next B;
6006              }
6007                                
6008                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6009              splice @{$self->{open_elements}}, $i;
6010    
6011                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6012    
6013                ## reprocess            !!!ack-later;
6014                redo B;            ## reprocess
6015              next B;
6016          } else {          } else {
6017            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6018              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6019            ## Ignore the token            ## Ignore the token
6020              !!!nack ('t299.3');
6021            !!!next-token;            !!!next-token;
6022            redo B;            next B;
6023            }
6024          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6025            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6026                    @{$self->{open_elements}} == 1) { # redundant, maybe
6027              !!!cp ('t299.1');
6028              !!!parse-error (type => 'in body:#eof', token => $token);
6029            } else {
6030              !!!cp ('t299.2');
6031          }          }
6032    
6033            ## Stop parsing.
6034            last B;
6035        } else {        } else {
6036          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6037        }        }
# Line 4726  sub _tree_construction_main ($) { Line 6045  sub _tree_construction_main ($) {
6045            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6046                        
6047            unless (length $token->{data}) {            unless (length $token->{data}) {
6048                !!!cp ('t300');
6049              !!!next-token;              !!!next-token;
6050              redo B;              next B;
6051            }            }
6052          }          }
6053                    
6054          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6055            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6056              !!!parse-error (type => 'after html:#character', token => $token);
6057    
6058            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6059            } else {
6060              !!!cp ('t302');
6061          }          }
6062                    
6063          ## "after body" insertion mode          ## "after body" insertion mode
6064          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6065    
6066          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6067          ## reprocess          ## reprocess
6068          redo B;          next B;
6069        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6070          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6071            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6072              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6073                        
6074            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6075            } else {
6076              !!!cp ('t304');
6077          }          }
6078    
6079          ## "after body" insertion mode          ## "after body" insertion mode
6080          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6081    
6082          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6083            !!!ack-later;
6084          ## reprocess          ## reprocess
6085          redo B;          next B;
6086        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6087          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6088            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6089              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6090                        
6091            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6092            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6093            } else {
6094              !!!cp ('t306');
6095          }          }
6096    
6097          ## "after body" insertion mode          ## "after body" insertion mode
6098          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6099            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6100              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6101                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6102              ## Ignore the token              ## Ignore the token
6103              !!!next-token;              !!!next-token;
6104              redo B;              next B;
6105            } else {            } else {
6106                !!!cp ('t308');
6107              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            }            }
6111          } else {          } else {
6112            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6113              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6114    
6115            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6116            ## reprocess            ## reprocess
6117            redo B;            next B;
6118          }          }
6119          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6120            !!!cp ('t309.2');
6121            ## Stop parsing
6122            last B;
6123        } else {        } else {
6124          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6125        }        }
# Line 4792  sub _tree_construction_main ($) { Line 6129  sub _tree_construction_main ($) {
6129            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6130                        
6131            unless (length $token->{data}) {            unless (length $token->{data}) {
6132                !!!cp ('t310');
6133              !!!next-token;              !!!next-token;
6134              redo B;              next B;
6135            }            }
6136          }          }
6137                    
6138          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6139            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6140              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6141                !!!parse-error (type => 'in frameset:#character', token => $token);
6142            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6143              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6144                !!!parse-error (type => 'after frameset:#character', token => $token);
6145            } else { # "after html frameset"            } else { # "after html frameset"
6146              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6147                !!!parse-error (type => 'after html:#character', token => $token);
6148    
6149              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6150              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6151              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6152            }            }
6153                        
6154            ## Ignore the token.            ## Ignore the token.
6155            if (length $token->{data}) {            if (length $token->{data}) {
6156                !!!cp ('t314');
6157              ## reprocess the rest of characters              ## reprocess the rest of characters
6158            } else {            } else {
6159                !!!cp ('t315');
6160              !!!next-token;              !!!next-token;
6161            }            }
6162            redo B;            next B;
6163          }          }
6164                    
6165          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6166        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6167          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6168            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6169              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6170    
6171            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6172            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6173          }          } else {
6174              !!!cp ('t317');
6175            }
6176    
6177          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6178              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6179            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6180              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6181              !!!nack ('t318.1');
6182            !!!next-token;            !!!next-token;
6183            redo B;            next B;
6184          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6185                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6186            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6187              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6188            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6189              !!!ack ('t319.1');
6190            !!!next-token;            !!!next-token;
6191            redo B;            next B;
6192          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6193              !!!cp ('t320');
6194            ## NOTE: As if in body.            ## NOTE: As if in body.
6195            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6196            redo B;            next B;
6197          } else {          } else {
6198            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6199              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6200                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6201            } else {            } else {
6202              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6203                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6204            }            }
6205            ## Ignore the token            ## Ignore the token
6206              !!!nack ('t322.1');
6207            !!!next-token;            !!!next-token;
6208            redo B;            next B;
6209          }          }
6210        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6211          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6212            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6213              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6214    
6215            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6216            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6217            } else {
6218              !!!cp ('t324');
6219          }          }
6220    
6221          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6222              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6223            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6224                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6225              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6226                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6227              ## Ignore the token              ## Ignore the token
6228              !!!next-token;              !!!next-token;
6229            } else {            } else {
6230                !!!cp ('t326');
6231              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6232              !!!next-token;              !!!next-token;
6233            }            }
6234    
6235            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6236                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6237                !!!cp ('t327');
6238              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6239              } else {
6240                !!!cp ('t328');
6241            }            }
6242            redo B;            next B;
6243          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6244                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6245              !!!cp ('t329');
6246            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6247            !!!next-token;            !!!next-token;
6248            redo B;            next B;
6249          } else {          } else {
6250            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6251              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6252                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6253            } else {            } else {
6254              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6255                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6256            }            }
6257            ## Ignore the token            ## Ignore the token
6258            !!!next-token;            !!!next-token;
6259            redo B;            next B;
6260            }
6261          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6262            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                    @{$self->{open_elements}} == 1) { # redundant, maybe
6264              !!!cp ('t331.1');
6265              !!!parse-error (type => 'in body:#eof', token => $token);
6266            } else {
6267              !!!cp ('t331.2');
6268          }          }
6269            
6270            ## Stop parsing
6271            last B;
6272        } else {        } else {
6273          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6274        }        }
# Line 4905  sub _tree_construction_main ($) { Line 6281  sub _tree_construction_main ($) {
6281      ## "in body" insertion mode      ## "in body" insertion mode
6282      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6283        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6284            !!!cp ('t332');
6285          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6286          $script_start_tag->($insert);          $script_start_tag->();
6287          redo B;          next B;
6288        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6289            !!!cp ('t333');
6290          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6291          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6292          redo B;          next B;
6293        } elsif ({        } elsif ({
6294                  base => 1, link => 1,                  base => 1, link => 1,
6295                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6296            !!!cp ('t334');
6297          ## 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
6298          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6299          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6300            !!!ack ('t334.1');
6301          !!!next-token;          !!!next-token;
6302          redo B;          next B;
6303        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6304          ## 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
6305          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6306          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.
6307    
6308          unless ($self->{confident}) {          unless ($self->{confident}) {
6309            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6310                !!!cp ('t335');
6311                ## NOTE: Whether the encoding is supported or not is handled
6312                ## in the {change_encoding} callback.
6313              $self->{change_encoding}              $self->{change_encoding}
6314                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6315                            
6316              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6317                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6318                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6319                                           ->{has_reference});                                           ->{has_reference});
6320            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6321              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6322                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6323                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6324                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6325                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6326                  !!!cp ('t336');
6327                  ## NOTE: Whether the encoding is supported or not is handled
6328                  ## in the {change_encoding} callback.
6329                $self->{change_encoding}                $self->{change_encoding}
6330                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6331                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6332                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6333                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 4951  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336            }            }
6337          } else {          } else {
6338            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
6339                !!!cp ('t337');
6340              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6341                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6342                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6343                                           ->{has_reference});                                           ->{has_reference});
6344            }            }
6345            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
6346                !!!cp ('t338');
6347              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6348                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6349                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4964  sub _tree_construction_main ($) { Line 6351  sub _tree_construction_main ($) {
6351            }            }
6352          }          }
6353    
6354            !!!ack ('t338.1');
6355          !!!next-token;          !!!next-token;
6356          redo B;          next B;
6357        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6358          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6359          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6360          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6361            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6362        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6363          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6364                                
6365          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6366              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6367              !!!cp ('t342');
6368            ## Ignore the token            ## Ignore the token
6369          } else {          } else {
6370            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6371            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6372              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6373                  !!!cp ('t343');
6374                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6375                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6376                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6377              }              }
6378            }            }
6379          }          }
6380            !!!nack ('t343.1');
6381          !!!next-token;          !!!next-token;
6382          redo B;          next B;
6383        } elsif ({        } elsif ({
6384                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6385                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6386                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6387                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6388                  pre => 1,                  pre => 1, listing => 1,
6389                    form => 1,
6390                    table => 1,
6391                    hr => 1,
6392                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6393            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6394              !!!cp ('t350');
6395              !!!parse-error (type => 'in form:form', token => $token);
6396              ## Ignore the token
6397              !!!nack ('t350.1');
6398              !!!next-token;
6399              next B;
6400            }
6401    
6402          ## has a p element in scope          ## has a p element in scope
6403          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6404            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6405              !!!back-token;              !!!cp ('t344');
6406              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6407              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6408            } elsif ({                        line => $token->{line}, column => $token->{column}};
6409                      table => 1, caption => 1, td => 1, th => 1,              next B;
6410                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6411                     }->{$_->[1]}) {              !!!cp ('t345');
6412              last INSCOPE;              last INSCOPE;
6413            }            }
6414          } # INSCOPE          } # INSCOPE
6415                        
6416          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6417          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6418              !!!nack ('t346.1');
6419            !!!next-token;            !!!next-token;
6420            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6421              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6422              unless (length $token->{data}) {              unless (length $token->{data}) {
6423                  !!!cp ('t346');
6424                !!!next-token;                !!!next-token;
6425                } else {
6426                  !!!cp ('t349');
6427              }              }
6428              } else {
6429                !!!cp ('t348');
6430            }            }
6431          } else {          } elsif ($token->{tag_name} eq 'form') {
6432              !!!cp ('t347.1');
6433              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6434    
6435              !!!nack ('t347.2');
6436            !!!next-token;            !!!next-token;
6437          }          } elsif ($token->{tag_name} eq 'table') {
6438          redo B;            !!!cp ('t382');
6439        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6440          if (defined $self->{form_element}) {            
6441            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6442            ## Ignore the token  
6443              !!!nack ('t382.1');
6444              !!!next-token;
6445            } elsif ($token->{tag_name} eq 'hr') {
6446              !!!cp ('t386');
6447              pop @{$self->{open_elements}};
6448            
6449              !!!nack ('t386.1');
6450            !!!next-token;            !!!next-token;
           redo B;  
6451          } else {          } else {
6452            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!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]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6453            !!!next-token;            !!!next-token;
           redo B;  
6454          }          }
6455        } elsif ($token->{tag_name} eq 'li') {          next B;
6456          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## 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') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
6457          ## has a p element in scope          ## has a p element in scope
6458          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6459            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6460              !!!back-token;              !!!cp ('t353');
6461              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6462              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6463            } elsif ({                        line => $token->{line}, column => $token->{column}};
6464                      table => 1, caption => 1, td => 1, th => 1,              next B;
6465                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6466                     }->{$_->[1]}) {              !!!cp ('t354');
6467              last INSCOPE;              last INSCOPE;
6468            }            }
6469          } # INSCOPE          } # INSCOPE
# Line 5119  sub _tree_construction_main ($) { Line 6471  sub _tree_construction_main ($) {
6471          ## Step 1          ## Step 1
6472          my $i = -1;          my $i = -1;
6473          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6474            my $li_or_dtdd = {li => {li => 1},
6475                              dt => {dt => 1, dd => 1},
6476                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6477          LI: {          LI: {
6478            ## Step 2            ## Step 2
6479            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6480              if ($i != -1) {              if ($i != -1) {
6481                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6482                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6483                                  value => $self->{open_elements}->[-1]->[0]
6484                                      ->manakai_local_name,
6485                                  token => $token);
6486                } else {
6487                  !!!cp ('t356');
6488              }              }
6489              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6490              last LI;              last LI;
6491              } else {
6492                !!!cp ('t357');
6493            }            }
6494                        
6495            ## Step 3            ## Step 3
6496            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6497                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6498                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6499                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6500                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6501                  not ($node->[1] & DIV_EL)) {
6502                !!!cp ('t358');
6503              last LI;              last LI;
6504            }            }
6505                        
6506              !!!cp ('t359');
6507            ## Step 4            ## Step 4
6508            $i--;            $i--;
6509            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6510            redo LI;            redo LI;
6511          } # LI          } # LI
6512                        
6513          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6514            !!!nack ('t359.1');
6515          !!!next-token;          !!!next-token;
6516          redo B;          next B;
6517        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6518          ## has a p element in scope          ## has a p element in scope
6519          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6520            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6521              !!!back-token;              !!!cp ('t367');
6522              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6523              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6524            } elsif ({                        line => $token->{line}, column => $token->{column}};
6525                      table => 1, caption => 1, td => 1, th => 1,              next B;
6526                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6527                     }->{$_->[1]}) {              !!!cp ('t368');
6528              last INSCOPE;              last INSCOPE;
6529            }            }
6530          } # INSCOPE          } # INSCOPE
6531                        
6532          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6533                        
6534          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6535                        
6536            !!!nack ('t368.1');
6537          !!!next-token;          !!!next-token;
6538          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') {  
             !!!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]}) {  
             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;  
6539        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6540          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6541            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6542            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6543              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6544                !!!parse-error (type => 'in a:a', token => $token);
6545                            
6546              !!!back-token;              !!!back-token; # <a>
6547              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6548              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6549                $formatting_end_tag->($token);
6550                            
6551              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6552                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6553                    !!!cp ('t372');
6554                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6555                  last AFE2;                  last AFE2;
6556                }                }
6557              } # AFE2              } # AFE2
6558              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6559                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6560                    !!!cp ('t373');
6561                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6562                  last OE;                  last OE;
6563                }                }
6564              } # OE              } # OE
6565              last AFE;              last AFE;
6566            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6567                !!!cp ('t374');
6568              last AFE;              last AFE;
6569            }            }
6570          } # AFE          } # AFE
6571                        
6572          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6573    
6574          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6575          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6576    
6577            !!!nack ('t374.1');
6578          !!!next-token;          !!!next-token;
6579          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}}) {  
         $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;  
6580        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6581          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6582    
6583          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6584          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6585            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6586            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6587              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6588              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6589              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6590              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6591            } elsif ({                        line => $token->{line}, column => $token->{column}};
6592                      table => 1, caption => 1, td => 1, th => 1,              next B;
6593                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6594                     }->{$node->[1]}) {              !!!cp ('t377');
6595              last INSCOPE;              last INSCOPE;
6596            }            }
6597          } # INSCOPE          } # INSCOPE
6598                    
6599          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6600          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6601                    
6602            !!!nack ('t377.1');
6603          !!!next-token;          !!!next-token;
6604          redo B;          next B;
6605        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6606          ## has a button element in scope          ## has a button element in scope
6607          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6608            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6609            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6610              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6611              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6612              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6613              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6614            } elsif ({                        line => $token->{line}, column => $token->{column}};
6615                      table => 1, caption => 1, td => 1, th => 1,              next B;
6616                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6617                     }->{$node->[1]}) {              !!!cp ('t379');
6618              last INSCOPE;              last INSCOPE;
6619            }            }
6620          } # INSCOPE          } # INSCOPE
6621                        
6622          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6623                        
6624          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6625          push @$active_formatting_elements, ['#marker', ''];  
6626            ## TODO: associate with $self->{form_element} if defined
6627    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6628          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6629            
6630          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $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') {  
             !!!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]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6631          !!!next-token;          !!!next-token;
6632          redo B;          next B;
6633        } elsif ({        } elsif ({
6634                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6635                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6636                  image => 1,                  noembed => 1,
6637                    noframes => 1,
6638                    noscript => 0, ## TODO: 1 if scripting is enabled
6639                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6640          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6641            !!!parse-error (type => 'image');            !!!cp ('t381');
6642            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6643            } else {
6644              !!!cp ('t399');
6645          }          }
6646            ## NOTE: There is an "as if in body" code clone.
6647          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6648          $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') {  
             !!!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]}) {  
             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') {  
         $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;  
6649        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6650          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6651                    
6652          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6653              !!!cp ('t389');
6654            ## Ignore the token            ## Ignore the token
6655              !!!nack ('t389'); ## NOTE: Not acknowledged.
6656            !!!next-token;            !!!next-token;
6657            redo B;            next B;
6658          } else {          } else {
6659            my $at = $token->{attributes};            my $at = $token->{attributes};
6660            my $form_attrs;            my $form_attrs;
# Line 5407  sub _tree_construction_main ($) { Line 6665  sub _tree_construction_main ($) {
6665            delete $at->{prompt};            delete $at->{prompt};
6666            my @tokens = (            my @tokens = (
6667                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6668                           attributes => $form_attrs},                           attributes => $form_attrs,
6669                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6670                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6671                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6672                            {type => START_TAG_TOKEN, tag_name => 'p',
6673                             line => $token->{line}, column => $token->{column}},
6674                            {type => START_TAG_TOKEN, tag_name => 'label',
6675                             line => $token->{line}, column => $token->{column}},
6676                         );                         );
6677            if ($prompt_attr) {            if ($prompt_attr) {
6678              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6679                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6680                               #line => $token->{line}, column => $token->{column},
6681                              };
6682            } else {            } else {
6683                !!!cp ('t391');
6684              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6685                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6686                               #line => $token->{line}, column => $token->{column},
6687                              }; # SHOULD
6688              ## TODO: make this configurable              ## TODO: make this configurable
6689            }            }
6690            push @tokens,            push @tokens,
6691                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6692                             line => $token->{line}, column => $token->{column}},
6693                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6694                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6695                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6696                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6697                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6698            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6699                             line => $token->{line}, column => $token->{column}},
6700                            {type => END_TAG_TOKEN, tag_name => 'form',
6701                             line => $token->{line}, column => $token->{column}};
6702              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6703            !!!back-token (@tokens);            !!!back-token (@tokens);
6704            redo B;            !!!next-token;
6705              next B;
6706          }          }
6707        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6708          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6709          my $el;          my $el;
6710          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6711                    
6712          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6713          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5442  sub _tree_construction_main ($) { Line 6716  sub _tree_construction_main ($) {
6716          $insert->($el);          $insert->($el);
6717                    
6718          my $text = '';          my $text = '';
6719            !!!nack ('t392.1');
6720          !!!next-token;          !!!next-token;
6721          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6722            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6723            unless (length $token->{data}) {            unless (length $token->{data}) {
6724                !!!cp ('t392');
6725              !!!next-token;              !!!next-token;
6726              } else {
6727                !!!cp ('t393');
6728            }            }
6729            } else {
6730              !!!cp ('t394');
6731          }          }
6732          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6733              !!!cp ('t395');
6734            $text .= $token->{data};            $text .= $token->{data};
6735            !!!next-token;            !!!next-token;
6736          }          }
6737          if (length $text) {          if (length $text) {
6738              !!!cp ('t396');
6739            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6740          }          }
6741                    
# Line 5461  sub _tree_construction_main ($) { Line 6743  sub _tree_construction_main ($) {
6743                    
6744          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6745              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6746              !!!cp ('t397');
6747            ## Ignore the token            ## Ignore the token
6748          } else {          } else {
6749            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6750              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6751          }          }
6752          !!!next-token;          !!!next-token;
6753          redo B;          next B;
6754        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6755                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6756          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6757    
6758            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6759    
6760            ## "adjust foreign attributes" - done in insert-element-f
6761                    
6762          !!!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);
6763                    
6764          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6765              pop @{$self->{open_elements}};
6766              !!!ack ('t398.1');
6767            } else {
6768              !!!cp ('t398.2');
6769              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6770              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6771              ## mode, "in body" (not "in foreign content") secondary insertion
6772              ## mode, maybe.
6773            }
6774    
6775          !!!next-token;          !!!next-token;
6776          redo B;          next B;
6777        } elsif ({        } elsif ({
6778                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6779                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6780                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6781                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6782                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6783          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6784            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6785          ## Ignore the token          ## Ignore the token
6786            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6787          !!!next-token;          !!!next-token;
6788          redo B;          next B;
6789                    
6790          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6791        } else {        } else {
6792            if ($token->{tag_name} eq 'image') {
6793              !!!cp ('t384');
6794              !!!parse-error (type => 'image', token => $token);
6795              $token->{tag_name} = 'img';
6796            } else {
6797              !!!cp ('t385');
6798            }
6799    
6800            ## NOTE: There is an "as if <br>" code clone.
6801          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6802                    
6803          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6804    
6805            if ({
6806                 applet => 1, marquee => 1, object => 1,
6807                }->{$token->{tag_name}}) {
6808              !!!cp ('t380');
6809              push @$active_formatting_elements, ['#marker', ''];
6810              !!!nack ('t380.1');
6811            } elsif ({
6812                      b => 1, big => 1, em => 1, font => 1, i => 1,
6813                      s => 1, small => 1, strile => 1,
6814                      strong => 1, tt => 1, u => 1,
6815                     }->{$token->{tag_name}}) {
6816              !!!cp ('t375');
6817              push @$active_formatting_elements, $self->{open_elements}->[-1];
6818              !!!nack ('t375.1');
6819            } elsif ($token->{tag_name} eq 'input') {
6820              !!!cp ('t388');
6821              ## TODO: associate with $self->{form_element} if defined
6822              pop @{$self->{open_elements}};
6823              !!!ack ('t388.2');
6824            } elsif ({
6825                      area => 1, basefont => 1, bgsound => 1, br => 1,
6826                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6827                      #image => 1,
6828                     }->{$token->{tag_name}}) {
6829              !!!cp ('t388.1');
6830              pop @{$self->{open_elements}};
6831              !!!ack ('t388.3');
6832            } elsif ($token->{tag_name} eq 'select') {
6833              ## TODO: associate with $self->{form_element} if defined
6834            
6835              if ($self->{insertion_mode} & TABLE_IMS or
6836                  $self->{insertion_mode} & BODY_TABLE_IMS or
6837                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6838                !!!cp ('t400.1');
6839                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6840              } else {
6841                !!!cp ('t400.2');
6842                $self->{insertion_mode} = IN_SELECT_IM;
6843              }
6844              !!!nack ('t400.3');
6845            } else {
6846              !!!nack ('t402');
6847            }
6848                    
6849          !!!next-token;          !!!next-token;
6850          redo B;          next B;
6851        }        }
6852      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6853        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6854          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6855              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6856            for (@{$self->{open_elements}}) {          INSCOPE: {
6857              unless ({            for (reverse @{$self->{open_elements}}) {
6858                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6859                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6860                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6861                      }->{$_->[1]}) {                last INSCOPE;
6862                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6863                  !!!cp ('t405.1');
6864                  last;
6865              }              }
6866            }            }
6867    
6868            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6869            !!!next-token;                            value => $token->{tag_name}, token => $token);
6870            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6871            !!!next-token;            !!!next-token;
6872            redo B;            next B;
6873            } # INSCOPE
6874    
6875            for (@{$self->{open_elements}}) {
6876              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6877                !!!cp ('t403');
6878                !!!parse-error (type => 'not closed',
6879                                value => $_->[0]->manakai_local_name,
6880                                token => $token);
6881                last;
6882              } else {
6883                !!!cp ('t404');
6884              }
6885          }          }
6886    
6887            $self->{insertion_mode} = AFTER_BODY_IM;
6888            !!!next-token;
6889            next B;
6890        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6891          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
6892            ## up-to-date, though it has same effect as speced.
6893            if (@{$self->{open_elements}} > 1 and
6894                $self->{open_elements}->[1]->[1] & BODY_EL) {
6895            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6896            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6897              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6898                !!!parse-error (type => 'not closed',
6899                                value => $self->{open_elements}->[1]->[0]
6900                                    ->manakai_local_name,
6901                                token => $token);
6902              } else {
6903                !!!cp ('t407');
6904            }            }
6905            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6906            ## reprocess            ## reprocess
6907            redo B;            next B;
6908          } else {          } else {
6909            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6910              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6911            ## Ignore the token            ## Ignore the token
6912            !!!next-token;            !!!next-token;
6913            redo B;            next B;
6914          }          }
6915        } elsif ({        } elsif ({
6916                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6917                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6918                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6919                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6920                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6921                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6922          ## has an element in scope          ## has an element in scope
6923          my $i;          my $i;
6924          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6925            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6926            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6927              ## generate implied end tags              !!!cp ('t410');
             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]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6928              $i = $_;              $i = $_;
6929              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6930            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6931                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6932              last INSCOPE;              last INSCOPE;
6933            }            }
6934          } # INSCOPE          } # INSCOPE
6935            
6936          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6937            if (defined $i) {            !!!cp ('t413');
6938              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6939            } else {
6940              ## Step 1. generate implied end tags
6941              while ({
6942                      dd => ($token->{tag_name} ne 'dd'),
6943                      dt => ($token->{tag_name} ne 'dt'),
6944                      li => ($token->{tag_name} ne 'li'),
6945                      p => 1,
6946                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6947                !!!cp ('t409');
6948                pop @{$self->{open_elements}};
6949              }
6950    
6951              ## Step 2.
6952              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6953                      ne $token->{tag_name}) {
6954                !!!cp ('t412');
6955                !!!parse-error (type => 'not closed',
6956                                value => $self->{open_elements}->[-1]->[0]
6957                                    ->manakai_local_name,
6958                                token => $token);
6959            } else {            } else {
6960              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6961            }            }
6962          }  
6963                      ## Step 3.
         if (defined $i) {  
6964            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6965          } elsif ($token->{tag_name} eq 'p') {  
6966            ## As if <p>, then reprocess the current token            ## Step 4.
6967            my $el;            $clear_up_to_marker->()
6968            !!!create-element ($el, 'p');                if {
6969            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6970                  }->{$token->{tag_name}};
6971          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6972          !!!next-token;          !!!next-token;
6973          redo B;          next B;
6974        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6975            undef $self->{form_element};
6976    
6977          ## has an element in scope          ## has an element in scope
6978            my $i;
6979          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6980            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6981            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6982              ## generate implied end tags              !!!cp ('t418');
6983              if ({              $i = $_;
                  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]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6984              last INSCOPE;              last INSCOPE;
6985            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6986                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6987              last INSCOPE;              last INSCOPE;
6988            }            }
6989          } # INSCOPE          } # INSCOPE
6990            
6991          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6992            pop @{$self->{open_elements}};            !!!cp ('t421');
6993          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6994            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6995              ## Step 1. generate implied end tags
6996              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6997                !!!cp ('t417');
6998                pop @{$self->{open_elements}};
6999              }
7000              
7001              ## Step 2.
7002              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7003                      ne $token->{tag_name}) {
7004                !!!cp ('t417.1');
7005                !!!parse-error (type => 'not closed',
7006                                value => $self->{open_elements}->[-1]->[0]
7007                                    ->manakai_local_name,
7008                                token => $token);
7009              } else {
7010                !!!cp ('t420');
7011              }  
7012              
7013              ## Step 3.
7014              splice @{$self->{open_elements}}, $i;
7015          }          }
7016    
         undef $self->{form_element};  
7017          !!!next-token;          !!!next-token;
7018          redo B;          next B;
7019        } elsif ({        } elsif ({
7020                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7021                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5642  sub _tree_construction_main ($) { Line 7023  sub _tree_construction_main ($) {
7023          my $i;          my $i;
7024          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7025            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7026            if ({            if ($node->[1] & HEADING_EL) {
7027                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$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]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7028              $i = $_;              $i = $_;
7029              last INSCOPE;              last INSCOPE;
7030            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7031                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7032              last INSCOPE;              last INSCOPE;
7033            }            }
7034          } # INSCOPE          } # INSCOPE
7035            
7036          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7037            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7038              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7039            } else {
7040              ## Step 1. generate implied end tags
7041              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7042                !!!cp ('t422');
7043                pop @{$self->{open_elements}};
7044              }
7045              
7046              ## Step 2.
7047              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7048                      ne $token->{tag_name}) {
7049                !!!cp ('t425');
7050                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7051              } else {
7052                !!!cp ('t426');
7053              }
7054    
7055              ## Step 3.
7056              splice @{$self->{open_elements}}, $i;
7057          }          }
7058                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7059          !!!next-token;          !!!next-token;
7060          redo B;          next B;
7061          } elsif ($token->{tag_name} eq 'p') {
7062            ## has an element in scope
7063            my $i;
7064            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7065              my $node = $self->{open_elements}->[$_];
7066              if ($node->[1] & P_EL) {
7067                !!!cp ('t410.1');
7068                $i = $_;
7069                last INSCOPE;
7070              } elsif ($node->[1] & SCOPING_EL) {
7071                !!!cp ('t411.1');
7072                last INSCOPE;
7073              }
7074            } # INSCOPE
7075    
7076            if (defined $i) {
7077              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7078                      ne $token->{tag_name}) {
7079                !!!cp ('t412.1');
7080                !!!parse-error (type => 'not closed',
7081                                value => $self->{open_elements}->[-1]->[0]
7082                                    ->manakai_local_name,
7083                                token => $token);
7084              } else {
7085                !!!cp ('t414.1');
7086              }
7087    
7088              splice @{$self->{open_elements}}, $i;
7089            } else {
7090              !!!cp ('t413.1');
7091              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7092    
7093              !!!cp ('t415.1');
7094              ## As if <p>, then reprocess the current token
7095              my $el;
7096              !!!create-element ($el, $HTML_NS, 'p',, $token);
7097              $insert->($el);
7098              ## NOTE: Not inserted into |$self->{open_elements}|.
7099            }
7100    
7101            !!!next-token;
7102            next B;
7103        } elsif ({        } elsif ({
7104                  a => 1,                  a => 1,
7105                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7106                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7107                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7108                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7109          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7110          redo B;          $formatting_end_tag->($token);
7111            next B;
7112        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7113          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7114            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7115    
7116          ## As if <br>          ## As if <br>
7117          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7118                    
7119          my $el;          my $el;
7120          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7121          $insert->($el);          $insert->($el);
7122                    
7123          ## Ignore the token.          ## Ignore the token.
7124          !!!next-token;          !!!next-token;
7125          redo B;          next B;
7126        } elsif ({        } elsif ({
7127                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7128                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5706  sub _tree_construction_main ($) { Line 7135  sub _tree_construction_main ($) {
7135                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7136                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7137                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7138          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7139            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7140          ## Ignore the token          ## Ignore the token
7141          !!!next-token;          !!!next-token;
7142          redo B;          next B;
7143                    
7144          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7145                    
# Line 5720  sub _tree_construction_main ($) { Line 7150  sub _tree_construction_main ($) {
7150    
7151          ## Step 2          ## Step 2
7152          S2: {          S2: {
7153            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7154              ## Step 1              ## Step 1
7155              ## generate implied end tags              ## generate implied end tags
7156              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7157                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7158                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7159                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7160              }              }
7161                    
7162              ## Step 2              ## Step 2
7163              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7164                        ne $token->{tag_name}) {
7165                  !!!cp ('t431');
7166                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7167                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7168                                  value => $self->{open_elements}->[-1]->[0]
7169                                      ->manakai_local_name,
7170                                  token => $token);
7171                } else {
7172                  !!!cp ('t432');
7173              }              }
7174                            
7175              ## Step 3              ## Step 3
# Line 5747  sub _tree_construction_main ($) { Line 7179  sub _tree_construction_main ($) {
7179              last S2;              last S2;
7180            } else {            } else {
7181              ## Step 3              ## Step 3
7182              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7183                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7184                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7185                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7186                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7187                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7188                ## Ignore the token                ## Ignore the token
7189                !!!next-token;                !!!next-token;
7190                last S2;                last S2;
7191              }              }
7192    
7193                !!!cp ('t434');
7194            }            }
7195                        
7196            ## Step 4            ## Step 4
# Line 5765  sub _tree_construction_main ($) { Line 7200  sub _tree_construction_main ($) {
7200            ## Step 5;            ## Step 5;
7201            redo S2;            redo S2;
7202          } # S2          } # S2
7203          redo B;          next B;
7204        }        }
7205      }      }
7206      redo B;      next B;
7207      } continue { # B
7208        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7209          ## NOTE: The code below is executed in cases where it does not have
7210          ## to be, but it it is harmless even in those cases.
7211          ## has an element in scope
7212          INSCOPE: {
7213            for (reverse 0..$#{$self->{open_elements}}) {
7214              my $node = $self->{open_elements}->[$_];
7215              if ($node->[1] & FOREIGN_EL) {
7216                last INSCOPE;
7217              } elsif ($node->[1] & SCOPING_EL) {
7218                last;
7219              }
7220            }
7221            
7222            ## NOTE: No foreign element in scope.
7223            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7224          } # INSCOPE
7225        }
7226    } # B    } # B
7227    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7228    ## Stop parsing # MUST    ## Stop parsing # MUST
7229        
7230    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5817  sub set_inner_html ($$$) { Line 7266  sub set_inner_html ($$$) {
7266      my $p = $class->new;      my $p = $class->new;
7267      $p->{document} = $doc;      $p->{document} = $doc;
7268    
7269      ## Step 9 # MUST      ## Step 8 # MUST
7270      my $i = 0;      my $i = 0;
7271      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7272      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7273      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7274        my $self = shift;        my $self = shift;
7275    
# Line 5829  sub set_inner_html ($$$) { Line 7278  sub set_inner_html ($$$) {
7278    
7279        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7280        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7281        $column++;  
7282          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7283          $p->{column}++;
7284    
7285        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7286          $line++;          $p->{line}++;
7287          $column = 0;          $p->{column} = 0;
7288            !!!cp ('i1');
7289        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7290          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7291          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7292          $line++;          $p->{line}++;
7293          $column = 0;          $p->{column} = 0;
7294            !!!cp ('i2');
7295        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7296          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7297            !!!cp ('i3');
7298        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7299            !!!cp ('i4');
7300          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7301          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7302          } elsif ($self->{next_char} <= 0x0008 or
7303                   (0x000E <= $self->{next_char} and
7304                    $self->{next_char} <= 0x001F) or
7305                   (0x007F <= $self->{next_char} and
7306                    $self->{next_char} <= 0x009F) or
7307                   (0xD800 <= $self->{next_char} and
7308                    $self->{next_char} <= 0xDFFF) or
7309                   (0xFDD0 <= $self->{next_char} and
7310                    $self->{next_char} <= 0xFDDF) or
7311                   {
7312                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7313                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7314                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7315                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7316                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7317                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7318                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7319                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7320                    0x10FFFE => 1, 0x10FFFF => 1,
7321                   }->{$self->{next_char}}) {
7322            !!!cp ('i4.1');
7323            !!!parse-error (type => 'control char', level => $self->{must_level});
7324    ## TODO: error type documentation
7325        }        }
7326      };      };
7327      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 5851  sub set_inner_html ($$$) { Line 7329  sub set_inner_html ($$$) {
7329            
7330      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7331        my (%opt) = @_;        my (%opt) = @_;
7332        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7333          my $column = $opt{column};
7334          if (defined $opt{token} and defined $opt{token}->{line}) {
7335            $line = $opt{token}->{line};
7336            $column = $opt{token}->{column};
7337          }
7338          warn "Parse error ($opt{type}) at line $line column $column\n";
7339      };      };
7340      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7341        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7342      };      };
7343            
7344      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 5878  sub set_inner_html ($$$) { Line 7362  sub set_inner_html ($$$) {
7362          unless defined $p->{content_model};          unless defined $p->{content_model};
7363          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7364    
7365      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7366          ## TODO: Foreign element OK?
7367    
7368      ## Step 4      ## Step 3
7369      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7370        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7371    
7372      ## Step 5 # MUST      ## Step 4 # MUST
7373      $doc->append_child ($root);      $doc->append_child ($root);
7374    
7375      ## Step 6 # MUST      ## Step 5 # MUST
7376      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7377    
7378      undef $p->{head_element};      undef $p->{head_element};
7379    
7380      ## Step 7 # MUST      ## Step 6 # MUST
7381      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7382    
7383      ## Step 8 # MUST      ## Step 7 # MUST
7384      my $anode = $node;      my $anode = $node;
7385      AN: while (defined $anode) {      AN: while (defined $anode) {
7386        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7387          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7388          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7389            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
7390                !!!cp ('i5');
7391              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7392              last AN;              last AN;
7393            }            }
# Line 5910  sub set_inner_html ($$$) { Line 7396  sub set_inner_html ($$$) {
7396        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7397      } # AN      } # AN
7398            
7399      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7400      {      {
7401        my $self = $p;        my $self = $p;
7402        !!!next-token;        !!!next-token;
7403      }      }
7404      $p->_tree_construction_main;      $p->_tree_construction_main;
7405    
7406      ## Step 11 # MUST      ## Step 10 # MUST
7407      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7408      for (@cn) {      for (@cn) {
7409        $node->remove_child ($_);        $node->remove_child ($_);
7410      }      }
7411      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7412    
7413      ## Step 12 # MUST      ## Step 11 # MUST
7414      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7415      for (@cn) {      for (@cn) {
7416        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5934  sub set_inner_html ($$$) { Line 7419  sub set_inner_html ($$$) {
7419      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7420    
7421      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7422    
7423        delete $p->{parse_error}; # delete loop
7424    } else {    } else {
7425      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";
7426    }    }

Legend:
Removed from v.1.78  
changed lines
  Added in v.1.139

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24