/[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.99 by wakaba, Sun Mar 9 03:46:43 2008 UTC revision 1.140 by wakaba, Sat May 24 09:59:52 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.  ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
# Line 325  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 338  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 358  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 384  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 447  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 463  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 471  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 490  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 510  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 518  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 551  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 569  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 588  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 601  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 609  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 618  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 634  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 657  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 690  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 712  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 747  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 770  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 825  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 836  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 865  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 891  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 963  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 987  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 1035  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 1069  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 1094  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 1143  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 1187  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 1234  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 1259  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 1331  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 1353  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 1385  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 1393  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 1408  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 1416  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 1452  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 1472  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 1480  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 1603  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 1619  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 1658  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 1667  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 2192  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 2203  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 2243  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 2252  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 2295  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 2336  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 2369  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 2459  sub _tree_construction_initial ($) { Line 3076  sub _tree_construction_initial ($) {
3076            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3077            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3078          !!!cp ('t1');          !!!cp ('t1');
3079          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3080        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3081          !!!cp ('t2');          !!!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 {        } else {
3085          !!!cp ('t3');          !!!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 2602  sub _tree_construction_initial ($) { Line 3221  sub _tree_construction_initial ($) {
3221                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3222               }->{$token->{type}}) {               }->{$token->{type}}) {
3223        !!!cp ('t14');        !!!cp ('t14');
3224        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3225        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3226        ## Go to the "before html" insertion mode.        ## 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
# Line 2623  sub _tree_construction_initial ($) { Line 3243  sub _tree_construction_initial ($) {
3243          !!!cp ('t17');          !!!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 "before html" insertion mode.        ## Go to the "before html" insertion mode.
3249        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3272  sub _tree_construction_root_element ($)
3272    B: {    B: {
3273        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3274          !!!cp ('t19');          !!!cp ('t19');
3275          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3276          ## Ignore the token          ## Ignore the token
3277          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3278          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3306  sub _tree_construction_root_element ($)
3306        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3307          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3308            my $root_element;            my $root_element;
3309            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3310            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3311            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3312                  [$root_element, $el_category->{html}];
3313    
3314            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3315              !!!cp ('t24');              !!!cp ('t24');
3316              $self->{application_cache_selection}              $self->{application_cache_selection}
3317                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3318              ## ISSUE: No relative reference resolution?              ## 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 {            } else {
3323              !!!cp ('t25');              !!!cp ('t25');
3324              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3325            }            }
3326    
3327              !!!nack ('t25c');
3328    
3329            !!!next-token;            !!!next-token;
3330            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3331          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3342  sub _tree_construction_root_element ($)
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        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3347      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3348      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3349    
3350      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3351    
3352      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3353        !!!ack-later;
3354      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3355    
3356      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2746  sub _reset_insertion_mode ($) { Line 3374  sub _reset_insertion_mode ($) {
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            !!!cp ('t28');
3378                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3379              !!!cp ('t27');          } else {
3380              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3381          }          }
3382        }        }
3383              
3384        ## Step 4..13        ## Step 4..14
3385        my $new_mode = {        my $new_mode;
3386          if ($node->[1] & FOREIGN_EL) {
3387            !!!cp ('t28.1');
3388            ## NOTE: Strictly spaking, the line below only applies to MathML and
3389            ## SVG elements.  Currently the HTML syntax supports only MathML and
3390            ## SVG elements as foreigners.
3391            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3392            ## ISSUE: What is set as the secondary insertion mode?
3393          } elsif ($node->[1] & TABLE_CELL_EL) {
3394            if ($last) {
3395              !!!cp ('t28.2');
3396              #
3397            } else {
3398              !!!cp ('t28.3');
3399              $new_mode = IN_CELL_IM;
3400            }
3401          } else {
3402            !!!cp ('t28.4');
3403            $new_mode = {
3404                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3405                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3406                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3407                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3408                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3409                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2774  sub _reset_insertion_mode ($) { Line 3414  sub _reset_insertion_mode ($) {
3414                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3415                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3416                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3417                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3418          }
3419        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3420                
3421        ## Step 14        ## Step 15
3422        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3423          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3424            !!!cp ('t29');            !!!cp ('t29');
3425            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2792  sub _reset_insertion_mode ($) { Line 3433  sub _reset_insertion_mode ($) {
3433          !!!cp ('t31');          !!!cp ('t31');
3434        }        }
3435                
3436        ## Step 15        ## Step 16
3437        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3438                
3439        ## Step 16        ## Step 17
3440        $i--;        $i--;
3441        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3442                
3443        ## Step 17        ## Step 18
3444        redo S3;        redo S3;
3445      } # S3      } # S3
3446    
# Line 2911  sub _tree_construction_main ($) { Line 3552  sub _tree_construction_main ($) {
3552      ## Step 1      ## Step 1
3553      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3554      my $el;      my $el;
3555      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3556    
3557      ## Step 2      ## Step 2
3558      $insert->($el);      $insert->($el);
# Line 2922  sub _tree_construction_main ($) { Line 3563  sub _tree_construction_main ($) {
3563    
3564      ## Step 4      ## Step 4
3565      my $text = '';      my $text = '';
3566        !!!nack ('t40.1');
3567      !!!next-token;      !!!next-token;
3568      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3569        !!!cp ('t40');        !!!cp ('t40');
# Line 2948  sub _tree_construction_main ($) { Line 3590  sub _tree_construction_main ($) {
3590        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3591        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3592          !!!cp ('t43');          !!!cp ('t43');
3593          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3594        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3595          !!!cp ('t44');          !!!cp ('t44');
3596          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3597        } else {        } else {
3598          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3599        }        }
# Line 2961  sub _tree_construction_main ($) { Line 3603  sub _tree_construction_main ($) {
3603    
3604    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3605      my $script_el;      my $script_el;
3606      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3607      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3608    
3609      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3610      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3611            
3612      my $text = '';      my $text = '';
3613        !!!nack ('t45.1');
3614      !!!next-token;      !!!next-token;
3615      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3616        !!!cp ('t45');        !!!cp ('t45');
# Line 2987  sub _tree_construction_main ($) { Line 3630  sub _tree_construction_main ($) {
3630        ## Ignore the token        ## Ignore the token
3631      } else {      } else {
3632        !!!cp ('t48');        !!!cp ('t48');
3633        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3634        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3635        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3636      }      }
# Line 3010  sub _tree_construction_main ($) { Line 3653  sub _tree_construction_main ($) {
3653      !!!next-token;      !!!next-token;
3654    }; # $script_start_tag    }; # $script_start_tag
3655    
3656      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3657      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3658      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3659    
3660    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3661      my $tag_name = shift;      my $end_tag_token = shift;
3662        my $tag_name = $end_tag_token->{tag_name};
3663    
3664        ## NOTE: The adoption agency algorithm (AAA).
3665    
3666      FET: {      FET: {
3667        ## Step 1        ## Step 1
3668        my $formatting_element;        my $formatting_element;
3669        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3670        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3671          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3672              !!!cp ('t52');
3673              last AFE;
3674            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3675                         eq $tag_name) {
3676            !!!cp ('t51');            !!!cp ('t51');
3677            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3678            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3679            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3680          }          }
3681        } # AFE        } # AFE
3682        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3683          !!!cp ('t53');          !!!cp ('t53');
3684          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3685          ## Ignore the token          ## Ignore the token
3686          !!!next-token;          !!!next-token;
3687          return;          return;
# Line 3047  sub _tree_construction_main ($) { Line 3698  sub _tree_construction_main ($) {
3698              last INSCOPE;              last INSCOPE;
3699            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3700              !!!cp ('t55');              !!!cp ('t55');
3701              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3702                                token => $end_tag_token);
3703              ## Ignore the token              ## Ignore the token
3704              !!!next-token;              !!!next-token;
3705              return;              return;
3706            }            }
3707          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3708            !!!cp ('t56');            !!!cp ('t56');
3709            $in_scope = 0;            $in_scope = 0;
3710          }          }
3711        } # INSCOPE        } # INSCOPE
3712        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3713          !!!cp ('t57');          !!!cp ('t57');
3714          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3715                            token => $end_tag_token);
3716          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3717          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3718          return;          return;
3719        }        }
3720        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3721          !!!cp ('t58');          !!!cp ('t58');
3722          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3723                            value => $self->{open_elements}->[-1]->[0]
3724                                ->manakai_local_name,
3725                            token => $end_tag_token);
3726        }        }
3727                
3728        ## Step 2        ## Step 2
# Line 3077  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3731        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3732          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3733          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3734              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3735              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3736               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3737            !!!cp ('t59');            !!!cp ('t59');
3738            $furthest_block = $node;            $furthest_block = $node;
3739            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3166  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819        } # S7          } # S7  
3820                
3821        ## Step 8        ## Step 8
3822        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3823            my $foster_parent_element;
3824            my $next_sibling;
3825            OE: for (reverse 0..$#{$self->{open_elements}}) {
3826              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3827                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3828                                 if (defined $parent and $parent->node_type == 1) {
3829                                   !!!cp ('t65.1');
3830                                   $foster_parent_element = $parent;
3831                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3832                                 } else {
3833                                   !!!cp ('t65.2');
3834                                   $foster_parent_element
3835                                     = $self->{open_elements}->[$_ - 1]->[0];
3836                                 }
3837                                 last OE;
3838                               }
3839                             } # OE
3840                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3841                               unless defined $foster_parent_element;
3842            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3843            $open_tables->[-1]->[1] = 1; # tainted
3844          } else {
3845            !!!cp ('t65.3');
3846            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3847          }
3848                
3849        ## Step 9        ## Step 9
3850        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3212  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890      } # FET      } # FET
3891    }; # $formatting_end_tag    }; # $formatting_end_tag
3892    
   ## NOTE: $open_tables->[-1]->[0] is the "current table".  
   ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.  
   my $open_tables = [[$self->{open_elements}->[0]->[0]]];  
   
3893    $insert = my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3894      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3895    }; # $insert_to_current    }; # $insert_to_current
3896    
3897    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3898      my $child = shift;      my $child = shift;
3899      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]}) {  
3900        # MUST        # MUST
3901        my $foster_parent_element;        my $foster_parent_element;
3902        my $next_sibling;        my $next_sibling;
3903                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3904                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3905                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3906                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3907                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3254  sub _tree_construction_main ($) { Line 3926  sub _tree_construction_main ($) {
3926      }      }
3927    }; # $insert_to_foster    }; # $insert_to_foster
3928    
3929    B: {    B: while (1) {
3930      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3931        !!!cp ('t73');        !!!cp ('t73');
3932        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3933        ## Ignore the token        ## Ignore the token
3934        ## Stay in the phase        ## Stay in the phase
3935        !!!next-token;        !!!next-token;
3936        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3937      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3938               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3939        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3940          !!!cp ('t79');          !!!cp ('t79');
3941          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3942          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3943        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3944          !!!cp ('t80');          !!!cp ('t80');
3945          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3946          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3947        } else {        } else {
3948          !!!cp ('t81');          !!!cp ('t81');
3949        }        }
3950    
3951        !!!cp ('t82');        !!!cp ('t82');
3952        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3953        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3954        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3955          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3319  sub _tree_construction_main ($) { Line 3959  sub _tree_construction_main ($) {
3959               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3960          }          }
3961        }        }
3962          !!!nack ('t84.1');
3963        !!!next-token;        !!!next-token;
3964        redo B;        next B;
3965      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3966        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3967        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3334  sub _tree_construction_main ($) { Line 3975  sub _tree_construction_main ($) {
3975          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3976        }        }
3977        !!!next-token;        !!!next-token;
3978        redo B;        next B;
3979      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3980          if ($token->{type} == CHARACTER_TOKEN) {
3981            !!!cp ('t87.1');
3982            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3983            !!!next-token;
3984            next B;
3985          } elsif ($token->{type} == START_TAG_TOKEN) {
3986            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3987                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3988                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3989                ($token->{tag_name} eq 'svg' and
3990                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3991              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3992              !!!cp ('t87.2');
3993              #
3994            } elsif ({
3995                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3996                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3997                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3998                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3999                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4000                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4001                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4002                      var => 1,
4003                     }->{$token->{tag_name}}) {
4004              !!!cp ('t87.2');
4005              !!!parse-error (type => 'not closed',
4006                              value => $self->{open_elements}->[-1]->[0]
4007                                  ->manakai_local_name,
4008                              token => $token);
4009    
4010              pop @{$self->{open_elements}}
4011                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4012    
4013              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4014              ## Reprocess.
4015              next B;
4016            } else {
4017              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4018              my $tag_name = $token->{tag_name};
4019              if ($nsuri eq $SVG_NS) {
4020                $tag_name = {
4021                   altglyph => 'altGlyph',
4022                   altglyphdef => 'altGlyphDef',
4023                   altglyphitem => 'altGlyphItem',
4024                   animatecolor => 'animateColor',
4025                   animatemotion => 'animateMotion',
4026                   animatetransform => 'animateTransform',
4027                   clippath => 'clipPath',
4028                   feblend => 'feBlend',
4029                   fecolormatrix => 'feColorMatrix',
4030                   fecomponenttransfer => 'feComponentTransfer',
4031                   fecomposite => 'feComposite',
4032                   feconvolvematrix => 'feConvolveMatrix',
4033                   fediffuselighting => 'feDiffuseLighting',
4034                   fedisplacementmap => 'feDisplacementMap',
4035                   fedistantlight => 'feDistantLight',
4036                   feflood => 'feFlood',
4037                   fefunca => 'feFuncA',
4038                   fefuncb => 'feFuncB',
4039                   fefuncg => 'feFuncG',
4040                   fefuncr => 'feFuncR',
4041                   fegaussianblur => 'feGaussianBlur',
4042                   feimage => 'feImage',
4043                   femerge => 'feMerge',
4044                   femergenode => 'feMergeNode',
4045                   femorphology => 'feMorphology',
4046                   feoffset => 'feOffset',
4047                   fepointlight => 'fePointLight',
4048                   fespecularlighting => 'feSpecularLighting',
4049                   fespotlight => 'feSpotLight',
4050                   fetile => 'feTile',
4051                   feturbulence => 'feTurbulence',
4052                   foreignobject => 'foreignObject',
4053                   glyphref => 'glyphRef',
4054                   lineargradient => 'linearGradient',
4055                   radialgradient => 'radialGradient',
4056                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4057                   textpath => 'textPath',  
4058                }->{$tag_name} || $tag_name;
4059              }
4060    
4061              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4062    
4063              ## "adjust foreign attributes" - done in insert-element-f
4064    
4065              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4066    
4067              if ($self->{self_closing}) {
4068                pop @{$self->{open_elements}};
4069                !!!ack ('t87.3');
4070              } else {
4071                !!!cp ('t87.4');
4072              }
4073    
4074              !!!next-token;
4075              next B;
4076            }
4077          } elsif ($token->{type} == END_TAG_TOKEN) {
4078            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4079            !!!cp ('t87.5');
4080            #
4081          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4082            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4083            !!!cp ('t87.6');
4084            #
4085            ## TODO: ...
4086          } else {
4087            die "$0: $token->{type}: Unknown token type";        
4088          }
4089        }
4090    
4091        if ($self->{insertion_mode} & HEAD_IMS) {
4092        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4093          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4094            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3345  sub _tree_construction_main ($) { Line 4098  sub _tree_construction_main ($) {
4098              !!!cp ('t88.1');              !!!cp ('t88.1');
4099              ## Ignore the token.              ## Ignore the token.
4100              !!!next-token;              !!!next-token;
4101              redo B;              next B;
4102            }            }
4103            unless (length $token->{data}) {            unless (length $token->{data}) {
4104              !!!cp ('t88');              !!!cp ('t88');
4105              !!!next-token;              !!!next-token;
4106              redo B;              next B;
4107            }            }
4108          }          }
4109    
4110          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111            !!!cp ('t89');            !!!cp ('t89');
4112            ## As if <head>            ## As if <head>
4113            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4116                  [$self->{head_element}, $el_category->{head}];
4117    
4118            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4119            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3369  sub _tree_construction_main ($) { Line 4123  sub _tree_construction_main ($) {
4123            !!!cp ('t90');            !!!cp ('t90');
4124            ## As if </noscript>            ## As if </noscript>
4125            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4126            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4127                        
4128            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4129            ## As if </head>            ## As if </head>
# Line 3385  sub _tree_construction_main ($) { Line 4139  sub _tree_construction_main ($) {
4139            !!!cp ('t92');            !!!cp ('t92');
4140          }          }
4141    
4142              ## "after head" insertion mode          ## "after head" insertion mode
4143              ## As if <body>          ## As if <body>
4144              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4145              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4146              ## reprocess          ## reprocess
4147              redo B;          next B;
4148            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4149              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4150                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                  !!!cp ('t93');              !!!cp ('t93');
4152                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4153                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4154                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4155                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4156                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4157                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4158                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4159                  !!!cp ('t94');              !!!next-token;
4160                  #              next B;
4161                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4162                  !!!cp ('t95');              !!!cp ('t93.2');
4163                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4164                  ## Ignore the token              ## Ignore the token
4165                  !!!next-token;              !!!nack ('t93.3');
4166                  redo B;              !!!next-token;
4167                }              next B;
4168              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4169                !!!cp ('t96');              !!!cp ('t95');
4170                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4171                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4172                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4173                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4174                next B;
4175              }
4176            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4177              !!!cp ('t96');
4178              ## As if <head>
4179              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4180              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4181              push @{$self->{open_elements}},
4182                  [$self->{head_element}, $el_category->{head}];
4183    
4184                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4185                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4186              } else {          } else {
4187                !!!cp ('t97');            !!!cp ('t97');
4188              }          }
4189    
4190              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4191                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4192                  !!!cp ('t98');                  !!!cp ('t98');
4193                  ## As if </noscript>                  ## As if </noscript>
4194                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4195                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4196                                
4197                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4198                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3440  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4204                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4205                  !!!cp ('t100');                  !!!cp ('t100');
4206                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4207                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4208                        [$self->{head_element}, $el_category->{head}];
4209                } else {                } else {
4210                  !!!cp ('t101');                  !!!cp ('t101');
4211                }                }
4212                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4213                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4214                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4215                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4216                  !!!nack ('t101.1');
4217                !!!next-token;                !!!next-token;
4218                redo B;                next B;
4219              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4220                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4221                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4222                  !!!cp ('t102');                  !!!cp ('t102');
4223                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4224                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4225                        [$self->{head_element}, $el_category->{head}];
4226                } else {                } else {
4227                  !!!cp ('t103');                  !!!cp ('t103');
4228                }                }
4229                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4230                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4231                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4232                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4233                  !!!ack ('t103.1');
4234                !!!next-token;                !!!next-token;
4235                redo B;                next B;
4236              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4237                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4238                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239                  !!!cp ('t104');                  !!!cp ('t104');
4240                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4242                        [$self->{head_element}, $el_category->{head}];
4243                } else {                } else {
4244                  !!!cp ('t105');                  !!!cp ('t105');
4245                }                }
4246                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4247                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.
4248    
4249                unless ($self->{confident}) {                unless ($self->{confident}) {
4250                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4251                    !!!cp ('t106');                    !!!cp ('t106');
4252                      ## NOTE: Whether the encoding is supported or not is handled
4253                      ## in the {change_encoding} callback.
4254                    $self->{change_encoding}                    $self->{change_encoding}
4255                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4256                             $token);
4257                                        
4258                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4259                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4260                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4261                                                 ->{has_reference});                                                 ->{has_reference});
4262                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4263                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4264                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4265                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4266                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4267                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4268                      !!!cp ('t107');                      !!!cp ('t107');
4269                        ## NOTE: Whether the encoding is supported or not is handled
4270                        ## in the {change_encoding} callback.
4271                      $self->{change_encoding}                      $self->{change_encoding}
4272                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4273                               $token);
4274                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4275                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4276                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3523  sub _tree_construction_main ($) { Line 4296  sub _tree_construction_main ($) {
4296                  }                  }
4297                }                }
4298    
4299                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4300                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                  !!!ack ('t110.1');
4302                !!!next-token;                !!!next-token;
4303                redo B;                next B;
4304              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4305                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4306                  !!!cp ('t111');                  !!!cp ('t111');
4307                  ## As if </noscript>                  ## As if </noscript>
4308                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4309                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4310                                
4311                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4312                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4313                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4314                  !!!cp ('t112');                  !!!cp ('t112');
4315                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4316                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4317                        [$self->{head_element}, $el_category->{head}];
4318                } else {                } else {
4319                  !!!cp ('t113');                  !!!cp ('t113');
4320                }                }
# Line 3548  sub _tree_construction_main ($) { Line 4323  sub _tree_construction_main ($) {
4323                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4324                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4325                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4326                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4327                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4328                redo B;                next B;
4329              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4330                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4331                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4332                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4333                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4334                  !!!cp ('t114');                  !!!cp ('t114');
4335                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4336                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4337                        [$self->{head_element}, $el_category->{head}];
4338                } else {                } else {
4339                  !!!cp ('t115');                  !!!cp ('t115');
4340                }                }
4341                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4342                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4343                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4344                redo B;                next B;
4345              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4346                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4347                  !!!cp ('t116');                  !!!cp ('t116');
4348                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4349                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4350                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4351                    !!!nack ('t116.1');
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                  !!!cp ('t117');                  !!!cp ('t117');
4356                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4357                  ## Ignore the token                  ## Ignore the token
4358                    !!!nack ('t117.1');
4359                  !!!next-token;                  !!!next-token;
4360                  redo B;                  next B;
4361                } else {                } else {
4362                  !!!cp ('t118');                  !!!cp ('t118');
4363                  #                  #
# Line 3589  sub _tree_construction_main ($) { Line 4367  sub _tree_construction_main ($) {
4367                  !!!cp ('t119');                  !!!cp ('t119');
4368                  ## As if </noscript>                  ## As if </noscript>
4369                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4370                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4371                                
4372                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4373                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4374                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4375                  !!!cp ('t120');                  !!!cp ('t120');
4376                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4377                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4378                        [$self->{head_element}, $el_category->{head}];
4379                } else {                } else {
4380                  !!!cp ('t121');                  !!!cp ('t121');
4381                }                }
4382    
4383                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4384                $script_start_tag->($insert_to_current);                $script_start_tag->();
4385                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4386                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4389                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4390                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4391                  !!!cp ('t122');                  !!!cp ('t122');
4392                  ## As if </noscript>                  ## As if </noscript>
4393                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4394                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4395                                    
4396                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4397                  ## As if </head>                  ## As if </head>
# Line 3629  sub _tree_construction_main ($) { Line 4408  sub _tree_construction_main ($) {
4408                }                }
4409    
4410                ## "after head" insertion mode                ## "after head" insertion mode
4411                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4412                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4413                  !!!cp ('t126');                  !!!cp ('t126');
4414                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3639  sub _tree_construction_main ($) { Line 4418  sub _tree_construction_main ($) {
4418                } else {                } else {
4419                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4420                }                }
4421                  !!!nack ('t127.1');
4422                !!!next-token;                !!!next-token;
4423                redo B;                next B;
4424              } else {              } else {
4425                !!!cp ('t128');                !!!cp ('t128');
4426                #                #
# Line 3650  sub _tree_construction_main ($) { Line 4430  sub _tree_construction_main ($) {
4430                !!!cp ('t129');                !!!cp ('t129');
4431                ## As if </noscript>                ## As if </noscript>
4432                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4433                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4434                                
4435                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4436                ## As if </head>                ## As if </head>
# Line 3669  sub _tree_construction_main ($) { Line 4449  sub _tree_construction_main ($) {
4449    
4450              ## "after head" insertion mode              ## "after head" insertion mode
4451              ## As if <body>              ## As if <body>
4452              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4453              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4454              ## reprocess              ## reprocess
4455              redo B;              !!!ack-later;
4456                next B;
4457            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4458              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4459                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4460                  !!!cp ('t132');                  !!!cp ('t132');
4461                  ## As if <head>                  ## As if <head>
4462                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4463                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4464                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4465                        [$self->{head_element}, $el_category->{head}];
4466    
4467                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4468                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4469                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4470                  !!!next-token;                  !!!next-token;
4471                  redo B;                  next B;
4472                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4473                  !!!cp ('t133');                  !!!cp ('t133');
4474                  ## As if </noscript>                  ## As if </noscript>
4475                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4476                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4477                                    
4478                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4479                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4480                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4481                  !!!next-token;                  !!!next-token;
4482                  redo B;                  next B;
4483                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4484                  !!!cp ('t134');                  !!!cp ('t134');
4485                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4486                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4487                  !!!next-token;                  !!!next-token;
4488                  redo B;                  next B;
4489                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4490                    !!!cp ('t134.1');
4491                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4492                    ## Ignore the token
4493                    !!!next-token;
4494                    next B;
4495                } else {                } else {
4496                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4497                }                }
4498              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4499                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3714  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4506                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4507                  !!!cp ('t137');                  !!!cp ('t137');
4508                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4509                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4510                  !!!next-token;                  !!!next-token;
4511                  redo B;                  next B;
4512                } else {                } else {
4513                  !!!cp ('t138');                  !!!cp ('t138');
4514                  #                  #
# Line 3728  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516              } elsif ({              } elsif ({
4517                        body => 1, html => 1,                        body => 1, html => 1,
4518                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4519                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4520                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4521                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head');  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4522                  !!!cp ('t140');                  !!!cp ('t140');
4523                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4524                  ## Ignore the token                  ## Ignore the token
4525                  !!!next-token;                  !!!next-token;
4526                  redo B;                  next B;
4527                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t140.1');
4529                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4530                    ## Ignore the token
4531                    !!!next-token;
4532                    next B;
4533                } else {                } else {
4534                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4535                }                }
4536                              } elsif ($token->{tag_name} eq 'p') {
4537                #                !!!cp ('t142');
4538              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4539                        p => 1, br => 1,                ## Ignore the token
4540                       }->{$token->{tag_name}}) {                !!!next-token;
4541                  next B;
4542                } elsif ($token->{tag_name} eq 'br') {
4543                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544                  !!!cp ('t142');                  !!!cp ('t142.2');
4545                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4546                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4547                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4548                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4549      
4550                    ## Reprocess in the "after head" insertion mode...
4551                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4552                    !!!cp ('t143.2');
4553                    ## As if </head>
4554                    pop @{$self->{open_elements}};
4555                    $self->{insertion_mode} = AFTER_HEAD_IM;
4556      
4557                    ## Reprocess in the "after head" insertion mode...
4558                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4559                    !!!cp ('t143.3');
4560                    ## ISSUE: Two parse errors for <head><noscript></br>
4561                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4562                    ## As if </noscript>
4563                    pop @{$self->{open_elements}};
4564                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4565    
4566                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4567                } else {                  ## As if </head>
4568                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4569                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4570    
4571                #                  ## Reprocess in the "after head" insertion mode...
4572              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4573                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4574                  #                  #
4575                } else {                } else {
4576                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4577                }                }
4578    
4579                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4580                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4581                  ## Ignore the token
4582                  !!!next-token;
4583                  next B;
4584                } else {
4585                  !!!cp ('t145');
4586                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4587                  ## Ignore the token
4588                  !!!next-token;
4589                  next B;
4590              }              }
4591    
4592              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4593                !!!cp ('t146');                !!!cp ('t146');
4594                ## As if </noscript>                ## As if </noscript>
4595                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4596                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4597                                
4598                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4599                ## As if </head>                ## As if </head>
# Line 3798  sub _tree_construction_main ($) { Line 4609  sub _tree_construction_main ($) {
4609              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4610  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4611                !!!cp ('t148');                !!!cp ('t148');
4612                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4613                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4614                !!!next-token;                !!!next-token;
4615                redo B;                next B;
4616              } else {              } else {
4617                !!!cp ('t149');                !!!cp ('t149');
4618              }              }
4619    
4620              ## "after head" insertion mode              ## "after head" insertion mode
4621              ## As if <body>              ## As if <body>
4622              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4623              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4624              ## reprocess              ## reprocess
4625              redo B;              next B;
4626            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4627              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4628            }            !!!cp ('t149.1');
4629    
4630              ## NOTE: As if <head>
4631              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4632              $self->{open_elements}->[-1]->[0]->append_child
4633                  ($self->{head_element});
4634              #push @{$self->{open_elements}},
4635              #    [$self->{head_element}, $el_category->{head}];
4636              #$self->{insertion_mode} = IN_HEAD_IM;
4637              ## NOTE: Reprocess.
4638    
4639              ## NOTE: As if </head>
4640              #pop @{$self->{open_elements}};
4641              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4642              ## NOTE: Reprocess.
4643              
4644              #
4645            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4646              !!!cp ('t149.2');
4647    
4648              ## NOTE: As if </head>
4649              pop @{$self->{open_elements}};
4650              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4651              ## NOTE: Reprocess.
4652    
4653              #
4654            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4655              !!!cp ('t149.3');
4656    
4657              !!!parse-error (type => 'in noscript:#eof', token => $token);
4658    
4659              ## As if </noscript>
4660              pop @{$self->{open_elements}};
4661              #$self->{insertion_mode} = IN_HEAD_IM;
4662              ## NOTE: Reprocess.
4663    
4664              ## NOTE: As if </head>
4665              pop @{$self->{open_elements}};
4666              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4667              ## NOTE: Reprocess.
4668    
4669              #
4670            } else {
4671              !!!cp ('t149.4');
4672              #
4673            }
4674    
4675            ## NOTE: As if <body>
4676            !!!insert-element ('body',, $token);
4677            $self->{insertion_mode} = IN_BODY_IM;
4678            ## NOTE: Reprocess.
4679            next B;
4680          } else {
4681            die "$0: $token->{type}: Unknown token type";
4682          }
4683    
4684            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4685      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4692    
4693              !!!next-token;              !!!next-token;
4694              redo B;              next B;
4695            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4696              if ({              if ({
4697                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3834  sub _tree_construction_main ($) { Line 4699  sub _tree_construction_main ($) {
4699                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4700                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4701                  ## have an element in table scope                  ## have an element in table scope
4702                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4705                      !!!cp ('t151');                      !!!cp ('t151');
4706                      $tn = $node->[1];  
4707                      last INSCOPE;                      ## Close the cell
4708                    } elsif ({                      !!!back-token; # <x>
4709                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4710                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4711                                  line => $token->{line},
4712                                  column => $token->{column}};
4713                        next B;
4714                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4715                      !!!cp ('t152');                      !!!cp ('t152');
4716                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4717                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4718                    }                    }
4719                                    }
4720                  !!!cp ('t154');  
4721                  ## Close the cell                  !!!cp ('t153');
4722                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4723                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4724                  redo B;                  ## Ignore the token
4725                    !!!nack ('t153.1');
4726                    !!!next-token;
4727                    next B;
4728                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4729                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4730                                    
4731                  ## As if </caption>                  ## NOTE: As if </caption>.
4732                  ## have a table element in table scope                  ## have a table element in table scope
4733                  my $i;                  my $i;
4734                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4735                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4736                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4737                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4738                      $i = $_;                        !!!cp ('t155');
4739                      last INSCOPE;                        $i = $_;
4740                    } elsif ({                        last INSCOPE;
4741                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4742                             }->{$node->[1]}) {                        !!!cp ('t156');
4743                      !!!cp ('t156');                        last;
4744                      last INSCOPE;                      }
4745                    }                    }
4746    
4747                      !!!cp ('t157');
4748                      !!!parse-error (type => 'start tag not allowed',
4749                                      value => $token->{tag_name}, token => $token);
4750                      ## Ignore the token
4751                      !!!nack ('t157.1');
4752                      !!!next-token;
4753                      next B;
4754                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4755                                    
4756                  ## generate implied end tags                  ## generate implied end tags
4757                  while ({                  while ($self->{open_elements}->[-1]->[1]
4758                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4759                    !!!cp ('t158');                    !!!cp ('t158');
4760                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4761                  }                  }
4762    
4763                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4764                    !!!cp ('t159');                    !!!cp ('t159');
4765                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4766                                      value => $self->{open_elements}->[-1]->[0]
4767                                          ->manakai_local_name,
4768                                      token => $token);
4769                  } else {                  } else {
4770                    !!!cp ('t160');                    !!!cp ('t160');
4771                  }                  }
# Line 3912  sub _tree_construction_main ($) { Line 4777  sub _tree_construction_main ($) {
4777                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4778                                    
4779                  ## reprocess                  ## reprocess
4780                  redo B;                  !!!ack-later;
4781                    next B;
4782                } else {                } else {
4783                  !!!cp ('t161');                  !!!cp ('t161');
4784                  #                  #
# Line 3928  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794                  my $i;                  my $i;
4795                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4796                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4797                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4798                      !!!cp ('t163');                      !!!cp ('t163');
4799                      $i = $_;                      $i = $_;
4800                      last INSCOPE;                      last INSCOPE;
4801                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4802                      !!!cp ('t164');                      !!!cp ('t164');
4803                      last INSCOPE;                      last INSCOPE;
4804                    }                    }
4805                  } # INSCOPE                  } # INSCOPE
4806                    unless (defined $i) {                    unless (defined $i) {
4807                      !!!cp ('t165');                      !!!cp ('t165');
4808                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4809                      ## Ignore the token                      ## Ignore the token
4810                      !!!next-token;                      !!!next-token;
4811                      redo B;                      next B;
4812                    }                    }
4813                                    
4814                  ## generate implied end tags                  ## generate implied end tags
4815                  while ({                  while ($self->{open_elements}->[-1]->[1]
4816                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4817                    !!!cp ('t166');                    !!!cp ('t166');
4818                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4819                  }                  }
4820    
4821                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4822                            ne $token->{tag_name}) {
4823                    !!!cp ('t167');                    !!!cp ('t167');
4824                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4825                                      value => $self->{open_elements}->[-1]->[0]
4826                                          ->manakai_local_name,
4827                                      token => $token);
4828                  } else {                  } else {
4829                    !!!cp ('t168');                    !!!cp ('t168');
4830                  }                  }
# Line 3969  sub _tree_construction_main ($) { Line 4836  sub _tree_construction_main ($) {
4836                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4837                                    
4838                  !!!next-token;                  !!!next-token;
4839                  redo B;                  next B;
4840                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4841                  !!!cp ('t169');                  !!!cp ('t169');
4842                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                  ## Ignore the token                  ## Ignore the token
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                } else {                } else {
4847                  !!!cp ('t170');                  !!!cp ('t170');
4848                  #                  #
# Line 3984  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4852                  ## have a table element in table scope                  ## have a table element in table scope
4853                  my $i;                  my $i;
4854                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4855                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4856                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4857                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4858                      $i = $_;                        !!!cp ('t171');
4859                      last INSCOPE;                        $i = $_;
4860                    } elsif ({                        last INSCOPE;
4861                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4862                             }->{$node->[1]}) {                        !!!cp ('t172');
4863                      !!!cp ('t172');                        last;
4864                      last INSCOPE;                      }
4865                    }                    }
4866    
4867                      !!!cp ('t173');
4868                      !!!parse-error (type => 'unmatched end tag',
4869                                      value => $token->{tag_name}, token => $token);
4870                      ## Ignore the token
4871                      !!!next-token;
4872                      next B;
4873                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4874                                    
4875                  ## generate implied end tags                  ## generate implied end tags
4876                  while ({                  while ($self->{open_elements}->[-1]->[1]
4877                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4878                    !!!cp ('t174');                    !!!cp ('t174');
4879                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4880                  }                  }
4881                                    
4882                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4883                    !!!cp ('t175');                    !!!cp ('t175');
4884                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4885                                      value => $self->{open_elements}->[-1]->[0]
4886                                          ->manakai_local_name,
4887                                      token => $token);
4888                  } else {                  } else {
4889                    !!!cp ('t176');                    !!!cp ('t176');
4890                  }                  }
# Line 4027  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4897                                    
4898                  !!!next-token;                  !!!next-token;
4899                  redo B;                  next B;
4900                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4901                  !!!cp ('t177');                  !!!cp ('t177');
4902                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4903                  ## Ignore the token                  ## Ignore the token
4904                  !!!next-token;                  !!!next-token;
4905                  redo B;                  next B;
4906                } else {                } else {
4907                  !!!cp ('t178');                  !!!cp ('t178');
4908                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4915  sub _tree_construction_main ($) {
4915                ## have an element in table scope                ## have an element in table scope
4916                my $i;                my $i;
4917                my $tn;                my $tn;
4918                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4919                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4920                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4921                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4922                    $i = $_;                      !!!cp ('t179');
4923                    last INSCOPE;                      $i = $_;
4924                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4925                    !!!cp ('t180');                      ## Close the cell
4926                    $tn = $node->[1];                      !!!back-token; # </x>
4927                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4928                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4929                  } elsif ({                                column => $token->{column}};
4930                            table => 1, html => 1,                      next B;
4931                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4932                    !!!cp ('t181');                      !!!cp ('t180');
4933                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4934                        ## NOTE: There is exactly one |td| or |th| element
4935                        ## in scope in the stack of open elements by definition.
4936                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4937                        ## ISSUE: Can this be reached?
4938                        !!!cp ('t181');
4939                        last;
4940                      }
4941                  }                  }
4942                } # INSCOPE  
               unless (defined $i) {  
4943                  !!!cp ('t182');                  !!!cp ('t182');
4944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4945                        value => $token->{tag_name}, token => $token);
4946                  ## Ignore the token                  ## Ignore the token
4947                  !!!next-token;                  !!!next-token;
4948                  redo B;                  next B;
4949                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4950              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4951                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4952                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4953    
4954                ## As if </caption>                ## As if </caption>
4955                ## have a table element in table scope                ## have a table element in table scope
4956                my $i;                my $i;
4957                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4958                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4959                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4960                    !!!cp ('t184');                    !!!cp ('t184');
4961                    $i = $_;                    $i = $_;
4962                    last INSCOPE;                    last INSCOPE;
4963                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4964                    !!!cp ('t185');                    !!!cp ('t185');
4965                    last INSCOPE;                    last INSCOPE;
4966                  }                  }
4967                } # INSCOPE                } # INSCOPE
4968                unless (defined $i) {                unless (defined $i) {
4969                  !!!cp ('t186');                  !!!cp ('t186');
4970                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4971                  ## Ignore the token                  ## Ignore the token
4972                  !!!next-token;                  !!!next-token;
4973                  redo B;                  next B;
4974                }                }
4975                                
4976                ## generate implied end tags                ## generate implied end tags
4977                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4978                  !!!cp ('t187');                  !!!cp ('t187');
4979                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4980                }                }
4981    
4982                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                  !!!cp ('t188');                  !!!cp ('t188');
4984                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4985                                    value => $self->{open_elements}->[-1]->[0]
4986                                        ->manakai_local_name,
4987                                    token => $token);
4988                } else {                } else {
4989                  !!!cp ('t189');                  !!!cp ('t189');
4990                }                }
# Line 4128  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4997    
4998                ## reprocess                ## reprocess
4999                redo B;                next B;
5000              } elsif ({              } elsif ({
5001                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5002                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5003                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5004                  !!!cp ('t190');                  !!!cp ('t190');
5005                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5006                  ## Ignore the token                  ## Ignore the token
5007                  !!!next-token;                  !!!next-token;
5008                  redo B;                  next B;
5009                } else {                } else {
5010                  !!!cp ('t191');                  !!!cp ('t191');
5011                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 5016  sub _tree_construction_main ($) {
5016                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5017                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5018                !!!cp ('t192');                !!!cp ('t192');
5019                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5020                ## Ignore the token                ## Ignore the token
5021                !!!next-token;                !!!next-token;
5022                redo B;                next B;
5023              } else {              } else {
5024                !!!cp ('t193');                !!!cp ('t193');
5025                #                #
5026              }              }
5027          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5028            for my $entry (@{$self->{open_elements}}) {
5029              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5030                !!!cp ('t75');
5031                !!!parse-error (type => 'in body:#eof', token => $token);
5032                last;
5033              }
5034            }
5035    
5036            ## Stop parsing.
5037            last B;
5038        } else {        } else {
5039          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5040        }        }
# Line 4171  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050            unless (length $token->{data}) {            unless (length $token->{data}) {
5051              !!!cp ('t194');              !!!cp ('t194');
5052              !!!next-token;              !!!next-token;
5053              redo B;              next B;
5054            } else {            } else {
5055              !!!cp ('t195');              !!!cp ('t195');
5056            }            }
5057          }          }
5058    
5059              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5060    
5061              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5062              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4185  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064              ## result in a new Text node.              ## result in a new Text node.
5065              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5066                            
5067              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]}) {  
5068                # MUST                # MUST
5069                my $foster_parent_element;                my $foster_parent_element;
5070                my $next_sibling;                my $next_sibling;
5071                my $prev_sibling;                my $prev_sibling;
5072                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5073                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5074                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5075                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5076                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4229  sub _tree_construction_main ($) { Line 5105  sub _tree_construction_main ($) {
5105          }          }
5106                            
5107          !!!next-token;          !!!next-token;
5108          redo B;          next B;
5109        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5110              if ({              if ({
5111                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4237  sub _tree_construction_main ($) { Line 5113  sub _tree_construction_main ($) {
5113                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5114                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5115                  ## Clear back to table context                  ## Clear back to table context
5116                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5117                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5118                    !!!cp ('t201');                    !!!cp ('t201');
5119                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5120                  }                  }
5121                                    
5122                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5123                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5124                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5125                }                }
# Line 4251  sub _tree_construction_main ($) { Line 5127  sub _tree_construction_main ($) {
5127                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5128                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5129                    !!!cp ('t202');                    !!!cp ('t202');
5130                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5131                  }                  }
5132                                    
5133                  ## Clear back to table body context                  ## Clear back to table body context
5134                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5135                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5136                    !!!cp ('t203');                    !!!cp ('t203');
5137                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5138                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4266  sub _tree_construction_main ($) { Line 5141  sub _tree_construction_main ($) {
5141                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5142                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5143                    !!!cp ('t204');                    !!!cp ('t204');
5144                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5145                      !!!nack ('t204');
5146                    !!!next-token;                    !!!next-token;
5147                    redo B;                    next B;
5148                  } else {                  } else {
5149                    !!!cp ('t205');                    !!!cp ('t205');
5150                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5151                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5152                  }                  }
5153                } else {                } else {
# Line 4279  sub _tree_construction_main ($) { Line 5155  sub _tree_construction_main ($) {
5155                }                }
5156    
5157                ## Clear back to table row context                ## Clear back to table row context
5158                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5159                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5160                  !!!cp ('t207');                  !!!cp ('t207');
5161                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5162                }                }
5163                                
5164                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5165                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5166    
5167                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5168                                
5169                  !!!nack ('t207.1');
5170                !!!next-token;                !!!next-token;
5171                redo B;                next B;
5172              } elsif ({              } elsif ({
5173                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5174                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                  my $i;                  my $i;
5181                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5183                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5184                      !!!cp ('t208');                      !!!cp ('t208');
5185                      $i = $_;                      $i = $_;
5186                      last INSCOPE;                      last INSCOPE;
5187                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5188                      !!!cp ('t209');                      !!!cp ('t209');
5189                      last INSCOPE;                      last INSCOPE;
5190                    }                    }
5191                  } # INSCOPE                  } # INSCOPE
5192                  unless (defined $i) {                  unless (defined $i) {
5193                   !!!cp ('t210');                    !!!cp ('t210');
5194  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5195                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5196                    ## Ignore the token                    ## Ignore the token
5197                      !!!nack ('t210.1');
5198                    !!!next-token;                    !!!next-token;
5199                    redo B;                    next B;
5200                  }                  }
5201                                    
5202                  ## Clear back to table row context                  ## Clear back to table row context
5203                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5204                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5205                    !!!cp ('t211');                    !!!cp ('t211');
5206                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5207                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4341  sub _tree_construction_main ($) { Line 5212  sub _tree_construction_main ($) {
5212                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5213                    !!!cp ('t212');                    !!!cp ('t212');
5214                    ## reprocess                    ## reprocess
5215                    redo B;                    !!!ack-later;
5216                      next B;
5217                  } else {                  } else {
5218                    !!!cp ('t213');                    !!!cp ('t213');
5219                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4353  sub _tree_construction_main ($) { Line 5225  sub _tree_construction_main ($) {
5225                  my $i;                  my $i;
5226                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5227                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5228                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5229                      !!!cp ('t214');                      !!!cp ('t214');
5230                      $i = $_;                      $i = $_;
5231                      last INSCOPE;                      last INSCOPE;
5232                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5233                      !!!cp ('t215');                      !!!cp ('t215');
5234                      last INSCOPE;                      last INSCOPE;
5235                    }                    }
# Line 4369  sub _tree_construction_main ($) { Line 5237  sub _tree_construction_main ($) {
5237                  unless (defined $i) {                  unless (defined $i) {
5238                    !!!cp ('t216');                    !!!cp ('t216');
5239  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5240                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5241                    ## Ignore the token                    ## Ignore the token
5242                      !!!nack ('t216.1');
5243                    !!!next-token;                    !!!next-token;
5244                    redo B;                    next B;
5245                  }                  }
5246    
5247                  ## Clear back to table body context                  ## Clear back to table body context
5248                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5249                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5250                    !!!cp ('t217');                    !!!cp ('t217');
5251                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5252                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4400  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268    
5269                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5270                  ## Clear back to table context                  ## Clear back to table context
5271                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5272                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5273                    !!!cp ('t219');                    !!!cp ('t219');
5274                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5275                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5276                  }                  }
5277                                    
5278                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5279                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5280                  ## reprocess                  ## reprocess
5281                  redo B;                  !!!ack-later;
5282                    next B;
5283                } elsif ({                } elsif ({
5284                          caption => 1,                          caption => 1,
5285                          colgroup => 1,                          colgroup => 1,
5286                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5287                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5288                  ## Clear back to table context                  ## Clear back to table context
5289                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5290                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5291                    !!!cp ('t220');                    !!!cp ('t220');
5292                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5293                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4427  sub _tree_construction_main ($) { Line 5296  sub _tree_construction_main ($) {
5296                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5297                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5298                                    
5299                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5300                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5301                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5302                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4436  sub _tree_construction_main ($) { Line 5305  sub _tree_construction_main ($) {
5305                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5306                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5307                  !!!next-token;                  !!!next-token;
5308                  redo B;                  !!!nack ('t220.1');
5309                    next B;
5310                } else {                } else {
5311                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5312                }                }
5313              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5314                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5315                                  value => $self->{open_elements}->[-1]->[0]
5316                                      ->manakai_local_name,
5317                                  token => $token);
5318    
5319                ## As if </table>                ## As if </table>
5320                ## have a table element in table scope                ## have a table element in table scope
5321                my $i;                my $i;
5322                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5323                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5324                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5325                    !!!cp ('t221');                    !!!cp ('t221');
5326                    $i = $_;                    $i = $_;
5327                    last INSCOPE;                    last INSCOPE;
5328                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5329                    !!!cp ('t222');                    !!!cp ('t222');
5330                    last INSCOPE;                    last INSCOPE;
5331                  }                  }
# Line 4463  sub _tree_construction_main ($) { Line 5333  sub _tree_construction_main ($) {
5333                unless (defined $i) {                unless (defined $i) {
5334                  !!!cp ('t223');                  !!!cp ('t223');
5335  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5336                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5337                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5338                    !!!nack ('t223.1');
5339                  !!!next-token;                  !!!next-token;
5340                  redo B;                  next B;
5341                }                }
5342                                
5343    ## TODO: Followings are removed from the latest spec.
5344                ## generate implied end tags                ## generate implied end tags
5345                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5346                  !!!cp ('t224');                  !!!cp ('t224');
5347                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5348                }                }
5349    
5350                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5351                  !!!cp ('t225');                  !!!cp ('t225');
5352  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5353                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5354                                    value => $self->{open_elements}->[-1]->[0]
5355                                        ->manakai_local_name,
5356                                    token => $token);
5357                } else {                } else {
5358                  !!!cp ('t226');                  !!!cp ('t226');
5359                }                }
# Line 4490  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363    
5364                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5365    
5366                ## reprocess            ## reprocess
5367                redo B;            !!!ack-later;
5368              next B;
5369            } elsif ($token->{tag_name} eq 'style') {
5370              if (not $open_tables->[-1]->[1]) { # tainted
5371                !!!cp ('t227.8');
5372                ## NOTE: This is a "as if in head" code clone.
5373                $parse_rcdata->(CDATA_CONTENT_MODEL);
5374                next B;
5375              } else {
5376                !!!cp ('t227.7');
5377                #
5378              }
5379            } elsif ($token->{tag_name} eq 'script') {
5380              if (not $open_tables->[-1]->[1]) { # tainted
5381                !!!cp ('t227.6');
5382                ## NOTE: This is a "as if in head" code clone.
5383                $script_start_tag->();
5384                next B;
5385              } else {
5386                !!!cp ('t227.5');
5387                #
5388              }
5389          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
5390            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5391              if ($token->{attributes}->{type}) { ## TODO: case              if ($token->{attributes}->{type}) { ## TODO: case
5392                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5393                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5394                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5395                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5396    
5397                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5398    
5399                  ## TODO: form element pointer                  ## TODO: form element pointer
5400    
5401                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5402    
5403                  !!!next-token;                  !!!next-token;
5404                  redo B;                  !!!ack ('t227.2.1');
5405                    next B;
5406                } else {                } else {
5407                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5408                  #                  #
# Line 4525  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420            #            #
5421          }          }
5422    
5423          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5424    
5425          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5426          #          #
# Line 4536  sub _tree_construction_main ($) { Line 5431  sub _tree_construction_main ($) {
5431                my $i;                my $i;
5432                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5433                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5434                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5435                    !!!cp ('t228');                    !!!cp ('t228');
5436                    $i = $_;                    $i = $_;
5437                    last INSCOPE;                    last INSCOPE;
5438                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5439                    !!!cp ('t229');                    !!!cp ('t229');
5440                    last INSCOPE;                    last INSCOPE;
5441                  }                  }
5442                } # INSCOPE                } # INSCOPE
5443                unless (defined $i) {                unless (defined $i) {
5444                  !!!cp ('t230');                  !!!cp ('t230');
5445                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                  ## Ignore the token                  ## Ignore the token
5447                    !!!nack ('t230.1');
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                } else {                } else {
5451                  !!!cp ('t232');                  !!!cp ('t232');
5452                }                }
5453    
5454                ## Clear back to table row context                ## Clear back to table row context
5455                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5456                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5457                  !!!cp ('t231');                  !!!cp ('t231');
5458  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4569  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5463                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5464                !!!next-token;                !!!next-token;
5465                redo B;                !!!nack ('t231.1');
5466                  next B;
5467              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5468                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5469                  ## As if </tr>                  ## As if </tr>
# Line 4577  sub _tree_construction_main ($) { Line 5471  sub _tree_construction_main ($) {
5471                  my $i;                  my $i;
5472                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5473                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5474                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5475                      !!!cp ('t233');                      !!!cp ('t233');
5476                      $i = $_;                      $i = $_;
5477                      last INSCOPE;                      last INSCOPE;
5478                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5479                      !!!cp ('t234');                      !!!cp ('t234');
5480                      last INSCOPE;                      last INSCOPE;
5481                    }                    }
# Line 4591  sub _tree_construction_main ($) { Line 5483  sub _tree_construction_main ($) {
5483                  unless (defined $i) {                  unless (defined $i) {
5484                    !!!cp ('t235');                    !!!cp ('t235');
5485  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5486                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5487                    ## Ignore the token                    ## Ignore the token
5488                      !!!nack ('t236.1');
5489                    !!!next-token;                    !!!next-token;
5490                    redo B;                    next B;
5491                  }                  }
5492                                    
5493                  ## Clear back to table row context                  ## Clear back to table row context
5494                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5495                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5496                    !!!cp ('t236');                    !!!cp ('t236');
5497  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5498                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4616  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                  my $i;                  my $i;
5509                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5511                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5512                      !!!cp ('t237');                      !!!cp ('t237');
5513                      $i = $_;                      $i = $_;
5514                      last INSCOPE;                      last INSCOPE;
5515                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5516                      !!!cp ('t238');                      !!!cp ('t238');
5517                      last INSCOPE;                      last INSCOPE;
5518                    }                    }
5519                  } # INSCOPE                  } # INSCOPE
5520                  unless (defined $i) {                  unless (defined $i) {
5521                    !!!cp ('t239');                    !!!cp ('t239');
5522                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                    ## Ignore the token                    ## Ignore the token
5524                      !!!nack ('t239.1');
5525                    !!!next-token;                    !!!next-token;
5526                    redo B;                    next B;
5527                  }                  }
5528                                    
5529                  ## Clear back to table body context                  ## Clear back to table body context
5530                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5531                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5532                    !!!cp ('t240');                    !!!cp ('t240');
5533                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5534                  }                  }
# Line 4666  sub _tree_construction_main ($) { Line 5554  sub _tree_construction_main ($) {
5554                my $i;                my $i;
5555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5556                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5557                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5558                    !!!cp ('t241');                    !!!cp ('t241');
5559                    $i = $_;                    $i = $_;
5560                    last INSCOPE;                    last INSCOPE;
5561                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5562                    !!!cp ('t242');                    !!!cp ('t242');
5563                    last INSCOPE;                    last INSCOPE;
5564                  }                  }
5565                } # INSCOPE                } # INSCOPE
5566                unless (defined $i) {                unless (defined $i) {
5567                  !!!cp ('t243');                  !!!cp ('t243');
5568                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5569                  ## Ignore the token                  ## Ignore the token
5570                    !!!nack ('t243.1');
5571                  !!!next-token;                  !!!next-token;
5572                  redo B;                  next B;
5573                }                }
5574                                    
5575                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4691  sub _tree_construction_main ($) { Line 5578  sub _tree_construction_main ($) {
5578                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5579                                
5580                !!!next-token;                !!!next-token;
5581                redo B;                next B;
5582              } elsif ({              } elsif ({
5583                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5584                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4701  sub _tree_construction_main ($) { Line 5588  sub _tree_construction_main ($) {
5588                  my $i;                  my $i;
5589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5591                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5592                      !!!cp ('t247');                      !!!cp ('t247');
5593                      $i = $_;                      $i = $_;
5594                      last INSCOPE;                      last INSCOPE;
5595                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5596                      !!!cp ('t248');                      !!!cp ('t248');
5597                      last INSCOPE;                      last INSCOPE;
5598                    }                    }
5599                  } # INSCOPE                  } # INSCOPE
5600                    unless (defined $i) {                    unless (defined $i) {
5601                      !!!cp ('t249');                      !!!cp ('t249');
5602                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                      ## Ignore the token                      ## Ignore the token
5604                        !!!nack ('t249.1');
5605                      !!!next-token;                      !!!next-token;
5606                      redo B;                      next B;
5607                    }                    }
5608                                    
5609                  ## As if </tr>                  ## As if </tr>
# Line 4725  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                  my $i;                  my $i;
5612                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5613                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5614                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5615                      !!!cp ('t250');                      !!!cp ('t250');
5616                      $i = $_;                      $i = $_;
5617                      last INSCOPE;                      last INSCOPE;
5618                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5619                      !!!cp ('t251');                      !!!cp ('t251');
5620                      last INSCOPE;                      last INSCOPE;
5621                    }                    }
5622                  } # INSCOPE                  } # INSCOPE
5623                    unless (defined $i) {                    unless (defined $i) {
5624                      !!!cp ('t252');                      !!!cp ('t252');
5625                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5626                      ## Ignore the token                      ## Ignore the token
5627                        !!!nack ('t252.1');
5628                      !!!next-token;                      !!!next-token;
5629                      redo B;                      next B;
5630                    }                    }
5631                                    
5632                  ## Clear back to table row context                  ## Clear back to table row context
5633                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5634                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5635                    !!!cp ('t253');                    !!!cp ('t253');
5636  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5637                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4762  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646                my $i;                my $i;
5647                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5648                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5649                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5650                    !!!cp ('t254');                    !!!cp ('t254');
5651                    $i = $_;                    $i = $_;
5652                    last INSCOPE;                    last INSCOPE;
5653                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5654                    !!!cp ('t255');                    !!!cp ('t255');
5655                    last INSCOPE;                    last INSCOPE;
5656                  }                  }
5657                } # INSCOPE                } # INSCOPE
5658                unless (defined $i) {                unless (defined $i) {
5659                  !!!cp ('t256');                  !!!cp ('t256');
5660                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                    !!!nack ('t256.1');
5663                  !!!next-token;                  !!!next-token;
5664                  redo B;                  next B;
5665                }                }
5666    
5667                ## Clear back to table body context                ## Clear back to table body context
5668                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5669                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5670                  !!!cp ('t257');                  !!!cp ('t257');
5671  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5672                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4792  sub _tree_construction_main ($) { Line 5674  sub _tree_construction_main ($) {
5674    
5675                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5676                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5677                  !!!nack ('t257.1');
5678                !!!next-token;                !!!next-token;
5679                redo B;                next B;
5680              } elsif ({              } elsif ({
5681                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5682                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5683                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5684                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5685                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5686                !!!cp ('t258');            !!!cp ('t258');
5687                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5688                ## Ignore the token            ## Ignore the token
5689                !!!next-token;            !!!nack ('t258.1');
5690                redo B;             !!!next-token;
5691              next B;
5692          } else {          } else {
5693            !!!cp ('t259');            !!!cp ('t259');
5694            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5695    
5696            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5697            #            #
5698          }          }
5699          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5700            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5701                    @{$self->{open_elements}} == 1) { # redundant, maybe
5702              !!!parse-error (type => 'in body:#eof', token => $token);
5703              !!!cp ('t259.1');
5704              #
5705            } else {
5706              !!!cp ('t259.2');
5707              #
5708            }
5709    
5710            ## Stop parsing
5711            last B;
5712        } else {        } else {
5713          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5714        }        }
# Line 4822  sub _tree_construction_main ($) { Line 5719  sub _tree_construction_main ($) {
5719                unless (length $token->{data}) {                unless (length $token->{data}) {
5720                  !!!cp ('t260');                  !!!cp ('t260');
5721                  !!!next-token;                  !!!next-token;
5722                  redo B;                  next B;
5723                }                }
5724              }              }
5725                            
# Line 4831  sub _tree_construction_main ($) { Line 5728  sub _tree_construction_main ($) {
5728            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5729              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5730                !!!cp ('t262');                !!!cp ('t262');
5731                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5732                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5733                  !!!ack ('t262.1');
5734                !!!next-token;                !!!next-token;
5735                redo B;                next B;
5736              } else {              } else {
5737                !!!cp ('t263');                !!!cp ('t263');
5738                #                #
5739              }              }
5740            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5741              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5742                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5743                  !!!cp ('t264');                  !!!cp ('t264');
5744                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5745                  ## Ignore the token                  ## Ignore the token
5746                  !!!next-token;                  !!!next-token;
5747                  redo B;                  next B;
5748                } else {                } else {
5749                  !!!cp ('t265');                  !!!cp ('t265');
5750                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5751                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                              next B;            
5754                }                }
5755              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5756                !!!cp ('t266');                !!!cp ('t266');
5757                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5758                ## Ignore the token                ## Ignore the token
5759                !!!next-token;                !!!next-token;
5760                redo B;                next B;
5761              } else {              } else {
5762                !!!cp ('t267');                !!!cp ('t267');
5763                #                #
5764              }              }
5765            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5766              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5767            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5768              !!!cp ('t270.2');
5769              ## Stop parsing.
5770              last B;
5771            } else {
5772              ## NOTE: As if </colgroup>.
5773              !!!cp ('t270.1');
5774              pop @{$self->{open_elements}}; # colgroup
5775              $self->{insertion_mode} = IN_TABLE_IM;
5776              ## Reprocess.
5777              next B;
5778            }
5779          } else {
5780            die "$0: $token->{type}: Unknown token type";
5781          }
5782    
5783            ## As if </colgroup>            ## As if </colgroup>
5784            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5785              !!!cp ('t269');              !!!cp ('t269');
5786              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5787                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5788              ## Ignore the token              ## Ignore the token
5789                !!!nack ('t269.1');
5790              !!!next-token;              !!!next-token;
5791              redo B;              next B;
5792            } else {            } else {
5793              !!!cp ('t270');              !!!cp ('t270');
5794              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5795              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5796                !!!ack-later;
5797              ## reprocess              ## reprocess
5798              redo B;              next B;
5799            }            }
5800      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5801        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5802          !!!cp ('t271');          !!!cp ('t271');
5803          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5804          !!!next-token;          !!!next-token;
5805          redo B;          next B;
5806        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5807              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5808                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5809                  !!!cp ('t272');              !!!cp ('t272');
5810                  ## As if </option>              ## As if </option>
5811                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5812                } else {            } else {
5813                  !!!cp ('t273');              !!!cp ('t273');
5814                }            }
5815    
5816                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5817                !!!next-token;            !!!nack ('t273.1');
5818                redo B;            !!!next-token;
5819              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5820                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5821                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5822                  ## As if </option>              !!!cp ('t274');
5823                  pop @{$self->{open_elements}};              ## As if </option>
5824                } else {              pop @{$self->{open_elements}};
5825                  !!!cp ('t275');            } else {
5826                }              !!!cp ('t275');
5827              }
5828    
5829                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5830                  !!!cp ('t276');              !!!cp ('t276');
5831                  ## As if </optgroup>              ## As if </optgroup>
5832                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5833                } else {            } else {
5834                  !!!cp ('t277');              !!!cp ('t277');
5835                }            }
5836    
5837                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5838                !!!next-token;            !!!nack ('t277.1');
5839                redo B;            !!!next-token;
5840              } elsif ($token->{tag_name} eq 'select') {            next B;
5841  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5842                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5843                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5844                ## have an element in table scope                    {
5845                my $i;                     caption => 1, table => 1,
5846                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5847                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5848                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5849                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5850                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5851                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5852                  } elsif ({            ## as if there were </select> (otherwise).
5853                            table => 1, html => 1,            ## have an element in table scope
5854                           }->{$node->[1]}) {            my $i;
5855                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5856                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5857                  }              if ($node->[1] & SELECT_EL) {
5858                } # INSCOPE                !!!cp ('t278');
5859                unless (defined $i) {                $i = $_;
5860                  !!!cp ('t280');                last INSCOPE;
5861                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5862                  ## Ignore the token                !!!cp ('t279');
5863                  !!!next-token;                last INSCOPE;
5864                  redo B;              }
5865                }            } # INSCOPE
5866              unless (defined $i) {
5867                !!!cp ('t280');
5868                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5869                ## Ignore the token
5870                !!!nack ('t280.1');
5871                !!!next-token;
5872                next B;
5873              }
5874                                
5875                !!!cp ('t281');            !!!cp ('t281');
5876                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5877    
5878                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5879    
5880                !!!next-token;            if ($token->{tag_name} eq 'select') {
5881                redo B;              !!!nack ('t281.2');
5882                !!!next-token;
5883                next B;
5884              } else {
5885                !!!cp ('t281.1');
5886                !!!ack-later;
5887                ## Reprocess the token.
5888                next B;
5889              }
5890          } else {          } else {
5891            !!!cp ('t282');            !!!cp ('t282');
5892            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5893            ## Ignore the token            ## Ignore the token
5894              !!!nack ('t282.1');
5895            !!!next-token;            !!!next-token;
5896            redo B;            next B;
5897          }          }
5898        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5899              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5900                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5901                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5902                  !!!cp ('t283');              !!!cp ('t283');
5903                  ## As if </option>              ## As if </option>
5904                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5905                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5906                  !!!cp ('t284');              !!!cp ('t284');
5907                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5908                } else {            } else {
5909                  !!!cp ('t285');              !!!cp ('t285');
5910                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5911                  ## Ignore the token              ## Ignore the token
5912                }            }
5913                !!!next-token;            !!!nack ('t285.1');
5914                redo B;            !!!next-token;
5915              } elsif ($token->{tag_name} eq 'option') {            next B;
5916                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5917                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5918                  pop @{$self->{open_elements}};              !!!cp ('t286');
5919                } else {              pop @{$self->{open_elements}};
5920                  !!!cp ('t287');            } else {
5921                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5922                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5923                }              ## Ignore the token
5924                !!!next-token;            }
5925                redo B;            !!!nack ('t287.1');
5926              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5927                ## have an element in table scope            next B;
5928                my $i;          } elsif ($token->{tag_name} eq 'select') {
5929                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5930                  my $node = $self->{open_elements}->[$_];            my $i;
5931                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5932                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5933                    $i = $_;              if ($node->[1] & SELECT_EL) {
5934                    last INSCOPE;                !!!cp ('t288');
5935                  } elsif ({                $i = $_;
5936                            table => 1, html => 1,                last INSCOPE;
5937                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5938                    !!!cp ('t289');                !!!cp ('t289');
5939                    last INSCOPE;                last INSCOPE;
5940                  }              }
5941                } # INSCOPE            } # INSCOPE
5942                unless (defined $i) {            unless (defined $i) {
5943                  !!!cp ('t290');              !!!cp ('t290');
5944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## Ignore the token              ## Ignore the token
5946                  !!!next-token;              !!!nack ('t290.1');
5947                  redo B;              !!!next-token;
5948                }              next B;
5949              }
5950                                
5951                !!!cp ('t291');            !!!cp ('t291');
5952                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5953    
5954                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5955    
5956                !!!next-token;            !!!nack ('t291.1');
5957                redo B;            !!!next-token;
5958              } elsif ({            next B;
5959                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5960                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5961                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5962                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5963                     }->{$token->{tag_name}}) {
5964  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5965                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5966                                
5967                ## have an element in table scope            ## have an element in table scope
5968                my $i;            my $i;
5969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5970                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5971                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5972                    !!!cp ('t292');                !!!cp ('t292');
5973                    $i = $_;                $i = $_;
5974                    last INSCOPE;                last INSCOPE;
5975                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5976                            table => 1, html => 1,                !!!cp ('t293');
5977                           }->{$node->[1]}) {                last INSCOPE;
5978                    !!!cp ('t293');              }
5979                    last INSCOPE;            } # INSCOPE
5980                  }            unless (defined $i) {
5981                } # INSCOPE              !!!cp ('t294');
5982                unless (defined $i) {              ## Ignore the token
5983                  !!!cp ('t294');              !!!nack ('t294.1');
5984                  ## Ignore the token              !!!next-token;
5985                  !!!next-token;              next B;
5986                  redo B;            }
               }  
5987                                
5988                ## As if </select>            ## As if </select>
5989                ## have an element in table scope            ## have an element in table scope
5990                undef $i;            undef $i;
5991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5992                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5993                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5994                    !!!cp ('t295');                !!!cp ('t295');
5995                    $i = $_;                $i = $_;
5996                    last INSCOPE;                last INSCOPE;
5997                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5998  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5999                    !!!cp ('t296');                !!!cp ('t296');
6000                    last INSCOPE;                last INSCOPE;
6001                  }              }
6002                } # INSCOPE            } # INSCOPE
6003                unless (defined $i) {            unless (defined $i) {
6004                  !!!cp ('t297');              !!!cp ('t297');
6005  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6006                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6007                  ## Ignore the </select> token              ## Ignore the </select> token
6008                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6009                  redo B;              !!!next-token; ## TODO: ok?
6010                }              next B;
6011              }
6012                                
6013                !!!cp ('t298');            !!!cp ('t298');
6014                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6015    
6016                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6017    
6018                ## reprocess            !!!ack-later;
6019                redo B;            ## reprocess
6020              next B;
6021          } else {          } else {
6022            !!!cp ('t299');            !!!cp ('t299');
6023            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6024            ## Ignore the token            ## Ignore the token
6025              !!!nack ('t299.3');
6026            !!!next-token;            !!!next-token;
6027            redo B;            next B;
6028          }          }
6029          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6030            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6031                    @{$self->{open_elements}} == 1) { # redundant, maybe
6032              !!!cp ('t299.1');
6033              !!!parse-error (type => 'in body:#eof', token => $token);
6034            } else {
6035              !!!cp ('t299.2');
6036            }
6037    
6038            ## Stop parsing.
6039            last B;
6040        } else {        } else {
6041          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6042        }        }
# Line 5105  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            unless (length $token->{data}) {            unless (length $token->{data}) {
6053              !!!cp ('t300');              !!!cp ('t300');
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            }            }
6057          }          }
6058                    
6059          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6060            !!!cp ('t301');            !!!cp ('t301');
6061            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6062    
6063            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6064          } else {          } else {
# Line 5119  sub _tree_construction_main ($) { Line 6066  sub _tree_construction_main ($) {
6066          }          }
6067                    
6068          ## "after body" insertion mode          ## "after body" insertion mode
6069          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6070    
6071          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6072          ## reprocess          ## reprocess
6073          redo B;          next B;
6074        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6075          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6076            !!!cp ('t303');            !!!cp ('t303');
6077            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6078                        
6079            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6080          } else {          } else {
# Line 5135  sub _tree_construction_main ($) { Line 6082  sub _tree_construction_main ($) {
6082          }          }
6083    
6084          ## "after body" insertion mode          ## "after body" insertion mode
6085          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6086    
6087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6088            !!!ack-later;
6089          ## reprocess          ## reprocess
6090          redo B;          next B;
6091        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6092          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6093            !!!cp ('t305');            !!!cp ('t305');
6094            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6095                        
6096            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6097            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5155  sub _tree_construction_main ($) { Line 6103  sub _tree_construction_main ($) {
6103          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6104            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6105              !!!cp ('t307');              !!!cp ('t307');
6106              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6107              ## Ignore the token              ## Ignore the token
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            } else {            } else {
6111              !!!cp ('t308');              !!!cp ('t308');
6112              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6113              !!!next-token;              !!!next-token;
6114              redo B;              next B;
6115            }            }
6116          } else {          } else {
6117            !!!cp ('t309');            !!!cp ('t309');
6118            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6119    
6120            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6121            ## reprocess            ## reprocess
6122            redo B;            next B;
6123          }          }
6124          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6125            !!!cp ('t309.2');
6126            ## Stop parsing
6127            last B;
6128        } else {        } else {
6129          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6130        }        }
# Line 5184  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136            unless (length $token->{data}) {            unless (length $token->{data}) {
6137              !!!cp ('t310');              !!!cp ('t310');
6138              !!!next-token;              !!!next-token;
6139              redo B;              next B;
6140            }            }
6141          }          }
6142                    
6143          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6144            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6145              !!!cp ('t311');              !!!cp ('t311');
6146              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6147            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6148              !!!cp ('t312');              !!!cp ('t312');
6149              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6150            } else { # "after html frameset"            } else { # "after html frameset"
6151              !!!cp ('t313');              !!!cp ('t313');
6152              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6153    
6154              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6155              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6156              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6157            }            }
6158                        
6159            ## Ignore the token.            ## Ignore the token.
# Line 5212  sub _tree_construction_main ($) { Line 6164  sub _tree_construction_main ($) {
6164              !!!cp ('t315');              !!!cp ('t315');
6165              !!!next-token;              !!!next-token;
6166            }            }
6167            redo B;            next B;
6168          }          }
6169                    
6170          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6171        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6172          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6173            !!!cp ('t316');            !!!cp ('t316');
6174            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6175    
6176            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6177            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5230  sub _tree_construction_main ($) { Line 6182  sub _tree_construction_main ($) {
6182          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6183              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6184            !!!cp ('t318');            !!!cp ('t318');
6185            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6186              !!!nack ('t318.1');
6187            !!!next-token;            !!!next-token;
6188            redo B;            next B;
6189          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6190                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6191            !!!cp ('t319');            !!!cp ('t319');
6192            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6194              !!!ack ('t319.1');
6195            !!!next-token;            !!!next-token;
6196            redo B;            next B;
6197          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6198            !!!cp ('t320');            !!!cp ('t320');
6199            ## NOTE: As if in body.            ## NOTE: As if in body.
6200            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6201            redo B;            next B;
6202          } else {          } else {
6203            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6204              !!!cp ('t321');              !!!cp ('t321');
6205              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6206            } else {            } else {
6207              !!!cp ('t322');              !!!cp ('t322');
6208              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6209            }            }
6210            ## Ignore the token            ## Ignore the token
6211              !!!nack ('t322.1');
6212            !!!next-token;            !!!next-token;
6213            redo B;            next B;
6214          }          }
6215        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6216          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6217            !!!cp ('t323');            !!!cp ('t323');
6218            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6219    
6220            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6221            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5270  sub _tree_construction_main ($) { Line 6225  sub _tree_construction_main ($) {
6225    
6226          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6227              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6228            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6229                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6230              !!!cp ('t325');              !!!cp ('t325');
6231              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6232              ## Ignore the token              ## Ignore the token
6233              !!!next-token;              !!!next-token;
6234            } else {            } else {
# Line 5283  sub _tree_construction_main ($) { Line 6238  sub _tree_construction_main ($) {
6238            }            }
6239    
6240            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6241                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6242              !!!cp ('t327');              !!!cp ('t327');
6243              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6244            } else {            } else {
6245              !!!cp ('t328');              !!!cp ('t328');
6246            }            }
6247            redo B;            next B;
6248          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6249                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6250            !!!cp ('t329');            !!!cp ('t329');
6251            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6252            !!!next-token;            !!!next-token;
6253            redo B;            next B;
6254          } else {          } else {
6255            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6256              !!!cp ('t330');              !!!cp ('t330');
6257              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6258            } else {            } else {
6259              !!!cp ('t331');              !!!cp ('t331');
6260              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6261            }            }
6262            ## Ignore the token            ## Ignore the token
6263            !!!next-token;            !!!next-token;
6264            redo B;            next B;
6265          }          }
6266          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6267            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6268                    @{$self->{open_elements}} == 1) { # redundant, maybe
6269              !!!cp ('t331.1');
6270              !!!parse-error (type => 'in body:#eof', token => $token);
6271            } else {
6272              !!!cp ('t331.2');
6273            }
6274            
6275            ## Stop parsing
6276            last B;
6277        } else {        } else {
6278          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6279        }        }
# Line 5322  sub _tree_construction_main ($) { Line 6288  sub _tree_construction_main ($) {
6288        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6289          !!!cp ('t332');          !!!cp ('t332');
6290          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6291          $script_start_tag->($insert);          $script_start_tag->();
6292          redo B;          next B;
6293        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6294          !!!cp ('t333');          !!!cp ('t333');
6295          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6296          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6297          redo B;          next B;
6298        } elsif ({        } elsif ({
6299                  base => 1, link => 1,                  base => 1, link => 1,
6300                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6301          !!!cp ('t334');          !!!cp ('t334');
6302          ## 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
6303          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6304          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6305            !!!ack ('t334.1');
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6309          ## 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
6310          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6311          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.
6312    
6313          unless ($self->{confident}) {          unless ($self->{confident}) {
6314            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6315              !!!cp ('t335');              !!!cp ('t335');
6316                ## NOTE: Whether the encoding is supported or not is handled
6317                ## in the {change_encoding} callback.
6318              $self->{change_encoding}              $self->{change_encoding}
6319                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6320                            
6321              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6322                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6323                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6324                                           ->{has_reference});                                           ->{has_reference});
6325            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6326              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6327                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6328                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6329                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6330                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6331                !!!cp ('t336');                !!!cp ('t336');
6332                  ## NOTE: Whether the encoding is supported or not is handled
6333                  ## in the {change_encoding} callback.
6334                $self->{change_encoding}                $self->{change_encoding}
6335                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6336                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6337                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6338                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5386  sub _tree_construction_main ($) { Line 6356  sub _tree_construction_main ($) {
6356            }            }
6357          }          }
6358    
6359            !!!ack ('t338.1');
6360          !!!next-token;          !!!next-token;
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6363          !!!cp ('t341');          !!!cp ('t341');
6364          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6365          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6366          redo B;          next B;
6367        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6368          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6369                                
6370          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6371              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6372            !!!cp ('t342');            !!!cp ('t342');
6373            ## Ignore the token            ## Ignore the token
6374          } else {          } else {
# Line 5411  sub _tree_construction_main ($) { Line 6382  sub _tree_construction_main ($) {
6382              }              }
6383            }            }
6384          }          }
6385            !!!nack ('t343.1');
6386          !!!next-token;          !!!next-token;
6387          redo B;          next B;
6388        } elsif ({        } elsif ({
6389                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6390                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6391                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6392                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6393                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6394                    form => 1,
6395                    table => 1,
6396                    hr => 1,
6397                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6398            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6399              !!!cp ('t350');
6400              !!!parse-error (type => 'in form:form', token => $token);
6401              ## Ignore the token
6402              !!!nack ('t350.1');
6403              !!!next-token;
6404              next B;
6405            }
6406    
6407          ## has a p element in scope          ## has a p element in scope
6408          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6409            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6410              !!!cp ('t344');              !!!cp ('t344');
6411              !!!back-token;              !!!back-token; # <form>
6412              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6413              redo B;                        line => $token->{line}, column => $token->{column}};
6414            } elsif ({              next B;
6415                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6416              !!!cp ('t345');              !!!cp ('t345');
6417              last INSCOPE;              last INSCOPE;
6418            }            }
6419          } # INSCOPE          } # INSCOPE
6420                        
6421          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6422          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6423              !!!nack ('t346.1');
6424            !!!next-token;            !!!next-token;
6425            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6426              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5450  sub _tree_construction_main ($) { Line 6433  sub _tree_construction_main ($) {
6433            } else {            } else {
6434              !!!cp ('t348');              !!!cp ('t348');
6435            }            }
6436          } else {          } elsif ($token->{tag_name} eq 'form') {
6437            !!!cp ('t347');            !!!cp ('t347.1');
6438              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6439    
6440              !!!nack ('t347.2');
6441            !!!next-token;            !!!next-token;
6442          }          } elsif ($token->{tag_name} eq 'table') {
6443          redo B;            !!!cp ('t382');
6444        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6445          if (defined $self->{form_element}) {            
6446            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6447            !!!parse-error (type => 'in form:form');  
6448            ## Ignore the token            !!!nack ('t382.1');
6449              !!!next-token;
6450            } elsif ($token->{tag_name} eq 'hr') {
6451              !!!cp ('t386');
6452              pop @{$self->{open_elements}};
6453            
6454              !!!nack ('t386.1');
6455            !!!next-token;            !!!next-token;
           redo B;  
6456          } else {          } else {
6457            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6458            !!!next-token;            !!!next-token;
           redo B;  
6459          }          }
6460        } elsif ($token->{tag_name} eq 'li') {          next B;
6461          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6462          ## has a p element in scope          ## has a p element in scope
6463          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6464            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6465              !!!cp ('t353');              !!!cp ('t353');
6466              !!!back-token;              !!!back-token; # <x>
6467              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6468              redo B;                        line => $token->{line}, column => $token->{column}};
6469            } elsif ({              next B;
6470                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6471              !!!cp ('t354');              !!!cp ('t354');
6472              last INSCOPE;              last INSCOPE;
6473            }            }
# Line 5504  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476          ## Step 1          ## Step 1
6477          my $i = -1;          my $i = -1;
6478          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6479            my $li_or_dtdd = {li => {li => 1},
6480                              dt => {dt => 1, dd => 1},
6481                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6482          LI: {          LI: {
6483            ## Step 2            ## Step 2
6484            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6485              if ($i != -1) {              if ($i != -1) {
6486                !!!cp ('t355');                !!!cp ('t355');
6487                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6488                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6489                                      ->manakai_local_name,
6490                                  token => $token);
6491              } else {              } else {
6492                !!!cp ('t356');                !!!cp ('t356');
6493              }              }
# Line 5521  sub _tree_construction_main ($) { Line 6498  sub _tree_construction_main ($) {
6498            }            }
6499                        
6500            ## Step 3            ## Step 3
6501            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6502                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6503                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6504                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6505                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6506                  not ($node->[1] & DIV_EL)) {
6507              !!!cp ('t358');              !!!cp ('t358');
6508              last LI;              last LI;
6509            }            }
# Line 5537  sub _tree_construction_main ($) { Line 6515  sub _tree_construction_main ($) {
6515            redo LI;            redo LI;
6516          } # LI          } # LI
6517                        
6518          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6519            !!!nack ('t359.1');
6520          !!!next-token;          !!!next-token;
6521          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6522        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6523          ## has a p element in scope          ## has a p element in scope
6524          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6525            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6526              !!!cp ('t367');              !!!cp ('t367');
6527              !!!back-token;              !!!back-token; # <plaintext>
6528              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6529              redo B;                        line => $token->{line}, column => $token->{column}};
6530            } elsif ({              next B;
6531                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6532              !!!cp ('t368');              !!!cp ('t368');
6533              last INSCOPE;              last INSCOPE;
6534            }            }
6535          } # INSCOPE          } # INSCOPE
6536                        
6537          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6538                        
6539          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6540                        
6541            !!!nack ('t368.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6545          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6546            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6547            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6548              !!!cp ('t371');              !!!cp ('t371');
6549              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6550                            
6551              !!!back-token;              !!!back-token; # <a>
6552              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6553              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6554                $formatting_end_tag->($token);
6555                            
6556              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6557                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5653  sub _tree_construction_main ($) { Line 6576  sub _tree_construction_main ($) {
6576                        
6577          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6578    
6579          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6580          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6581    
6582            !!!nack ('t374.1');
6583          !!!next-token;          !!!next-token;
6584          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6585        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6586          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6587    
6588          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6589          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6590            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6591            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6592              !!!cp ('t376');              !!!cp ('t376');
6593              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6594              !!!back-token;              !!!back-token; # <nobr>
6595              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6596              redo B;                        line => $token->{line}, column => $token->{column}};
6597            } elsif ({              next B;
6598                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6599              !!!cp ('t377');              !!!cp ('t377');
6600              last INSCOPE;              last INSCOPE;
6601            }            }
6602          } # INSCOPE          } # INSCOPE
6603                    
6604          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6605          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6606                    
6607            !!!nack ('t377.1');
6608          !!!next-token;          !!!next-token;
6609          redo B;          next B;
6610        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6611          ## has a button element in scope          ## has a button element in scope
6612          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6613            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6614            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6615              !!!cp ('t378');              !!!cp ('t378');
6616              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6617              !!!back-token;              !!!back-token; # <button>
6618              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6619              redo B;                        line => $token->{line}, column => $token->{column}};
6620            } elsif ({              next B;
6621                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6622              !!!cp ('t379');              !!!cp ('t379');
6623              last INSCOPE;              last INSCOPE;
6624            }            }
# Line 5718  sub _tree_construction_main ($) { Line 6626  sub _tree_construction_main ($) {
6626                        
6627          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6628                        
6629          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6630    
6631          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6632    
6633          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6634    
6635            !!!nack ('t379.1');
6636          !!!next-token;          !!!next-token;
6637          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6638        } elsif ({        } elsif ({
6639                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6640                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6641                  image => 1,                  noembed => 1,
6642                    noframes => 1,
6643                    noscript => 0, ## TODO: 1 if scripting is enabled
6644                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6645          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6646            !!!cp ('t384');            !!!cp ('t381');
6647            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6648          } else {          } else {
6649            !!!cp ('t385');            !!!cp ('t399');
6650          }          }
6651            ## NOTE: There is an "as if in body" code clone.
6652          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6653          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6654        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6655          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6656                    
6657          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6658            !!!cp ('t389');            !!!cp ('t389');
6659            ## Ignore the token            ## Ignore the token
6660              !!!nack ('t389'); ## NOTE: Not acknowledged.
6661            !!!next-token;            !!!next-token;
6662            redo B;            next B;
6663          } else {          } else {
6664            my $at = $token->{attributes};            my $at = $token->{attributes};
6665            my $form_attrs;            my $form_attrs;
# Line 5836  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670            delete $at->{prompt};            delete $at->{prompt};
6671            my @tokens = (            my @tokens = (
6672                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6673                           attributes => $form_attrs},                           attributes => $form_attrs,
6674                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6675                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6676                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6677                            {type => START_TAG_TOKEN, tag_name => 'p',
6678                             line => $token->{line}, column => $token->{column}},
6679                            {type => START_TAG_TOKEN, tag_name => 'label',
6680                             line => $token->{line}, column => $token->{column}},
6681                         );                         );
6682            if ($prompt_attr) {            if ($prompt_attr) {
6683              !!!cp ('t390');              !!!cp ('t390');
6684              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6685                               #line => $token->{line}, column => $token->{column},
6686                              };
6687            } else {            } else {
6688              !!!cp ('t391');              !!!cp ('t391');
6689              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6690                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6691                               #line => $token->{line}, column => $token->{column},
6692                              }; # SHOULD
6693              ## TODO: make this configurable              ## TODO: make this configurable
6694            }            }
6695            push @tokens,            push @tokens,
6696                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6697                             line => $token->{line}, column => $token->{column}},
6698                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6699                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6700                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6701                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6702                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6703            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6704                             line => $token->{line}, column => $token->{column}},
6705                            {type => END_TAG_TOKEN, tag_name => 'form',
6706                             line => $token->{line}, column => $token->{column}};
6707              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6708            !!!back-token (@tokens);            !!!back-token (@tokens);
6709            redo B;            !!!next-token;
6710              next B;
6711          }          }
6712        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6713          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6714          my $el;          my $el;
6715          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6716                    
6717          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6718          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5873  sub _tree_construction_main ($) { Line 6721  sub _tree_construction_main ($) {
6721          $insert->($el);          $insert->($el);
6722                    
6723          my $text = '';          my $text = '';
6724            !!!nack ('t392.1');
6725          !!!next-token;          !!!next-token;
6726          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6727            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5903  sub _tree_construction_main ($) { Line 6752  sub _tree_construction_main ($) {
6752            ## Ignore the token            ## Ignore the token
6753          } else {          } else {
6754            !!!cp ('t398');            !!!cp ('t398');
6755            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6756          }          }
6757          !!!next-token;          !!!next-token;
6758          redo B;          next B;
6759        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6760                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6761          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6762    
6763          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6764    
6765            ## "adjust foreign attributes" - done in insert-element-f
6766                    
6767          $self->{insertion_mode} = IN_SELECT_IM;          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6768            
6769            if ($self->{self_closing}) {
6770              pop @{$self->{open_elements}};
6771              !!!ack ('t398.1');
6772            } else {
6773              !!!cp ('t398.2');
6774              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6775              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6776              ## mode, "in body" (not "in foreign content") secondary insertion
6777              ## mode, maybe.
6778            }
6779    
6780          !!!next-token;          !!!next-token;
6781          redo B;          next B;
6782        } elsif ({        } elsif ({
6783                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6784                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5935  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6787                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6788          !!!cp ('t401');          !!!cp ('t401');
6789          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6790          ## Ignore the token          ## Ignore the token
6791            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794                    
6795          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6796        } else {        } else {
6797          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6798              !!!cp ('t384');
6799              !!!parse-error (type => 'image', token => $token);
6800              $token->{tag_name} = 'img';
6801            } else {
6802              !!!cp ('t385');
6803            }
6804    
6805            ## NOTE: There is an "as if <br>" code clone.
6806          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6807                    
6808          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6809    
6810            if ({
6811                 applet => 1, marquee => 1, object => 1,
6812                }->{$token->{tag_name}}) {
6813              !!!cp ('t380');
6814              push @$active_formatting_elements, ['#marker', ''];
6815              !!!nack ('t380.1');
6816            } elsif ({
6817                      b => 1, big => 1, em => 1, font => 1, i => 1,
6818                      s => 1, small => 1, strile => 1,
6819                      strong => 1, tt => 1, u => 1,
6820                     }->{$token->{tag_name}}) {
6821              !!!cp ('t375');
6822              push @$active_formatting_elements, $self->{open_elements}->[-1];
6823              !!!nack ('t375.1');
6824            } elsif ($token->{tag_name} eq 'input') {
6825              !!!cp ('t388');
6826              ## TODO: associate with $self->{form_element} if defined
6827              pop @{$self->{open_elements}};
6828              !!!ack ('t388.2');
6829            } elsif ({
6830                      area => 1, basefont => 1, bgsound => 1, br => 1,
6831                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6832                      #image => 1,
6833                     }->{$token->{tag_name}}) {
6834              !!!cp ('t388.1');
6835              pop @{$self->{open_elements}};
6836              !!!ack ('t388.3');
6837            } elsif ($token->{tag_name} eq 'select') {
6838              ## TODO: associate with $self->{form_element} if defined
6839            
6840              if ($self->{insertion_mode} & TABLE_IMS or
6841                  $self->{insertion_mode} & BODY_TABLE_IMS or
6842                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6843                !!!cp ('t400.1');
6844                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6845              } else {
6846                !!!cp ('t400.2');
6847                $self->{insertion_mode} = IN_SELECT_IM;
6848              }
6849              !!!nack ('t400.3');
6850            } else {
6851              !!!nack ('t402');
6852            }
6853                    
6854          !!!next-token;          !!!next-token;
6855          redo B;          next B;
6856        }        }
6857      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6858        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6859          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6860              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6861            for (@{$self->{open_elements}}) {          INSCOPE: {
6862              unless ({            for (reverse @{$self->{open_elements}}) {
6863                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6864                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6865                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6866                      }->{$_->[1]}) {                last INSCOPE;
6867                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6868                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6869              } else {                last;
               !!!cp ('t404');  
6870              }              }
6871            }            }
6872    
6873            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6874            !!!next-token;                            value => $token->{tag_name}, token => $token);
6875            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6876            !!!next-token;            !!!next-token;
6877            redo B;            next B;
6878            } # INSCOPE
6879    
6880            for (@{$self->{open_elements}}) {
6881              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6882                !!!cp ('t403');
6883                !!!parse-error (type => 'not closed',
6884                                value => $_->[0]->manakai_local_name,
6885                                token => $token);
6886                last;
6887              } else {
6888                !!!cp ('t404');
6889              }
6890          }          }
6891    
6892            $self->{insertion_mode} = AFTER_BODY_IM;
6893            !!!next-token;
6894            next B;
6895        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6896          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
6897            ## up-to-date, though it has same effect as speced.
6898            if (@{$self->{open_elements}} > 1 and
6899                $self->{open_elements}->[1]->[1] & BODY_EL) {
6900            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6901            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6902              !!!cp ('t406');              !!!cp ('t406');
6903              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6904                                value => $self->{open_elements}->[1]->[0]
6905                                    ->manakai_local_name,
6906                                token => $token);
6907            } else {            } else {
6908              !!!cp ('t407');              !!!cp ('t407');
6909            }            }
6910            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6911            ## reprocess            ## reprocess
6912            redo B;            next B;
6913          } else {          } else {
6914            !!!cp ('t408');            !!!cp ('t408');
6915            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6916            ## Ignore the token            ## Ignore the token
6917            !!!next-token;            !!!next-token;
6918            redo B;            next B;
6919          }          }
6920        } elsif ({        } elsif ({
6921                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6922                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6923                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6924                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6925                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6926                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6927          ## has an element in scope          ## has an element in scope
6928          my $i;          my $i;
6929          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6930            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6931            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6932              !!!cp ('t410');              !!!cp ('t410');
6933              $i = $_;              $i = $_;
6934              last INSCOPE;              last INSCOPE;
6935            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6936              !!!cp ('t411');              !!!cp ('t411');
6937              last INSCOPE;              last INSCOPE;
6938            }            }
# Line 6022  sub _tree_construction_main ($) { Line 6940  sub _tree_construction_main ($) {
6940    
6941          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6942            !!!cp ('t413');            !!!cp ('t413');
6943            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6944          } else {          } else {
6945            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6946            while ({            while ({
# Line 6030  sub _tree_construction_main ($) { Line 6948  sub _tree_construction_main ($) {
6948                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6949                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6950                    p => 1,                    p => 1,
6951                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6952              !!!cp ('t409');              !!!cp ('t409');
6953              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6954            }            }
6955    
6956            ## Step 2.            ## Step 2.
6957            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6958                      ne $token->{tag_name}) {
6959              !!!cp ('t412');              !!!cp ('t412');
6960              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6961                                value => $self->{open_elements}->[-1]->[0]
6962                                    ->manakai_local_name,
6963                                token => $token);
6964            } else {            } else {
6965              !!!cp ('t414');              !!!cp ('t414');
6966            }            }
# Line 6049  sub _tree_construction_main ($) { Line 6971  sub _tree_construction_main ($) {
6971            ## Step 4.            ## Step 4.
6972            $clear_up_to_marker->()            $clear_up_to_marker->()
6973                if {                if {
6974                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6975                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6976          }          }
6977          !!!next-token;          !!!next-token;
6978          redo B;          next B;
6979        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6980          undef $self->{form_element};          undef $self->{form_element};
6981    
# Line 6061  sub _tree_construction_main ($) { Line 6983  sub _tree_construction_main ($) {
6983          my $i;          my $i;
6984          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6985            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6986            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6987              !!!cp ('t418');              !!!cp ('t418');
6988              $i = $_;              $i = $_;
6989              last INSCOPE;              last INSCOPE;
6990            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6991              !!!cp ('t419');              !!!cp ('t419');
6992              last INSCOPE;              last INSCOPE;
6993            }            }
# Line 6076  sub _tree_construction_main ($) { Line 6995  sub _tree_construction_main ($) {
6995    
6996          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6997            !!!cp ('t421');            !!!cp ('t421');
6998            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6999          } else {          } else {
7000            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7001            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7002              !!!cp ('t417');              !!!cp ('t417');
7003              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7004            }            }
7005                        
7006            ## Step 2.            ## Step 2.
7007            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7008                      ne $token->{tag_name}) {
7009              !!!cp ('t417.1');              !!!cp ('t417.1');
7010              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7011                                value => $self->{open_elements}->[-1]->[0]
7012                                    ->manakai_local_name,
7013                                token => $token);
7014            } else {            } else {
7015              !!!cp ('t420');              !!!cp ('t420');
7016            }              }  
# Line 6099  sub _tree_construction_main ($) { Line 7020  sub _tree_construction_main ($) {
7020          }          }
7021    
7022          !!!next-token;          !!!next-token;
7023          redo B;          next B;
7024        } elsif ({        } elsif ({
7025                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7026                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6107  sub _tree_construction_main ($) { Line 7028  sub _tree_construction_main ($) {
7028          my $i;          my $i;
7029          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7030            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7031            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7032              !!!cp ('t423');              !!!cp ('t423');
7033              $i = $_;              $i = $_;
7034              last INSCOPE;              last INSCOPE;
7035            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7036              !!!cp ('t424');              !!!cp ('t424');
7037              last INSCOPE;              last INSCOPE;
7038            }            }
# Line 6124  sub _tree_construction_main ($) { Line 7040  sub _tree_construction_main ($) {
7040    
7041          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7042            !!!cp ('t425.1');            !!!cp ('t425.1');
7043            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044          } else {          } else {
7045            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7046            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7047              !!!cp ('t422');              !!!cp ('t422');
7048              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7049            }            }
7050                        
7051            ## Step 2.            ## Step 2.
7052            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7053                      ne $token->{tag_name}) {
7054              !!!cp ('t425');              !!!cp ('t425');
7055              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7056            } else {            } else {
7057              !!!cp ('t426');              !!!cp ('t426');
7058            }            }
# Line 6147  sub _tree_construction_main ($) { Line 7062  sub _tree_construction_main ($) {
7062          }          }
7063                    
7064          !!!next-token;          !!!next-token;
7065          redo B;          next B;
7066        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7067          ## has an element in scope          ## has an element in scope
7068          my $i;          my $i;
7069          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7070            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7071            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7072              !!!cp ('t410.1');              !!!cp ('t410.1');
7073              $i = $_;              $i = $_;
7074              last INSCOPE;              last INSCOPE;
7075            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7076              !!!cp ('t411.1');              !!!cp ('t411.1');
7077              last INSCOPE;              last INSCOPE;
7078            }            }
7079          } # INSCOPE          } # INSCOPE
7080    
7081          if (defined $i) {          if (defined $i) {
7082            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7083                      ne $token->{tag_name}) {
7084              !!!cp ('t412.1');              !!!cp ('t412.1');
7085              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7086                                value => $self->{open_elements}->[-1]->[0]
7087                                    ->manakai_local_name,
7088                                token => $token);
7089            } else {            } else {
7090              !!!cp ('t414.1');              !!!cp ('t414.1');
7091            }            }
# Line 6177  sub _tree_construction_main ($) { Line 7093  sub _tree_construction_main ($) {
7093            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7094          } else {          } else {
7095            !!!cp ('t413.1');            !!!cp ('t413.1');
7096            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7097    
7098            !!!cp ('t415.1');            !!!cp ('t415.1');
7099            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7100            my $el;            my $el;
7101            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7102            $insert->($el);            $insert->($el);
7103            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7104          }          }
7105    
7106          !!!next-token;          !!!next-token;
7107          redo B;          next B;
7108        } elsif ({        } elsif ({
7109                  a => 1,                  a => 1,
7110                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6196  sub _tree_construction_main ($) { Line 7112  sub _tree_construction_main ($) {
7112                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7113                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7114          !!!cp ('t427');          !!!cp ('t427');
7115          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7116          redo B;          next B;
7117        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7118          !!!cp ('t428');          !!!cp ('t428');
7119          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7120    
7121          ## As if <br>          ## As if <br>
7122          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7123                    
7124          my $el;          my $el;
7125          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7126          $insert->($el);          $insert->($el);
7127                    
7128          ## Ignore the token.          ## Ignore the token.
7129          !!!next-token;          !!!next-token;
7130          redo B;          next B;
7131        } elsif ({        } elsif ({
7132                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7133                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6225  sub _tree_construction_main ($) { Line 7141  sub _tree_construction_main ($) {
7141                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7142                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7143          !!!cp ('t429');          !!!cp ('t429');
7144          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7145          ## Ignore the token          ## Ignore the token
7146          !!!next-token;          !!!next-token;
7147          redo B;          next B;
7148                    
7149          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7150                    
# Line 6239  sub _tree_construction_main ($) { Line 7155  sub _tree_construction_main ($) {
7155    
7156          ## Step 2          ## Step 2
7157          S2: {          S2: {
7158            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7159              ## Step 1              ## Step 1
7160              ## generate implied end tags              ## generate implied end tags
7161              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7162                !!!cp ('t430');                !!!cp ('t430');
7163                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7164                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7165              }              }
7166                    
7167              ## Step 2              ## Step 2
7168              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7169                        ne $token->{tag_name}) {
7170                !!!cp ('t431');                !!!cp ('t431');
7171                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7172                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7173                                  value => $self->{open_elements}->[-1]->[0]
7174                                      ->manakai_local_name,
7175                                  token => $token);
7176              } else {              } else {
7177                !!!cp ('t432');                !!!cp ('t432');
7178              }              }
# Line 6266  sub _tree_construction_main ($) { Line 7184  sub _tree_construction_main ($) {
7184              last S2;              last S2;
7185            } else {            } else {
7186              ## Step 3              ## Step 3
7187              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7188                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7189                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7190                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7191                !!!cp ('t433');                !!!cp ('t433');
7192                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7193                ## Ignore the token                ## Ignore the token
7194                !!!next-token;                !!!next-token;
7195                last S2;                last S2;
# Line 6287  sub _tree_construction_main ($) { Line 7205  sub _tree_construction_main ($) {
7205            ## Step 5;            ## Step 5;
7206            redo S2;            redo S2;
7207          } # S2          } # S2
7208          redo B;          next B;
7209        }        }
7210      }      }
7211      redo B;      next B;
7212      } continue { # B
7213        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7214          ## NOTE: The code below is executed in cases where it does not have
7215          ## to be, but it it is harmless even in those cases.
7216          ## has an element in scope
7217          INSCOPE: {
7218            for (reverse 0..$#{$self->{open_elements}}) {
7219              my $node = $self->{open_elements}->[$_];
7220              if ($node->[1] & FOREIGN_EL) {
7221                last INSCOPE;
7222              } elsif ($node->[1] & SCOPING_EL) {
7223                last;
7224              }
7225            }
7226            
7227            ## NOTE: No foreign element in scope.
7228            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7229          } # INSCOPE
7230        }
7231    } # B    } # B
7232    
7233    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6336  sub set_inner_html ($$$) { Line 7273  sub set_inner_html ($$$) {
7273    
7274      ## Step 8 # MUST      ## Step 8 # MUST
7275      my $i = 0;      my $i = 0;
7276      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7277      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7278      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7279        my $self = shift;        my $self = shift;
7280    
# Line 6346  sub set_inner_html ($$$) { Line 7283  sub set_inner_html ($$$) {
7283    
7284        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7285        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7286        $column++;  
7287          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7288          $p->{column}++;
7289    
7290        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7291          $line++;          $p->{line}++;
7292          $column = 0;          $p->{column} = 0;
7293          !!!cp ('i1');          !!!cp ('i1');
7294        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7295          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7296          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7297          $line++;          $p->{line}++;
7298          $column = 0;          $p->{column} = 0;
7299          !!!cp ('i2');          !!!cp ('i2');
7300        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7301          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6365  sub set_inner_html ($$$) { Line 7304  sub set_inner_html ($$$) {
7304          !!!cp ('i4');          !!!cp ('i4');
7305          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7306          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7307          } elsif ($self->{next_char} <= 0x0008 or
7308                   (0x000E <= $self->{next_char} and
7309                    $self->{next_char} <= 0x001F) or
7310                   (0x007F <= $self->{next_char} and
7311                    $self->{next_char} <= 0x009F) or
7312                   (0xD800 <= $self->{next_char} and
7313                    $self->{next_char} <= 0xDFFF) or
7314                   (0xFDD0 <= $self->{next_char} and
7315                    $self->{next_char} <= 0xFDDF) or
7316                   {
7317                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7318                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7319                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7320                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7321                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7322                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7323                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7324                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7325                    0x10FFFE => 1, 0x10FFFF => 1,
7326                   }->{$self->{next_char}}) {
7327            !!!cp ('i4.1');
7328            !!!parse-error (type => 'control char', level => $self->{must_level});
7329    ## TODO: error type documentation
7330        }        }
7331      };      };
7332      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6372  sub set_inner_html ($$$) { Line 7334  sub set_inner_html ($$$) {
7334            
7335      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7336        my (%opt) = @_;        my (%opt) = @_;
7337        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7338          my $column = $opt{column};
7339          if (defined $opt{token} and defined $opt{token}->{line}) {
7340            $line = $opt{token}->{line};
7341            $column = $opt{token}->{column};
7342          }
7343          warn "Parse error ($opt{type}) at line $line column $column\n";
7344      };      };
7345      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7346        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7347      };      };
7348            
7349      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6399  sub set_inner_html ($$$) { Line 7367  sub set_inner_html ($$$) {
7367          unless defined $p->{content_model};          unless defined $p->{content_model};
7368          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7369    
7370      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7371          ## TODO: Foreign element OK?
7372    
7373      ## Step 3      ## Step 3
7374      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6409  sub set_inner_html ($$$) { Line 7378  sub set_inner_html ($$$) {
7378      $doc->append_child ($root);      $doc->append_child ($root);
7379    
7380      ## Step 5 # MUST      ## Step 5 # MUST
7381      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7382    
7383      undef $p->{head_element};      undef $p->{head_element};
7384    
# Line 6455  sub set_inner_html ($$$) { Line 7424  sub set_inner_html ($$$) {
7424      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7425    
7426      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7427    
7428        delete $p->{parse_error}; # delete loop
7429    } else {    } else {
7430      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";
7431    }    }

Legend:
Removed from v.1.99  
changed lines
  Added in v.1.140

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24