/[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.91 by wakaba, Sat Mar 8 03:04:09 2008 UTC revision 1.137 by wakaba, Sun May 18 03:46:27 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset_name = shift;
337    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    open my $byte_stream, '<', ref $_[0] ? $_[0] : \($_[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;  
   }  
338    
339    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
340      my $self = shift;      my (%opt) = @_;
341      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
342      ## TODO: if $charset is supported    };
343      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
344    
345      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
346      require Message::Charset::Info;
347      ## Step 1        my $charset;
348      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
349        $charset = 'utf-8';    my ($char_stream, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($char_stream, $e_status) = $charset->get_decode_handle
359              ($byte_stream, allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($char_stream) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          } else {
365            ## TODO: unsupported error
366          }
367      }      }
368    
369      ## Step 2      ## Step 2
370      if (defined $self->{input_encoding} and      my $byte_buffer = '';
371          $self->{input_encoding} eq $charset) {      for (1..1024) {
372          my $char = $byte_stream->getc;
373          last unless defined $char;
374          $byte_buffer .= $char;
375        } ## TODO: timeout
376    
377        ## Step 3
378        if ($byte_buffer =~ /^\xFE\xFF/) {
379          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
380          ($char_stream, $e_status) = $charset->get_decode_handle
381              ($byte_stream, allow_error_reporting => 1,
382               allow_fallback => 1, byte_buffer => \$byte_buffer);
383        $self->{confident} = 1;        $self->{confident} = 1;
384        return;        last SNIFFING;
385        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390          $self->{confident} = 1;
391          last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399      }      }
400    
401      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
402          ':'.$charset, level => 'w');      ## TODO: <meta charset>
403    
404      ## Step 3      ## Step 5
405      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
406    
407      ## Step 4      ## Step 6
408      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
409        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
410            ($byte_buffer);
411        if (defined $charset_name) {
412          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
413    
414          ## ISSUE: Unsupported encoding is not ignored according to the spec.
415          require Whatpm::Charset::DecodeHandle;
416          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
417              ($byte_stream);
418          ($char_stream, $e_status) = $charset->get_decode_handle
419              ($buffer, allow_error_reporting => 1,
420               allow_fallback => 1, byte_buffer => \$byte_buffer);
421          if ($char_stream) {
422            $buffer->{buffer} = $byte_buffer;
423            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
424                            value => $charset_name,
425                            level => $self->{info_level},
426                            line => 1, column => 1);
427            $self->{confident} = 0;
428            last SNIFFING;
429          }
430        }
431    
432        ## Step 7: default
433        ## TODO: Make this configurable.
434        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
435            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
436            ## detectable in the step 6.
437        require Whatpm::Charset::DecodeHandle;
438        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
439            ($byte_stream);
440        ($char_stream, $e_status)
441            = $charset->get_decode_handle ($buffer,
442                                           allow_error_reporting => 1,
443                                           allow_fallback => 1,
444                                           byte_buffer => \$byte_buffer);
445        $buffer->{buffer} = $byte_buffer;
446        !!!parse-error (type => 'sniffing:default', ## TODO: type name
447                        value => 'windows-1252',
448                        level => $self->{info_level},
449                        line => 1, column => 1);
450        $self->{confident} = 0;
451      } # SNIFFING
452    
453      $self->{input_encoding} = $charset->get_iana_name;
454      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
455        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
456                        value => $self->{input_encoding},
457                        level => $self->{unsupported_level},
458                        line => 1, column => 1);
459      } elsif (not ($e_status &
460                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
461        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      }
466    
467      $self->{change_encoding} = sub {
468        my $self = shift;
469        $charset_name = shift;
470        my $token = shift;
471    
472        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
473        ($char_stream, $e_status) = $charset->get_decode_handle
474            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
475             byte_buffer => \ $buffer->{buffer});
476        
477        if ($char_stream) { # if supported
478          ## "Change the encoding" algorithm:
479    
480          ## Step 1    
481          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
482            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
483            ($char_stream, $e_status) = $charset->get_decode_handle
484                ($byte_stream,
485                 byte_buffer => \ $buffer->{buffer});
486          }
487          $charset_name = $charset->get_iana_name;
488          
489          ## Step 2
490          if (defined $self->{input_encoding} and
491              $self->{input_encoding} eq $charset_name) {
492            !!!parse-error (type => 'charset label:matching', ## TODO: type
493                            value => $charset_name,
494                            level => $self->{info_level});
495            $self->{confident} = 1;
496            return;
497          }
498    
499          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
500              ':'.$charset_name, level => 'w', token => $token);
501          
502          ## Step 3
503          # if (can) {
504            ## change the encoding on the fly.
505            #$self->{confident} = 1;
506            #return;
507          # }
508          
509          ## Step 4
510          throw Whatpm::HTML::RestartParser ();
511        }
512    }; # $self->{change_encoding}    }; # $self->{change_encoding}
513    
514      my $char_onerror = sub {
515        my (undef, $type, %opt) = @_;
516        !!!parse-error (%opt, type => $type,
517                        line => $self->{line}, column => $self->{column} + 1);
518        if ($opt{octets}) {
519          ${$opt{octets}} = "\x{FFFD}"; # relacement character
520        }
521      };
522      $char_stream->onerror ($char_onerror);
523    
524    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
525    my $return;    my $return;
526    try {    try {
527      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
528    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
529      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
530      $s = \ (Encode::decode ($charset, $$bytes_s));      
531      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
532        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
533          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
534                          value => $self->{input_encoding},
535                          level => $self->{unsupported_level},
536                          line => 1, column => 1);
537        } elsif (not ($e_status &
538                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
539          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        }
544      $self->{confident} = 1;      $self->{confident} = 1;
545      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
546        $return = $self->parse_char_stream ($char_stream, @args);
547    };    };
548    return $return;    return $return;
549  } # parse_byte_string  } # parse_byte_string
# Line 162  sub parse_byte_string ($$$$;$) { Line 557  sub parse_byte_string ($$$$;$) {
557  ## 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
558  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
559    
560  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
561      my $self = shift;
562      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
563      return $self->parse_char_stream ($input, @_[1..$#_]);
564    } # parse_char_string
565    *parse_string = \&parse_char_string;
566    
567  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
568    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
569    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
570    $self->{document} = $_[1];    $self->{document} = $_[1];
571    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
572    
# Line 177  sub parse_string ($$$;$) { Line 577  sub parse_string ($$$;$) {
577        if defined $self->{input_encoding};        if defined $self->{input_encoding};
578    
579    my $i = 0;    my $i = 0;
580    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
581    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
582    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
583      my $self = shift;      my $self = shift;
584    
585      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
586      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
587    
588      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = $input->getc;
589      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
590      $column++;      $self->{next_char} = ord $char;
591    
592        ($self->{line_prev}, $self->{column_prev})
593            = ($self->{line}, $self->{column});
594        $self->{column}++;
595            
596      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
597        $line++;        !!!cp ('j1');
598        $column = 0;        $self->{line}++;
599          $self->{column} = 0;
600      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
601        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
602          my $next = $input->getc;
603          if ($next ne "\x0A") {
604            $input->ungetc ($next);
605          }
606        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
607        $line++;        $self->{line}++;
608        $column = 0;        $self->{column} = 0;
609      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
610          !!!cp ('j3');
611        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
612      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
613          !!!cp ('j4');
614        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
615        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
616        } elsif ($self->{next_char} <= 0x0008 or
617                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
618                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
619                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
620                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
621                 {
622                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
623                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
624                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
625                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
626                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
627                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
628                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
629                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
630                  0x10FFFE => 1, 0x10FFFF => 1,
631                 }->{$self->{next_char}}) {
632          !!!cp ('j5');
633          !!!parse-error (type => 'control char', level => $self->{must_level});
634    ## TODO: error type documentation
635      }      }
636    };    };
637    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 639  sub parse_string ($$$;$) {
639    
640    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
641      my (%opt) = @_;      my (%opt) = @_;
642      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
643        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
644        warn "Parse error ($opt{type}) at line $line column $column\n";
645    };    };
646    $self->{parse_error} = sub {    $self->{parse_error} = sub {
647      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
648    };    };
649    
650    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 652  sub parse_string ($$$;$) {
652    $self->_construct_tree;    $self->_construct_tree;
653    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
654    
655      delete $self->{parse_error}; # remove loop
656    
657    return $self->{document};    return $self->{document};
658  } # parse_string  } # parse_char_stream
659    
660  sub new ($) {  sub new ($) {
661    my $class = shift;    my $class = shift;
662    my $self = bless {}, $class;    my $self = bless {
663        must_level => 'm',
664        should_level => 's',
665        good_level => 'w',
666        warn_level => 'w',
667        info_level => 'i',
668        unsupported_level => 'u',
669      }, $class;
670    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
671      $self->{next_char} = -1;      $self->{next_char} = -1;
672    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 728  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
728  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
729  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
730  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
731    sub SELF_CLOSING_START_TAG_STATE () { 34 }
732    sub CDATA_BLOCK_STATE () { 35 }
733    
734  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
735  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 746  sub TABLE_IMS ()      { 0b1000000 }
746  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
747  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
748  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
749    sub SELECT_IMS ()     { 0b10000000000 }
750    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
751        ## NOTE: "in foreign content" insertion mode is special; it is combined
752        ## with the secondary insertion mode.  In this parser, they are stored
753        ## together in the bit-or'ed form.
754    
755  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
756    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 773  sub IN_TABLE_IM () { TABLE_IMS }
773  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
774  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
775  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
776  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
777    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
778  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
779    
780  ## 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 787  sub _initialize_tokenizer ($) {
787    undef $self->{current_attribute};    undef $self->{current_attribute};
788    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
789    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
790      delete $self->{self_closing};
791    $self->{char} = [];    $self->{char} = [];
792    # $self->{next_char}    # $self->{next_char}
793    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 808  sub _initialize_tokenizer ($) {
808  ##        ->{value}  ##        ->{value}
809  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
810  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
811    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
812    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
813    ##     while the token is pushed back to the stack.
814    
815    ## ISSUE: "When a DOCTYPE token is created, its
816    ## <i>self-closing flag</i> must be unset (its other state is that it
817    ## be set), and its attributes list must be empty.": Wrong subject?
818    
819  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
820    
# Line 384  sub _initialize_tokenizer ($) { Line 841  sub _initialize_tokenizer ($) {
841    
842  sub _get_next_token ($) {  sub _get_next_token ($) {
843    my $self = shift;    my $self = shift;
844    
845      if ($self->{self_closing}) {
846        !!!parse-error (type => 'nestc', token => $self->{current_token});
847        ## NOTE: The |self_closing| flag is only set by start tag token.
848        ## In addition, when a start tag token is emitted, it is always set to
849        ## |current_token|.
850        delete $self->{self_closing};
851      }
852    
853    if (@{$self->{token}}) {    if (@{$self->{token}}) {
854        $self->{self_closing} = $self->{token}->[0]->{self_closing};
855      return shift @{$self->{token}};      return shift @{$self->{token}};
856    }    }
857    
# Line 447  sub _get_next_token ($) { Line 914  sub _get_next_token ($) {
914          #          #
915        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
916          !!!cp (11);          !!!cp (11);
917          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
918                      line => $self->{line}, column => $self->{column}});
919          last A; ## TODO: ok?          last A; ## TODO: ok?
920        } else {        } else {
921          !!!cp (12);          !!!cp (12);
922        }        }
923        # Anything else        # Anything else
924        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
925                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
926                       line => $self->{line}, column => $self->{column},
927                      };
928        ## Stay in the data state        ## Stay in the data state
929        !!!next-input-character;        !!!next-input-character;
930    
# Line 463  sub _get_next_token ($) { Line 933  sub _get_next_token ($) {
933        redo A;        redo A;
934      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
935        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
936    
937          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
938                
939        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
940    
# Line 471  sub _get_next_token ($) { Line 943  sub _get_next_token ($) {
943    
944        unless (defined $token) {        unless (defined $token) {
945          !!!cp (13);          !!!cp (13);
946          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
947                      line => $l, column => $c,
948                     });
949        } else {        } else {
950          !!!cp (14);          !!!cp (14);
951          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964            ## reconsume            ## reconsume
965            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
966    
967            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
968                        line => $self->{line_prev},
969                        column => $self->{column_prev},
970                       });
971    
972            redo A;            redo A;
973          }          }
# Line 510  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987            !!!cp (19);            !!!cp (19);
988            $self->{current_token}            $self->{current_token}
989              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
990                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
991                   line => $self->{line_prev},
992                   column => $self->{column_prev}};
993            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
994            !!!next-input-character;            !!!next-input-character;
995            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 997  sub _get_next_token ($) {
997                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
998            !!!cp (20);            !!!cp (20);
999            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1000                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1001                                        line => $self->{line_prev},
1002                                        column => $self->{column_prev}};
1003            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1004            !!!next-input-character;            !!!next-input-character;
1005            redo A;            redo A;
1006          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1007            !!!cp (21);            !!!cp (21);
1008            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1009                              line => $self->{line_prev},
1010                              column => $self->{column_prev});
1011            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1012            !!!next-input-character;            !!!next-input-character;
1013    
1014            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1015                        line => $self->{line_prev},
1016                        column => $self->{column_prev},
1017                       });
1018    
1019            redo A;            redo A;
1020          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1021            !!!cp (22);            !!!cp (22);
1022            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1023                              line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1026              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1027                                        line => $self->{line_prev},
1028                                        column => $self->{column_prev},
1029                                       };
1030            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1031            redo A;            redo A;
1032          } else {          } else {
1033            !!!cp (23);            !!!cp (23);
1034            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1035                              line => $self->{line_prev},
1036                              column => $self->{column_prev});
1037            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1038            ## reconsume            ## reconsume
1039    
1040            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1041                        line => $self->{line_prev},
1042                        column => $self->{column_prev},
1043                       });
1044    
1045            redo A;            redo A;
1046          }          }
# Line 551  sub _get_next_token ($) { Line 1048  sub _get_next_token ($) {
1048          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1049        }        }
1050      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1051          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1052        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1053          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1054    
1055            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1056            my @next_char;            my @next_char;
1057            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 1068  sub _get_next_token ($) {
1068                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1069                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1070    
1071                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1072                            line => $l, column => $c,
1073                           });
1074        
1075                redo A;                redo A;
1076              }              }
# Line 588  sub _get_next_token ($) { Line 1089  sub _get_next_token ($) {
1089              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1090              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1091              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1092              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1093                          line => $l, column => $c,
1094                         });
1095              redo A;              redo A;
1096            } else {            } else {
1097              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104            !!!cp (28);            !!!cp (28);
1105            # next-input-character is already done            # next-input-character is already done
1106            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1107            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1108                        line => $l, column => $c,
1109                       });
1110            redo A;            redo A;
1111          }          }
1112        }        }
# Line 609  sub _get_next_token ($) { Line 1114  sub _get_next_token ($) {
1114        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1115            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1116          !!!cp (29);          !!!cp (29);
1117          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1118                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1119                   tag_name => chr ($self->{next_char} + 0x0020),
1120                   line => $l, column => $c};
1121          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1122          !!!next-input-character;          !!!next-input-character;
1123          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1125  sub _get_next_token ($) {
1125                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1126          !!!cp (30);          !!!cp (30);
1127          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1128                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1129                                      line => $l, column => $c};
1130          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1131          !!!next-input-character;          !!!next-input-character;
1132          redo A;          redo A;
1133        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1134          !!!cp (31);          !!!cp (31);
1135          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1136                            line => $self->{line_prev}, ## "<" in "</>"
1137                            column => $self->{column_prev} - 1);
1138          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1144  sub _get_next_token ($) {
1144          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1145          # reconsume          # reconsume
1146    
1147          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1148                      line => $l, column => $c,
1149                     });
1150    
1151          redo A;          redo A;
1152        } else {        } else {
1153          !!!cp (33);          !!!cp (33);
1154          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1155          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1156            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1157                                      line => $self->{line_prev}, # "<" of "</"
1158                                      column => $self->{column_prev} - 1,
1159                                     };
1160          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1161          redo A;          redo A;
1162        }        }
# Line 657  sub _get_next_token ($) { Line 1173  sub _get_next_token ($) {
1173        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1174          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1175            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1176            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1177          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1178            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1205          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1206            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1207            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1208          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1209            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1224  sub _get_next_token ($) {
1224    
1225          redo A;          redo A;
1226        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1227            !!!cp (42);
1228            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1229          !!!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  
1230          redo A;          redo A;
1231        } else {        } else {
1232          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1249  sub _get_next_token ($) {
1249        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1250          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1251            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1252            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1253          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1254            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1270  sub _get_next_token ($) {
1270        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1271                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1272          !!!cp (49);          !!!cp (49);
1273          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1274                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1275                   value => '',
1276                   line => $self->{line}, column => $self->{column}};
1277          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
1280        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1281            !!!cp (50);
1282            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1283          !!!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  
1284          redo A;          redo A;
1285        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1286          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1287          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1288            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1289            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1290          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1291            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1315  sub _get_next_token ($) {
1315          } else {          } else {
1316            !!!cp (56);            !!!cp (56);
1317          }          }
1318          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1319                                value => ''};              = {name => chr ($self->{next_char}),
1320                   value => '',
1321                   line => $self->{line}, column => $self->{column}};
1322          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1323          !!!next-input-character;          !!!next-input-character;
1324          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1328  sub _get_next_token ($) {
1328          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1329              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1330            !!!cp (57);            !!!cp (57);
1331            !!!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});
1332            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1333          } else {          } else {
1334            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1357  sub _get_next_token ($) {
1357          $before_leave->();          $before_leave->();
1358          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1359            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1360            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1361          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1362            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1381  sub _get_next_token ($) {
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (64);
1385          $before_leave->();          $before_leave->();
1386            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1387          !!!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  
1388          redo A;          redo A;
1389        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1390          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1391          $before_leave->();          $before_leave->();
1392          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1393            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1394            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1395          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1396            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1441  sub _get_next_token ($) {
1441        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1442          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1443            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1444            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1445          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1446            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1463  sub _get_next_token ($) {
1463        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1464                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1465          !!!cp (76);          !!!cp (76);
1466          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1467                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1468                   value => '',
1469                   line => $self->{line}, column => $self->{column}};
1470          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1471          !!!next-input-character;          !!!next-input-character;
1472          redo A;          redo A;
1473        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1474            !!!cp (77);
1475            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1476          !!!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  
1477          redo A;          redo A;
1478        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1479          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1480          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1481            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1482            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1483          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1484            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1500  sub _get_next_token ($) {
1500          redo A;          redo A;
1501        } else {        } else {
1502          !!!cp (82);          !!!cp (82);
1503          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1504                                value => ''};              = {name => chr ($self->{next_char}),
1505                   value => '',
1506                   line => $self->{line}, column => $self->{column}};
1507          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1508          !!!next-input-character;          !!!next-input-character;
1509          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1536  sub _get_next_token ($) {
1536        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1537          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1538            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1539            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1540          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1541            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1560          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1561            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1562            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1563          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1564            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1606  sub _get_next_token ($) {
1606          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1607          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1608            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1609            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1610          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1611            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1648  sub _get_next_token ($) {
1648          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1649          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1650            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1651            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1652          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1653            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1693  sub _get_next_token ($) {
1693        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1694          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1695            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1696            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1697          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1698            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1716  sub _get_next_token ($) {
1716          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1717          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1718            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1719            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1720          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1721            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1786  sub _get_next_token ($) {
1786        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1787          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1788            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1789            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1790          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1791            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1806  sub _get_next_token ($) {
1806    
1807          redo A;          redo A;
1808        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1809            !!!cp (122);
1810            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1811          !!!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  
1812          redo A;          redo A;
1813        } else {        } else {
1814          !!!cp (124);          !!!cp ('124.1');
1815          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1816          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1817          ## reconsume          ## reconsume
1818          redo A;          redo A;
1819        }        }
1820        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1821          if ($self->{next_char} == 0x003E) { # >
1822            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823              !!!cp ('124.2');
1824              !!!parse-error (type => 'nestc', token => $self->{current_token});
1825              ## TODO: Different type than slash in start tag
1826              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1827              if ($self->{current_token}->{attributes}) {
1828                !!!cp ('124.4');
1829                !!!parse-error (type => 'end tag attribute');
1830              } else {
1831                !!!cp ('124.5');
1832              }
1833              ## TODO: Test |<title></title/>|
1834            } else {
1835              !!!cp ('124.3');
1836              $self->{self_closing} = 1;
1837            }
1838    
1839            $self->{state} = DATA_STATE;
1840            !!!next-input-character;
1841    
1842            !!!emit ($self->{current_token}); # start tag or end tag
1843    
1844            redo A;
1845          } else {
1846            !!!cp ('124.4');
1847            !!!parse-error (type => 'nestc');
1848            ## TODO: This error type is wrong.
1849            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1850            ## Reconsume.
1851            redo A;
1852          }
1853      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1854        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1855                
1856        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1857          #my $token = {type => COMMENT_TOKEN, data => ''};
1858    
1859        BC: {        BC: {
1860          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1862  sub _get_next_token ($) {
1862            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1863            !!!next-input-character;            !!!next-input-character;
1864    
1865            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1866    
1867            redo A;            redo A;
1868          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1870  sub _get_next_token ($) {
1870            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1871            ## reconsume            ## reconsume
1872    
1873            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1874    
1875            redo A;            redo A;
1876          } else {          } else {
1877            !!!cp (126);            !!!cp (126);
1878            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1879            !!!next-input-character;            !!!next-input-character;
1880            redo BC;            redo BC;
1881          }          }
# Line 1408  sub _get_next_token ($) { Line 1885  sub _get_next_token ($) {
1885      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1886        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1887    
1888          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1889    
1890        my @next_char;        my @next_char;
1891        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1892                
# Line 1416  sub _get_next_token ($) { Line 1895  sub _get_next_token ($) {
1895          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1896          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1897            !!!cp (127);            !!!cp (127);
1898            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1899                                        line => $l, column => $c,
1900                                       };
1901            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1902            !!!next-input-character;            !!!next-input-character;
1903            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1933  sub _get_next_token ($) {
1933                      !!!cp (129);                      !!!cp (129);
1934                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1935                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1936                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1937                                                  quirks => 1,
1938                                                  line => $l, column => $c,
1939                                                 };
1940                      !!!next-input-character;                      !!!next-input-character;
1941                      redo A;                      redo A;
1942                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1957  sub _get_next_token ($) {
1957          } else {          } else {
1958            !!!cp (135);            !!!cp (135);
1959          }          }
1960          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1961                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1962                   $self->{next_char} == 0x005B) { # [
1963            !!!next-input-character;
1964            push @next_char, $self->{next_char};
1965            if ($self->{next_char} == 0x0043) { # C
1966              !!!next-input-character;
1967              push @next_char, $self->{next_char};
1968              if ($self->{next_char} == 0x0044) { # D
1969                !!!next-input-character;
1970                push @next_char, $self->{next_char};
1971                if ($self->{next_char} == 0x0041) { # A
1972                  !!!next-input-character;
1973                  push @next_char, $self->{next_char};
1974                  if ($self->{next_char} == 0x0054) { # T
1975                    !!!next-input-character;
1976                    push @next_char, $self->{next_char};
1977                    if ($self->{next_char} == 0x0041) { # A
1978                      !!!next-input-character;
1979                      push @next_char, $self->{next_char};
1980                      if ($self->{next_char} == 0x005B) { # [
1981                        !!!cp (135.1);
1982                        $self->{state} = CDATA_BLOCK_STATE;
1983                        !!!next-input-character;
1984                        redo A;
1985                      } else {
1986                        !!!cp (135.2);
1987                      }
1988                    } else {
1989                      !!!cp (135.3);
1990                    }
1991                  } else {
1992                    !!!cp (135.4);                
1993                  }
1994                } else {
1995                  !!!cp (135.5);
1996                }
1997              } else {
1998                !!!cp (135.6);
1999              }
2000            } else {
2001              !!!cp (135.7);
2002            }
2003        } else {        } else {
2004          !!!cp (136);          !!!cp (136);
2005        }        }
# Line 1480  sub _get_next_token ($) { Line 2008  sub _get_next_token ($) {
2008        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2009        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2010        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2011          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2012                                    line => $l, column => $c,
2013                                   };
2014        redo A;        redo A;
2015                
2016        ## 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 2134  sub _get_next_token ($) {
2134          redo A;          redo A;
2135        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2136          !!!cp (152);          !!!cp (152);
2137          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2138                            line => $self->{line_prev},
2139                            column => $self->{column_prev});
2140          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2141          ## Stay in the state          ## Stay in the state
2142          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2152  sub _get_next_token ($) {
2152          redo A;          redo A;
2153        } else {        } else {
2154          !!!cp (154);          !!!cp (154);
2155          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2156                            line => $self->{line_prev},
2157                            column => $self->{column_prev});
2158          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2159          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2160          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2193  sub _get_next_token ($) {
2193          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2194          !!!next-input-character;          !!!next-input-character;
2195    
2196          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2197    
2198          redo A;          redo A;
2199        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2202  sub _get_next_token ($) {
2202          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2203          ## reconsume          ## reconsume
2204    
2205          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2206    
2207          redo A;          redo A;
2208        } else {        } else {
2209          !!!cp (160);          !!!cp (160);
2210          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2211              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2212  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2213          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2214          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2724  sub _get_next_token ($) {
2724          !!!next-input-character;          !!!next-input-character;
2725          redo A;          redo A;
2726        }        }
2727        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2728          my $s = '';
2729          
2730          my ($l, $c) = ($self->{line}, $self->{column});
2731    
2732          CS: while ($self->{next_char} != -1) {
2733            if ($self->{next_char} == 0x005D) { # ]
2734              !!!next-input-character;
2735              if ($self->{next_char} == 0x005D) { # ]
2736                !!!next-input-character;
2737                MDC: {
2738                  if ($self->{next_char} == 0x003E) { # >
2739                    !!!cp (221.1);
2740                    !!!next-input-character;
2741                    last CS;
2742                  } elsif ($self->{next_char} == 0x005D) { # ]
2743                    !!!cp (221.2);
2744                    $s .= ']';
2745                    !!!next-input-character;
2746                    redo MDC;
2747                  } else {
2748                    !!!cp (221.3);
2749                    $s .= ']]';
2750                    #
2751                  }
2752                } # MDC
2753              } else {
2754                !!!cp (221.4);
2755                $s .= ']';
2756                #
2757              }
2758            } else {
2759              !!!cp (221.5);
2760              #
2761            }
2762            $s .= chr $self->{next_char};
2763            !!!next-input-character;
2764          } # CS
2765    
2766          $self->{state} = DATA_STATE;
2767          ## next-input-character done or EOF, which is reconsumed.
2768    
2769          if (length $s) {
2770            !!!cp (221.6);
2771            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2772                      line => $l, column => $c});
2773          } else {
2774            !!!cp (221.7);
2775          }
2776    
2777          redo A;
2778    
2779          ## ISSUE: "text tokens" in spec.
2780          ## TODO: Streaming support
2781      } else {      } else {
2782        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2783      }      }
# Line 2203  sub _get_next_token ($) { Line 2789  sub _get_next_token ($) {
2789  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2790    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2791    
2792      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2793    
2794    if ({    if ({
2795         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2796         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 2831  sub _tokenize_attempt_to_consume_an_enti
2831            redo X;            redo X;
2832          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2833            !!!cp (1005);            !!!cp (1005);
2834            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2835            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2836            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2837            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2840  sub _tokenize_attempt_to_consume_an_enti
2840            !!!next-input-character;            !!!next-input-character;
2841          } else {          } else {
2842            !!!cp (1007);            !!!cp (1007);
2843            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2844          }          }
2845    
2846          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2847            !!!cp (1008);            !!!cp (1008);
2848            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2849            $code = 0xFFFD;            $code = 0xFFFD;
2850          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2851            !!!cp (1009);            !!!cp (1009);
2852            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2853            $code = 0xFFFD;            $code = 0xFFFD;
2854          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2855            !!!cp (1010);            !!!cp (1010);
2856            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2857            $code = 0x000A;            $code = 0x000A;
2858          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2859            !!!cp (1011);            !!!cp (1011);
2860            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2861            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2862          }          }
2863    
2864          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2865                  has_reference => 1};                  has_reference => 1,
2866                    line => $l, column => $c,
2867                   };
2868        } # X        } # X
2869      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2870               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2885  sub _tokenize_attempt_to_consume_an_enti
2885          !!!next-input-character;          !!!next-input-character;
2886        } else {        } else {
2887          !!!cp (1014);          !!!cp (1014);
2888          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2889        }        }
2890    
2891        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2892          !!!cp (1015);          !!!cp (1015);
2893          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2894          $code = 0xFFFD;          $code = 0xFFFD;
2895        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2896          !!!cp (1016);          !!!cp (1016);
2897          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2898          $code = 0xFFFD;          $code = 0xFFFD;
2899        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2900          !!!cp (1017);          !!!cp (1017);
2901          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2902          $code = 0x000A;          $code = 0x000A;
2903        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2904          !!!cp (1018);          !!!cp (1018);
2905          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2906          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2907        }        }
2908                
2909        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2910                  line => $l, column => $c,
2911                 };
2912      } else {      } else {
2913        !!!cp (1019);        !!!cp (1019);
2914        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2915        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2916        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2917        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2928  sub _tokenize_attempt_to_consume_an_enti
2928      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2929      our $EntityChar;      our $EntityChar;
2930    
2931      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2932             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2933             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2934               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2961  sub _tokenize_attempt_to_consume_an_enti
2961            
2962      if ($match > 0) {      if ($match > 0) {
2963        !!!cp (1023);        !!!cp (1023);
2964        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2965                  line => $l, column => $c,
2966                 };
2967      } elsif ($match < 0) {      } elsif ($match < 0) {
2968        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2969        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2970          !!!cp (1024);          !!!cp (1024);
2971          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2972                    line => $l, column => $c,
2973                   };
2974        } else {        } else {
2975          !!!cp (1025);          !!!cp (1025);
2976          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2977                    line => $l, column => $c,
2978                   };
2979        }        }
2980      } else {      } else {
2981        !!!cp (1026);        !!!cp (1026);
2982        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2983        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2984        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2985                  line => $l, column => $c,
2986                 };
2987      }      }
2988    } else {    } else {
2989      !!!cp (1027);      !!!cp (1027);
2990      ## no characters are consumed      ## no characters are consumed
2991      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2992      return undef;      return undef;
2993    }    }
2994  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 3059  sub _tree_construction_initial ($) {
3059            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3060            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3061          !!!cp ('t1');          !!!cp ('t1');
3062          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3063        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3064          !!!cp ('t2');          !!!cp ('t2');
3065          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3066          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3067        } else {        } else {
3068          !!!cp ('t3');          !!!cp ('t3');
3069        }        }
3070                
3071        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3072          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3073          ## NOTE: Default value for both |public_id| and |system_id| attributes
3074          ## are empty strings, so that we don't set any value in missing cases.
3075        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3076            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3077        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3204  sub _tree_construction_initial ($) {
3204                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3205               }->{$token->{type}}) {               }->{$token->{type}}) {
3206        !!!cp ('t14');        !!!cp ('t14');
3207        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3208        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3209        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3210        ## reprocess        ## reprocess
3211          !!!ack-later;
3212        return;        return;
3213      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3214        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 3226  sub _tree_construction_initial ($) {
3226          !!!cp ('t17');          !!!cp ('t17');
3227        }        }
3228    
3229        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3230        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3231        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3232        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3255  sub _tree_construction_root_element ($)
3255    B: {    B: {
3256        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3257          !!!cp ('t19');          !!!cp ('t19');
3258          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3259          ## Ignore the token          ## Ignore the token
3260          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3261          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3289  sub _tree_construction_root_element ($)
3289        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3290          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3291            my $root_element;            my $root_element;
3292            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3293            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3294            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3295                  [$root_element, $el_category->{html}];
3296    
3297            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3298              !!!cp ('t24');              !!!cp ('t24');
3299              $self->{application_cache_selection}              $self->{application_cache_selection}
3300                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3301              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3302                ## According to Hixie (#whatwg 2008-03-19), it should be
3303                ## resolved against the base URI of the document in HTML
3304                ## or xml:base of the element in XHTML.
3305            } else {            } else {
3306              !!!cp ('t25');              !!!cp ('t25');
3307              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3308            }            }
3309    
3310              !!!nack ('t25c');
3311    
3312            !!!next-token;            !!!next-token;
3313            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3314          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3325  sub _tree_construction_root_element ($)
3325          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3326        }        }
3327    
3328      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3329        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3330      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3331      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3332    
3333      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3334    
3335      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3336        !!!ack-later;
3337      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3338    
3339      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3354  sub _reset_insertion_mode ($) {
3354            
3355      ## Step 3      ## Step 3
3356      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3357        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3358          $last = 1;          $last = 1;
3359          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3360            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3361              !!!cp ('t27');              !!!cp ('t27');
3362              #              #
3363            } else {            } else {
# Line 2762  sub _reset_insertion_mode ($) { Line 3367  sub _reset_insertion_mode ($) {
3367          }          }
3368        }        }
3369            
3370        ## Step 4..13      ## Step 4..14
3371        my $new_mode = {      my $new_mode;
3372        if ($node->[1] & FOREIGN_EL) {
3373          ## NOTE: Strictly spaking, the line below only applies to MathML and
3374          ## SVG elements.  Currently the HTML syntax supports only MathML and
3375          ## SVG elements as foreigners.
3376          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3377          ## ISSUE: What is set as the secondary insertion mode?
3378        } else {
3379          $new_mode = {
3380                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3381                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3382                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2779  sub _reset_insertion_mode ($) { Line 3392  sub _reset_insertion_mode ($) {
3392                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3393                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3394                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3395                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3396        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3397        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3398                
3399        ## Step 14        ## Step 15
3400        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3401          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3402            !!!cp ('t29');            !!!cp ('t29');
3403            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3411  sub _reset_insertion_mode ($) {
3411          !!!cp ('t31');          !!!cp ('t31');
3412        }        }
3413                
3414        ## Step 15        ## Step 16
3415        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3416                
3417        ## Step 16        ## Step 17
3418        $i--;        $i--;
3419        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3420                
3421        ## Step 17        ## Step 18
3422        redo S3;        redo S3;
3423      } # S3      } # S3
3424    
# Line 2908  sub _tree_construction_main ($) { Line 3522  sub _tree_construction_main ($) {
3522      !!!cp ('t39');      !!!cp ('t39');
3523    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3524    
3525    my $parse_rcdata = sub ($$) {    my $insert;
3526      my ($content_model_flag, $insert) = @_;  
3527      my $parse_rcdata = sub ($) {
3528        my ($content_model_flag) = @_;
3529    
3530      ## Step 1      ## Step 1
3531      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3532      my $el;      my $el;
3533      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3534    
3535      ## Step 2      ## Step 2
3536      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3537    
3538      ## Step 3      ## Step 3
3539      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3541  sub _tree_construction_main ($) {
3541    
3542      ## Step 4      ## Step 4
3543      my $text = '';      my $text = '';
3544        !!!nack ('t40.1');
3545      !!!next-token;      !!!next-token;
3546      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3547        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3564  sub _tree_construction_main ($) {
3564          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3565        !!!cp ('t42');        !!!cp ('t42');
3566        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3567      } else {      } else {
3568        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3569          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3570            !!!cp ('t43');
3571            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3572          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3573            !!!cp ('t44');
3574            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3575          } else {
3576            die "$0: $content_model_flag in parse_rcdata";
3577          }
3578      }      }
3579      !!!next-token;      !!!next-token;
3580    }; # $parse_rcdata    }; # $parse_rcdata
3581    
3582    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3583      my $script_el;      my $script_el;
3584      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3585      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3586    
3587      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3588      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3589            
3590      my $text = '';      my $text = '';
3591        !!!nack ('t45.1');
3592      !!!next-token;      !!!next-token;
3593      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3594        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3608  sub _tree_construction_main ($) {
3608        ## Ignore the token        ## Ignore the token
3609      } else {      } else {
3610        !!!cp ('t48');        !!!cp ('t48');
3611        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3612        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3613        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3614      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631      !!!next-token;      !!!next-token;
3632    }; # $script_start_tag    }; # $script_start_tag
3633    
3634      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3635      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3636      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3637    
3638    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3639      my $tag_name = shift;      my $end_tag_token = shift;
3640        my $tag_name = $end_tag_token->{tag_name};
3641    
3642        ## NOTE: The adoption agency algorithm (AAA).
3643    
3644      FET: {      FET: {
3645        ## Step 1        ## Step 1
3646        my $formatting_element;        my $formatting_element;
3647        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3648        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3649          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3650              !!!cp ('t52');
3651              last AFE;
3652            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3653                         eq $tag_name) {
3654            !!!cp ('t51');            !!!cp ('t51');
3655            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3656            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3657            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3658          }          }
3659        } # AFE        } # AFE
3660        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3661          !!!cp ('t53');          !!!cp ('t53');
3662          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3663          ## Ignore the token          ## Ignore the token
3664          !!!next-token;          !!!next-token;
3665          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3676  sub _tree_construction_main ($) {
3676              last INSCOPE;              last INSCOPE;
3677            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3678              !!!cp ('t55');              !!!cp ('t55');
3679              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3680                                token => $end_tag_token);
3681              ## Ignore the token              ## Ignore the token
3682              !!!next-token;              !!!next-token;
3683              return;              return;
3684            }            }
3685          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3686            !!!cp ('t56');            !!!cp ('t56');
3687            $in_scope = 0;            $in_scope = 0;
3688          }          }
3689        } # INSCOPE        } # INSCOPE
3690        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3691          !!!cp ('t57');          !!!cp ('t57');
3692          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3693                            token => $end_tag_token);
3694          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3695          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3696          return;          return;
3697        }        }
3698        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3699          !!!cp ('t58');          !!!cp ('t58');
3700          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3701                            value => $self->{open_elements}->[-1]->[0]
3702                                ->manakai_local_name,
3703                            token => $end_tag_token);
3704        }        }
3705                
3706        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3708  sub _tree_construction_main ($) {
3708        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3709        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3710          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3711          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3712              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3713              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3714               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3715            !!!cp ('t59');            !!!cp ('t59');
3716            $furthest_block = $node;            $furthest_block = $node;
3717            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797        } # S7          } # S7  
3798                
3799        ## Step 8        ## Step 8
3800        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3801            my $foster_parent_element;
3802            my $next_sibling;
3803            OE: for (reverse 0..$#{$self->{open_elements}}) {
3804              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3805                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3806                                 if (defined $parent and $parent->node_type == 1) {
3807                                   !!!cp ('t65.1');
3808                                   $foster_parent_element = $parent;
3809                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3810                                 } else {
3811                                   !!!cp ('t65.2');
3812                                   $foster_parent_element
3813                                     = $self->{open_elements}->[$_ - 1]->[0];
3814                                 }
3815                                 last OE;
3816                               }
3817                             } # OE
3818                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3819                               unless defined $foster_parent_element;
3820            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3821            $open_tables->[-1]->[1] = 1; # tainted
3822          } else {
3823            !!!cp ('t65.3');
3824            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3825          }
3826                
3827        ## Step 9        ## Step 9
3828        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868      } # FET      } # FET
3869    }; # $formatting_end_tag    }; # $formatting_end_tag
3870    
3871    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3872      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3873    }; # $insert_to_current    }; # $insert_to_current
3874    
3875    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3876                         my $child = shift;      my $child = shift;
3877                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3878                              table => 1, tbody => 1, tfoot => 1,        # MUST
3879                              thead => 1, tr => 1,        my $foster_parent_element;
3880                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3881                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3882                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3883                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3884                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3885                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3897  sub _tree_construction_main ($) {
3897                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3898                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3899                             ($child, $next_sibling);                             ($child, $next_sibling);
3900                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3901                           !!!cp ('t72');      } else {
3902                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3903                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3904        }
3905    }; # $insert_to_foster    }; # $insert_to_foster
3906    
3907    my $insert;    B: while (1) {
   
   B: {  
3908      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3909        !!!cp ('t73');        !!!cp ('t73');
3910        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3911        ## Ignore the token        ## Ignore the token
3912        ## Stay in the phase        ## Stay in the phase
3913        !!!next-token;        !!!next-token;
3914        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;  
3915      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3916               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3917        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3918          !!!cp ('t79');          !!!cp ('t79');
3919          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3920          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3921        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3922          !!!cp ('t80');          !!!cp ('t80');
3923          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3924          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3925        } else {        } else {
3926          !!!cp ('t81');          !!!cp ('t81');
3927        }        }
3928    
3929        !!!cp ('t82');        !!!cp ('t82');
3930        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3931        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3932        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3933          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3937  sub _tree_construction_main ($) {
3937               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3938          }          }
3939        }        }
3940          !!!nack ('t84.1');
3941        !!!next-token;        !!!next-token;
3942        redo B;        next B;
3943      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3944        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3945        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 3953  sub _tree_construction_main ($) {
3953          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3954        }        }
3955        !!!next-token;        !!!next-token;
3956        redo B;        next B;
3957      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3958          if ($token->{type} == CHARACTER_TOKEN) {
3959            !!!cp ('t87.1');
3960            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3961            !!!next-token;
3962            next B;
3963          } elsif ($token->{type} == START_TAG_TOKEN) {
3964            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3965                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3966                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3967                ($token->{tag_name} eq 'svg' and
3968                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3969              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3970              !!!cp ('t87.2');
3971              #
3972            } elsif ({
3973                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3974                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3975                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3976                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3977                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3978                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3979                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3980                      var => 1,
3981                     }->{$token->{tag_name}}) {
3982              !!!cp ('t87.2');
3983              !!!parse-error (type => 'not closed',
3984                              value => $self->{open_elements}->[-1]->[0]
3985                                  ->manakai_local_name,
3986                              token => $token);
3987    
3988              pop @{$self->{open_elements}}
3989                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3990    
3991              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3992              ## Reprocess.
3993              next B;
3994            } else {
3995              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3996              my $tag_name = $token->{tag_name};
3997              if ($nsuri eq $SVG_NS) {
3998                $tag_name = {
3999                   altglyph => 'altGlyph',
4000                   altglyphdef => 'altGlyphDef',
4001                   altglyphitem => 'altGlyphItem',
4002                   animatecolor => 'animateColor',
4003                   animatemotion => 'animateMotion',
4004                   animatetransform => 'animateTransform',
4005                   clippath => 'clipPath',
4006                   feblend => 'feBlend',
4007                   fecolormatrix => 'feColorMatrix',
4008                   fecomponenttransfer => 'feComponentTransfer',
4009                   fecomposite => 'feComposite',
4010                   feconvolvematrix => 'feConvolveMatrix',
4011                   fediffuselighting => 'feDiffuseLighting',
4012                   fedisplacementmap => 'feDisplacementMap',
4013                   fedistantlight => 'feDistantLight',
4014                   feflood => 'feFlood',
4015                   fefunca => 'feFuncA',
4016                   fefuncb => 'feFuncB',
4017                   fefuncg => 'feFuncG',
4018                   fefuncr => 'feFuncR',
4019                   fegaussianblur => 'feGaussianBlur',
4020                   feimage => 'feImage',
4021                   femerge => 'feMerge',
4022                   femergenode => 'feMergeNode',
4023                   femorphology => 'feMorphology',
4024                   feoffset => 'feOffset',
4025                   fepointlight => 'fePointLight',
4026                   fespecularlighting => 'feSpecularLighting',
4027                   fespotlight => 'feSpotLight',
4028                   fetile => 'feTile',
4029                   feturbulence => 'feTurbulence',
4030                   foreignobject => 'foreignObject',
4031                   glyphref => 'glyphRef',
4032                   lineargradient => 'linearGradient',
4033                   radialgradient => 'radialGradient',
4034                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4035                   textpath => 'textPath',  
4036                }->{$tag_name} || $tag_name;
4037              }
4038    
4039              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4040    
4041              ## "adjust foreign attributes" - done in insert-element-f
4042    
4043              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4044    
4045              if ($self->{self_closing}) {
4046                pop @{$self->{open_elements}};
4047                !!!ack ('t87.3');
4048              } else {
4049                !!!cp ('t87.4');
4050              }
4051    
4052              !!!next-token;
4053              next B;
4054            }
4055          } elsif ($token->{type} == END_TAG_TOKEN) {
4056            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4057            !!!cp ('t87.5');
4058            #
4059          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4060            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4061            !!!cp ('t87.6');
4062            #
4063            ## TODO: ...
4064          } else {
4065            die "$0: $token->{type}: Unknown token type";        
4066          }
4067        }
4068    
4069        if ($self->{insertion_mode} & HEAD_IMS) {
4070        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4071          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4072            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4073                !!!cp ('t88.2');
4074                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4075              } else {
4076                !!!cp ('t88.1');
4077                ## Ignore the token.
4078                !!!next-token;
4079                next B;
4080              }
4081            unless (length $token->{data}) {            unless (length $token->{data}) {
4082              !!!cp ('t88');              !!!cp ('t88');
4083              !!!next-token;              !!!next-token;
4084              redo B;              next B;
4085            }            }
4086          }          }
4087    
4088          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4089            !!!cp ('t89');            !!!cp ('t89');
4090            ## As if <head>            ## As if <head>
4091            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4092            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4093            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4094                  [$self->{head_element}, $el_category->{head}];
4095    
4096            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4097            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 4101  sub _tree_construction_main ($) {
4101            !!!cp ('t90');            !!!cp ('t90');
4102            ## As if </noscript>            ## As if </noscript>
4103            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4104            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4105                        
4106            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4107            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 4117  sub _tree_construction_main ($) {
4117            !!!cp ('t92');            !!!cp ('t92');
4118          }          }
4119    
4120              ## "after head" insertion mode          ## "after head" insertion mode
4121              ## As if <body>          ## As if <body>
4122              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4123              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4124              ## reprocess          ## reprocess
4125              redo B;          next B;
4126            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4127              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4128                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4129                  !!!cp ('t93');              !!!cp ('t93');
4130                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4131                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4132                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4133                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4134                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4135                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4136                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4137                  !!!cp ('t94');              !!!next-token;
4138                  #              next B;
4139                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4140                  !!!cp ('t95');              !!!cp ('t94');
4141                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
4142                  ## Ignore the token            } else {
4143                  !!!next-token;              !!!cp ('t95');
4144                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4145                }              ## Ignore the token
4146              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4147                !!!cp ('t96');              !!!next-token;
4148                ## As if <head>              next B;
4149                !!!create-element ($self->{head_element}, 'head');            }
4150                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4152              ## As if <head>
4153              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4154              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4155              push @{$self->{open_elements}},
4156                  [$self->{head_element}, $el_category->{head}];
4157    
4158                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4159                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4160              } else {          } else {
4161                !!!cp ('t97');            !!!cp ('t97');
4162              }          }
4163    
4164              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4165                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4166                  !!!cp ('t98');                  !!!cp ('t98');
4167                  ## As if </noscript>                  ## As if </noscript>
4168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4169                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4170                                
4171                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4172                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 4177  sub _tree_construction_main ($) {
4177                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4178                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4179                  !!!cp ('t100');                  !!!cp ('t100');
4180                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4181                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4182                        [$self->{head_element}, $el_category->{head}];
4183                } else {                } else {
4184                  !!!cp ('t101');                  !!!cp ('t101');
4185                }                }
4186                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4187                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4188                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4189                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4190                  !!!nack ('t101.1');
4191                !!!next-token;                !!!next-token;
4192                redo B;                next B;
4193              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4194                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4195                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                  !!!cp ('t102');                  !!!cp ('t102');
4197                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4198                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4199                        [$self->{head_element}, $el_category->{head}];
4200                } else {                } else {
4201                  !!!cp ('t103');                  !!!cp ('t103');
4202                }                }
4203                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4204                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4205                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4206                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4207                  !!!ack ('t103.1');
4208                !!!next-token;                !!!next-token;
4209                redo B;                next B;
4210              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4211                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4212                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4213                  !!!cp ('t104');                  !!!cp ('t104');
4214                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4215                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4216                        [$self->{head_element}, $el_category->{head}];
4217                } else {                } else {
4218                  !!!cp ('t105');                  !!!cp ('t105');
4219                }                }
4220                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4221                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.
4222    
4223                unless ($self->{confident}) {                unless ($self->{confident}) {
4224                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4225                    !!!cp ('t106');                    !!!cp ('t106');
4226                      ## NOTE: Whether the encoding is supported or not is handled
4227                      ## in the {change_encoding} callback.
4228                    $self->{change_encoding}                    $self->{change_encoding}
4229                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4230                             $token);
4231                                        
4232                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4233                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4234                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4235                                                 ->{has_reference});                                                 ->{has_reference});
4236                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4237                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4238                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4239                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4240                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4241                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4242                      !!!cp ('t107');                      !!!cp ('t107');
4243                        ## NOTE: Whether the encoding is supported or not is handled
4244                        ## in the {change_encoding} callback.
4245                      $self->{change_encoding}                      $self->{change_encoding}
4246                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4247                               $token);
4248                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4249                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4250                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4270  sub _tree_construction_main ($) {
4270                  }                  }
4271                }                }
4272    
4273                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4274                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4275                  !!!ack ('t110.1');
4276                !!!next-token;                !!!next-token;
4277                redo B;                next B;
4278              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4279                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4280                  !!!cp ('t111');                  !!!cp ('t111');
4281                  ## As if </noscript>                  ## As if </noscript>
4282                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4283                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4284                                
4285                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4286                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4287                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4288                  !!!cp ('t112');                  !!!cp ('t112');
4289                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4290                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4291                        [$self->{head_element}, $el_category->{head}];
4292                } else {                } else {
4293                  !!!cp ('t113');                  !!!cp ('t113');
4294                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4296  sub _tree_construction_main ($) {
4296                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4297                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4298                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4299                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4300                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4301                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4302                redo B;                next B;
4303              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4304                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4305                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4306                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4307                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4308                  !!!cp ('t114');                  !!!cp ('t114');
4309                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4310                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4311                        [$self->{head_element}, $el_category->{head}];
4312                } else {                } else {
4313                  !!!cp ('t115');                  !!!cp ('t115');
4314                }                }
4315                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4316                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4317                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4318                redo B;                next B;
4319              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4320                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4321                  !!!cp ('t116');                  !!!cp ('t116');
4322                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4323                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4324                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4325                    !!!nack ('t116.1');
4326                  !!!next-token;                  !!!next-token;
4327                  redo B;                  next B;
4328                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4329                  !!!cp ('t117');                  !!!cp ('t117');
4330                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4331                  ## Ignore the token                  ## Ignore the token
4332                    !!!nack ('t117.1');
4333                  !!!next-token;                  !!!next-token;
4334                  redo B;                  next B;
4335                } else {                } else {
4336                  !!!cp ('t118');                  !!!cp ('t118');
4337                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4341  sub _tree_construction_main ($) {
4341                  !!!cp ('t119');                  !!!cp ('t119');
4342                  ## As if </noscript>                  ## As if </noscript>
4343                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4344                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4345                                
4346                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4347                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4348                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!cp ('t120');                  !!!cp ('t120');
4350                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4351                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4352                        [$self->{head_element}, $el_category->{head}];
4353                } else {                } else {
4354                  !!!cp ('t121');                  !!!cp ('t121');
4355                }                }
4356    
4357                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4358                $script_start_tag->($insert_to_current);                $script_start_tag->();
4359                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4360                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                redo B;                next B;
4362              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4363                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4364                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4365                  !!!cp ('t122');                  !!!cp ('t122');
4366                  ## As if </noscript>                  ## As if </noscript>
4367                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4368                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4369                                    
4370                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4371                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4382  sub _tree_construction_main ($) {
4382                }                }
4383    
4384                ## "after head" insertion mode                ## "after head" insertion mode
4385                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4386                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4387                  !!!cp ('t126');                  !!!cp ('t126');
4388                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4392  sub _tree_construction_main ($) {
4392                } else {                } else {
4393                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4394                }                }
4395                  !!!nack ('t127.1');
4396                !!!next-token;                !!!next-token;
4397                redo B;                next B;
4398              } else {              } else {
4399                !!!cp ('t128');                !!!cp ('t128');
4400                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4404  sub _tree_construction_main ($) {
4404                !!!cp ('t129');                !!!cp ('t129');
4405                ## As if </noscript>                ## As if </noscript>
4406                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4407                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4408                                
4409                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4410                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4423  sub _tree_construction_main ($) {
4423    
4424              ## "after head" insertion mode              ## "after head" insertion mode
4425              ## As if <body>              ## As if <body>
4426              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4427              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4428              ## reprocess              ## reprocess
4429              redo B;              !!!ack-later;
4430                next B;
4431            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4432              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4433                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4434                  !!!cp ('t132');                  !!!cp ('t132');
4435                  ## As if <head>                  ## As if <head>
4436                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4437                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4438                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4439                        [$self->{head_element}, $el_category->{head}];
4440    
4441                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4442                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4443                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4444                  !!!next-token;                  !!!next-token;
4445                  redo B;                  next B;
4446                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4447                  !!!cp ('t133');                  !!!cp ('t133');
4448                  ## As if </noscript>                  ## As if </noscript>
4449                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4450                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4451                                    
4452                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4453                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4454                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4455                  !!!next-token;                  !!!next-token;
4456                  redo B;                  next B;
4457                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4458                  !!!cp ('t134');                  !!!cp ('t134');
4459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4460                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4461                  !!!next-token;                  !!!next-token;
4462                  redo B;                  next B;
4463                } else {                } else {
4464                  !!!cp ('t135');                  !!!cp ('t135');
4465                  #                  #
# Line 3706  sub _tree_construction_main ($) { Line 4470  sub _tree_construction_main ($) {
4470                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4471                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4472                  !!!next-token;                  !!!next-token;
4473                  redo B;                  next B;
4474                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4475                  !!!cp ('t137');                  !!!cp ('t137');
4476                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4477                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4478                  !!!next-token;                  !!!next-token;
4479                  redo B;                  next B;
4480                } else {                } else {
4481                  !!!cp ('t138');                  !!!cp ('t138');
4482                  #                  #
# Line 3723  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4488                  !!!cp ('t139');                  !!!cp ('t139');
4489                  ## As if <head>                  ## As if <head>
4490                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4491                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4492                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4493                        [$self->{head_element}, $el_category->{head}];
4494    
4495                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4496                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4497                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4498                  !!!cp ('t140');                  !!!cp ('t140');
4499                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4500                  ## Ignore the token                  ## Ignore the token
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                } else {                } else {
4504                  !!!cp ('t141');                  !!!cp ('t141');
4505                }                }
# Line 3746  sub _tree_construction_main ($) { Line 4511  sub _tree_construction_main ($) {
4511                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4512                  !!!cp ('t142');                  !!!cp ('t142');
4513                  ## As if <head>                  ## As if <head>
4514                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4515                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4516                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4517                        [$self->{head_element}, $el_category->{head}];
4518    
4519                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4520                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3763  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529                  #                  #
4530                } else {                } else {
4531                  !!!cp ('t145');                  !!!cp ('t145');
4532                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4533                  ## Ignore the token                  ## Ignore the token
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                }                }
4537              }              }
4538    
# Line 3774  sub _tree_construction_main ($) { Line 4540  sub _tree_construction_main ($) {
4540                !!!cp ('t146');                !!!cp ('t146');
4541                ## As if </noscript>                ## As if </noscript>
4542                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4543                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4544                                
4545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4546                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4556  sub _tree_construction_main ($) {
4556              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4557  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4558                !!!cp ('t148');                !!!cp ('t148');
4559                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4560                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4561                !!!next-token;                !!!next-token;
4562                redo B;                next B;
4563              } else {              } else {
4564                !!!cp ('t149');                !!!cp ('t149');
4565              }              }
4566    
4567              ## "after head" insertion mode              ## "after head" insertion mode
4568              ## As if <body>              ## As if <body>
4569              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4570              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4571              ## reprocess              ## reprocess
4572              redo B;              next B;
4573            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4574              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575            }            !!!cp ('t149.1');
4576    
4577              ## NOTE: As if <head>
4578              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4579              $self->{open_elements}->[-1]->[0]->append_child
4580                  ($self->{head_element});
4581              #push @{$self->{open_elements}},
4582              #    [$self->{head_element}, $el_category->{head}];
4583              #$self->{insertion_mode} = IN_HEAD_IM;
4584              ## NOTE: Reprocess.
4585    
4586              ## NOTE: As if </head>
4587              #pop @{$self->{open_elements}};
4588              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4589              ## NOTE: Reprocess.
4590              
4591              #
4592            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4593              !!!cp ('t149.2');
4594    
4595              ## NOTE: As if </head>
4596              pop @{$self->{open_elements}};
4597              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4598              ## NOTE: Reprocess.
4599    
4600              #
4601            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4602              !!!cp ('t149.3');
4603    
4604              !!!parse-error (type => 'in noscript:#eof', token => $token);
4605    
4606              ## As if </noscript>
4607              pop @{$self->{open_elements}};
4608              #$self->{insertion_mode} = IN_HEAD_IM;
4609              ## NOTE: Reprocess.
4610    
4611              ## NOTE: As if </head>
4612              pop @{$self->{open_elements}};
4613              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4614              ## NOTE: Reprocess.
4615    
4616              #
4617            } else {
4618              !!!cp ('t149.4');
4619              #
4620            }
4621    
4622            ## NOTE: As if <body>
4623            !!!insert-element ('body',, $token);
4624            $self->{insertion_mode} = IN_BODY_IM;
4625            ## NOTE: Reprocess.
4626            next B;
4627          } else {
4628            die "$0: $token->{type}: Unknown token type";
4629          }
4630    
4631            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4632      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4638  sub _tree_construction_main ($) {
4638              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4639    
4640              !!!next-token;              !!!next-token;
4641              redo B;              next B;
4642            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4643              if ({              if ({
4644                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4646  sub _tree_construction_main ($) {
4646                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4647                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4648                  ## have an element in table scope                  ## have an element in table scope
4649                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4651                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4652                      !!!cp ('t151');                      !!!cp ('t151');
4653                      $tn = $node->[1];  
4654                      last INSCOPE;                      ## Close the cell
4655                    } elsif ({                      !!!back-token; # <x>
4656                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4657                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4658                                  line => $token->{line},
4659                                  column => $token->{column}};
4660                        next B;
4661                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4662                      !!!cp ('t152');                      !!!cp ('t152');
4663                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4664                    }                      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;  
4665                    }                    }
4666                                    }
4667                  !!!cp ('t154');  
4668                  ## Close the cell                  !!!cp ('t153');
4669                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4670                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4671                  redo B;                  ## Ignore the token
4672                    !!!nack ('t153.1');
4673                    !!!next-token;
4674                    next B;
4675                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4676                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4677                                    
4678                  ## As if </caption>                  ## NOTE: As if </caption>.
4679                  ## have a table element in table scope                  ## have a table element in table scope
4680                  my $i;                  my $i;
4681                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4682                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4683                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4684                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4685                      $i = $_;                        !!!cp ('t155');
4686                      last INSCOPE;                        $i = $_;
4687                    } elsif ({                        last INSCOPE;
4688                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4689                             }->{$node->[1]}) {                        !!!cp ('t156');
4690                      !!!cp ('t156');                        last;
4691                      last INSCOPE;                      }
4692                    }                    }
4693    
4694                      !!!cp ('t157');
4695                      !!!parse-error (type => 'start tag not allowed',
4696                                      value => $token->{tag_name}, token => $token);
4697                      ## Ignore the token
4698                      !!!nack ('t157.1');
4699                      !!!next-token;
4700                      next B;
4701                  } # 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;  
                   }  
4702                                    
4703                  ## generate implied end tags                  ## generate implied end tags
4704                  while ({                  while ($self->{open_elements}->[-1]->[1]
4705                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4706                    !!!cp ('t158');                    !!!cp ('t158');
4707                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4708                  }                  }
4709    
4710                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4711                    !!!cp ('t159');                    !!!cp ('t159');
4712                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4713                                      value => $self->{open_elements}->[-1]->[0]
4714                                          ->manakai_local_name,
4715                                      token => $token);
4716                  } else {                  } else {
4717                    !!!cp ('t160');                    !!!cp ('t160');
4718                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4725                                    
4726                  ## reprocess                  ## reprocess
4727                  redo B;                  !!!ack-later;
4728                    next B;
4729                } else {                } else {
4730                  !!!cp ('t161');                  !!!cp ('t161');
4731                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 4741  sub _tree_construction_main ($) {
4741                  my $i;                  my $i;
4742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4744                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4745                      !!!cp ('t163');                      !!!cp ('t163');
4746                      $i = $_;                      $i = $_;
4747                      last INSCOPE;                      last INSCOPE;
4748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4749                      !!!cp ('t164');                      !!!cp ('t164');
4750                      last INSCOPE;                      last INSCOPE;
4751                    }                    }
4752                  } # INSCOPE                  } # INSCOPE
4753                    unless (defined $i) {                    unless (defined $i) {
4754                      !!!cp ('t165');                      !!!cp ('t165');
4755                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4756                      ## Ignore the token                      ## Ignore the token
4757                      !!!next-token;                      !!!next-token;
4758                      redo B;                      next B;
4759                    }                    }
4760                                    
4761                  ## generate implied end tags                  ## generate implied end tags
4762                  while ({                  while ($self->{open_elements}->[-1]->[1]
4763                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4764                    !!!cp ('t166');                    !!!cp ('t166');
4765                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4766                  }                  }
4767    
4768                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4769                            ne $token->{tag_name}) {
4770                    !!!cp ('t167');                    !!!cp ('t167');
4771                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4772                                      value => $self->{open_elements}->[-1]->[0]
4773                                          ->manakai_local_name,
4774                                      token => $token);
4775                  } else {                  } else {
4776                    !!!cp ('t168');                    !!!cp ('t168');
4777                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4784                                    
4785                  !!!next-token;                  !!!next-token;
4786                  redo B;                  next B;
4787                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4788                  !!!cp ('t169');                  !!!cp ('t169');
4789                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4790                  ## Ignore the token                  ## Ignore the token
4791                  !!!next-token;                  !!!next-token;
4792                  redo B;                  next B;
4793                } else {                } else {
4794                  !!!cp ('t170');                  !!!cp ('t170');
4795                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 4798  sub _tree_construction_main ($) {
4798                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4799                  ## have a table element in table scope                  ## have a table element in table scope
4800                  my $i;                  my $i;
4801                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4802                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4803                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4804                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4805                      $i = $_;                        !!!cp ('t171');
4806                      last INSCOPE;                        $i = $_;
4807                    } elsif ({                        last INSCOPE;
4808                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4809                             }->{$node->[1]}) {                        !!!cp ('t172');
4810                      !!!cp ('t172');                        last;
4811                      last INSCOPE;                      }
4812                    }                    }
4813    
4814                      !!!cp ('t173');
4815                      !!!parse-error (type => 'unmatched end tag',
4816                                      value => $token->{tag_name}, token => $token);
4817                      ## Ignore the token
4818                      !!!next-token;
4819                      next B;
4820                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4821                                    
4822                  ## generate implied end tags                  ## generate implied end tags
4823                  while ({                  while ($self->{open_elements}->[-1]->[1]
4824                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4825                    !!!cp ('t174');                    !!!cp ('t174');
4826                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4827                  }                  }
4828                                    
4829                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4830                    !!!cp ('t175');                    !!!cp ('t175');
4831                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4832                                      value => $self->{open_elements}->[-1]->[0]
4833                                          ->manakai_local_name,
4834                                      token => $token);
4835                  } else {                  } else {
4836                    !!!cp ('t176');                    !!!cp ('t176');
4837                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 4843  sub _tree_construction_main ($) {
4843                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4844                                    
4845                  !!!next-token;                  !!!next-token;
4846                  redo B;                  next B;
4847                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4848                  !!!cp ('t177');                  !!!cp ('t177');
4849                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4850                  ## Ignore the token                  ## Ignore the token
4851                  !!!next-token;                  !!!next-token;
4852                  redo B;                  next B;
4853                } else {                } else {
4854                  !!!cp ('t178');                  !!!cp ('t178');
4855                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 4862  sub _tree_construction_main ($) {
4862                ## have an element in table scope                ## have an element in table scope
4863                my $i;                my $i;
4864                my $tn;                my $tn;
4865                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4866                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4867                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4868                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4869                    $i = $_;                      !!!cp ('t179');
4870                    last INSCOPE;                      $i = $_;
4871                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4872                    !!!cp ('t180');                      ## Close the cell
4873                    $tn = $node->[1];                      !!!back-token; # </x>
4874                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4875                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4876                  } elsif ({                                column => $token->{column}};
4877                            table => 1, html => 1,                      next B;
4878                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4879                    !!!cp ('t181');                      !!!cp ('t180');
4880                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4881                        ## NOTE: There is exactly one |td| or |th| element
4882                        ## in scope in the stack of open elements by definition.
4883                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4884                        ## ISSUE: Can this be reached?
4885                        !!!cp ('t181');
4886                        last;
4887                      }
4888                  }                  }
4889                } # INSCOPE  
               unless (defined $i) {  
4890                  !!!cp ('t182');                  !!!cp ('t182');
4891                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4892                        value => $token->{tag_name}, token => $token);
4893                  ## Ignore the token                  ## Ignore the token
4894                  !!!next-token;                  !!!next-token;
4895                  redo B;                  next B;
4896                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4897              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4898                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4899                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4900    
4901                ## As if </caption>                ## As if </caption>
4902                ## have a table element in table scope                ## have a table element in table scope
4903                my $i;                my $i;
4904                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4905                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4906                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4907                    !!!cp ('t184');                    !!!cp ('t184');
4908                    $i = $_;                    $i = $_;
4909                    last INSCOPE;                    last INSCOPE;
4910                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4911                    !!!cp ('t185');                    !!!cp ('t185');
4912                    last INSCOPE;                    last INSCOPE;
4913                  }                  }
4914                } # INSCOPE                } # INSCOPE
4915                unless (defined $i) {                unless (defined $i) {
4916                  !!!cp ('t186');                  !!!cp ('t186');
4917                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4918                  ## Ignore the token                  ## Ignore the token
4919                  !!!next-token;                  !!!next-token;
4920                  redo B;                  next B;
4921                }                }
4922                                
4923                ## generate implied end tags                ## generate implied end tags
4924                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4925                  !!!cp ('t187');                  !!!cp ('t187');
4926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4927                }                }
4928    
4929                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4930                  !!!cp ('t188');                  !!!cp ('t188');
4931                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4932                                    value => $self->{open_elements}->[-1]->[0]
4933                                        ->manakai_local_name,
4934                                    token => $token);
4935                } else {                } else {
4936                  !!!cp ('t189');                  !!!cp ('t189');
4937                }                }
# Line 4120  sub _tree_construction_main ($) { Line 4943  sub _tree_construction_main ($) {
4943                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4944    
4945                ## reprocess                ## reprocess
4946                redo B;                next B;
4947              } elsif ({              } elsif ({
4948                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4949                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4950                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4951                  !!!cp ('t190');                  !!!cp ('t190');
4952                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4953                  ## Ignore the token                  ## Ignore the token
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } else {                } else {
4957                  !!!cp ('t191');                  !!!cp ('t191');
4958                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 4963  sub _tree_construction_main ($) {
4963                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4964                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4965                !!!cp ('t192');                !!!cp ('t192');
4966                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4967                ## Ignore the token                ## Ignore the token
4968                !!!next-token;                !!!next-token;
4969                redo B;                next B;
4970              } else {              } else {
4971                !!!cp ('t193');                !!!cp ('t193');
4972                #                #
4973              }              }
4974          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4975            for my $entry (@{$self->{open_elements}}) {
4976              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4977                !!!cp ('t75');
4978                !!!parse-error (type => 'in body:#eof', token => $token);
4979                last;
4980              }
4981            }
4982    
4983            ## Stop parsing.
4984            last B;
4985        } else {        } else {
4986          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4987        }        }
# Line 4156  sub _tree_construction_main ($) { Line 4990  sub _tree_construction_main ($) {
4990        #        #
4991      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4992        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4993              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4994                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4995              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4996                                
4997                unless (length $token->{data}) {            unless (length $token->{data}) {
4998                  !!!cp ('t194');              !!!cp ('t194');
4999                  !!!next-token;              !!!next-token;
5000                  redo B;              next B;
5001                } else {            } else {
5002                  !!!cp ('t195');              !!!cp ('t195');
5003                }            }
5004              }          }
5005    
5006              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5007    
5008              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5009              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011              ## result in a new Text node.              ## result in a new Text node.
5012              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5013                            
5014              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]}) {  
5015                # MUST                # MUST
5016                my $foster_parent_element;                my $foster_parent_element;
5017                my $next_sibling;                my $next_sibling;
5018                my $prev_sibling;                my $prev_sibling;
5019                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5020                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5021                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5022                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5023                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 5045  sub _tree_construction_main ($) {
5045                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5046                     $next_sibling);                     $next_sibling);
5047                }                }
5048              } else {            $open_tables->[-1]->[1] = 1; # tainted
5049                !!!cp ('t200');          } else {
5050                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5051              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5052            }
5053                            
5054              !!!next-token;          !!!next-token;
5055              redo B;          next B;
5056        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5057              if ({              if ({
5058                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4227  sub _tree_construction_main ($) { Line 5060  sub _tree_construction_main ($) {
5060                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5061                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5062                  ## Clear back to table context                  ## Clear back to table context
5063                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5064                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5065                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5066                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5067                  }                  }
5068                                    
5069                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5070                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5071                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5072                }                }
# Line 4242  sub _tree_construction_main ($) { Line 5074  sub _tree_construction_main ($) {
5074                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5075                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5076                    !!!cp ('t202');                    !!!cp ('t202');
5077                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5078                  }                  }
5079                                    
5080                  ## Clear back to table body context                  ## Clear back to table body context
5081                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5082                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5083                    !!!cp ('t203');                    !!!cp ('t203');
5084                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5085                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5086                  }                  }
5087                                    
5088                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5089                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5090                    !!!cp ('t204');                    !!!cp ('t204');
5091                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5092                      !!!nack ('t204');
5093                    !!!next-token;                    !!!next-token;
5094                    redo B;                    next B;
5095                  } else {                  } else {
5096                    !!!cp ('t205');                    !!!cp ('t205');
5097                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5098                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5099                  }                  }
5100                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 5102  sub _tree_construction_main ($) {
5102                }                }
5103    
5104                ## Clear back to table row context                ## Clear back to table row context
5105                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5106                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5107                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5108                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5109                }                }
5110                                
5111                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5112                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5113    
5114                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5115                                
5116                  !!!nack ('t207.1');
5117                !!!next-token;                !!!next-token;
5118                redo B;                next B;
5119              } elsif ({              } elsif ({
5120                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5121                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 5127  sub _tree_construction_main ($) {
5127                  my $i;                  my $i;
5128                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5129                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5130                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5131                      !!!cp ('t208');                      !!!cp ('t208');
5132                      $i = $_;                      $i = $_;
5133                      last INSCOPE;                      last INSCOPE;
5134                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5135                      !!!cp ('t209');                      !!!cp ('t209');
5136                      last INSCOPE;                      last INSCOPE;
5137                    }                    }
5138                  } # INSCOPE                  } # INSCOPE
5139                  unless (defined $i) {                  unless (defined $i) {
5140                   !!!cp ('t210');                    !!!cp ('t210');
5141  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5142                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5143                    ## Ignore the token                    ## Ignore the token
5144                      !!!nack ('t210.1');
5145                    !!!next-token;                    !!!next-token;
5146                    redo B;                    next B;
5147                  }                  }
5148                                    
5149                  ## Clear back to table row context                  ## Clear back to table row context
5150                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5151                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5152                    !!!cp ('t211');                    !!!cp ('t211');
5153                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5154                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5155                  }                  }
5156                                    
# Line 4335  sub _tree_construction_main ($) { Line 5159  sub _tree_construction_main ($) {
5159                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5160                    !!!cp ('t212');                    !!!cp ('t212');
5161                    ## reprocess                    ## reprocess
5162                    redo B;                    !!!ack-later;
5163                      next B;
5164                  } else {                  } else {
5165                    !!!cp ('t213');                    !!!cp ('t213');
5166                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 5172  sub _tree_construction_main ($) {
5172                  my $i;                  my $i;
5173                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5174                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5175                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5176                      !!!cp ('t214');                      !!!cp ('t214');
5177                      $i = $_;                      $i = $_;
5178                      last INSCOPE;                      last INSCOPE;
5179                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5180                      !!!cp ('t215');                      !!!cp ('t215');
5181                      last INSCOPE;                      last INSCOPE;
5182                    }                    }
# Line 4363  sub _tree_construction_main ($) { Line 5184  sub _tree_construction_main ($) {
5184                  unless (defined $i) {                  unless (defined $i) {
5185                    !!!cp ('t216');                    !!!cp ('t216');
5186  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5187                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5188                    ## Ignore the token                    ## Ignore the token
5189                      !!!nack ('t216.1');
5190                    !!!next-token;                    !!!next-token;
5191                    redo B;                    next B;
5192                  }                  }
5193    
5194                  ## Clear back to table body context                  ## Clear back to table body context
5195                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5196                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5197                    !!!cp ('t217');                    !!!cp ('t217');
5198                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5199                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5200                  }                  }
5201                                    
# Line 4395  sub _tree_construction_main ($) { Line 5215  sub _tree_construction_main ($) {
5215    
5216                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5217                  ## Clear back to table context                  ## Clear back to table context
5218                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5219                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5220                    !!!cp ('t219');                    !!!cp ('t219');
5221                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5222                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5223                  }                  }
5224                                    
5225                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5226                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5227                  ## reprocess                  ## reprocess
5228                  redo B;                  !!!ack-later;
5229                    next B;
5230                } elsif ({                } elsif ({
5231                          caption => 1,                          caption => 1,
5232                          colgroup => 1,                          colgroup => 1,
5233                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5234                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5235                  ## Clear back to table context                  ## Clear back to table context
5236                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5237                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5238                    !!!cp ('t220');                    !!!cp ('t220');
5239                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5240                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5241                  }                  }
5242                                    
5243                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5244                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5245                                    
5246                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5247                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5248                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5249                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5252  sub _tree_construction_main ($) {
5252                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5253                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5254                  !!!next-token;                  !!!next-token;
5255                  redo B;                  !!!nack ('t220.1');
5256                    next B;
5257                } else {                } else {
5258                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5259                }                }
5260              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5261                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5262                                  value => $self->{open_elements}->[-1]->[0]
5263                                      ->manakai_local_name,
5264                                  token => $token);
5265    
5266                ## As if </table>                ## As if </table>
5267                ## have a table element in table scope                ## have a table element in table scope
5268                my $i;                my $i;
5269                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5271                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5272                    !!!cp ('t221');                    !!!cp ('t221');
5273                    $i = $_;                    $i = $_;
5274                    last INSCOPE;                    last INSCOPE;
5275                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5276                    !!!cp ('t222');                    !!!cp ('t222');
5277                    last INSCOPE;                    last INSCOPE;
5278                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5280  sub _tree_construction_main ($) {
5280                unless (defined $i) {                unless (defined $i) {
5281                  !!!cp ('t223');                  !!!cp ('t223');
5282  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5283                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5284                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5285                    !!!nack ('t223.1');
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                }                }
5289                                
5290    ## TODO: Followings are removed from the latest spec.
5291                ## generate implied end tags                ## generate implied end tags
5292                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5293                  !!!cp ('t224');                  !!!cp ('t224');
5294                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5295                }                }
5296    
5297                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5298                  !!!cp ('t225');                  !!!cp ('t225');
5299  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5300                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5301                                    value => $self->{open_elements}->[-1]->[0]
5302                                        ->manakai_local_name,
5303                                    token => $token);
5304                } else {                } else {
5305                  !!!cp ('t226');                  !!!cp ('t226');
5306                }                }
5307    
5308                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5309                  pop @{$open_tables};
5310    
5311                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5312    
5313                ## reprocess            ## reprocess
5314                redo B;            !!!ack-later;
5315              next B;
5316            } elsif ($token->{tag_name} eq 'style') {
5317              if (not $open_tables->[-1]->[1]) { # tainted
5318                !!!cp ('t227.8');
5319                ## NOTE: This is a "as if in head" code clone.
5320                $parse_rcdata->(CDATA_CONTENT_MODEL);
5321                next B;
5322              } else {
5323                !!!cp ('t227.7');
5324                #
5325              }
5326            } elsif ($token->{tag_name} eq 'script') {
5327              if (not $open_tables->[-1]->[1]) { # tainted
5328                !!!cp ('t227.6');
5329                ## NOTE: This is a "as if in head" code clone.
5330                $script_start_tag->();
5331                next B;
5332              } else {
5333                !!!cp ('t227.5');
5334                #
5335              }
5336            } elsif ($token->{tag_name} eq 'input') {
5337              if (not $open_tables->[-1]->[1]) { # tainted
5338                if ($token->{attributes}->{type}) { ## TODO: case
5339                  my $type = lc $token->{attributes}->{type}->{value};
5340                  if ($type eq 'hidden') {
5341                    !!!cp ('t227.3');
5342                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5343    
5344                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5345    
5346                    ## TODO: form element pointer
5347    
5348                    pop @{$self->{open_elements}};
5349    
5350                    !!!next-token;
5351                    !!!ack ('t227.2.1');
5352                    next B;
5353                  } else {
5354                    !!!cp ('t227.2');
5355                    #
5356                  }
5357                } else {
5358                  !!!cp ('t227.1');
5359                  #
5360                }
5361              } else {
5362                !!!cp ('t227.4');
5363                #
5364              }
5365          } else {          } else {
5366            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5367            #            #
5368          }          }
5369    
5370            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5371    
5372            $insert = $insert_to_foster;
5373            #
5374        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5375              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5376                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                my $i;                my $i;
5379                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5381                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5382                    !!!cp ('t228');                    !!!cp ('t228');
5383                    $i = $_;                    $i = $_;
5384                    last INSCOPE;                    last INSCOPE;
5385                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5386                    !!!cp ('t229');                    !!!cp ('t229');
5387                    last INSCOPE;                    last INSCOPE;
5388                  }                  }
5389                } # INSCOPE                } # INSCOPE
5390                unless (defined $i) {                unless (defined $i) {
5391                  !!!cp ('t230');                  !!!cp ('t230');
5392                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5393                  ## Ignore the token                  ## Ignore the token
5394                    !!!nack ('t230.1');
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                } else {                } else {
5398                  !!!cp ('t232');                  !!!cp ('t232');
5399                }                }
5400    
5401                ## Clear back to table row context                ## Clear back to table row context
5402                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5403                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5404                  !!!cp ('t231');                  !!!cp ('t231');
5405  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5406                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5407                }                }
5408    
5409                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5410                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5411                !!!next-token;                !!!next-token;
5412                redo B;                !!!nack ('t231.1');
5413                  next B;
5414              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5415                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5416                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5418  sub _tree_construction_main ($) {
5418                  my $i;                  my $i;
5419                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5420                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5421                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5422                      !!!cp ('t233');                      !!!cp ('t233');
5423                      $i = $_;                      $i = $_;
5424                      last INSCOPE;                      last INSCOPE;
5425                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5426                      !!!cp ('t234');                      !!!cp ('t234');
5427                      last INSCOPE;                      last INSCOPE;
5428                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5430  sub _tree_construction_main ($) {
5430                  unless (defined $i) {                  unless (defined $i) {
5431                    !!!cp ('t235');                    !!!cp ('t235');
5432  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5433                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5434                    ## Ignore the token                    ## Ignore the token
5435                      !!!nack ('t236.1');
5436                    !!!next-token;                    !!!next-token;
5437                    redo B;                    next B;
5438                  }                  }
5439                                    
5440                  ## Clear back to table row context                  ## Clear back to table row context
5441                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5442                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5443                    !!!cp ('t236');                    !!!cp ('t236');
5444  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5445                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5446                  }                  }
5447                                    
# Line 4584  sub _tree_construction_main ($) { Line 5455  sub _tree_construction_main ($) {
5455                  my $i;                  my $i;
5456                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5457                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5458                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5459                      !!!cp ('t237');                      !!!cp ('t237');
5460                      $i = $_;                      $i = $_;
5461                      last INSCOPE;                      last INSCOPE;
5462                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5463                      !!!cp ('t238');                      !!!cp ('t238');
5464                      last INSCOPE;                      last INSCOPE;
5465                    }                    }
5466                  } # INSCOPE                  } # INSCOPE
5467                  unless (defined $i) {                  unless (defined $i) {
5468                    !!!cp ('t239');                    !!!cp ('t239');
5469                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5470                    ## Ignore the token                    ## Ignore the token
5471                      !!!nack ('t239.1');
5472                    !!!next-token;                    !!!next-token;
5473                    redo B;                    next B;
5474                  }                  }
5475                                    
5476                  ## Clear back to table body context                  ## Clear back to table body context
5477                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5478                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5479                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5480                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5481                  }                  }
5482                                    
# Line 4626  sub _tree_construction_main ($) { Line 5492  sub _tree_construction_main ($) {
5492                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5493                }                }
5494    
5495                  ## NOTE: </table> in the "in table" insertion mode.
5496                  ## When you edit the code fragment below, please ensure that
5497                  ## the code for <table> in the "in table" insertion mode
5498                  ## is synced with it.
5499    
5500                ## have a table element in table scope                ## have a table element in table scope
5501                my $i;                my $i;
5502                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5503                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5504                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5505                    !!!cp ('t241');                    !!!cp ('t241');
5506                    $i = $_;                    $i = $_;
5507                    last INSCOPE;                    last INSCOPE;
5508                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5509                    !!!cp ('t242');                    !!!cp ('t242');
5510                    last INSCOPE;                    last INSCOPE;
5511                  }                  }
5512                } # INSCOPE                } # INSCOPE
5513                unless (defined $i) {                unless (defined $i) {
5514                  !!!cp ('t243');                  !!!cp ('t243');
5515                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5516                  ## Ignore the token                  ## Ignore the token
5517                    !!!nack ('t243.1');
5518                  !!!next-token;                  !!!next-token;
5519                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5520                }                }
5521                                    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524                                
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526                                
5527                !!!next-token;                !!!next-token;
5528                redo B;                next B;
5529              } elsif ({              } elsif ({
5530                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5531                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 5535  sub _tree_construction_main ($) {
5535                  my $i;                  my $i;
5536                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5537                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5538                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5539                      !!!cp ('t247');                      !!!cp ('t247');
5540                      $i = $_;                      $i = $_;
5541                      last INSCOPE;                      last INSCOPE;
5542                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5543                      !!!cp ('t248');                      !!!cp ('t248');
5544                      last INSCOPE;                      last INSCOPE;
5545                    }                    }
5546                  } # INSCOPE                  } # INSCOPE
5547                    unless (defined $i) {                    unless (defined $i) {
5548                      !!!cp ('t249');                      !!!cp ('t249');
5549                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5550                      ## Ignore the token                      ## Ignore the token
5551                        !!!nack ('t249.1');
5552                      !!!next-token;                      !!!next-token;
5553                      redo B;                      next B;
5554                    }                    }
5555                                    
5556                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 5558  sub _tree_construction_main ($) {
5558                  my $i;                  my $i;
5559                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5560                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5561                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5562                      !!!cp ('t250');                      !!!cp ('t250');
5563                      $i = $_;                      $i = $_;
5564                      last INSCOPE;                      last INSCOPE;
5565                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5566                      !!!cp ('t251');                      !!!cp ('t251');
5567                      last INSCOPE;                      last INSCOPE;
5568                    }                    }
5569                  } # INSCOPE                  } # INSCOPE
5570                    unless (defined $i) {                    unless (defined $i) {
5571                      !!!cp ('t252');                      !!!cp ('t252');
5572                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5573                      ## Ignore the token                      ## Ignore the token
5574                        !!!nack ('t252.1');
5575                      !!!next-token;                      !!!next-token;
5576                      redo B;                      next B;
5577                    }                    }
5578                                    
5579                  ## Clear back to table row context                  ## Clear back to table row context
5580                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5581                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5582                    !!!cp ('t253');                    !!!cp ('t253');
5583  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5584                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5585                  }                  }
5586                                    
# Line 4742  sub _tree_construction_main ($) { Line 5593  sub _tree_construction_main ($) {
5593                my $i;                my $i;
5594                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5595                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5596                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5597                    !!!cp ('t254');                    !!!cp ('t254');
5598                    $i = $_;                    $i = $_;
5599                    last INSCOPE;                    last INSCOPE;
5600                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5601                    !!!cp ('t255');                    !!!cp ('t255');
5602                    last INSCOPE;                    last INSCOPE;
5603                  }                  }
5604                } # INSCOPE                } # INSCOPE
5605                unless (defined $i) {                unless (defined $i) {
5606                  !!!cp ('t256');                  !!!cp ('t256');
5607                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5608                  ## Ignore the token                  ## Ignore the token
5609                    !!!nack ('t256.1');
5610                  !!!next-token;                  !!!next-token;
5611                  redo B;                  next B;
5612                }                }
5613    
5614                ## Clear back to table body context                ## Clear back to table body context
5615                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5616                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5617                  !!!cp ('t257');                  !!!cp ('t257');
5618  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5619                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5620                }                }
5621    
5622                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5623                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5624                  !!!nack ('t257.1');
5625                !!!next-token;                !!!next-token;
5626                redo B;                next B;
5627              } elsif ({              } elsif ({
5628                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5629                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5630                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5631                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5632                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5633                !!!cp ('t258');            !!!cp ('t258');
5634                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5635                ## Ignore the token            ## Ignore the token
5636                !!!next-token;            !!!nack ('t258.1');
5637                redo B;             !!!next-token;
5638              next B;
5639          } else {          } else {
5640            !!!cp ('t259');            !!!cp ('t259');
5641            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5642    
5643            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5644            #            #
5645          }          }
5646          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5647            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5648                    @{$self->{open_elements}} == 1) { # redundant, maybe
5649              !!!parse-error (type => 'in body:#eof', token => $token);
5650              !!!cp ('t259.1');
5651              #
5652            } else {
5653              !!!cp ('t259.2');
5654              #
5655            }
5656    
5657            ## Stop parsing
5658            last B;
5659        } else {        } else {
5660          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5661        }        }
# Line 4803  sub _tree_construction_main ($) { Line 5666  sub _tree_construction_main ($) {
5666                unless (length $token->{data}) {                unless (length $token->{data}) {
5667                  !!!cp ('t260');                  !!!cp ('t260');
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                  next B;
5670                }                }
5671              }              }
5672                            
# Line 4812  sub _tree_construction_main ($) { Line 5675  sub _tree_construction_main ($) {
5675            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5676              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5677                !!!cp ('t262');                !!!cp ('t262');
5678                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5679                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5680                  !!!ack ('t262.1');
5681                !!!next-token;                !!!next-token;
5682                redo B;                next B;
5683              } else {              } else {
5684                !!!cp ('t263');                !!!cp ('t263');
5685                #                #
5686              }              }
5687            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5688              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5689                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5690                  !!!cp ('t264');                  !!!cp ('t264');
5691                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                  !!!next-token;                  !!!next-token;
5694                  redo B;                  next B;
5695                } else {                } else {
5696                  !!!cp ('t265');                  !!!cp ('t265');
5697                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5698                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5699                  !!!next-token;                  !!!next-token;
5700                  redo B;                              next B;            
5701                }                }
5702              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5703                !!!cp ('t266');                !!!cp ('t266');
5704                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5705                ## Ignore the token                ## Ignore the token
5706                !!!next-token;                !!!next-token;
5707                redo B;                next B;
5708              } else {              } else {
5709                !!!cp ('t267');                !!!cp ('t267');
5710                #                #
5711              }              }
5712            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5713              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5714            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5715              !!!cp ('t270.2');
5716              ## Stop parsing.
5717              last B;
5718            } else {
5719              ## NOTE: As if </colgroup>.
5720              !!!cp ('t270.1');
5721              pop @{$self->{open_elements}}; # colgroup
5722              $self->{insertion_mode} = IN_TABLE_IM;
5723              ## Reprocess.
5724              next B;
5725            }
5726          } else {
5727            die "$0: $token->{type}: Unknown token type";
5728          }
5729    
5730            ## As if </colgroup>            ## As if </colgroup>
5731            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5732              !!!cp ('t269');              !!!cp ('t269');
5733              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5734                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5735              ## Ignore the token              ## Ignore the token
5736                !!!nack ('t269.1');
5737              !!!next-token;              !!!next-token;
5738              redo B;              next B;
5739            } else {            } else {
5740              !!!cp ('t270');              !!!cp ('t270');
5741              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5742              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5743                !!!ack-later;
5744              ## reprocess              ## reprocess
5745              redo B;              next B;
5746            }            }
5747      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5748        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5749          !!!cp ('t271');          !!!cp ('t271');
5750          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5751          !!!next-token;          !!!next-token;
5752          redo B;          next B;
5753        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5754              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5755                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5756                  !!!cp ('t272');              !!!cp ('t272');
5757                  ## As if </option>              ## As if </option>
5758                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5759                } else {            } else {
5760                  !!!cp ('t273');              !!!cp ('t273');
5761                }            }
5762    
5763                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5764                !!!next-token;            !!!nack ('t273.1');
5765                redo B;            !!!next-token;
5766              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5767                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5768                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5769                  ## As if </option>              !!!cp ('t274');
5770                  pop @{$self->{open_elements}};              ## As if </option>
5771                } else {              pop @{$self->{open_elements}};
5772                  !!!cp ('t275');            } else {
5773                }              !!!cp ('t275');
5774              }
5775    
5776                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5777                  !!!cp ('t276');              !!!cp ('t276');
5778                  ## As if </optgroup>              ## As if </optgroup>
5779                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5780                } else {            } else {
5781                  !!!cp ('t277');              !!!cp ('t277');
5782                }            }
5783    
5784                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                !!!next-token;            !!!nack ('t277.1');
5786                redo B;            !!!next-token;
5787              } elsif ($token->{tag_name} eq 'select') {            next B;
5788  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5789                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5790                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5791                ## have an element in table scope                    {
5792                my $i;                     caption => 1, table => 1,
5793                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5794                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5795                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5796                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5797                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5798                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5799                  } elsif ({            ## as if there were </select> (otherwise).
5800                            table => 1, html => 1,            ## have an element in table scope
5801                           }->{$node->[1]}) {            my $i;
5802                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5803                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5804                  }              if ($node->[1] & SELECT_EL) {
5805                } # INSCOPE                !!!cp ('t278');
5806                unless (defined $i) {                $i = $_;
5807                  !!!cp ('t280');                last INSCOPE;
5808                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5809                  ## Ignore the token                !!!cp ('t279');
5810                  !!!next-token;                last INSCOPE;
5811                  redo B;              }
5812                }            } # INSCOPE
5813              unless (defined $i) {
5814                !!!cp ('t280');
5815                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5816                ## Ignore the token
5817                !!!nack ('t280.1');
5818                !!!next-token;
5819                next B;
5820              }
5821                                
5822                !!!cp ('t281');            !!!cp ('t281');
5823                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5824    
5825                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5826    
5827                !!!next-token;            if ($token->{tag_name} eq 'select') {
5828                redo B;              !!!nack ('t281.2');
5829                !!!next-token;
5830                next B;
5831              } else {
5832                !!!cp ('t281.1');
5833                !!!ack-later;
5834                ## Reprocess the token.
5835                next B;
5836              }
5837          } else {          } else {
5838            !!!cp ('t282');            !!!cp ('t282');
5839            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5840            ## Ignore the token            ## Ignore the token
5841              !!!nack ('t282.1');
5842            !!!next-token;            !!!next-token;
5843            redo B;            next B;
5844          }          }
5845        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5846              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5847                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5848                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5849                  !!!cp ('t283');              !!!cp ('t283');
5850                  ## As if </option>              ## As if </option>
5851                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5852                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5853                  !!!cp ('t284');              !!!cp ('t284');
5854                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5855                } else {            } else {
5856                  !!!cp ('t285');              !!!cp ('t285');
5857                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5858                  ## Ignore the token              ## Ignore the token
5859                }            }
5860                !!!next-token;            !!!nack ('t285.1');
5861                redo B;            !!!next-token;
5862              } elsif ($token->{tag_name} eq 'option') {            next B;
5863                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5864                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5865                  pop @{$self->{open_elements}};              !!!cp ('t286');
5866                } else {              pop @{$self->{open_elements}};
5867                  !!!cp ('t287');            } else {
5868                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5869                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5870                }              ## Ignore the token
5871                !!!next-token;            }
5872                redo B;            !!!nack ('t287.1');
5873              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5874                ## have an element in table scope            next B;
5875                my $i;          } elsif ($token->{tag_name} eq 'select') {
5876                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5877                  my $node = $self->{open_elements}->[$_];            my $i;
5878                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5879                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5880                    $i = $_;              if ($node->[1] & SELECT_EL) {
5881                    last INSCOPE;                !!!cp ('t288');
5882                  } elsif ({                $i = $_;
5883                            table => 1, html => 1,                last INSCOPE;
5884                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5885                    !!!cp ('t289');                !!!cp ('t289');
5886                    last INSCOPE;                last INSCOPE;
5887                  }              }
5888                } # INSCOPE            } # INSCOPE
5889                unless (defined $i) {            unless (defined $i) {
5890                  !!!cp ('t290');              !!!cp ('t290');
5891                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5892                  ## Ignore the token              ## Ignore the token
5893                  !!!next-token;              !!!nack ('t290.1');
5894                  redo B;              !!!next-token;
5895                }              next B;
5896              }
5897                                
5898                !!!cp ('t291');            !!!cp ('t291');
5899                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5900    
5901                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5902    
5903                !!!next-token;            !!!nack ('t291.1');
5904                redo B;            !!!next-token;
5905              } elsif ({            next B;
5906                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5907                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5908                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5909                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5910                     }->{$token->{tag_name}}) {
5911  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5912                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5913                                
5914                ## have an element in table scope            ## have an element in table scope
5915                my $i;            my $i;
5916                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5917                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5918                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5919                    !!!cp ('t292');                !!!cp ('t292');
5920                    $i = $_;                $i = $_;
5921                    last INSCOPE;                last INSCOPE;
5922                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5923                            table => 1, html => 1,                !!!cp ('t293');
5924                           }->{$node->[1]}) {                last INSCOPE;
5925                    !!!cp ('t293');              }
5926                    last INSCOPE;            } # INSCOPE
5927                  }            unless (defined $i) {
5928                } # INSCOPE              !!!cp ('t294');
5929                unless (defined $i) {              ## Ignore the token
5930                  !!!cp ('t294');              !!!nack ('t294.1');
5931                  ## Ignore the token              !!!next-token;
5932                  !!!next-token;              next B;
5933                  redo B;            }
               }  
5934                                
5935                ## As if </select>            ## As if </select>
5936                ## have an element in table scope            ## have an element in table scope
5937                undef $i;            undef $i;
5938                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5939                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5940                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5941                    !!!cp ('t295');                !!!cp ('t295');
5942                    $i = $_;                $i = $_;
5943                    last INSCOPE;                last INSCOPE;
5944                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5945  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5946                    !!!cp ('t296');                !!!cp ('t296');
5947                    last INSCOPE;                last INSCOPE;
5948                  }              }
5949                } # INSCOPE            } # INSCOPE
5950                unless (defined $i) {            unless (defined $i) {
5951                  !!!cp ('t297');              !!!cp ('t297');
5952  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5953                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5954                  ## Ignore the </select> token              ## Ignore the </select> token
5955                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5956                  redo B;              !!!next-token; ## TODO: ok?
5957                }              next B;
5958              }
5959                                
5960                !!!cp ('t298');            !!!cp ('t298');
5961                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5962    
5963                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5964    
5965                ## reprocess            !!!ack-later;
5966                redo B;            ## reprocess
5967              next B;
5968          } else {          } else {
5969            !!!cp ('t299');            !!!cp ('t299');
5970            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5971            ## Ignore the token            ## Ignore the token
5972              !!!nack ('t299.3');
5973            !!!next-token;            !!!next-token;
5974            redo B;            next B;
5975            }
5976          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5977            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5978                    @{$self->{open_elements}} == 1) { # redundant, maybe
5979              !!!cp ('t299.1');
5980              !!!parse-error (type => 'in body:#eof', token => $token);
5981            } else {
5982              !!!cp ('t299.2');
5983          }          }
5984    
5985            ## Stop parsing.
5986            last B;
5987        } else {        } else {
5988          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5989        }        }
# Line 5086  sub _tree_construction_main ($) { Line 5999  sub _tree_construction_main ($) {
5999            unless (length $token->{data}) {            unless (length $token->{data}) {
6000              !!!cp ('t300');              !!!cp ('t300');
6001              !!!next-token;              !!!next-token;
6002              redo B;              next B;
6003            }            }
6004          }          }
6005                    
6006          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6007            !!!cp ('t301');            !!!cp ('t301');
6008            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
6009    
6010            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6011          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 6013  sub _tree_construction_main ($) {
6013          }          }
6014                    
6015          ## "after body" insertion mode          ## "after body" insertion mode
6016          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6017    
6018          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6019          ## reprocess          ## reprocess
6020          redo B;          next B;
6021        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6022          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6023            !!!cp ('t303');            !!!cp ('t303');
6024            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6025                        
6026            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6027          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 6029  sub _tree_construction_main ($) {
6029          }          }
6030    
6031          ## "after body" insertion mode          ## "after body" insertion mode
6032          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6033    
6034          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6035            !!!ack-later;
6036          ## reprocess          ## reprocess
6037          redo B;          next B;
6038        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6039          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6040            !!!cp ('t305');            !!!cp ('t305');
6041            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6042                        
6043            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6044            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 6050  sub _tree_construction_main ($) {
6050          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6051            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6052              !!!cp ('t307');              !!!cp ('t307');
6053              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6054              ## Ignore the token              ## Ignore the token
6055              !!!next-token;              !!!next-token;
6056              redo B;              next B;
6057            } else {            } else {
6058              !!!cp ('t308');              !!!cp ('t308');
6059              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6060              !!!next-token;              !!!next-token;
6061              redo B;              next B;
6062            }            }
6063          } else {          } else {
6064            !!!cp ('t309');            !!!cp ('t309');
6065            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6066    
6067            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6068            ## reprocess            ## reprocess
6069            redo B;            next B;
6070          }          }
6071          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6072            !!!cp ('t309.2');
6073            ## Stop parsing
6074            last B;
6075        } else {        } else {
6076          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6077        }        }
# Line 5165  sub _tree_construction_main ($) { Line 6083  sub _tree_construction_main ($) {
6083            unless (length $token->{data}) {            unless (length $token->{data}) {
6084              !!!cp ('t310');              !!!cp ('t310');
6085              !!!next-token;              !!!next-token;
6086              redo B;              next B;
6087            }            }
6088          }          }
6089                    
6090          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6091            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6092              !!!cp ('t311');              !!!cp ('t311');
6093              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
6094            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6095              !!!cp ('t312');              !!!cp ('t312');
6096              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6097            } else { # "after html frameset"            } else { # "after html frameset"
6098              !!!cp ('t313');              !!!cp ('t313');
6099              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
6100    
6101              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6102              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6103              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6104            }            }
6105                        
6106            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 6111  sub _tree_construction_main ($) {
6111              !!!cp ('t315');              !!!cp ('t315');
6112              !!!next-token;              !!!next-token;
6113            }            }
6114            redo B;            next B;
6115          }          }
6116                    
6117          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6118        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6119          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6120            !!!cp ('t316');            !!!cp ('t316');
6121            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6122    
6123            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6124            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 6129  sub _tree_construction_main ($) {
6129          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6130              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6131            !!!cp ('t318');            !!!cp ('t318');
6132            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6133              !!!nack ('t318.1');
6134            !!!next-token;            !!!next-token;
6135            redo B;            next B;
6136          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6137                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6138            !!!cp ('t319');            !!!cp ('t319');
6139            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6140            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6141              !!!ack ('t319.1');
6142            !!!next-token;            !!!next-token;
6143            redo B;            next B;
6144          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6145            !!!cp ('t320');            !!!cp ('t320');
6146            ## NOTE: As if in body.            ## NOTE: As if in body.
6147            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6148            redo B;            next B;
6149          } else {          } else {
6150            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6151              !!!cp ('t321');              !!!cp ('t321');
6152              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6153            } else {            } else {
6154              !!!cp ('t322');              !!!cp ('t322');
6155              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6156            }            }
6157            ## Ignore the token            ## Ignore the token
6158              !!!nack ('t322.1');
6159            !!!next-token;            !!!next-token;
6160            redo B;            next B;
6161          }          }
6162        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6163          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6164            !!!cp ('t323');            !!!cp ('t323');
6165            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6166    
6167            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6168            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5251  sub _tree_construction_main ($) { Line 6172  sub _tree_construction_main ($) {
6172    
6173          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6174              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6175            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6176                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6177              !!!cp ('t325');              !!!cp ('t325');
6178              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6179              ## Ignore the token              ## Ignore the token
6180              !!!next-token;              !!!next-token;
6181            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 6185  sub _tree_construction_main ($) {
6185            }            }
6186    
6187            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6188                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6189              !!!cp ('t327');              !!!cp ('t327');
6190              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6191            } else {            } else {
6192              !!!cp ('t328');              !!!cp ('t328');
6193            }            }
6194            redo B;            next B;
6195          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6196                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6197            !!!cp ('t329');            !!!cp ('t329');
6198            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6199            !!!next-token;            !!!next-token;
6200            redo B;            next B;
6201          } else {          } else {
6202            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6203              !!!cp ('t330');              !!!cp ('t330');
6204              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6205            } else {            } else {
6206              !!!cp ('t331');              !!!cp ('t331');
6207              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6208            }            }
6209            ## Ignore the token            ## Ignore the token
6210            !!!next-token;            !!!next-token;
6211            redo B;            next B;
6212          }          }
6213          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6214            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6215                    @{$self->{open_elements}} == 1) { # redundant, maybe
6216              !!!cp ('t331.1');
6217              !!!parse-error (type => 'in body:#eof', token => $token);
6218            } else {
6219              !!!cp ('t331.2');
6220            }
6221            
6222            ## Stop parsing
6223            last B;
6224        } else {        } else {
6225          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6226        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6235  sub _tree_construction_main ($) {
6235        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6236          !!!cp ('t332');          !!!cp ('t332');
6237          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6238          $script_start_tag->($insert);          $script_start_tag->();
6239          redo B;          next B;
6240        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6241          !!!cp ('t333');          !!!cp ('t333');
6242          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6243          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6244          redo B;          next B;
6245        } elsif ({        } elsif ({
6246                  base => 1, link => 1,                  base => 1, link => 1,
6247                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6248          !!!cp ('t334');          !!!cp ('t334');
6249          ## 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
6250          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6251          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6252            !!!ack ('t334.1');
6253          !!!next-token;          !!!next-token;
6254          redo B;          next B;
6255        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6256          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6257          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6258          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.
6259    
6260          unless ($self->{confident}) {          unless ($self->{confident}) {
6261            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6262              !!!cp ('t335');              !!!cp ('t335');
6263                ## NOTE: Whether the encoding is supported or not is handled
6264                ## in the {change_encoding} callback.
6265              $self->{change_encoding}              $self->{change_encoding}
6266                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6267                            
6268              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6269                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6270                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6271                                           ->{has_reference});                                           ->{has_reference});
6272            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6273              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6274                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6275                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6276                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6277                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6278                !!!cp ('t336');                !!!cp ('t336');
6279                  ## NOTE: Whether the encoding is supported or not is handled
6280                  ## in the {change_encoding} callback.
6281                $self->{change_encoding}                $self->{change_encoding}
6282                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6283                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6284                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6285                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6303  sub _tree_construction_main ($) {
6303            }            }
6304          }          }
6305    
6306            !!!ack ('t338.1');
6307          !!!next-token;          !!!next-token;
6308          redo B;          next B;
6309        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6310          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6311          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6312          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6313            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6314        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6315          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6316                                
6317          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6318              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6319            !!!cp ('t342');            !!!cp ('t342');
6320            ## Ignore the token            ## Ignore the token
6321          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6329  sub _tree_construction_main ($) {
6329              }              }
6330            }            }
6331          }          }
6332            !!!nack ('t343.1');
6333          !!!next-token;          !!!next-token;
6334          redo B;          next B;
6335        } elsif ({        } elsif ({
6336                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6337                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6338                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6339                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6340                  pre => 1,                  pre => 1, listing => 1,
6341                    form => 1,
6342                    table => 1,
6343                    hr => 1,
6344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6345            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6346              !!!cp ('t350');
6347              !!!parse-error (type => 'in form:form', token => $token);
6348              ## Ignore the token
6349              !!!nack ('t350.1');
6350              !!!next-token;
6351              next B;
6352            }
6353    
6354          ## has a p element in scope          ## has a p element in scope
6355          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6356            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6357              !!!cp ('t344');              !!!cp ('t344');
6358              !!!back-token;              !!!back-token; # <form>
6359              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6360              redo B;                        line => $token->{line}, column => $token->{column}};
6361            } elsif ({              next B;
6362                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6363              !!!cp ('t345');              !!!cp ('t345');
6364              last INSCOPE;              last INSCOPE;
6365            }            }
6366          } # INSCOPE          } # INSCOPE
6367                        
6368          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6369          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6370              !!!nack ('t346.1');
6371            !!!next-token;            !!!next-token;
6372            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6373              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6380  sub _tree_construction_main ($) {
6380            } else {            } else {
6381              !!!cp ('t348');              !!!cp ('t348');
6382            }            }
6383          } else {          } elsif ($token->{tag_name} eq 'form') {
6384            !!!cp ('t347');            !!!cp ('t347.1');
6385              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6386    
6387              !!!nack ('t347.2');
6388            !!!next-token;            !!!next-token;
6389          }          } elsif ($token->{tag_name} eq 'table') {
6390          redo B;            !!!cp ('t382');
6391        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6392          if (defined $self->{form_element}) {            
6393            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6394            !!!parse-error (type => 'in form:form');  
6395            ## Ignore the token            !!!nack ('t382.1');
6396              !!!next-token;
6397            } elsif ($token->{tag_name} eq 'hr') {
6398              !!!cp ('t386');
6399              pop @{$self->{open_elements}};
6400            
6401              !!!nack ('t386.1');
6402            !!!next-token;            !!!next-token;
           redo B;  
6403          } else {          } else {
6404            ## 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];  
6405            !!!next-token;            !!!next-token;
           redo B;  
6406          }          }
6407        } elsif ($token->{tag_name} eq 'li') {          next B;
6408          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6409          ## has a p element in scope          ## has a p element in scope
6410          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6411            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6412              !!!cp ('t353');              !!!cp ('t353');
6413              !!!back-token;              !!!back-token; # <x>
6414              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6415              redo B;                        line => $token->{line}, column => $token->{column}};
6416            } elsif ({              next B;
6417                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6418              !!!cp ('t354');              !!!cp ('t354');
6419              last INSCOPE;              last INSCOPE;
6420            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6423  sub _tree_construction_main ($) {
6423          ## Step 1          ## Step 1
6424          my $i = -1;          my $i = -1;
6425          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6426            my $li_or_dtdd = {li => {li => 1},
6427                              dt => {dt => 1, dd => 1},
6428                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6429          LI: {          LI: {
6430            ## Step 2            ## Step 2
6431            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6432              if ($i != -1) {              if ($i != -1) {
6433                !!!cp ('t355');                !!!cp ('t355');
6434                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6435                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6436                                      ->manakai_local_name,
6437                                  token => $token);
6438              } else {              } else {
6439                !!!cp ('t356');                !!!cp ('t356');
6440              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6445  sub _tree_construction_main ($) {
6445            }            }
6446                        
6447            ## Step 3            ## Step 3
6448            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6449                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6450                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6451                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6452                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6453                  not ($node->[1] & DIV_EL)) {
6454              !!!cp ('t358');              !!!cp ('t358');
6455              last LI;              last LI;
6456            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6462  sub _tree_construction_main ($) {
6462            redo LI;            redo LI;
6463          } # LI          } # LI
6464                        
6465          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6466          !!!next-token;          !!!nack ('t359.1');
         redo 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});  
6467          !!!next-token;          !!!next-token;
6468          redo B;          next B;
6469        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6470          ## has a p element in scope          ## has a p element in scope
6471          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6472            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6473              !!!cp ('t367');              !!!cp ('t367');
6474              !!!back-token;              !!!back-token; # <plaintext>
6475              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6476              redo B;                        line => $token->{line}, column => $token->{column}};
6477            } elsif ({              next B;
6478                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6479              !!!cp ('t368');              !!!cp ('t368');
6480              last INSCOPE;              last INSCOPE;
6481            }            }
6482          } # INSCOPE          } # INSCOPE
6483                        
6484          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6485                        
6486          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6487                        
6488            !!!nack ('t368.1');
6489          !!!next-token;          !!!next-token;
6490          redo B;          next B;
6491        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6492          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6493            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6494            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6495              !!!cp ('t371');              !!!cp ('t371');
6496              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6497                            
6498              !!!back-token;              !!!back-token; # <a>
6499              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6500              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6501                $formatting_end_tag->($token);
6502                            
6503              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6504                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 6523  sub _tree_construction_main ($) {
6523                        
6524          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6525    
6526          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6527          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6528    
6529            !!!nack ('t374.1');
6530          !!!next-token;          !!!next-token;
6531          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;  
6532        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6533          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6534    
6535          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6536          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6537            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6538            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6539              !!!cp ('t376');              !!!cp ('t376');
6540              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6541              !!!back-token;              !!!back-token; # <nobr>
6542              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6543              redo B;                        line => $token->{line}, column => $token->{column}};
6544            } elsif ({              next B;
6545                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6546              !!!cp ('t377');              !!!cp ('t377');
6547              last INSCOPE;              last INSCOPE;
6548            }            }
6549          } # INSCOPE          } # INSCOPE
6550                    
6551          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6552          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6553                    
6554            !!!nack ('t377.1');
6555          !!!next-token;          !!!next-token;
6556          redo B;          next B;
6557        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6558          ## has a button element in scope          ## has a button element in scope
6559          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6560            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6561            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6562              !!!cp ('t378');              !!!cp ('t378');
6563              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6564              !!!back-token;              !!!back-token; # <button>
6565              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6566              redo B;                        line => $token->{line}, column => $token->{column}};
6567            } elsif ({              next B;
6568                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6569              !!!cp ('t379');              !!!cp ('t379');
6570              last INSCOPE;              last INSCOPE;
6571            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6573  sub _tree_construction_main ($) {
6573                        
6574          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6575                        
6576          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6577    
6578          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6579    
6580          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6581    
6582            !!!nack ('t379.1');
6583          !!!next-token;          !!!next-token;
6584          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, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6585        } elsif ({        } elsif ({
6586                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6587                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6588                  image => 1,                  noembed => 1,
6589                    noframes => 1,
6590                    noscript => 0, ## TODO: 1 if scripting is enabled
6591                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6592          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6593            !!!cp ('t384');            !!!cp ('t381');
6594            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6595          } else {          } else {
6596            !!!cp ('t385');            !!!cp ('t399');
6597          }          }
6598            ## NOTE: There is an "as if in body" code clone.
6599          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6600          $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;  
6601        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6602          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6603                    
6604          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6605            !!!cp ('t389');            !!!cp ('t389');
6606            ## Ignore the token            ## Ignore the token
6607              !!!nack ('t389'); ## NOTE: Not acknowledged.
6608            !!!next-token;            !!!next-token;
6609            redo B;            next B;
6610          } else {          } else {
6611            my $at = $token->{attributes};            my $at = $token->{attributes};
6612            my $form_attrs;            my $form_attrs;
# Line 5825  sub _tree_construction_main ($) { Line 6617  sub _tree_construction_main ($) {
6617            delete $at->{prompt};            delete $at->{prompt};
6618            my @tokens = (            my @tokens = (
6619                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6620                           attributes => $form_attrs},                           attributes => $form_attrs,
6621                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6622                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6623                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6624                            {type => START_TAG_TOKEN, tag_name => 'p',
6625                             line => $token->{line}, column => $token->{column}},
6626                            {type => START_TAG_TOKEN, tag_name => 'label',
6627                             line => $token->{line}, column => $token->{column}},
6628                         );                         );
6629            if ($prompt_attr) {            if ($prompt_attr) {
6630              !!!cp ('t390');              !!!cp ('t390');
6631              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6632                               #line => $token->{line}, column => $token->{column},
6633                              };
6634            } else {            } else {
6635              !!!cp ('t391');              !!!cp ('t391');
6636              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6637                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6638                               #line => $token->{line}, column => $token->{column},
6639                              }; # SHOULD
6640              ## TODO: make this configurable              ## TODO: make this configurable
6641            }            }
6642            push @tokens,            push @tokens,
6643                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6644                             line => $token->{line}, column => $token->{column}},
6645                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6646                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6647                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6648                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6649                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6650            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6651                             line => $token->{line}, column => $token->{column}},
6652                            {type => END_TAG_TOKEN, tag_name => 'form',
6653                             line => $token->{line}, column => $token->{column}};
6654              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6655            !!!back-token (@tokens);            !!!back-token (@tokens);
6656            redo B;            !!!next-token;
6657              next B;
6658          }          }
6659        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6660          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6661          my $el;          my $el;
6662          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6663                    
6664          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6665          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 6668  sub _tree_construction_main ($) {
6668          $insert->($el);          $insert->($el);
6669                    
6670          my $text = '';          my $text = '';
6671            !!!nack ('t392.1');
6672          !!!next-token;          !!!next-token;
6673          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6674            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 6699  sub _tree_construction_main ($) {
6699            ## Ignore the token            ## Ignore the token
6700          } else {          } else {
6701            !!!cp ('t398');            !!!cp ('t398');
6702            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6703          }          }
6704          !!!next-token;          !!!next-token;
6705          redo B;          next B;
6706        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6707                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6708          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6709    
6710          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6711    
6712            ## "adjust foreign attributes" - done in insert-element-f
6713            
6714            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6715                    
6716          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6717              pop @{$self->{open_elements}};
6718              !!!ack ('t398.1');
6719            } else {
6720              !!!cp ('t398.2');
6721              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6722              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6723              ## mode, "in body" (not "in foreign content") secondary insertion
6724              ## mode, maybe.
6725            }
6726    
6727          !!!next-token;          !!!next-token;
6728          redo B;          next B;
6729        } elsif ({        } elsif ({
6730                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6731                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 6733  sub _tree_construction_main ($) {
6733                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6734                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6735          !!!cp ('t401');          !!!cp ('t401');
6736          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6737          ## Ignore the token          ## Ignore the token
6738            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6739          !!!next-token;          !!!next-token;
6740          redo B;          next B;
6741                    
6742          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6743        } else {        } else {
6744          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6745              !!!cp ('t384');
6746              !!!parse-error (type => 'image', token => $token);
6747              $token->{tag_name} = 'img';
6748            } else {
6749              !!!cp ('t385');
6750            }
6751    
6752            ## NOTE: There is an "as if <br>" code clone.
6753          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6754                    
6755          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6756    
6757            if ({
6758                 applet => 1, marquee => 1, object => 1,
6759                }->{$token->{tag_name}}) {
6760              !!!cp ('t380');
6761              push @$active_formatting_elements, ['#marker', ''];
6762              !!!nack ('t380.1');
6763            } elsif ({
6764                      b => 1, big => 1, em => 1, font => 1, i => 1,
6765                      s => 1, small => 1, strile => 1,
6766                      strong => 1, tt => 1, u => 1,
6767                     }->{$token->{tag_name}}) {
6768              !!!cp ('t375');
6769              push @$active_formatting_elements, $self->{open_elements}->[-1];
6770              !!!nack ('t375.1');
6771            } elsif ($token->{tag_name} eq 'input') {
6772              !!!cp ('t388');
6773              ## TODO: associate with $self->{form_element} if defined
6774              pop @{$self->{open_elements}};
6775              !!!ack ('t388.2');
6776            } elsif ({
6777                      area => 1, basefont => 1, bgsound => 1, br => 1,
6778                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6779                      #image => 1,
6780                     }->{$token->{tag_name}}) {
6781              !!!cp ('t388.1');
6782              pop @{$self->{open_elements}};
6783              !!!ack ('t388.3');
6784            } elsif ($token->{tag_name} eq 'select') {
6785              ## TODO: associate with $self->{form_element} if defined
6786            
6787              if ($self->{insertion_mode} & TABLE_IMS or
6788                  $self->{insertion_mode} & BODY_TABLE_IMS or
6789                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6790                !!!cp ('t400.1');
6791                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6792              } else {
6793                !!!cp ('t400.2');
6794                $self->{insertion_mode} = IN_SELECT_IM;
6795              }
6796              !!!nack ('t400.3');
6797            } else {
6798              !!!nack ('t402');
6799            }
6800                    
6801          !!!next-token;          !!!next-token;
6802          redo B;          next B;
6803        }        }
6804      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6805        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6806          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6807              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6808            for (@{$self->{open_elements}}) {          INSCOPE: {
6809              unless ({            for (reverse @{$self->{open_elements}}) {
6810                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6811                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6812                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6813                      }->{$_->[1]}) {                last INSCOPE;
6814                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6815                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6816              } else {                last;
               !!!cp ('t404');  
6817              }              }
6818            }            }
6819    
6820            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6821            !!!next-token;                            value => $token->{tag_name}, token => $token);
6822            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6823            !!!next-token;            !!!next-token;
6824            redo B;            next B;
6825            } # INSCOPE
6826    
6827            for (@{$self->{open_elements}}) {
6828              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6829                !!!cp ('t403');
6830                !!!parse-error (type => 'not closed',
6831                                value => $_->[0]->manakai_local_name,
6832                                token => $token);
6833                last;
6834              } else {
6835                !!!cp ('t404');
6836              }
6837          }          }
6838    
6839            $self->{insertion_mode} = AFTER_BODY_IM;
6840            !!!next-token;
6841            next B;
6842        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6843          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
6844            ## up-to-date, though it has same effect as speced.
6845            if (@{$self->{open_elements}} > 1 and
6846                $self->{open_elements}->[1]->[1] & BODY_EL) {
6847            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6848            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6849              !!!cp ('t406');              !!!cp ('t406');
6850              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6851                                value => $self->{open_elements}->[1]->[0]
6852                                    ->manakai_local_name,
6853                                token => $token);
6854            } else {            } else {
6855              !!!cp ('t407');              !!!cp ('t407');
6856            }            }
6857            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6858            ## reprocess            ## reprocess
6859            redo B;            next B;
6860          } else {          } else {
6861            !!!cp ('t408');            !!!cp ('t408');
6862            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6863            ## Ignore the token            ## Ignore the token
6864            !!!next-token;            !!!next-token;
6865            redo B;            next B;
6866          }          }
6867        } elsif ({        } elsif ({
6868                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6869                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6870                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6871                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6872                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6873                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6874          ## has an element in scope          ## has an element in scope
6875          my $i;          my $i;
6876          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6877            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6878            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6879              !!!cp ('t410');              !!!cp ('t410');
6880              $i = $_;              $i = $_;
6881              last INSCOPE;              last INSCOPE;
6882            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6883              !!!cp ('t411');              !!!cp ('t411');
6884              last INSCOPE;              last INSCOPE;
6885            }            }
# Line 6011  sub _tree_construction_main ($) { Line 6887  sub _tree_construction_main ($) {
6887    
6888          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6889            !!!cp ('t413');            !!!cp ('t413');
6890            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6891          } else {          } else {
6892            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6893            while ({            while ({
# Line 6019  sub _tree_construction_main ($) { Line 6895  sub _tree_construction_main ($) {
6895                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6896                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6897                    p => 1,                    p => 1,
6898                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6899              !!!cp ('t409');              !!!cp ('t409');
6900              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6901            }            }
6902    
6903            ## Step 2.            ## Step 2.
6904            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6905                      ne $token->{tag_name}) {
6906              !!!cp ('t412');              !!!cp ('t412');
6907              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6908                                value => $self->{open_elements}->[-1]->[0]
6909                                    ->manakai_local_name,
6910                                token => $token);
6911            } else {            } else {
6912              !!!cp ('t414');              !!!cp ('t414');
6913            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6918  sub _tree_construction_main ($) {
6918            ## Step 4.            ## Step 4.
6919            $clear_up_to_marker->()            $clear_up_to_marker->()
6920                if {                if {
6921                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6922                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6923          }          }
6924          !!!next-token;          !!!next-token;
6925          redo B;          next B;
6926        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6927            undef $self->{form_element};
6928    
6929          ## has an element in scope          ## has an element in scope
6930            my $i;
6931          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6932            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6933            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
6934              !!!cp ('t418');              !!!cp ('t418');
6935                $i = $_;
6936              last INSCOPE;              last INSCOPE;
6937            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6938              !!!cp ('t419');              !!!cp ('t419');
6939              last INSCOPE;              last INSCOPE;
6940            }            }
6941          } # INSCOPE          } # INSCOPE
6942            
6943          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6944            !!!cp ('t421');            !!!cp ('t421');
6945            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6946            } else {
6947              ## Step 1. generate implied end tags
6948              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6949                !!!cp ('t417');
6950                pop @{$self->{open_elements}};
6951              }
6952              
6953              ## Step 2.
6954              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6955                      ne $token->{tag_name}) {
6956                !!!cp ('t417.1');
6957                !!!parse-error (type => 'not closed',
6958                                value => $self->{open_elements}->[-1]->[0]
6959                                    ->manakai_local_name,
6960                                token => $token);
6961              } else {
6962                !!!cp ('t420');
6963              }  
6964              
6965              ## Step 3.
6966              splice @{$self->{open_elements}}, $i;
6967          }          }
6968    
         undef $self->{form_element};  
6969          !!!next-token;          !!!next-token;
6970          redo B;          next B;
6971        } elsif ({        } elsif ({
6972                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6973                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6085  sub _tree_construction_main ($) { Line 6975  sub _tree_construction_main ($) {
6975          my $i;          my $i;
6976          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6977            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6978            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
6979              !!!cp ('t423');              !!!cp ('t423');
6980              $i = $_;              $i = $_;
6981              last INSCOPE;              last INSCOPE;
6982            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6983              !!!cp ('t424');              !!!cp ('t424');
6984              last INSCOPE;              last INSCOPE;
6985            }            }
6986          } # INSCOPE          } # INSCOPE
6987            
6988          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6989            !!!cp ('t425');            !!!cp ('t425.1');
6990            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6991          } else {          } else {
6992            !!!cp ('t426');            ## Step 1. generate implied end tags
6993              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6994                !!!cp ('t422');
6995                pop @{$self->{open_elements}};
6996              }
6997              
6998              ## Step 2.
6999              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7000                      ne $token->{tag_name}) {
7001                !!!cp ('t425');
7002                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7003              } else {
7004                !!!cp ('t426');
7005              }
7006    
7007              ## Step 3.
7008              splice @{$self->{open_elements}}, $i;
7009          }          }
7010                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7014          ## has an element in scope          ## has an element in scope
7015          my $i;          my $i;
7016          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7017            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7018            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7019              !!!cp ('t410.1');              !!!cp ('t410.1');
7020              $i = $_;              $i = $_;
7021              last INSCOPE;              last INSCOPE;
7022            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7023              !!!cp ('t411.1');              !!!cp ('t411.1');
7024              last INSCOPE;              last INSCOPE;
7025            }            }
7026          } # INSCOPE          } # INSCOPE
7027    
7028          if (defined $i) {          if (defined $i) {
7029            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7030                      ne $token->{tag_name}) {
7031              !!!cp ('t412.1');              !!!cp ('t412.1');
7032              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7033                                value => $self->{open_elements}->[-1]->[0]
7034                                    ->manakai_local_name,
7035                                token => $token);
7036            } else {            } else {
7037              !!!cp ('t414.1');              !!!cp ('t414.1');
7038            }            }
# Line 6147  sub _tree_construction_main ($) { Line 7040  sub _tree_construction_main ($) {
7040            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7041          } else {          } else {
7042            !!!cp ('t413.1');            !!!cp ('t413.1');
7043            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044    
7045            !!!cp ('t415.1');            !!!cp ('t415.1');
7046            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7047            my $el;            my $el;
7048            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7049            $insert->($el);            $insert->($el);
7050            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7051          }          }
7052    
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055        } elsif ({        } elsif ({
7056                  a => 1,                  a => 1,
7057                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6166  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7060                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7061          !!!cp ('t427');          !!!cp ('t427');
7062          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7063          redo B;          next B;
7064        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7065          !!!cp ('t428');          !!!cp ('t428');
7066          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
7067    
7068          ## As if <br>          ## As if <br>
7069          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7070                    
7071          my $el;          my $el;
7072          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7073          $insert->($el);          $insert->($el);
7074                    
7075          ## Ignore the token.          ## Ignore the token.
7076          !!!next-token;          !!!next-token;
7077          redo B;          next B;
7078        } elsif ({        } elsif ({
7079                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7080                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6195  sub _tree_construction_main ($) { Line 7088  sub _tree_construction_main ($) {
7088                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7089                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7090          !!!cp ('t429');          !!!cp ('t429');
7091          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7092          ## Ignore the token          ## Ignore the token
7093          !!!next-token;          !!!next-token;
7094          redo B;          next B;
7095                    
7096          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7097                    
# Line 6209  sub _tree_construction_main ($) { Line 7102  sub _tree_construction_main ($) {
7102    
7103          ## Step 2          ## Step 2
7104          S2: {          S2: {
7105            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7106              ## Step 1              ## Step 1
7107              ## generate implied end tags              ## generate implied end tags
7108              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7109                !!!cp ('t430');                !!!cp ('t430');
7110                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7111                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7112              }              }
7113                    
7114              ## Step 2              ## Step 2
7115              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7116                        ne $token->{tag_name}) {
7117                !!!cp ('t431');                !!!cp ('t431');
7118                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7119                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7120                                  value => $self->{open_elements}->[-1]->[0]
7121                                      ->manakai_local_name,
7122                                  token => $token);
7123              } else {              } else {
7124                !!!cp ('t432');                !!!cp ('t432');
7125              }              }
# Line 6236  sub _tree_construction_main ($) { Line 7131  sub _tree_construction_main ($) {
7131              last S2;              last S2;
7132            } else {            } else {
7133              ## Step 3              ## Step 3
7134              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7135                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7136                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7137                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7138                !!!cp ('t433');                !!!cp ('t433');
7139                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7140                ## Ignore the token                ## Ignore the token
7141                !!!next-token;                !!!next-token;
7142                last S2;                last S2;
# Line 6257  sub _tree_construction_main ($) { Line 7152  sub _tree_construction_main ($) {
7152            ## Step 5;            ## Step 5;
7153            redo S2;            redo S2;
7154          } # S2          } # S2
7155          redo B;          next B;
7156        }        }
7157      }      }
7158      redo B;      next B;
7159      } continue { # B
7160        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7161          ## NOTE: The code below is executed in cases where it does not have
7162          ## to be, but it it is harmless even in those cases.
7163          ## has an element in scope
7164          INSCOPE: {
7165            for (reverse 0..$#{$self->{open_elements}}) {
7166              my $node = $self->{open_elements}->[$_];
7167              if ($node->[1] & FOREIGN_EL) {
7168                last INSCOPE;
7169              } elsif ($node->[1] & SCOPING_EL) {
7170                last;
7171              }
7172            }
7173            
7174            ## NOTE: No foreign element in scope.
7175            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7176          } # INSCOPE
7177        }
7178    } # B    } # B
7179    
7180    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6306  sub set_inner_html ($$$) { Line 7220  sub set_inner_html ($$$) {
7220    
7221      ## Step 8 # MUST      ## Step 8 # MUST
7222      my $i = 0;      my $i = 0;
7223      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7224      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7225      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7226        my $self = shift;        my $self = shift;
7227    
# Line 6316  sub set_inner_html ($$$) { Line 7230  sub set_inner_html ($$$) {
7230    
7231        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7232        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7233        $column++;  
7234          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7235          $p->{column}++;
7236    
7237        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7238          $line++;          $p->{line}++;
7239          $column = 0;          $p->{column} = 0;
7240          !!!cp ('i1');          !!!cp ('i1');
7241        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7242          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7243          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7244          $line++;          $p->{line}++;
7245          $column = 0;          $p->{column} = 0;
7246          !!!cp ('i2');          !!!cp ('i2');
7247        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7248          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6335  sub set_inner_html ($$$) { Line 7251  sub set_inner_html ($$$) {
7251          !!!cp ('i4');          !!!cp ('i4');
7252          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7253          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7254          } elsif ($self->{next_char} <= 0x0008 or
7255                   (0x000E <= $self->{next_char} and
7256                    $self->{next_char} <= 0x001F) or
7257                   (0x007F <= $self->{next_char} and
7258                    $self->{next_char} <= 0x009F) or
7259                   (0xD800 <= $self->{next_char} and
7260                    $self->{next_char} <= 0xDFFF) or
7261                   (0xFDD0 <= $self->{next_char} and
7262                    $self->{next_char} <= 0xFDDF) or
7263                   {
7264                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7265                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7266                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7267                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7268                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7269                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7270                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7271                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7272                    0x10FFFE => 1, 0x10FFFF => 1,
7273                   }->{$self->{next_char}}) {
7274            !!!cp ('i4.1');
7275            !!!parse-error (type => 'control char', level => $self->{must_level});
7276    ## TODO: error type documentation
7277        }        }
7278      };      };
7279      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6342  sub set_inner_html ($$$) { Line 7281  sub set_inner_html ($$$) {
7281            
7282      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7283        my (%opt) = @_;        my (%opt) = @_;
7284        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7285          my $column = $opt{column};
7286          if (defined $opt{token} and defined $opt{token}->{line}) {
7287            $line = $opt{token}->{line};
7288            $column = $opt{token}->{column};
7289          }
7290          warn "Parse error ($opt{type}) at line $line column $column\n";
7291      };      };
7292      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7293        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7294      };      };
7295            
7296      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6369  sub set_inner_html ($$$) { Line 7314  sub set_inner_html ($$$) {
7314          unless defined $p->{content_model};          unless defined $p->{content_model};
7315          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7316    
7317      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7318          ## TODO: Foreign element OK?
7319    
7320      ## Step 3      ## Step 3
7321      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6379  sub set_inner_html ($$$) { Line 7325  sub set_inner_html ($$$) {
7325      $doc->append_child ($root);      $doc->append_child ($root);
7326    
7327      ## Step 5 # MUST      ## Step 5 # MUST
7328      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7329    
7330      undef $p->{head_element};      undef $p->{head_element};
7331    
# Line 6425  sub set_inner_html ($$$) { Line 7371  sub set_inner_html ($$$) {
7371      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7372    
7373      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7374    
7375        delete $p->{parse_error}; # delete loop
7376    } else {    } else {
7377      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";
7378    }    }

Legend:
Removed from v.1.91  
changed lines
  Added in v.1.137

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24