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

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

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

revision 1.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC revision 1.136 by wakaba, Sat May 17 12:29:24 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 = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
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      my $token = shift;    };
343      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
344      ## TODO: normalize charset name  
345      ## HTML5 encoding sniffing algorithm
346      require Message::Charset::Info;
347      my $charset;
348      my $buffer;
349      my ($char_stream, $e_status);
350    
351      ## "Change the encoding" algorithm:    SNIFFING: {
352    
353      ## Step 1          ## Step 1
354      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
355        $charset = 'utf-8';        $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', token => $token);      ## 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        if ($opt{octets}) {
518          ${$opt{octets}} = "\x{FFFD}"; # relacement character
519        }
520      };
521      $char_stream->onerror ($char_onerror);
522    
523    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
524    my $return;    my $return;
525    try {    try {
526      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
527    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
528      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
529      $s = \ (Encode::decode ($charset, $$bytes_s));      
530      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
531        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
532          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
533                          value => $self->{input_encoding},
534                          level => $self->{unsupported_level},
535                          line => 1, column => 1);
536        } elsif (not ($e_status &
537                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
538          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
539                          value => $self->{input_encoding},
540                          level => $self->{unsupported_level},
541                          line => 1, column => 1);
542        }
543      $self->{confident} = 1;      $self->{confident} = 1;
544      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
545        $return = $self->parse_char_stream ($char_stream, @args);
546    };    };
547    return $return;    return $return;
548  } # parse_byte_string  } # parse_byte_string
# Line 163  sub parse_byte_string ($$$$;$) { Line 556  sub parse_byte_string ($$$$;$) {
556  ## 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
557  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
558    
559  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
560      my $self = shift;
561      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
562      return $self->parse_char_stream ($input, @_[1..$#_]);
563    } # parse_char_string
564    *parse_string = \&parse_char_string;
565    
566  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
567    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
568    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
569    $self->{document} = $_[1];    $self->{document} = $_[1];
570    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
571    
# Line 186  sub parse_string ($$$;$) { Line 584  sub parse_string ($$$;$) {
584      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
585      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
586    
587      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = $input->getc;
588      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
589        $self->{next_char} = ord $char;
590    
591      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
592          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
593      $self->{column}++;      $self->{column}++;
594            
595      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
596          !!!cp ('j1');
597        $self->{line}++;        $self->{line}++;
598        $self->{column} = 0;        $self->{column} = 0;
599      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
600        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
601          my $next = $input->getc;
602          if ($next ne "\x0A") {
603            $input->ungetc ($next);
604          }
605        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
606        $self->{line}++;        $self->{line}++;
607        $self->{column} = 0;        $self->{column} = 0;
608      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
609          !!!cp ('j3');
610        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
611      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
612          !!!cp ('j4');
613        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
614        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
615        } elsif ($self->{next_char} <= 0x0008 or
616                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
617                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
618                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
619                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
620                 {
621                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
622                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
623                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
624                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
625                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
626                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
627                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
628                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
629                  0x10FFFE => 1, 0x10FFFF => 1,
630                 }->{$self->{next_char}}) {
631          !!!cp ('j5');
632          !!!parse-error (type => 'control char', level => $self->{must_level});
633    ## TODO: error type documentation
634      }      }
635    };    };
636    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 654  sub parse_string ($$$;$) {
654    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
655    
656    return $self->{document};    return $self->{document};
657  } # parse_string  } # parse_char_stream
658    
659  sub new ($) {  sub new ($) {
660    my $class = shift;    my $class = shift;
661    my $self = bless {}, $class;    my $self = bless {
662        must_level => 'm',
663        should_level => 's',
664        good_level => 'w',
665        warn_level => 'w',
666        info_level => 'i',
667        unsupported_level => 'u',
668      }, $class;
669    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
670      $self->{next_char} = -1;      $self->{next_char} = -1;
671    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 727  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
727  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
728  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
729  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
730    sub SELF_CLOSING_START_TAG_STATE () { 34 }
731    sub CDATA_BLOCK_STATE () { 35 }
732    
733  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
734  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 746  sub ROW_IMS ()        { 0b10000000 }
746  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
747  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
748  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
749    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
750        ## NOTE: "in foreign content" insertion mode is special; it is combined
751        ## with the secondary insertion mode.  In this parser, they are stored
752        ## together in the bit-or'ed form.
753    
754  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
755    
# Line 348  sub _initialize_tokenizer ($) { Line 786  sub _initialize_tokenizer ($) {
786    undef $self->{current_attribute};    undef $self->{current_attribute};
787    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
788    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
789      delete $self->{self_closing};
790    $self->{char} = [];    $self->{char} = [];
791    # $self->{next_char}    # $self->{next_char}
792    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 807  sub _initialize_tokenizer ($) {
807  ##        ->{value}  ##        ->{value}
808  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
809  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
810    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
811    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
812    ##     while the token is pushed back to the stack.
813    
814    ## ISSUE: "When a DOCTYPE token is created, its
815    ## <i>self-closing flag</i> must be unset (its other state is that it
816    ## be set), and its attributes list must be empty.": Wrong subject?
817    
818  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
819    
# Line 394  sub _initialize_tokenizer ($) { Line 840  sub _initialize_tokenizer ($) {
840    
841  sub _get_next_token ($) {  sub _get_next_token ($) {
842    my $self = shift;    my $self = shift;
843    
844      if ($self->{self_closing}) {
845        !!!parse-error (type => 'nestc', token => $self->{current_token});
846        ## NOTE: The |self_closing| flag is only set by start tag token.
847        ## In addition, when a start tag token is emitted, it is always set to
848        ## |current_token|.
849        delete $self->{self_closing};
850      }
851    
852    if (@{$self->{token}}) {    if (@{$self->{token}}) {
853        $self->{self_closing} = $self->{token}->[0]->{self_closing};
854      return shift @{$self->{token}};      return shift @{$self->{token}};
855    }    }
856    
# Line 466  sub _get_next_token ($) { Line 922  sub _get_next_token ($) {
922        # Anything else        # Anything else
923        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
924                     data => chr $self->{next_char},                     data => chr $self->{next_char},
925                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
926                      };
927        ## Stay in the data state        ## Stay in the data state
928        !!!next-input-character;        !!!next-input-character;
929    
# Line 486  sub _get_next_token ($) { Line 943  sub _get_next_token ($) {
943        unless (defined $token) {        unless (defined $token) {
944          !!!cp (13);          !!!cp (13);
945          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
946                    line => $l, column => $c});                    line => $l, column => $c,
947                     });
948        } else {        } else {
949          !!!cp (14);          !!!cp (14);
950          !!!emit ($token);          !!!emit ($token);
# Line 507  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965    
966            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
967                      line => $self->{line_prev},                      line => $self->{line_prev},
968                      column => $self->{column_prev}});                      column => $self->{column_prev},
969                       });
970    
971            redo A;            redo A;
972          }          }
# Line 553  sub _get_next_token ($) { Line 1012  sub _get_next_token ($) {
1012    
1013            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1014                      line => $self->{line_prev},                      line => $self->{line_prev},
1015                      column => $self->{column_prev}});                      column => $self->{column_prev},
1016                       });
1017    
1018            redo A;            redo A;
1019          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1024  sub _get_next_token ($) {
1024            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1025            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1026                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1027                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1028                                       };
1029            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1030            redo A;            redo A;
1031          } else {          } else {
1032            !!!cp (23);            !!!cp (23);
1033            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1034                              line => $self->{line_prev},
1035                              column => $self->{column_prev});
1036            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1037            ## reconsume            ## reconsume
1038    
1039            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1040                      line => $self->{line_prev},                      line => $self->{line_prev},
1041                      column => $self->{column_prev}});                      column => $self->{column_prev},
1042                       });
1043    
1044            redo A;            redo A;
1045          }          }
# Line 604  sub _get_next_token ($) { Line 1068  sub _get_next_token ($) {
1068                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1069    
1070                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1071                          line => $l, column => $c});                          line => $l, column => $c,
1072                           });
1073        
1074                redo A;                redo A;
1075              }              }
# Line 624  sub _get_next_token ($) { Line 1089  sub _get_next_token ($) {
1089              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1090              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1091              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1092                        line => $l, column => $c});                        line => $l, column => $c,
1093                         });
1094              redo A;              redo A;
1095            } else {            } else {
1096              !!!cp (27);              !!!cp (27);
# Line 638  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104            # next-input-character is already done            # next-input-character is already done
1105            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1106            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                      line => $l, column => $c});                      line => $l, column => $c,
1108                       });
1109            redo A;            redo A;
1110          }          }
1111        }        }
# Line 677  sub _get_next_token ($) { Line 1144  sub _get_next_token ($) {
1144          # reconsume          # reconsume
1145    
1146          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                    line => $l, column => $c});                    line => $l, column => $c,
1148                     });
1149    
1150          redo A;          redo A;
1151        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1154  sub _get_next_token ($) {
1154          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1155          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1156                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1157                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1158                                     };
1159          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1160          redo A;          redo A;
1161        }        }
# Line 703  sub _get_next_token ($) { Line 1172  sub _get_next_token ($) {
1172        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1173          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1174            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1175            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1176          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1177            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1203  sub _get_next_token ($) {
1203          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1204          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1205            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1206            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1207          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1208            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223    
1224          redo A;          redo A;
1225        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1226            !!!cp (42);
1227            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1228          !!!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  
1229          redo A;          redo A;
1230        } else {        } else {
1231          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1248  sub _get_next_token ($) {
1248        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1249          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1250            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1251            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1252          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1253            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1269  sub _get_next_token ($) {
1269        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1270                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1271          !!!cp (49);          !!!cp (49);
1272          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1273                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1274                   value => '',
1275                   line => $self->{line}, column => $self->{column}};
1276          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1277          !!!next-input-character;          !!!next-input-character;
1278          redo A;          redo A;
1279        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1280            !!!cp (50);
1281            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1282          !!!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  
1283          redo A;          redo A;
1284        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1285          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1286          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1287            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1288            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1289          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1290            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1314  sub _get_next_token ($) {
1314          } else {          } else {
1315            !!!cp (56);            !!!cp (56);
1316          }          }
1317          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1318                                value => ''};              = {name => chr ($self->{next_char}),
1319                   value => '',
1320                   line => $self->{line}, column => $self->{column}};
1321          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1328              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1329            !!!cp (57);            !!!cp (57);
1330            !!!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});
1331            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1332          } else {          } else {
1333            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1356  sub _get_next_token ($) {
1356          $before_leave->();          $before_leave->();
1357          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1380  sub _get_next_token ($) {
1380          !!!next-input-character;          !!!next-input-character;
1381          redo A;          redo A;
1382        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1383            !!!cp (64);
1384          $before_leave->();          $before_leave->();
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!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  
1387          redo A;          redo A;
1388        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          $before_leave->();          $before_leave->();
1391          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1441          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1442            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1443            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1444          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1445            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1462  sub _get_next_token ($) {
1462        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1463                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1464          !!!cp (76);          !!!cp (76);
1465          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1466                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1467                   value => '',
1468                   line => $self->{line}, column => $self->{column}};
1469          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1470          !!!next-input-character;          !!!next-input-character;
1471          redo A;          redo A;
1472        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1473            !!!cp (77);
1474            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1475          !!!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  
1476          redo A;          redo A;
1477        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1478          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1479          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1480            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1481            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1482          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1483            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1081  sub _get_next_token ($) { Line 1499  sub _get_next_token ($) {
1499          redo A;          redo A;
1500        } else {        } else {
1501          !!!cp (82);          !!!cp (82);
1502          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1503                                value => ''};              = {name => chr ($self->{next_char}),
1504                   value => '',
1505                   line => $self->{line}, column => $self->{column}};
1506          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;                  redo A;        
# Line 1115  sub _get_next_token ($) { Line 1535  sub _get_next_token ($) {
1535        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1536          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1537            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1538            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1539          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1540            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1558  sub _get_next_token ($) {
1558          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1559          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1560            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1561            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1562          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1563            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1605  sub _get_next_token ($) {
1605          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1606          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1607            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1608            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1609          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1610            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1233  sub _get_next_token ($) { Line 1647  sub _get_next_token ($) {
1647          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1648          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1649            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1650            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1651          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1652            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1280  sub _get_next_token ($) { Line 1692  sub _get_next_token ($) {
1692        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1693          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1694            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1696          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1697            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1305  sub _get_next_token ($) { Line 1715  sub _get_next_token ($) {
1715          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1716          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1717            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1718            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1719          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1720            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1785  sub _get_next_token ($) {
1785        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1786          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1787            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1789          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1790            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1805  sub _get_next_token ($) {
1805    
1806          redo A;          redo A;
1807        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1808            !!!cp (122);
1809            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1810          !!!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  
1811          redo A;          redo A;
1812        } else {        } else {
1813          !!!cp (124);          !!!cp ('124.1');
1814          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1815          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1816          ## reconsume          ## reconsume
1817          redo A;          redo A;
1818        }        }
1819        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1820          if ($self->{next_char} == 0x003E) { # >
1821            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1822              !!!cp ('124.2');
1823              !!!parse-error (type => 'nestc', token => $self->{current_token});
1824              ## TODO: Different type than slash in start tag
1825              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1826              if ($self->{current_token}->{attributes}) {
1827                !!!cp ('124.4');
1828                !!!parse-error (type => 'end tag attribute');
1829              } else {
1830                !!!cp ('124.5');
1831              }
1832              ## TODO: Test |<title></title/>|
1833            } else {
1834              !!!cp ('124.3');
1835              $self->{self_closing} = 1;
1836            }
1837    
1838            $self->{state} = DATA_STATE;
1839            !!!next-input-character;
1840    
1841            !!!emit ($self->{current_token}); # start tag or end tag
1842    
1843            redo A;
1844          } else {
1845            !!!cp ('124.4');
1846            !!!parse-error (type => 'nestc');
1847            ## TODO: This error type is wrong.
1848            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1849            ## Reconsume.
1850            redo A;
1851          }
1852      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1853        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1854                
# Line 1466  sub _get_next_token ($) { Line 1895  sub _get_next_token ($) {
1895          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1896            !!!cp (127);            !!!cp (127);
1897            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1898                                      line => $l, column => $c};                                      line => $l, column => $c,
1899                                       };
1900            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1901            !!!next-input-character;            !!!next-input-character;
1902            redo A;            redo A;
# Line 1504  sub _get_next_token ($) { Line 1934  sub _get_next_token ($) {
1934                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1935                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
1936                                                quirks => 1,                                                quirks => 1,
1937                                                line => $l, column => $c};                                                line => $l, column => $c,
1938                                                 };
1939                      !!!next-input-character;                      !!!next-input-character;
1940                      redo A;                      redo A;
1941                    } else {                    } else {
# Line 1525  sub _get_next_token ($) { Line 1956  sub _get_next_token ($) {
1956          } else {          } else {
1957            !!!cp (135);            !!!cp (135);
1958          }          }
1959          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1960                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1961                   $self->{next_char} == 0x005B) { # [
1962            !!!next-input-character;
1963            push @next_char, $self->{next_char};
1964            if ($self->{next_char} == 0x0043) { # C
1965              !!!next-input-character;
1966              push @next_char, $self->{next_char};
1967              if ($self->{next_char} == 0x0044) { # D
1968                !!!next-input-character;
1969                push @next_char, $self->{next_char};
1970                if ($self->{next_char} == 0x0041) { # A
1971                  !!!next-input-character;
1972                  push @next_char, $self->{next_char};
1973                  if ($self->{next_char} == 0x0054) { # T
1974                    !!!next-input-character;
1975                    push @next_char, $self->{next_char};
1976                    if ($self->{next_char} == 0x0041) { # A
1977                      !!!next-input-character;
1978                      push @next_char, $self->{next_char};
1979                      if ($self->{next_char} == 0x005B) { # [
1980                        !!!cp (135.1);
1981                        $self->{state} = CDATA_BLOCK_STATE;
1982                        !!!next-input-character;
1983                        redo A;
1984                      } else {
1985                        !!!cp (135.2);
1986                      }
1987                    } else {
1988                      !!!cp (135.3);
1989                    }
1990                  } else {
1991                    !!!cp (135.4);                
1992                  }
1993                } else {
1994                  !!!cp (135.5);
1995                }
1996              } else {
1997                !!!cp (135.6);
1998              }
1999            } else {
2000              !!!cp (135.7);
2001            }
2002        } else {        } else {
2003          !!!cp (136);          !!!cp (136);
2004        }        }
# Line 1534  sub _get_next_token ($) { Line 2008  sub _get_next_token ($) {
2008        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2009        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2010        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2011                                  line => $l, column => $c};                                  line => $l, column => $c,
2012                                   };
2013        redo A;        redo A;
2014                
2015        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 2248  sub _get_next_token ($) { Line 2723  sub _get_next_token ($) {
2723          !!!next-input-character;          !!!next-input-character;
2724          redo A;          redo A;
2725        }        }
2726        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2727          my $s = '';
2728          
2729          my ($l, $c) = ($self->{line}, $self->{column});
2730    
2731          CS: while ($self->{next_char} != -1) {
2732            if ($self->{next_char} == 0x005D) { # ]
2733              !!!next-input-character;
2734              if ($self->{next_char} == 0x005D) { # ]
2735                !!!next-input-character;
2736                MDC: {
2737                  if ($self->{next_char} == 0x003E) { # >
2738                    !!!cp (221.1);
2739                    !!!next-input-character;
2740                    last CS;
2741                  } elsif ($self->{next_char} == 0x005D) { # ]
2742                    !!!cp (221.2);
2743                    $s .= ']';
2744                    !!!next-input-character;
2745                    redo MDC;
2746                  } else {
2747                    !!!cp (221.3);
2748                    $s .= ']]';
2749                    #
2750                  }
2751                } # MDC
2752              } else {
2753                !!!cp (221.4);
2754                $s .= ']';
2755                #
2756              }
2757            } else {
2758              !!!cp (221.5);
2759              #
2760            }
2761            $s .= chr $self->{next_char};
2762            !!!next-input-character;
2763          } # CS
2764    
2765          $self->{state} = DATA_STATE;
2766          ## next-input-character done or EOF, which is reconsumed.
2767    
2768          if (length $s) {
2769            !!!cp (221.6);
2770            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2771                      line => $l, column => $c});
2772          } else {
2773            !!!cp (221.7);
2774          }
2775    
2776          redo A;
2777    
2778          ## ISSUE: "text tokens" in spec.
2779          ## TODO: Streaming support
2780      } else {      } else {
2781        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2782      }      }
# Line 2332  sub _tokenize_attempt_to_consume_an_enti Line 2861  sub _tokenize_attempt_to_consume_an_enti
2861          }          }
2862    
2863          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2864                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
2865                    line => $l, column => $c,
2866                   };
2867        } # X        } # X
2868      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2869               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2375  sub _tokenize_attempt_to_consume_an_enti Line 2906  sub _tokenize_attempt_to_consume_an_enti
2906        }        }
2907                
2908        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2909                line => $l, column => $c};                line => $l, column => $c,
2910                 };
2911      } else {      } else {
2912        !!!cp (1019);        !!!cp (1019);
2913        !!!parse-error (type => 'bare nero', line => $l, column => $c);        !!!parse-error (type => 'bare nero', line => $l, column => $c);
# Line 2395  sub _tokenize_attempt_to_consume_an_enti Line 2927  sub _tokenize_attempt_to_consume_an_enti
2927      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2928      our $EntityChar;      our $EntityChar;
2929    
2930      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2931             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2932             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2933               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2429  sub _tokenize_attempt_to_consume_an_enti Line 2961  sub _tokenize_attempt_to_consume_an_enti
2961      if ($match > 0) {      if ($match > 0) {
2962        !!!cp (1023);        !!!cp (1023);
2963        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2964                line => $l, column => $c};                line => $l, column => $c,
2965                 };
2966      } elsif ($match < 0) {      } elsif ($match < 0) {
2967        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
2968        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2969          !!!cp (1024);          !!!cp (1024);
2970          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2971                  line => $l, column => $c};                  line => $l, column => $c,
2972                   };
2973        } else {        } else {
2974          !!!cp (1025);          !!!cp (1025);
2975          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2976                  line => $l, column => $c};                  line => $l, column => $c,
2977                   };
2978        }        }
2979      } else {      } else {
2980        !!!cp (1026);        !!!cp (1026);
2981        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2982        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2983        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
2984                line => $l, column => $c};                line => $l, column => $c,
2985                 };
2986      }      }
2987    } else {    } else {
2988      !!!cp (1027);      !!!cp (1027);
# Line 2533  sub _tree_construction_initial ($) { Line 3069  sub _tree_construction_initial ($) {
3069                
3070        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3071          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3072          ## NOTE: Default value for both |public_id| and |system_id| attributes
3073          ## are empty strings, so that we don't set any value in missing cases.
3074        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3075            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3076        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2669  sub _tree_construction_initial ($) { Line 3207  sub _tree_construction_initial ($) {
3207        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3208        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3209        ## reprocess        ## reprocess
3210          !!!ack-later;
3211        return;        return;
3212      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3213        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2749  sub _tree_construction_root_element ($) Line 3288  sub _tree_construction_root_element ($)
3288        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3289          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3290            my $root_element;            my $root_element;
3291            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3292            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3293            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3294                  [$root_element, $el_category->{html}];
3295    
3296            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3297              !!!cp ('t24');              !!!cp ('t24');
3298              $self->{application_cache_selection}              $self->{application_cache_selection}
3299                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3300              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3301                ## According to Hixie (#whatwg 2008-03-19), it should be
3302                ## resolved against the base URI of the document in HTML
3303                ## or xml:base of the element in XHTML.
3304            } else {            } else {
3305              !!!cp ('t25');              !!!cp ('t25');
3306              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3307            }            }
3308    
3309              !!!nack ('t25c');
3310    
3311            !!!next-token;            !!!next-token;
3312            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3313          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3324  sub _tree_construction_root_element ($)
3324          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3325        }        }
3326    
3327      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3328        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3329      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3330      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3331    
3332      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3333    
3334      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3335        !!!ack-later;
3336      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3337    
3338      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3356  sub _reset_insertion_mode ($) {
3356        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3357          $last = 1;          $last = 1;
3358          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3359            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') {  
3360              !!!cp ('t27');              !!!cp ('t27');
3361              #              #
3362            } else {            } else {
# Line 2820  sub _reset_insertion_mode ($) { Line 3366  sub _reset_insertion_mode ($) {
3366          }          }
3367        }        }
3368            
3369        ## Step 4..13      ## Step 4..14
3370        my $new_mode = {      my $new_mode;
3371        if ($node->[1] & FOREIGN_EL) {
3372          ## NOTE: Strictly spaking, the line below only applies to MathML and
3373          ## SVG elements.  Currently the HTML syntax supports only MathML and
3374          ## SVG elements as foreigners.
3375          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3376          ## ISSUE: What is set as the secondary insertion mode?
3377        } else {
3378          $new_mode = {
3379                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3380                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3381                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2837  sub _reset_insertion_mode ($) { Line 3391  sub _reset_insertion_mode ($) {
3391                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3392                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3393                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3394                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3395        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3396        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3397                
3398        ## Step 14        ## Step 15
3399        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3400          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3401            !!!cp ('t29');            !!!cp ('t29');
3402            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3410  sub _reset_insertion_mode ($) {
3410          !!!cp ('t31');          !!!cp ('t31');
3411        }        }
3412                
3413        ## Step 15        ## Step 16
3414        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3415                
3416        ## Step 16        ## Step 17
3417        $i--;        $i--;
3418        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3419                
3420        ## Step 17        ## Step 18
3421        redo S3;        redo S3;
3422      } # S3      } # S3
3423    
# Line 2974  sub _tree_construction_main ($) { Line 3529  sub _tree_construction_main ($) {
3529      ## Step 1      ## Step 1
3530      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3531      my $el;      my $el;
3532      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3533    
3534      ## Step 2      ## Step 2
3535      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3540  sub _tree_construction_main ($) {
3540    
3541      ## Step 4      ## Step 4
3542      my $text = '';      my $text = '';
3543        !!!nack ('t40.1');
3544      !!!next-token;      !!!next-token;
3545      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3546        !!!cp ('t40');        !!!cp ('t40');
# Line 3024  sub _tree_construction_main ($) { Line 3580  sub _tree_construction_main ($) {
3580    
3581    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3582      my $script_el;      my $script_el;
3583      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3584      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3585    
3586      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3587      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3588            
3589      my $text = '';      my $text = '';
3590        !!!nack ('t45.1');
3591      !!!next-token;      !!!next-token;
3592      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3593        !!!cp ('t45');        !!!cp ('t45');
# Line 3088  sub _tree_construction_main ($) { Line 3645  sub _tree_construction_main ($) {
3645        my $formatting_element;        my $formatting_element;
3646        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3647        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3648          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3649              !!!cp ('t52');
3650              last AFE;
3651            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3652                         eq $tag_name) {
3653            !!!cp ('t51');            !!!cp ('t51');
3654            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3655            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3656            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3657          }          }
3658        } # AFE        } # AFE
3659        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3123  sub _tree_construction_main ($) { Line 3681  sub _tree_construction_main ($) {
3681              !!!next-token;              !!!next-token;
3682              return;              return;
3683            }            }
3684          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3685            !!!cp ('t56');            !!!cp ('t56');
3686            $in_scope = 0;            $in_scope = 0;
3687          }          }
# Line 3141  sub _tree_construction_main ($) { Line 3696  sub _tree_construction_main ($) {
3696        }        }
3697        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3698          !!!cp ('t58');          !!!cp ('t58');
3699          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3700                            value => $self->{open_elements}->[-1]->[0]
3701                                ->manakai_local_name,
3702                          token => $end_tag_token);                          token => $end_tag_token);
3703        }        }
3704                
# Line 3150  sub _tree_construction_main ($) { Line 3707  sub _tree_construction_main ($) {
3707        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3708        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3709          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3710          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3711              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3712              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3713               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3714            !!!cp ('t59');            !!!cp ('t59');
3715            $furthest_block = $node;            $furthest_block = $node;
3716            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 3796  sub _tree_construction_main ($) {
3796        } # S7          } # S7  
3797                
3798        ## Step 8        ## Step 8
3799        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3800          my $foster_parent_element;          my $foster_parent_element;
3801          my $next_sibling;          my $next_sibling;
3802                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3803                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3804                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3805                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3806                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873    
3874    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3875      my $child = shift;      my $child = shift;
3876      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]}) {  
3877        # MUST        # MUST
3878        my $foster_parent_element;        my $foster_parent_element;
3879        my $next_sibling;        my $next_sibling;
3880                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3881                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3882                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3883                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3884                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 3903  sub _tree_construction_main ($) {
3903      }      }
3904    }; # $insert_to_foster    }; # $insert_to_foster
3905    
3906    B: {    B: while (1) {
3907      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3908        !!!cp ('t73');        !!!cp ('t73');
3909        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3910        ## Ignore the token        ## Ignore the token
3911        ## Stay in the phase        ## Stay in the phase
3912        !!!next-token;        !!!next-token;
3913        redo B;        next B;
3914      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3915               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3916        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3383  sub _tree_construction_main ($) { Line 3936  sub _tree_construction_main ($) {
3936               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3937          }          }
3938        }        }
3939          !!!nack ('t84.1');
3940        !!!next-token;        !!!next-token;
3941        redo B;        next B;
3942      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3943        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3944        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 3952  sub _tree_construction_main ($) {
3952          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3953        }        }
3954        !!!next-token;        !!!next-token;
3955        redo B;        next B;
3956      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3957          if ($token->{type} == CHARACTER_TOKEN) {
3958            !!!cp ('t87.1');
3959            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3960            !!!next-token;
3961            next B;
3962          } elsif ($token->{type} == START_TAG_TOKEN) {
3963            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3964                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3965                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3966                ($token->{tag_name} eq 'svg' and
3967                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3968              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3969              !!!cp ('t87.2');
3970              #
3971            } elsif ({
3972                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3973                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3974                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3975                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3976                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3977                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3978                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3979                      var => 1,
3980                     }->{$token->{tag_name}}) {
3981              !!!cp ('t87.2');
3982              !!!parse-error (type => 'not closed',
3983                              value => $self->{open_elements}->[-1]->[0]
3984                                  ->manakai_local_name,
3985                              token => $token);
3986    
3987              pop @{$self->{open_elements}}
3988                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3989    
3990              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3991              ## Reprocess.
3992              next B;
3993            } else {
3994              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3995              my $tag_name = $token->{tag_name};
3996              if ($nsuri eq $SVG_NS) {
3997                $tag_name = {
3998                   altglyph => 'altGlyph',
3999                   altglyphdef => 'altGlyphDef',
4000                   altglyphitem => 'altGlyphItem',
4001                   animatecolor => 'animateColor',
4002                   animatemotion => 'animateMotion',
4003                   animatetransform => 'animateTransform',
4004                   clippath => 'clipPath',
4005                   feblend => 'feBlend',
4006                   fecolormatrix => 'feColorMatrix',
4007                   fecomponenttransfer => 'feComponentTransfer',
4008                   fecomposite => 'feComposite',
4009                   feconvolvematrix => 'feConvolveMatrix',
4010                   fediffuselighting => 'feDiffuseLighting',
4011                   fedisplacementmap => 'feDisplacementMap',
4012                   fedistantlight => 'feDistantLight',
4013                   feflood => 'feFlood',
4014                   fefunca => 'feFuncA',
4015                   fefuncb => 'feFuncB',
4016                   fefuncg => 'feFuncG',
4017                   fefuncr => 'feFuncR',
4018                   fegaussianblur => 'feGaussianBlur',
4019                   feimage => 'feImage',
4020                   femerge => 'feMerge',
4021                   femergenode => 'feMergeNode',
4022                   femorphology => 'feMorphology',
4023                   feoffset => 'feOffset',
4024                   fepointlight => 'fePointLight',
4025                   fespecularlighting => 'feSpecularLighting',
4026                   fespotlight => 'feSpotLight',
4027                   fetile => 'feTile',
4028                   feturbulence => 'feTurbulence',
4029                   foreignobject => 'foreignObject',
4030                   glyphref => 'glyphRef',
4031                   lineargradient => 'linearGradient',
4032                   radialgradient => 'radialGradient',
4033                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4034                   textpath => 'textPath',  
4035                }->{$tag_name} || $tag_name;
4036              }
4037    
4038              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4039    
4040              ## "adjust foreign attributes" - done in insert-element-f
4041    
4042              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4043    
4044              if ($self->{self_closing}) {
4045                pop @{$self->{open_elements}};
4046                !!!ack ('t87.3');
4047              } else {
4048                !!!cp ('t87.4');
4049              }
4050    
4051              !!!next-token;
4052              next B;
4053            }
4054          } elsif ($token->{type} == END_TAG_TOKEN) {
4055            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4056            !!!cp ('t87.5');
4057            #
4058          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4059            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4060            !!!cp ('t87.6');
4061            #
4062            ## TODO: ...
4063          } else {
4064            die "$0: $token->{type}: Unknown token type";        
4065          }
4066        }
4067    
4068        if ($self->{insertion_mode} & HEAD_IMS) {
4069        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4070          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4071            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4075  sub _tree_construction_main ($) {
4075              !!!cp ('t88.1');              !!!cp ('t88.1');
4076              ## Ignore the token.              ## Ignore the token.
4077              !!!next-token;              !!!next-token;
4078              redo B;              next B;
4079            }            }
4080            unless (length $token->{data}) {            unless (length $token->{data}) {
4081              !!!cp ('t88');              !!!cp ('t88');
4082              !!!next-token;              !!!next-token;
4083              redo B;              next B;
4084            }            }
4085          }          }
4086    
4087          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4088            !!!cp ('t89');            !!!cp ('t89');
4089            ## As if <head>            ## As if <head>
4090            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4091            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4092            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4093                  [$self->{head_element}, $el_category->{head}];
4094    
4095            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4096            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3449  sub _tree_construction_main ($) { Line 4116  sub _tree_construction_main ($) {
4116            !!!cp ('t92');            !!!cp ('t92');
4117          }          }
4118    
4119              ## "after head" insertion mode          ## "after head" insertion mode
4120              ## As if <body>          ## As if <body>
4121              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4122              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4123              ## reprocess          ## reprocess
4124              redo B;          next B;
4125            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4126              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4127                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4128                  !!!cp ('t93');              !!!cp ('t93');
4129                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4130                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4131                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4132                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4133                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4134                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4135                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4136                  !!!cp ('t94');              !!!next-token;
4137                  #              next B;
4138                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4139                  !!!cp ('t95');              !!!cp ('t94');
4140                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              #
4141                  ## Ignore the token            } else {
4142                  !!!next-token;              !!!cp ('t95');
4143                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4144                }              ## Ignore the token
4145              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4146                !!!cp ('t96');              !!!next-token;
4147                ## As if <head>              next B;
4148                !!!create-element ($self->{head_element}, 'head',, $token);            }
4149                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4150                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4151              ## As if <head>
4152              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4153              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4154              push @{$self->{open_elements}},
4155                  [$self->{head_element}, $el_category->{head}];
4156    
4157                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4158                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4159              } else {          } else {
4160                !!!cp ('t97');            !!!cp ('t97');
4161              }          }
4162    
4163              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4164                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3505  sub _tree_construction_main ($) { Line 4177  sub _tree_construction_main ($) {
4177                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4178                  !!!cp ('t100');                  !!!cp ('t100');
4179                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4180                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4181                        [$self->{head_element}, $el_category->{head}];
4182                } else {                } else {
4183                  !!!cp ('t101');                  !!!cp ('t101');
4184                }                }
# Line 3513  sub _tree_construction_main ($) { Line 4186  sub _tree_construction_main ($) {
4186                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4187                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4188                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4189                  !!!nack ('t101.1');
4190                !!!next-token;                !!!next-token;
4191                redo B;                next B;
4192              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4193                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4194                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4195                  !!!cp ('t102');                  !!!cp ('t102');
4196                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4197                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4198                        [$self->{head_element}, $el_category->{head}];
4199                } else {                } else {
4200                  !!!cp ('t103');                  !!!cp ('t103');
4201                }                }
# Line 3528  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4204                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4205                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4206                  !!!ack ('t103.1');
4207                !!!next-token;                !!!next-token;
4208                redo B;                next B;
4209              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4210                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4211                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4212                  !!!cp ('t104');                  !!!cp ('t104');
4213                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4214                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4215                        [$self->{head_element}, $el_category->{head}];
4216                } else {                } else {
4217                  !!!cp ('t105');                  !!!cp ('t105');
4218                }                }
# Line 3543  sub _tree_construction_main ($) { Line 4220  sub _tree_construction_main ($) {
4220                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.
4221    
4222                unless ($self->{confident}) {                unless ($self->{confident}) {
4223                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4224                    !!!cp ('t106');                    !!!cp ('t106');
4225                      ## NOTE: Whether the encoding is supported or not is handled
4226                      ## in the {change_encoding} callback.
4227                    $self->{change_encoding}                    $self->{change_encoding}
4228                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4229                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4233  sub _tree_construction_main ($) {
4233                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4234                                                 ->{has_reference});                                                 ->{has_reference});
4235                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4236                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4237                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4238                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4239                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4240                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4241                      !!!cp ('t107');                      !!!cp ('t107');
4242                        ## NOTE: Whether the encoding is supported or not is handled
4243                        ## in the {change_encoding} callback.
4244                      $self->{change_encoding}                      $self->{change_encoding}
4245                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4246                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4271  sub _tree_construction_main ($) {
4271    
4272                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4273                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4274                  !!!ack ('t110.1');
4275                !!!next-token;                !!!next-token;
4276                redo B;                next B;
4277              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4278                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4279                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3605  sub _tree_construction_main ($) { Line 4286  sub _tree_construction_main ($) {
4286                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4287                  !!!cp ('t112');                  !!!cp ('t112');
4288                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4289                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4290                        [$self->{head_element}, $el_category->{head}];
4291                } else {                } else {
4292                  !!!cp ('t113');                  !!!cp ('t113');
4293                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4299                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4300                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                redo B;                next B;
4302              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4303                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4304                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
# Line 3624  sub _tree_construction_main ($) { Line 4306  sub _tree_construction_main ($) {
4306                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4307                  !!!cp ('t114');                  !!!cp ('t114');
4308                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4309                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4310                        [$self->{head_element}, $el_category->{head}];
4311                } else {                } else {
4312                  !!!cp ('t115');                  !!!cp ('t115');
4313                }                }
4314                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4315                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4316                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4317                redo B;                next B;
4318              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4319                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4320                  !!!cp ('t116');                  !!!cp ('t116');
4321                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4322                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4323                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4324                    !!!nack ('t116.1');
4325                  !!!next-token;                  !!!next-token;
4326                  redo B;                  next B;
4327                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4328                  !!!cp ('t117');                  !!!cp ('t117');
4329                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4330                  ## Ignore the token                  ## Ignore the token
4331                    !!!nack ('t117.1');
4332                  !!!next-token;                  !!!next-token;
4333                  redo B;                  next B;
4334                } else {                } else {
4335                  !!!cp ('t118');                  !!!cp ('t118');
4336                  #                  #
# Line 3662  sub _tree_construction_main ($) { Line 4347  sub _tree_construction_main ($) {
4347                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4348                  !!!cp ('t120');                  !!!cp ('t120');
4349                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4350                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4351                        [$self->{head_element}, $el_category->{head}];
4352                } else {                } else {
4353                  !!!cp ('t121');                  !!!cp ('t121');
4354                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357                $script_start_tag->();                $script_start_tag->();
4358                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4359                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4360                redo B;                next B;
4361              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4362                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4363                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3705  sub _tree_construction_main ($) { Line 4391  sub _tree_construction_main ($) {
4391                } else {                } else {
4392                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4393                }                }
4394                  !!!nack ('t127.1');
4395                !!!next-token;                !!!next-token;
4396                redo B;                next B;
4397              } else {              } else {
4398                !!!cp ('t128');                !!!cp ('t128');
4399                #                #
# Line 3738  sub _tree_construction_main ($) { Line 4425  sub _tree_construction_main ($) {
4425              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4426              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4427              ## reprocess              ## reprocess
4428              redo B;              !!!ack-later;
4429                next B;
4430            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4431              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4432                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4433                  !!!cp ('t132');                  !!!cp ('t132');
4434                  ## As if <head>                  ## As if <head>
4435                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4436                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4437                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4438                        [$self->{head_element}, $el_category->{head}];
4439    
4440                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4441                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4442                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4443                  !!!next-token;                  !!!next-token;
4444                  redo B;                  next B;
4445                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4446                  !!!cp ('t133');                  !!!cp ('t133');
4447                  ## As if </noscript>                  ## As if </noscript>
# Line 3763  sub _tree_construction_main ($) { Line 4452  sub _tree_construction_main ($) {
4452                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4453                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4454                  !!!next-token;                  !!!next-token;
4455                  redo B;                  next B;
4456                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4457                  !!!cp ('t134');                  !!!cp ('t134');
4458                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4459                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4460                  !!!next-token;                  !!!next-token;
4461                  redo B;                  next B;
4462                } else {                } else {
4463                  !!!cp ('t135');                  !!!cp ('t135');
4464                  #                  #
# Line 3780  sub _tree_construction_main ($) { Line 4469  sub _tree_construction_main ($) {
4469                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4470                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4471                  !!!next-token;                  !!!next-token;
4472                  redo B;                  next B;
4473                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4474                  !!!cp ('t137');                  !!!cp ('t137');
4475                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4476                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4477                  !!!next-token;                  !!!next-token;
4478                  redo B;                  next B;
4479                } else {                } else {
4480                  !!!cp ('t138');                  !!!cp ('t138');
4481                  #                  #
# Line 3797  sub _tree_construction_main ($) { Line 4486  sub _tree_construction_main ($) {
4486                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4487                  !!!cp ('t139');                  !!!cp ('t139');
4488                  ## As if <head>                  ## As if <head>
4489                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4490                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4491                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4492                        [$self->{head_element}, $el_category->{head}];
4493    
4494                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4495                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3808  sub _tree_construction_main ($) { Line 4498  sub _tree_construction_main ($) {
4498                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4499                  ## Ignore the token                  ## Ignore the token
4500                  !!!next-token;                  !!!next-token;
4501                  redo B;                  next B;
4502                } else {                } else {
4503                  !!!cp ('t141');                  !!!cp ('t141');
4504                }                }
# Line 3820  sub _tree_construction_main ($) { Line 4510  sub _tree_construction_main ($) {
4510                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4511                  !!!cp ('t142');                  !!!cp ('t142');
4512                  ## As if <head>                  ## As if <head>
4513                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4514                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4515                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4516                        [$self->{head_element}, $el_category->{head}];
4517    
4518                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4519                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3840  sub _tree_construction_main ($) { Line 4531  sub _tree_construction_main ($) {
4531                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4532                  ## Ignore the token                  ## Ignore the token
4533                  !!!next-token;                  !!!next-token;
4534                  redo B;                  next B;
4535                }                }
4536              }              }
4537    
# Line 3867  sub _tree_construction_main ($) { Line 4558  sub _tree_construction_main ($) {
4558                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4559                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4560                !!!next-token;                !!!next-token;
4561                redo B;                next B;
4562              } else {              } else {
4563                !!!cp ('t149');                !!!cp ('t149');
4564              }              }
# Line 3877  sub _tree_construction_main ($) { Line 4568  sub _tree_construction_main ($) {
4568              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4569              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4570              ## reprocess              ## reprocess
4571              redo B;              next B;
4572        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4573          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4574            !!!cp ('t149.1');            !!!cp ('t149.1');
4575    
4576            ## NOTE: As if <head>            ## NOTE: As if <head>
4577            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4579                ($self->{head_element});                ($self->{head_element});
4580            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4581              #    [$self->{head_element}, $el_category->{head}];
4582            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4583            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4584    
# Line 3930  sub _tree_construction_main ($) { Line 4622  sub _tree_construction_main ($) {
4622          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4623          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4624          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4625          redo B;          next B;
4626        } else {        } else {
4627          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4628        }        }
# Line 3945  sub _tree_construction_main ($) { Line 4637  sub _tree_construction_main ($) {
4637              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4638    
4639              !!!next-token;              !!!next-token;
4640              redo B;              next B;
4641            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4642              if ({              if ({
4643                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 4647  sub _tree_construction_main ($) {
4647                  ## have an element in table scope                  ## have an element in table scope
4648                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4649                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4650                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4651                      !!!cp ('t151');                      !!!cp ('t151');
4652    
4653                      ## Close the cell                      ## Close the cell
4654                      !!!back-token; # <?>                      !!!back-token; # <x>
4655                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4656                                  tag_name => $node->[0]->manakai_local_name,
4657                                line => $token->{line},                                line => $token->{line},
4658                                column => $token->{column}};                                column => $token->{column}};
4659                      redo B;                      next B;
4660                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4661                      !!!cp ('t152');                      !!!cp ('t152');
4662                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4663                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4668  sub _tree_construction_main ($) {
4668                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4669                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4670                  ## Ignore the token                  ## Ignore the token
4671                    !!!nack ('t153.1');
4672                  !!!next-token;                  !!!next-token;
4673                  redo B;                  next B;
4674                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4675                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4676                                    
# Line 3988  sub _tree_construction_main ($) { Line 4680  sub _tree_construction_main ($) {
4680                  INSCOPE: {                  INSCOPE: {
4681                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4682                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4683                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4684                        !!!cp ('t155');                        !!!cp ('t155');
4685                        $i = $_;                        $i = $_;
4686                        last INSCOPE;                        last INSCOPE;
4687                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4688                        !!!cp ('t156');                        !!!cp ('t156');
4689                        last;                        last;
4690                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4694  sub _tree_construction_main ($) {
4694                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4695                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4696                    ## Ignore the token                    ## Ignore the token
4697                      !!!nack ('t157.1');
4698                    !!!next-token;                    !!!next-token;
4699                    redo B;                    next B;
4700                  } # INSCOPE                  } # INSCOPE
4701                                    
4702                  ## generate implied end tags                  ## generate implied end tags
4703                  while ({                  while ($self->{open_elements}->[-1]->[1]
4704                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4705                    !!!cp ('t158');                    !!!cp ('t158');
4706                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4707                  }                  }
4708    
4709                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4710                    !!!cp ('t159');                    !!!cp ('t159');
4711                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4712                                      value => $self->{open_elements}->[-1]->[0]
4713                                          ->manakai_local_name,
4714                                      token => $token);
4715                  } else {                  } else {
4716                    !!!cp ('t160');                    !!!cp ('t160');
4717                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 4723  sub _tree_construction_main ($) {
4723                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4724                                    
4725                  ## reprocess                  ## reprocess
4726                  redo B;                  !!!ack-later;
4727                    next B;
4728                } else {                } else {
4729                  !!!cp ('t161');                  !!!cp ('t161');
4730                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4740  sub _tree_construction_main ($) {
4740                  my $i;                  my $i;
4741                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4742                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4743                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4744                      !!!cp ('t163');                      !!!cp ('t163');
4745                      $i = $_;                      $i = $_;
4746                      last INSCOPE;                      last INSCOPE;
4747                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4748                      !!!cp ('t164');                      !!!cp ('t164');
4749                      last INSCOPE;                      last INSCOPE;
4750                    }                    }
# Line 4062  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4755                      ## Ignore the token                      ## Ignore the token
4756                      !!!next-token;                      !!!next-token;
4757                      redo B;                      next B;
4758                    }                    }
4759                                    
4760                  ## generate implied end tags                  ## generate implied end tags
4761                  while ({                  while ($self->{open_elements}->[-1]->[1]
4762                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4763                    !!!cp ('t166');                    !!!cp ('t166');
4764                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4765                  }                  }
4766    
4767                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4768                            ne $token->{tag_name}) {
4769                    !!!cp ('t167');                    !!!cp ('t167');
4770                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4771                                      value => $self->{open_elements}->[-1]->[0]
4772                                          ->manakai_local_name,
4773                                      token => $token);
4774                  } else {                  } else {
4775                    !!!cp ('t168');                    !!!cp ('t168');
4776                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 4782  sub _tree_construction_main ($) {
4782                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4783                                    
4784                  !!!next-token;                  !!!next-token;
4785                  redo B;                  next B;
4786                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4787                  !!!cp ('t169');                  !!!cp ('t169');
4788                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4789                  ## Ignore the token                  ## Ignore the token
4790                  !!!next-token;                  !!!next-token;
4791                  redo B;                  next B;
4792                } else {                } else {
4793                  !!!cp ('t170');                  !!!cp ('t170');
4794                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 4800  sub _tree_construction_main ($) {
4800                  INSCOPE: {                  INSCOPE: {
4801                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4802                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4803                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4804                        !!!cp ('t171');                        !!!cp ('t171');
4805                        $i = $_;                        $i = $_;
4806                        last INSCOPE;                        last INSCOPE;
4807                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4808                        !!!cp ('t172');                        !!!cp ('t172');
4809                        last;                        last;
4810                      }                      }
# Line 4122  sub _tree_construction_main ($) { Line 4815  sub _tree_construction_main ($) {
4815                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4816                    ## Ignore the token                    ## Ignore the token
4817                    !!!next-token;                    !!!next-token;
4818                    redo B;                    next B;
4819                  } # INSCOPE                  } # INSCOPE
4820                                    
4821                  ## generate implied end tags                  ## generate implied end tags
4822                  while ({                  while ($self->{open_elements}->[-1]->[1]
4823                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4824                    !!!cp ('t174');                    !!!cp ('t174');
4825                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4826                  }                  }
4827                                    
4828                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4829                    !!!cp ('t175');                    !!!cp ('t175');
4830                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4831                                      value => $self->{open_elements}->[-1]->[0]
4832                                          ->manakai_local_name,
4833                                      token => $token);
4834                  } else {                  } else {
4835                    !!!cp ('t176');                    !!!cp ('t176');
4836                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 4842  sub _tree_construction_main ($) {
4842                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4843                                    
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4847                  !!!cp ('t177');                  !!!cp ('t177');
4848                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4849                  ## Ignore the token                  ## Ignore the token
4850                  !!!next-token;                  !!!next-token;
4851                  redo B;                  next B;
4852                } else {                } else {
4853                  !!!cp ('t178');                  !!!cp ('t178');
4854                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 4864  sub _tree_construction_main ($) {
4864                INSCOPE: {                INSCOPE: {
4865                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4866                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4867                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4868                      !!!cp ('t179');                      !!!cp ('t179');
4869                      $i = $_;                      $i = $_;
4870    
4871                      ## Close the cell                      ## Close the cell
4872                      !!!back-token; # </?>                      !!!back-token; # </x>
4873                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4874                                line => $token->{line},                                line => $token->{line},
4875                                column => $token->{column}};                                column => $token->{column}};
4876                      redo B;                      next B;
4877                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4878                      !!!cp ('t180');                      !!!cp ('t180');
4879                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4880                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4881                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4882                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4883                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4884                      !!!cp ('t181');                      !!!cp ('t181');
4885                      last;                      last;
# Line 4198  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4892                  ## Ignore the token                  ## Ignore the token
4893                  !!!next-token;                  !!!next-token;
4894                  redo B;                  next B;
4895                } # INSCOPE                } # INSCOPE
4896              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4897                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4209  sub _tree_construction_main ($) { Line 4902  sub _tree_construction_main ($) {
4902                my $i;                my $i;
4903                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4904                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4905                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4906                    !!!cp ('t184');                    !!!cp ('t184');
4907                    $i = $_;                    $i = $_;
4908                    last INSCOPE;                    last INSCOPE;
4909                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4910                    !!!cp ('t185');                    !!!cp ('t185');
4911                    last INSCOPE;                    last INSCOPE;
4912                  }                  }
# Line 4225  sub _tree_construction_main ($) { Line 4916  sub _tree_construction_main ($) {
4916                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4917                  ## Ignore the token                  ## Ignore the token
4918                  !!!next-token;                  !!!next-token;
4919                  redo B;                  next B;
4920                }                }
4921                                
4922                ## generate implied end tags                ## generate implied end tags
4923                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4924                  !!!cp ('t187');                  !!!cp ('t187');
4925                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4926                }                }
4927    
4928                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4929                  !!!cp ('t188');                  !!!cp ('t188');
4930                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
4931                                    value => $self->{open_elements}->[-1]->[0]
4932                                        ->manakai_local_name,
4933                                    token => $token);
4934                } else {                } else {
4935                  !!!cp ('t189');                  !!!cp ('t189');
4936                }                }
# Line 4250  sub _tree_construction_main ($) { Line 4942  sub _tree_construction_main ($) {
4942                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4943    
4944                ## reprocess                ## reprocess
4945                redo B;                next B;
4946              } elsif ({              } elsif ({
4947                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4948                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4259  sub _tree_construction_main ($) { Line 4951  sub _tree_construction_main ($) {
4951                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4952                  ## Ignore the token                  ## Ignore the token
4953                  !!!next-token;                  !!!next-token;
4954                  redo B;                  next B;
4955                } else {                } else {
4956                  !!!cp ('t191');                  !!!cp ('t191');
4957                  #                  #
# Line 4273  sub _tree_construction_main ($) { Line 4965  sub _tree_construction_main ($) {
4965                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4966                ## Ignore the token                ## Ignore the token
4967                !!!next-token;                !!!next-token;
4968                redo B;                next B;
4969              } else {              } else {
4970                !!!cp ('t193');                !!!cp ('t193');
4971                #                #
4972              }              }
4973        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4974          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
4975            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
4976              !!!cp ('t75');              !!!cp ('t75');
4977              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
4978              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996            unless (length $token->{data}) {            unless (length $token->{data}) {
4997              !!!cp ('t194');              !!!cp ('t194');
4998              !!!next-token;              !!!next-token;
4999              redo B;              next B;
5000            } else {            } else {
5001              !!!cp ('t195');              !!!cp ('t195');
5002            }            }
# Line 4321  sub _tree_construction_main ($) { Line 5010  sub _tree_construction_main ($) {
5010              ## result in a new Text node.              ## result in a new Text node.
5011              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5012                            
5013              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]}) {  
5014                # MUST                # MUST
5015                my $foster_parent_element;                my $foster_parent_element;
5016                my $next_sibling;                my $next_sibling;
5017                my $prev_sibling;                my $prev_sibling;
5018                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5019                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5020                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5021                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5022                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5051  sub _tree_construction_main ($) {
5051          }          }
5052                            
5053          !!!next-token;          !!!next-token;
5054          redo B;          next B;
5055        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5056              if ({              if ({
5057                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4373  sub _tree_construction_main ($) { Line 5059  sub _tree_construction_main ($) {
5059                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5060                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5061                  ## Clear back to table context                  ## Clear back to table context
5062                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5063                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5064                    !!!cp ('t201');                    !!!cp ('t201');
5065                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5066                  }                  }
# Line 4391  sub _tree_construction_main ($) { Line 5077  sub _tree_construction_main ($) {
5077                  }                  }
5078                                    
5079                  ## Clear back to table body context                  ## Clear back to table body context
5080                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5081                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5082                    !!!cp ('t203');                    !!!cp ('t203');
5083                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5084                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4403  sub _tree_construction_main ($) { Line 5088  sub _tree_construction_main ($) {
5088                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5089                    !!!cp ('t204');                    !!!cp ('t204');
5090                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5091                      !!!nack ('t204');
5092                    !!!next-token;                    !!!next-token;
5093                    redo B;                    next B;
5094                  } else {                  } else {
5095                    !!!cp ('t205');                    !!!cp ('t205');
5096                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4415  sub _tree_construction_main ($) { Line 5101  sub _tree_construction_main ($) {
5101                }                }
5102    
5103                ## Clear back to table row context                ## Clear back to table row context
5104                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5105                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5106                  !!!cp ('t207');                  !!!cp ('t207');
5107                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5108                }                }
# Line 4427  sub _tree_construction_main ($) { Line 5112  sub _tree_construction_main ($) {
5112    
5113                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5114                                
5115                  !!!nack ('t207.1');
5116                !!!next-token;                !!!next-token;
5117                redo B;                next B;
5118              } elsif ({              } elsif ({
5119                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5120                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5126  sub _tree_construction_main ($) {
5126                  my $i;                  my $i;
5127                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5128                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5129                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5130                      !!!cp ('t208');                      !!!cp ('t208');
5131                      $i = $_;                      $i = $_;
5132                      last INSCOPE;                      last INSCOPE;
5133                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5134                      !!!cp ('t209');                      !!!cp ('t209');
5135                      last INSCOPE;                      last INSCOPE;
5136                    }                    }
5137                  } # INSCOPE                  } # INSCOPE
5138                  unless (defined $i) {                  unless (defined $i) {
5139                   !!!cp ('t210');                    !!!cp ('t210');
5140  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5141                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5142                    ## Ignore the token                    ## Ignore the token
5143                      !!!nack ('t210.1');
5144                    !!!next-token;                    !!!next-token;
5145                    redo B;                    next B;
5146                  }                  }
5147                                    
5148                  ## Clear back to table row context                  ## Clear back to table row context
5149                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5150                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5151                    !!!cp ('t211');                    !!!cp ('t211');
5152                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5153                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5158  sub _tree_construction_main ($) {
5158                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5159                    !!!cp ('t212');                    !!!cp ('t212');
5160                    ## reprocess                    ## reprocess
5161                    redo B;                    !!!ack-later;
5162                      next B;
5163                  } else {                  } else {
5164                    !!!cp ('t213');                    !!!cp ('t213');
5165                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5171  sub _tree_construction_main ($) {
5171                  my $i;                  my $i;
5172                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5173                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5174                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5175                      !!!cp ('t214');                      !!!cp ('t214');
5176                      $i = $_;                      $i = $_;
5177                      last INSCOPE;                      last INSCOPE;
5178                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5179                      !!!cp ('t215');                      !!!cp ('t215');
5180                      last INSCOPE;                      last INSCOPE;
5181                    }                    }
# Line 4507  sub _tree_construction_main ($) { Line 5185  sub _tree_construction_main ($) {
5185  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5186                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5187                    ## Ignore the token                    ## Ignore the token
5188                      !!!nack ('t216.1');
5189                    !!!next-token;                    !!!next-token;
5190                    redo B;                    next B;
5191                  }                  }
5192    
5193                  ## Clear back to table body context                  ## Clear back to table body context
5194                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5195                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5196                    !!!cp ('t217');                    !!!cp ('t217');
5197                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5198                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5214  sub _tree_construction_main ($) {
5214    
5215                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5216                  ## Clear back to table context                  ## Clear back to table context
5217                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5218                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5219                    !!!cp ('t219');                    !!!cp ('t219');
5220                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5221                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5224  sub _tree_construction_main ($) {
5224                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5225                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5226                  ## reprocess                  ## reprocess
5227                  redo B;                  !!!ack-later;
5228                    next B;
5229                } elsif ({                } elsif ({
5230                          caption => 1,                          caption => 1,
5231                          colgroup => 1,                          colgroup => 1,
5232                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5233                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5234                  ## Clear back to table context                  ## Clear back to table context
5235                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5236                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5237                    !!!cp ('t220');                    !!!cp ('t220');
5238                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5239                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4572  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5252                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5253                  !!!next-token;                  !!!next-token;
5254                  redo B;                  !!!nack ('t220.1');
5255                    next B;
5256                } else {                } else {
5257                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5258                }                }
5259              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5260                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5261                                  value => $self->{open_elements}->[-1]->[0]
5262                                      ->manakai_local_name,
5263                                  token => $token);
5264    
5265                ## As if </table>                ## As if </table>
5266                ## have a table element in table scope                ## have a table element in table scope
5267                my $i;                my $i;
5268                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5269                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5270                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5271                    !!!cp ('t221');                    !!!cp ('t221');
5272                    $i = $_;                    $i = $_;
5273                    last INSCOPE;                    last INSCOPE;
5274                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5275                    !!!cp ('t222');                    !!!cp ('t222');
5276                    last INSCOPE;                    last INSCOPE;
5277                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5281  sub _tree_construction_main ($) {
5281  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5282                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5283                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5284                    !!!nack ('t223.1');
5285                  !!!next-token;                  !!!next-token;
5286                  redo B;                  next B;
5287                }                }
5288                                
5289  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5290                ## generate implied end tags                ## generate implied end tags
5291                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5292                  !!!cp ('t224');                  !!!cp ('t224');
5293                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5294                }                }
5295    
5296                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5297                  !!!cp ('t225');                  !!!cp ('t225');
5298  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5299                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5300                                    value => $self->{open_elements}->[-1]->[0]
5301                                        ->manakai_local_name,
5302                                    token => $token);
5303                } else {                } else {
5304                  !!!cp ('t226');                  !!!cp ('t226');
5305                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5309  sub _tree_construction_main ($) {
5309    
5310                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5311    
5312                ## reprocess            ## reprocess
5313                redo B;            !!!ack-later;
5314              next B;
5315          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5316            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5317              !!!cp ('t227.8');              !!!cp ('t227.8');
5318              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5319              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5320              redo B;              next B;
5321            } else {            } else {
5322              !!!cp ('t227.7');              !!!cp ('t227.7');
5323              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5327  sub _tree_construction_main ($) {
5327              !!!cp ('t227.6');              !!!cp ('t227.6');
5328              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5329              $script_start_tag->();              $script_start_tag->();
5330              redo B;              next B;
5331            } else {            } else {
5332              !!!cp ('t227.5');              !!!cp ('t227.5');
5333              #              #
# Line 4664  sub _tree_construction_main ($) { Line 5347  sub _tree_construction_main ($) {
5347                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5348    
5349                  !!!next-token;                  !!!next-token;
5350                  redo B;                  !!!ack ('t227.2.1');
5351                    next B;
5352                } else {                } else {
5353                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5354                  #                  #
# Line 4693  sub _tree_construction_main ($) { Line 5377  sub _tree_construction_main ($) {
5377                my $i;                my $i;
5378                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5379                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5380                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5381                    !!!cp ('t228');                    !!!cp ('t228');
5382                    $i = $_;                    $i = $_;
5383                    last INSCOPE;                    last INSCOPE;
5384                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5385                    !!!cp ('t229');                    !!!cp ('t229');
5386                    last INSCOPE;                    last INSCOPE;
5387                  }                  }
# Line 4708  sub _tree_construction_main ($) { Line 5390  sub _tree_construction_main ($) {
5390                  !!!cp ('t230');                  !!!cp ('t230');
5391                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5392                  ## Ignore the token                  ## Ignore the token
5393                    !!!nack ('t230.1');
5394                  !!!next-token;                  !!!next-token;
5395                  redo B;                  next B;
5396                } else {                } else {
5397                  !!!cp ('t232');                  !!!cp ('t232');
5398                }                }
5399    
5400                ## Clear back to table row context                ## Clear back to table row context
5401                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5402                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5403                  !!!cp ('t231');                  !!!cp ('t231');
5404  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5405                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5408  sub _tree_construction_main ($) {
5408                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5409                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5410                !!!next-token;                !!!next-token;
5411                redo B;                !!!nack ('t231.1');
5412                  next B;
5413              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5414                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5415                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5417  sub _tree_construction_main ($) {
5417                  my $i;                  my $i;
5418                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5419                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5420                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5421                      !!!cp ('t233');                      !!!cp ('t233');
5422                      $i = $_;                      $i = $_;
5423                      last INSCOPE;                      last INSCOPE;
5424                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5425                      !!!cp ('t234');                      !!!cp ('t234');
5426                      last INSCOPE;                      last INSCOPE;
5427                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5431  sub _tree_construction_main ($) {
5431  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5432                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5433                    ## Ignore the token                    ## Ignore the token
5434                      !!!nack ('t236.1');
5435                    !!!next-token;                    !!!next-token;
5436                    redo B;                    next B;
5437                  }                  }
5438                                    
5439                  ## Clear back to table row context                  ## Clear back to table row context
5440                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5441                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5442                    !!!cp ('t236');                    !!!cp ('t236');
5443  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5444                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5454  sub _tree_construction_main ($) {
5454                  my $i;                  my $i;
5455                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5456                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5457                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5458                      !!!cp ('t237');                      !!!cp ('t237');
5459                      $i = $_;                      $i = $_;
5460                      last INSCOPE;                      last INSCOPE;
5461                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5462                      !!!cp ('t238');                      !!!cp ('t238');
5463                      last INSCOPE;                      last INSCOPE;
5464                    }                    }
# Line 4790  sub _tree_construction_main ($) { Line 5467  sub _tree_construction_main ($) {
5467                    !!!cp ('t239');                    !!!cp ('t239');
5468                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5469                    ## Ignore the token                    ## Ignore the token
5470                      !!!nack ('t239.1');
5471                    !!!next-token;                    !!!next-token;
5472                    redo B;                    next B;
5473                  }                  }
5474                                    
5475                  ## Clear back to table body context                  ## Clear back to table body context
5476                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5477                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5478                    !!!cp ('t240');                    !!!cp ('t240');
5479                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5480                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5500  sub _tree_construction_main ($) {
5500                my $i;                my $i;
5501                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5502                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5503                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5504                    !!!cp ('t241');                    !!!cp ('t241');
5505                    $i = $_;                    $i = $_;
5506                    last INSCOPE;                    last INSCOPE;
5507                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5508                    !!!cp ('t242');                    !!!cp ('t242');
5509                    last INSCOPE;                    last INSCOPE;
5510                  }                  }
# Line 4838  sub _tree_construction_main ($) { Line 5513  sub _tree_construction_main ($) {
5513                  !!!cp ('t243');                  !!!cp ('t243');
5514                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5515                  ## Ignore the token                  ## Ignore the token
5516                    !!!nack ('t243.1');
5517                  !!!next-token;                  !!!next-token;
5518                  redo B;                  next B;
5519                }                }
5520                                    
5521                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5525                                
5526                !!!next-token;                !!!next-token;
5527                redo B;                next B;
5528              } elsif ({              } elsif ({
5529                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5530                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 5534  sub _tree_construction_main ($) {
5534                  my $i;                  my $i;
5535                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5536                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5537                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5538                      !!!cp ('t247');                      !!!cp ('t247');
5539                      $i = $_;                      $i = $_;
5540                      last INSCOPE;                      last INSCOPE;
5541                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5542                      !!!cp ('t248');                      !!!cp ('t248');
5543                      last INSCOPE;                      last INSCOPE;
5544                    }                    }
# Line 4873  sub _tree_construction_main ($) { Line 5547  sub _tree_construction_main ($) {
5547                      !!!cp ('t249');                      !!!cp ('t249');
5548                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5549                      ## Ignore the token                      ## Ignore the token
5550                        !!!nack ('t249.1');
5551                      !!!next-token;                      !!!next-token;
5552                      redo B;                      next B;
5553                    }                    }
5554                                    
5555                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 5557  sub _tree_construction_main ($) {
5557                  my $i;                  my $i;
5558                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5559                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5560                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5561                      !!!cp ('t250');                      !!!cp ('t250');
5562                      $i = $_;                      $i = $_;
5563                      last INSCOPE;                      last INSCOPE;
5564                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5565                      !!!cp ('t251');                      !!!cp ('t251');
5566                      last INSCOPE;                      last INSCOPE;
5567                    }                    }
# Line 4897  sub _tree_construction_main ($) { Line 5570  sub _tree_construction_main ($) {
5570                      !!!cp ('t252');                      !!!cp ('t252');
5571                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5572                      ## Ignore the token                      ## Ignore the token
5573                        !!!nack ('t252.1');
5574                      !!!next-token;                      !!!next-token;
5575                      redo B;                      next B;
5576                    }                    }
5577                                    
5578                  ## Clear back to table row context                  ## Clear back to table row context
5579                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5580                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5581                    !!!cp ('t253');                    !!!cp ('t253');
5582  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5583                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 5592  sub _tree_construction_main ($) {
5592                my $i;                my $i;
5593                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5594                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5595                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5596                    !!!cp ('t254');                    !!!cp ('t254');
5597                    $i = $_;                    $i = $_;
5598                    last INSCOPE;                    last INSCOPE;
5599                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5600                    !!!cp ('t255');                    !!!cp ('t255');
5601                    last INSCOPE;                    last INSCOPE;
5602                  }                  }
# Line 4934  sub _tree_construction_main ($) { Line 5605  sub _tree_construction_main ($) {
5605                  !!!cp ('t256');                  !!!cp ('t256');
5606                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5607                  ## Ignore the token                  ## Ignore the token
5608                    !!!nack ('t256.1');
5609                  !!!next-token;                  !!!next-token;
5610                  redo B;                  next B;
5611                }                }
5612    
5613                ## Clear back to table body context                ## Clear back to table body context
5614                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5615                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5616                  !!!cp ('t257');                  !!!cp ('t257');
5617  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5618                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 5620  sub _tree_construction_main ($) {
5620    
5621                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5622                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5623                  !!!nack ('t257.1');
5624                !!!next-token;                !!!next-token;
5625                redo B;                next B;
5626              } elsif ({              } elsif ({
5627                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5628                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5629                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5630                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5631                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5632                !!!cp ('t258');            !!!cp ('t258');
5633                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                ## Ignore the token            ## Ignore the token
5635                !!!next-token;            !!!nack ('t258.1');
5636                redo B;             !!!next-token;
5637              next B;
5638          } else {          } else {
5639            !!!cp ('t259');            !!!cp ('t259');
5640            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4970  sub _tree_construction_main ($) { Line 5643  sub _tree_construction_main ($) {
5643            #            #
5644          }          }
5645        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5646          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5647                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5648            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5649            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 5665  sub _tree_construction_main ($) {
5665                unless (length $token->{data}) {                unless (length $token->{data}) {
5666                  !!!cp ('t260');                  !!!cp ('t260');
5667                  !!!next-token;                  !!!next-token;
5668                  redo B;                  next B;
5669                }                }
5670              }              }
5671                            
# Line 5003  sub _tree_construction_main ($) { Line 5676  sub _tree_construction_main ($) {
5676                !!!cp ('t262');                !!!cp ('t262');
5677                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5678                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5679                  !!!ack ('t262.1');
5680                !!!next-token;                !!!next-token;
5681                redo B;                next B;
5682              } else {              } else {
5683                !!!cp ('t263');                !!!cp ('t263');
5684                #                #
5685              }              }
5686            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5687              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5688                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5689                  !!!cp ('t264');                  !!!cp ('t264');
5690                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5691                  ## Ignore the token                  ## Ignore the token
5692                  !!!next-token;                  !!!next-token;
5693                  redo B;                  next B;
5694                } else {                } else {
5695                  !!!cp ('t265');                  !!!cp ('t265');
5696                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5697                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5698                  !!!next-token;                  !!!next-token;
5699                  redo B;                              next B;            
5700                }                }
5701              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5702                !!!cp ('t266');                !!!cp ('t266');
5703                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5704                ## Ignore the token                ## Ignore the token
5705                !!!next-token;                !!!next-token;
5706                redo B;                next B;
5707              } else {              } else {
5708                !!!cp ('t267');                !!!cp ('t267');
5709                #                #
5710              }              }
5711        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5712          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5713              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5714            !!!cp ('t270.2');            !!!cp ('t270.2');
5715            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 5720  sub _tree_construction_main ($) {
5720            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5721            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5722            ## Reprocess.            ## Reprocess.
5723            redo B;            next B;
5724          }          }
5725        } else {        } else {
5726          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5727        }        }
5728    
5729            ## As if </colgroup>            ## As if </colgroup>
5730            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5731              !!!cp ('t269');              !!!cp ('t269');
5732  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5733              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5734              ## Ignore the token              ## Ignore the token
5735                !!!nack ('t269.1');
5736              !!!next-token;              !!!next-token;
5737              redo B;              next B;
5738            } else {            } else {
5739              !!!cp ('t270');              !!!cp ('t270');
5740              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5741              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5742                !!!ack-later;
5743              ## reprocess              ## reprocess
5744              redo B;              next B;
5745            }            }
5746      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5747        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5748          !!!cp ('t271');          !!!cp ('t271');
5749          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5750          !!!next-token;          !!!next-token;
5751          redo B;          next B;
5752        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5753              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5754                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5755                  !!!cp ('t272');              !!!cp ('t272');
5756                  ## As if </option>              ## As if </option>
5757                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5758                } else {            } else {
5759                  !!!cp ('t273');              !!!cp ('t273');
5760                }            }
5761    
5762                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                !!!next-token;            !!!nack ('t273.1');
5764                redo B;            !!!next-token;
5765              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5766                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5767                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5768                  ## As if </option>              !!!cp ('t274');
5769                  pop @{$self->{open_elements}};              ## As if </option>
5770                } else {              pop @{$self->{open_elements}};
5771                  !!!cp ('t275');            } else {
5772                }              !!!cp ('t275');
5773              }
5774    
5775                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5776                  !!!cp ('t276');              !!!cp ('t276');
5777                  ## As if </optgroup>              ## As if </optgroup>
5778                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5779                } else {            } else {
5780                  !!!cp ('t277');              !!!cp ('t277');
5781                }            }
5782    
5783                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5784                !!!next-token;            !!!nack ('t277.1');
5785                redo B;            !!!next-token;
5786              next B;
5787          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5788                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5789                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5118  sub _tree_construction_main ($) { Line 5796  sub _tree_construction_main ($) {
5796            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5797            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5798            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5799                ## have an element in table scope            ## have an element in table scope
5800                my $i;            my $i;
5801                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5802                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5803                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5804                    !!!cp ('t278');                !!!cp ('t278');
5805                    $i = $_;                $i = $_;
5806                    last INSCOPE;                last INSCOPE;
5807                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5808                            table => 1, html => 1,                !!!cp ('t279');
5809                           }->{$node->[1]}) {                last INSCOPE;
5810                    !!!cp ('t279');              }
5811                    last INSCOPE;            } # INSCOPE
5812                  }            unless (defined $i) {
5813                } # INSCOPE              !!!cp ('t280');
5814                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5815                  !!!cp ('t280');              ## Ignore the token
5816                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5817                  ## Ignore the token              !!!next-token;
5818                  !!!next-token;              next B;
5819                  redo B;            }
               }  
5820                                
5821                !!!cp ('t281');            !!!cp ('t281');
5822                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5823    
5824                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5825    
5826            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5827              !!!cp ('t281.2');              !!!nack ('t281.2');
5828              !!!next-token;              !!!next-token;
5829              redo B;              next B;
5830            } else {            } else {
5831              !!!cp ('t281.1');              !!!cp ('t281.1');
5832                !!!ack-later;
5833              ## Reprocess the token.              ## Reprocess the token.
5834              redo B;              next B;
5835            }            }
5836          } else {          } else {
5837            !!!cp ('t282');            !!!cp ('t282');
5838            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5839            ## Ignore the token            ## Ignore the token
5840              !!!nack ('t282.1');
5841            !!!next-token;            !!!next-token;
5842            redo B;            next B;
5843          }          }
5844        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5845              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5846                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5847                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5848                  !!!cp ('t283');              !!!cp ('t283');
5849                  ## As if </option>              ## As if </option>
5850                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5851                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5852                  !!!cp ('t284');              !!!cp ('t284');
5853                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5854                } else {            } else {
5855                  !!!cp ('t285');              !!!cp ('t285');
5856                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5857                  ## Ignore the token              ## Ignore the token
5858                }            }
5859                !!!next-token;            !!!nack ('t285.1');
5860                redo B;            !!!next-token;
5861              } elsif ($token->{tag_name} eq 'option') {            next B;
5862                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5863                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5864                  pop @{$self->{open_elements}};              !!!cp ('t286');
5865                } else {              pop @{$self->{open_elements}};
5866                  !!!cp ('t287');            } else {
5867                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5868                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5869                }              ## Ignore the token
5870                !!!next-token;            }
5871                redo B;            !!!nack ('t287.1');
5872              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5873                ## have an element in table scope            next B;
5874                my $i;          } elsif ($token->{tag_name} eq 'select') {
5875                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5876                  my $node = $self->{open_elements}->[$_];            my $i;
5877                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5878                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5879                    $i = $_;              if ($node->[1] & SELECT_EL) {
5880                    last INSCOPE;                !!!cp ('t288');
5881                  } elsif ({                $i = $_;
5882                            table => 1, html => 1,                last INSCOPE;
5883                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5884                    !!!cp ('t289');                !!!cp ('t289');
5885                    last INSCOPE;                last INSCOPE;
5886                  }              }
5887                } # INSCOPE            } # INSCOPE
5888                unless (defined $i) {            unless (defined $i) {
5889                  !!!cp ('t290');              !!!cp ('t290');
5890                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5891                  ## Ignore the token              ## Ignore the token
5892                  !!!next-token;              !!!nack ('t290.1');
5893                  redo B;              !!!next-token;
5894                }              next B;
5895              }
5896                                
5897                !!!cp ('t291');            !!!cp ('t291');
5898                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5899    
5900                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5901    
5902                !!!next-token;            !!!nack ('t291.1');
5903                redo B;            !!!next-token;
5904              next B;
5905          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5906                   {                   {
5907                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5908                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5909                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5910  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5911                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5912                                
5913                ## have an element in table scope            ## have an element in table scope
5914                my $i;            my $i;
5915                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5916                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5917                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5918                    !!!cp ('t292');                !!!cp ('t292');
5919                    $i = $_;                $i = $_;
5920                    last INSCOPE;                last INSCOPE;
5921                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5922                            table => 1, html => 1,                !!!cp ('t293');
5923                           }->{$node->[1]}) {                last INSCOPE;
5924                    !!!cp ('t293');              }
5925                    last INSCOPE;            } # INSCOPE
5926                  }            unless (defined $i) {
5927                } # INSCOPE              !!!cp ('t294');
5928                unless (defined $i) {              ## Ignore the token
5929                  !!!cp ('t294');              !!!nack ('t294.1');
5930                  ## Ignore the token              !!!next-token;
5931                  !!!next-token;              next B;
5932                  redo B;            }
               }  
5933                                
5934                ## As if </select>            ## As if </select>
5935                ## have an element in table scope            ## have an element in table scope
5936                undef $i;            undef $i;
5937                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5938                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5939                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5940                    !!!cp ('t295');                !!!cp ('t295');
5941                    $i = $_;                $i = $_;
5942                    last INSCOPE;                last INSCOPE;
5943                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5944  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5945                    !!!cp ('t296');                !!!cp ('t296');
5946                    last INSCOPE;                last INSCOPE;
5947                  }              }
5948                } # INSCOPE            } # INSCOPE
5949                unless (defined $i) {            unless (defined $i) {
5950                  !!!cp ('t297');              !!!cp ('t297');
5951  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5952                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5953                  ## Ignore the </select> token              ## Ignore the </select> token
5954                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5955                  redo B;              !!!next-token; ## TODO: ok?
5956                }              next B;
5957              }
5958                                
5959                !!!cp ('t298');            !!!cp ('t298');
5960                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5961    
5962                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5963    
5964                ## reprocess            !!!ack-later;
5965                redo B;            ## reprocess
5966              next B;
5967          } else {          } else {
5968            !!!cp ('t299');            !!!cp ('t299');
5969            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5970            ## Ignore the token            ## Ignore the token
5971              !!!nack ('t299.3');
5972            !!!next-token;            !!!next-token;
5973            redo B;            next B;
5974          }          }
5975        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5976          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5977                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5978            !!!cp ('t299.1');            !!!cp ('t299.1');
5979            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 5998  sub _tree_construction_main ($) {
5998            unless (length $token->{data}) {            unless (length $token->{data}) {
5999              !!!cp ('t300');              !!!cp ('t300');
6000              !!!next-token;              !!!next-token;
6001              redo B;              next B;
6002            }            }
6003          }          }
6004                    
# Line 5335  sub _tree_construction_main ($) { Line 6016  sub _tree_construction_main ($) {
6016    
6017          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6018          ## reprocess          ## reprocess
6019          redo B;          next B;
6020        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6021          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6022            !!!cp ('t303');            !!!cp ('t303');
# Line 5350  sub _tree_construction_main ($) { Line 6031  sub _tree_construction_main ($) {
6031          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6032    
6033          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6034            !!!ack-later;
6035          ## reprocess          ## reprocess
6036          redo B;          next B;
6037        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6038          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6039            !!!cp ('t305');            !!!cp ('t305');
# Line 5370  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6053              ## Ignore the token              ## Ignore the token
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            } else {            } else {
6057              !!!cp ('t308');              !!!cp ('t308');
6058              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6059              !!!next-token;              !!!next-token;
6060              redo B;              next B;
6061            }            }
6062          } else {          } else {
6063            !!!cp ('t309');            !!!cp ('t309');
# Line 5383  sub _tree_construction_main ($) { Line 6065  sub _tree_construction_main ($) {
6065    
6066            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6067            ## reprocess            ## reprocess
6068            redo B;            next B;
6069          }          }
6070        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6071          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6082  sub _tree_construction_main ($) {
6082            unless (length $token->{data}) {            unless (length $token->{data}) {
6083              !!!cp ('t310');              !!!cp ('t310');
6084              !!!next-token;              !!!next-token;
6085              redo B;              next B;
6086            }            }
6087          }          }
6088                    
# Line 5428  sub _tree_construction_main ($) { Line 6110  sub _tree_construction_main ($) {
6110              !!!cp ('t315');              !!!cp ('t315');
6111              !!!next-token;              !!!next-token;
6112            }            }
6113            redo B;            next B;
6114          }          }
6115                    
6116          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5447  sub _tree_construction_main ($) { Line 6129  sub _tree_construction_main ($) {
6129              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6130            !!!cp ('t318');            !!!cp ('t318');
6131            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6132              !!!nack ('t318.1');
6133            !!!next-token;            !!!next-token;
6134            redo B;            next B;
6135          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6136                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6137            !!!cp ('t319');            !!!cp ('t319');
6138            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6139            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6140              !!!ack ('t319.1');
6141            !!!next-token;            !!!next-token;
6142            redo B;            next B;
6143          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6144            !!!cp ('t320');            !!!cp ('t320');
6145            ## NOTE: As if in body.            ## NOTE: As if in body.
6146            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6147            redo B;            next B;
6148          } else {          } else {
6149            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6150              !!!cp ('t321');              !!!cp ('t321');
# Line 5470  sub _tree_construction_main ($) { Line 6154  sub _tree_construction_main ($) {
6154              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6155            }            }
6156            ## Ignore the token            ## Ignore the token
6157              !!!nack ('t322.1');
6158            !!!next-token;            !!!next-token;
6159            redo B;            next B;
6160          }          }
6161        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6162          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5486  sub _tree_construction_main ($) { Line 6171  sub _tree_construction_main ($) {
6171    
6172          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6173              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6174            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6175                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6176              !!!cp ('t325');              !!!cp ('t325');
6177              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5499  sub _tree_construction_main ($) { Line 6184  sub _tree_construction_main ($) {
6184            }            }
6185    
6186            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6187                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6188              !!!cp ('t327');              !!!cp ('t327');
6189              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6190            } else {            } else {
6191              !!!cp ('t328');              !!!cp ('t328');
6192            }            }
6193            redo B;            next B;
6194          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6195                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6196            !!!cp ('t329');            !!!cp ('t329');
6197            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6198            !!!next-token;            !!!next-token;
6199            redo B;            next B;
6200          } else {          } else {
6201            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6202              !!!cp ('t330');              !!!cp ('t330');
# Line 5522  sub _tree_construction_main ($) { Line 6207  sub _tree_construction_main ($) {
6207            }            }
6208            ## Ignore the token            ## Ignore the token
6209            !!!next-token;            !!!next-token;
6210            redo B;            next B;
6211          }          }
6212        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6213          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6214                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6215            !!!cp ('t331.1');            !!!cp ('t331.1');
6216            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6235  sub _tree_construction_main ($) {
6235          !!!cp ('t332');          !!!cp ('t332');
6236          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6237          $script_start_tag->();          $script_start_tag->();
6238          redo B;          next B;
6239        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6240          !!!cp ('t333');          !!!cp ('t333');
6241          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6242          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6243          redo B;          next B;
6244        } elsif ({        } elsif ({
6245                  base => 1, link => 1,                  base => 1, link => 1,
6246                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5563  sub _tree_construction_main ($) { Line 6248  sub _tree_construction_main ($) {
6248          ## 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
6249          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6250          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6251            !!!ack ('t334.1');
6252          !!!next-token;          !!!next-token;
6253          redo B;          next B;
6254        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6255          ## 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
6256          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6257          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.
6258    
6259          unless ($self->{confident}) {          unless ($self->{confident}) {
6260            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6261              !!!cp ('t335');              !!!cp ('t335');
6262                ## NOTE: Whether the encoding is supported or not is handled
6263                ## in the {change_encoding} callback.
6264              $self->{change_encoding}              $self->{change_encoding}
6265                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6266                            
# Line 5581  sub _tree_construction_main ($) { Line 6269  sub _tree_construction_main ($) {
6269                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6270                                           ->{has_reference});                                           ->{has_reference});
6271            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6272              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6273                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6274                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6275                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6276                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6277                !!!cp ('t336');                !!!cp ('t336');
6278                  ## NOTE: Whether the encoding is supported or not is handled
6279                  ## in the {change_encoding} callback.
6280                $self->{change_encoding}                $self->{change_encoding}
6281                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6282                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5613  sub _tree_construction_main ($) { Line 6302  sub _tree_construction_main ($) {
6302            }            }
6303          }          }
6304    
6305            !!!ack ('t338.1');
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6309          !!!cp ('t341');          !!!cp ('t341');
6310          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6311          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6312          redo B;          next B;
6313        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6314          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6315                                
6316          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6317              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6318            !!!cp ('t342');            !!!cp ('t342');
6319            ## Ignore the token            ## Ignore the token
6320          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6328  sub _tree_construction_main ($) {
6328              }              }
6329            }            }
6330          }          }
6331            !!!nack ('t343.1');
6332          !!!next-token;          !!!next-token;
6333          redo B;          next B;
6334        } elsif ({        } elsif ({
6335                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6336                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6345  sub _tree_construction_main ($) {
6345            !!!cp ('t350');            !!!cp ('t350');
6346            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6347            ## Ignore the token            ## Ignore the token
6348              !!!nack ('t350.1');
6349            !!!next-token;            !!!next-token;
6350            redo B;            next B;
6351          }          }
6352    
6353          ## has a p element in scope          ## has a p element in scope
6354          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6355            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6356              !!!cp ('t344');              !!!cp ('t344');
6357              !!!back-token;              !!!back-token; # <form>
6358              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6359                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6360              redo B;              next B;
6361            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6362              !!!cp ('t345');              !!!cp ('t345');
6363              last INSCOPE;              last INSCOPE;
6364            }            }
# Line 5677  sub _tree_construction_main ($) { Line 6366  sub _tree_construction_main ($) {
6366                        
6367          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6368          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6369              !!!nack ('t346.1');
6370            !!!next-token;            !!!next-token;
6371            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6372              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383            !!!cp ('t347.1');            !!!cp ('t347.1');
6384            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6385    
6386              !!!nack ('t347.2');
6387            !!!next-token;            !!!next-token;
6388          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6389            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6391  sub _tree_construction_main ($) {
6391                        
6392            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6393    
6394              !!!nack ('t382.1');
6395            !!!next-token;            !!!next-token;
6396          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6397            !!!cp ('t386');            !!!cp ('t386');
6398            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6399                    
6400              !!!nack ('t386.1');
6401            !!!next-token;            !!!next-token;
6402          } else {          } else {
6403            !!!cp ('t347');            !!!nack ('t347.1');
6404            !!!next-token;            !!!next-token;
6405          }          }
6406          redo B;          next B;
6407        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6408          ## has a p element in scope          ## has a p element in scope
6409          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6410            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6411              !!!cp ('t353');              !!!cp ('t353');
6412              !!!back-token;              !!!back-token; # <x>
6413              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6414                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6415              redo B;              next B;
6416            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6417              !!!cp ('t354');              !!!cp ('t354');
6418              last INSCOPE;              last INSCOPE;
6419            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6427  sub _tree_construction_main ($) {
6427                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6428          LI: {          LI: {
6429            ## Step 2            ## Step 2
6430            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6431              if ($i != -1) {              if ($i != -1) {
6432                !!!cp ('t355');                !!!cp ('t355');
6433                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6434                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6435                                      ->manakai_local_name,
6436                                  token => $token);
6437              } else {              } else {
6438                !!!cp ('t356');                !!!cp ('t356');
6439              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6444  sub _tree_construction_main ($) {
6444            }            }
6445                        
6446            ## Step 3            ## Step 3
6447            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6448                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6449                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6450                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6451                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6452                  not ($node->[1] & DIV_EL)) {
6453              !!!cp ('t358');              !!!cp ('t358');
6454              last LI;              last LI;
6455            }            }
# Line 5769  sub _tree_construction_main ($) { Line 6462  sub _tree_construction_main ($) {
6462          } # LI          } # LI
6463                        
6464          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465            !!!nack ('t359.1');
6466          !!!next-token;          !!!next-token;
6467          redo B;          next B;
6468        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6469          ## has a p element in scope          ## has a p element in scope
6470          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6471            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6472              !!!cp ('t367');              !!!cp ('t367');
6473              !!!back-token;              !!!back-token; # <plaintext>
6474              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6475                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6476              redo B;              next B;
6477            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6478              !!!cp ('t368');              !!!cp ('t368');
6479              last INSCOPE;              last INSCOPE;
6480            }            }
# Line 5793  sub _tree_construction_main ($) { Line 6484  sub _tree_construction_main ($) {
6484                        
6485          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6486                        
6487            !!!nack ('t368.1');
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
6490        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6491          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6492            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6493            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6494              !!!cp ('t371');              !!!cp ('t371');
6495              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6496                            
6497              !!!back-token;              !!!back-token; # <a>
6498              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6499                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6500              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5833  sub _tree_construction_main ($) { Line 6525  sub _tree_construction_main ($) {
6525          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6526          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6527    
6528            !!!nack ('t374.1');
6529          !!!next-token;          !!!next-token;
6530          redo B;          next B;
6531        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6532          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6533    
6534          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6535          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6536            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6537            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6538              !!!cp ('t376');              !!!cp ('t376');
6539              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6540              !!!back-token;              !!!back-token; # <nobr>
6541              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6542                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6543              redo B;              next B;
6544            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6545              !!!cp ('t377');              !!!cp ('t377');
6546              last INSCOPE;              last INSCOPE;
6547            }            }
# Line 5860  sub _tree_construction_main ($) { Line 6550  sub _tree_construction_main ($) {
6550          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6551          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6552                    
6553            !!!nack ('t377.1');
6554          !!!next-token;          !!!next-token;
6555          redo B;          next B;
6556        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6557          ## has a button element in scope          ## has a button element in scope
6558          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6559            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6560            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6561              !!!cp ('t378');              !!!cp ('t378');
6562              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6563              !!!back-token;              !!!back-token; # <button>
6564              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6565                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6566              redo B;              next B;
6567            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6568              !!!cp ('t379');              !!!cp ('t379');
6569              last INSCOPE;              last INSCOPE;
6570            }            }
# Line 5890  sub _tree_construction_main ($) { Line 6578  sub _tree_construction_main ($) {
6578    
6579          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6580    
6581            !!!nack ('t379.1');
6582          !!!next-token;          !!!next-token;
6583          redo B;          next B;
6584        } elsif ({        } elsif ({
6585                  xmp => 1,                  xmp => 1,
6586                  iframe => 1,                  iframe => 1,
# Line 5907  sub _tree_construction_main ($) { Line 6596  sub _tree_construction_main ($) {
6596          }          }
6597          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6598          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6599          redo B;          next B;
6600        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6601          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6602                    
6603          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6604            !!!cp ('t389');            !!!cp ('t389');
6605            ## Ignore the token            ## Ignore the token
6606              !!!nack ('t389'); ## NOTE: Not acknowledged.
6607            !!!next-token;            !!!next-token;
6608            redo B;            next B;
6609          } else {          } else {
6610            my $at = $token->{attributes};            my $at = $token->{attributes};
6611            my $form_attrs;            my $form_attrs;
# Line 5938  sub _tree_construction_main ($) { Line 6628  sub _tree_construction_main ($) {
6628            if ($prompt_attr) {            if ($prompt_attr) {
6629              !!!cp ('t390');              !!!cp ('t390');
6630              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6631                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
6632                              };
6633            } else {            } else {
6634              !!!cp ('t391');              !!!cp ('t391');
6635              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6636                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
6637                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
6638                              }; # SHOULD
6639              ## TODO: make this configurable              ## TODO: make this configurable
6640            }            }
6641            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 6650  sub _tree_construction_main ($) {
6650                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6651                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6652                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
6653            $token = shift @tokens;            !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6654            !!!back-token (@tokens);            !!!back-token (@tokens);
6655            redo B;            !!!next-token;
6656              next B;
6657          }          }
6658        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6659          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6660          my $el;          my $el;
6661          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6662                    
6663          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6664          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 6667  sub _tree_construction_main ($) {
6667          $insert->($el);          $insert->($el);
6668                    
6669          my $text = '';          my $text = '';
6670            !!!nack ('t392.1');
6671          !!!next-token;          !!!next-token;
6672          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6673            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6007  sub _tree_construction_main ($) { Line 6701  sub _tree_construction_main ($) {
6701            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6702          }          }
6703          !!!next-token;          !!!next-token;
6704          redo B;          next B;
6705          } elsif ($token->{tag_name} eq 'math' or
6706                   $token->{tag_name} eq 'svg') {
6707            $reconstruct_active_formatting_elements->($insert_to_current);
6708    
6709            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6710    
6711            ## "adjust foreign attributes" - done in insert-element-f
6712            
6713            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6714            
6715            if ($self->{self_closing}) {
6716              pop @{$self->{open_elements}};
6717              !!!ack ('t398.1');
6718            } else {
6719              !!!cp ('t398.2');
6720              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6721              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6722              ## mode, "in body" (not "in foreign content") secondary insertion
6723              ## mode, maybe.
6724            }
6725    
6726            !!!next-token;
6727            next B;
6728        } elsif ({        } elsif ({
6729                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6730                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6017  sub _tree_construction_main ($) { Line 6734  sub _tree_construction_main ($) {
6734          !!!cp ('t401');          !!!cp ('t401');
6735          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6736          ## Ignore the token          ## Ignore the token
6737            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6738          !!!next-token;          !!!next-token;
6739          redo B;          next B;
6740                    
6741          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6742        } else {        } else {
# Line 6040  sub _tree_construction_main ($) { Line 6758  sub _tree_construction_main ($) {
6758              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6759            !!!cp ('t380');            !!!cp ('t380');
6760            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6761              !!!nack ('t380.1');
6762          } elsif ({          } elsif ({
6763                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6764                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 6766  sub _tree_construction_main ($) {
6766                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6767            !!!cp ('t375');            !!!cp ('t375');
6768            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6769              !!!nack ('t375.1');
6770          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6771            !!!cp ('t388');            !!!cp ('t388');
6772            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6773            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6774              !!!ack ('t388.2');
6775          } elsif ({          } elsif ({
6776                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6777                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6058  sub _tree_construction_main ($) { Line 6779  sub _tree_construction_main ($) {
6779                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6780            !!!cp ('t388.1');            !!!cp ('t388.1');
6781            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6782              !!!ack ('t388.3');
6783          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6784            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6785                    
# Line 6070  sub _tree_construction_main ($) { Line 6792  sub _tree_construction_main ($) {
6792              !!!cp ('t400.2');              !!!cp ('t400.2');
6793              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6794            }            }
6795              !!!nack ('t400.3');
6796          } else {          } else {
6797            !!!cp ('t402');            !!!nack ('t402');
6798          }          }
6799                    
6800          !!!next-token;          !!!next-token;
6801          redo B;          next B;
6802        }        }
6803      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6804        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 6806  sub _tree_construction_main ($) {
6806          my $i;          my $i;
6807          INSCOPE: {          INSCOPE: {
6808            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6809              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6810                !!!cp ('t405');                !!!cp ('t405');
6811                $i = $_;                $i = $_;
6812                last INSCOPE;                last INSCOPE;
6813              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6814                !!!cp ('t405.1');                !!!cp ('t405.1');
6815                last;                last;
6816              }              }
# Line 6100  sub _tree_construction_main ($) { Line 6820  sub _tree_construction_main ($) {
6820                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6821            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6822            !!!next-token;            !!!next-token;
6823            redo B;            next B;
6824          } # INSCOPE          } # INSCOPE
6825    
6826          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6827            unless ({            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                    th => 1, tr => 1, body => 1, html => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$_->[1]}) {  
6828              !!!cp ('t403');              !!!cp ('t403');
6829              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6830                                value => $_->[0]->manakai_local_name,
6831                                token => $token);
6832              last;              last;
6833            } else {            } else {
6834              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 6837  sub _tree_construction_main ($) {
6837    
6838          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6839          !!!next-token;          !!!next-token;
6840          redo B;          next B;
6841        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6842          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
6843            ## up-to-date, though it has same effect as speced.
6844            if (@{$self->{open_elements}} > 1 and
6845                $self->{open_elements}->[1]->[1] & BODY_EL) {
6846            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6847            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6848              !!!cp ('t406');              !!!cp ('t406');
6849              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6850                                value => $self->{open_elements}->[1]->[0]
6851                                    ->manakai_local_name,
6852                                token => $token);
6853            } else {            } else {
6854              !!!cp ('t407');              !!!cp ('t407');
6855            }            }
6856            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6857            ## reprocess            ## reprocess
6858            redo B;            next B;
6859          } else {          } else {
6860            !!!cp ('t408');            !!!cp ('t408');
6861            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6862            ## Ignore the token            ## Ignore the token
6863            !!!next-token;            !!!next-token;
6864            redo B;            next B;
6865          }          }
6866        } elsif ({        } elsif ({
6867                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 6874  sub _tree_construction_main ($) {
6874          my $i;          my $i;
6875          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6876            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6877            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6878              !!!cp ('t410');              !!!cp ('t410');
6879              $i = $_;              $i = $_;
6880              last INSCOPE;              last INSCOPE;
6881            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6882              !!!cp ('t411');              !!!cp ('t411');
6883              last INSCOPE;              last INSCOPE;
6884            }            }
# Line 6173  sub _tree_construction_main ($) { Line 6894  sub _tree_construction_main ($) {
6894                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6895                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6896                    p => 1,                    p => 1,
6897                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6898              !!!cp ('t409');              !!!cp ('t409');
6899              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6900            }            }
6901    
6902            ## Step 2.            ## Step 2.
6903            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6904                      ne $token->{tag_name}) {
6905              !!!cp ('t412');              !!!cp ('t412');
6906              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6907                                value => $self->{open_elements}->[-1]->[0]
6908                                    ->manakai_local_name,
6909                                token => $token);
6910            } else {            } else {
6911              !!!cp ('t414');              !!!cp ('t414');
6912            }            }
# Line 6196  sub _tree_construction_main ($) { Line 6921  sub _tree_construction_main ($) {
6921                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6922          }          }
6923          !!!next-token;          !!!next-token;
6924          redo B;          next B;
6925        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6926          undef $self->{form_element};          undef $self->{form_element};
6927    
# Line 6204  sub _tree_construction_main ($) { Line 6929  sub _tree_construction_main ($) {
6929          my $i;          my $i;
6930          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6931            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6932            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6933              !!!cp ('t418');              !!!cp ('t418');
6934              $i = $_;              $i = $_;
6935              last INSCOPE;              last INSCOPE;
6936            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6937              !!!cp ('t419');              !!!cp ('t419');
6938              last INSCOPE;              last INSCOPE;
6939            }            }
# Line 6222  sub _tree_construction_main ($) { Line 6944  sub _tree_construction_main ($) {
6944            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6945          } else {          } else {
6946            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6947            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6948              !!!cp ('t417');              !!!cp ('t417');
6949              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6950            }            }
6951                        
6952            ## Step 2.            ## Step 2.
6953            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6954                      ne $token->{tag_name}) {
6955              !!!cp ('t417.1');              !!!cp ('t417.1');
6956              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6957                                value => $self->{open_elements}->[-1]->[0]
6958                                    ->manakai_local_name,
6959                                token => $token);
6960            } else {            } else {
6961              !!!cp ('t420');              !!!cp ('t420');
6962            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 6966  sub _tree_construction_main ($) {
6966          }          }
6967    
6968          !!!next-token;          !!!next-token;
6969          redo B;          next B;
6970        } elsif ({        } elsif ({
6971                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6972                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 6974  sub _tree_construction_main ($) {
6974          my $i;          my $i;
6975          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6976            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6977            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6978              !!!cp ('t423');              !!!cp ('t423');
6979              $i = $_;              $i = $_;
6980              last INSCOPE;              last INSCOPE;
6981            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6982              !!!cp ('t424');              !!!cp ('t424');
6983              last INSCOPE;              last INSCOPE;
6984            }            }
# Line 6270  sub _tree_construction_main ($) { Line 6989  sub _tree_construction_main ($) {
6989            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6990          } else {          } else {
6991            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6992            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6993              !!!cp ('t422');              !!!cp ('t422');
6994              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6995            }            }
6996                        
6997            ## Step 2.            ## Step 2.
6998            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6999                      ne $token->{tag_name}) {
7000              !!!cp ('t425');              !!!cp ('t425');
7001              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7002            } else {            } else {
# Line 6290  sub _tree_construction_main ($) { Line 7008  sub _tree_construction_main ($) {
7008          }          }
7009                    
7010          !!!next-token;          !!!next-token;
7011          redo B;          next B;
7012        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7013          ## has an element in scope          ## has an element in scope
7014          my $i;          my $i;
7015          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7017            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7018              !!!cp ('t410.1');              !!!cp ('t410.1');
7019              $i = $_;              $i = $_;
7020              last INSCOPE;              last INSCOPE;
7021            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7022              !!!cp ('t411.1');              !!!cp ('t411.1');
7023              last INSCOPE;              last INSCOPE;
7024            }            }
7025          } # INSCOPE          } # INSCOPE
7026    
7027          if (defined $i) {          if (defined $i) {
7028            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7029                      ne $token->{tag_name}) {
7030              !!!cp ('t412.1');              !!!cp ('t412.1');
7031              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7032                                value => $self->{open_elements}->[-1]->[0]
7033                                    ->manakai_local_name,
7034                                token => $token);
7035            } else {            } else {
7036              !!!cp ('t414.1');              !!!cp ('t414.1');
7037            }            }
# Line 6325  sub _tree_construction_main ($) { Line 7044  sub _tree_construction_main ($) {
7044            !!!cp ('t415.1');            !!!cp ('t415.1');
7045            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7046            my $el;            my $el;
7047            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7048            $insert->($el);            $insert->($el);
7049            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7050          }          }
7051    
7052          !!!next-token;          !!!next-token;
7053          redo B;          next B;
7054        } elsif ({        } elsif ({
7055                  a => 1,                  a => 1,
7056                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6340  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7060          !!!cp ('t427');          !!!cp ('t427');
7061          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7062          redo B;          next B;
7063        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7064          !!!cp ('t428');          !!!cp ('t428');
7065          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6349  sub _tree_construction_main ($) { Line 7068  sub _tree_construction_main ($) {
7068          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7069                    
7070          my $el;          my $el;
7071          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7072          $insert->($el);          $insert->($el);
7073                    
7074          ## Ignore the token.          ## Ignore the token.
7075          !!!next-token;          !!!next-token;
7076          redo B;          next B;
7077        } elsif ({        } elsif ({
7078                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7079                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6371  sub _tree_construction_main ($) { Line 7090  sub _tree_construction_main ($) {
7090          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7091          ## Ignore the token          ## Ignore the token
7092          !!!next-token;          !!!next-token;
7093          redo B;          next B;
7094                    
7095          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7096                    
# Line 6382  sub _tree_construction_main ($) { Line 7101  sub _tree_construction_main ($) {
7101    
7102          ## Step 2          ## Step 2
7103          S2: {          S2: {
7104            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7105              ## Step 1              ## Step 1
7106              ## generate implied end tags              ## generate implied end tags
7107              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7108                !!!cp ('t430');                !!!cp ('t430');
7109                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7110                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7111              }              }
7112                    
7113              ## Step 2              ## Step 2
7114              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7115                        ne $token->{tag_name}) {
7116                !!!cp ('t431');                !!!cp ('t431');
7117                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7118                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7119                                  value => $self->{open_elements}->[-1]->[0]
7120                                      ->manakai_local_name,
7121                                  token => $token);
7122              } else {              } else {
7123                !!!cp ('t432');                !!!cp ('t432');
7124              }              }
# Line 6409  sub _tree_construction_main ($) { Line 7130  sub _tree_construction_main ($) {
7130              last S2;              last S2;
7131            } else {            } else {
7132              ## Step 3              ## Step 3
7133              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7134                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7135                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7136                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7137                !!!cp ('t433');                !!!cp ('t433');
7138                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7139                ## Ignore the token                ## Ignore the token
# Line 6430  sub _tree_construction_main ($) { Line 7151  sub _tree_construction_main ($) {
7151            ## Step 5;            ## Step 5;
7152            redo S2;            redo S2;
7153          } # S2          } # S2
7154          redo B;          next B;
7155        }        }
7156      }      }
7157      redo B;      next B;
7158      } continue { # B
7159        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7160          ## NOTE: The code below is executed in cases where it does not have
7161          ## to be, but it it is harmless even in those cases.
7162          ## has an element in scope
7163          INSCOPE: {
7164            for (reverse 0..$#{$self->{open_elements}}) {
7165              my $node = $self->{open_elements}->[$_];
7166              if ($node->[1] & FOREIGN_EL) {
7167                last INSCOPE;
7168              } elsif ($node->[1] & SCOPING_EL) {
7169                last;
7170              }
7171            }
7172            
7173            ## NOTE: No foreign element in scope.
7174            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7175          } # INSCOPE
7176        }
7177    } # B    } # B
7178    
7179    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6479  sub set_inner_html ($$$) { Line 7219  sub set_inner_html ($$$) {
7219    
7220      ## Step 8 # MUST      ## Step 8 # MUST
7221      my $i = 0;      my $i = 0;
7222      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7223      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7224      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7225        my $self = shift;        my $self = shift;
7226    
# Line 6489  sub set_inner_html ($$$) { Line 7229  sub set_inner_html ($$$) {
7229    
7230        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7231        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7232        $column++;  
7233          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7234          $p->{column}++;
7235    
7236        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7237          $line++;          $p->{line}++;
7238          $column = 0;          $p->{column} = 0;
7239          !!!cp ('i1');          !!!cp ('i1');
7240        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7241          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7242          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7243          $line++;          $p->{line}++;
7244          $column = 0;          $p->{column} = 0;
7245          !!!cp ('i2');          !!!cp ('i2');
7246        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7247          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7250  sub set_inner_html ($$$) {
7250          !!!cp ('i4');          !!!cp ('i4');
7251          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7252          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7253          } elsif ($self->{next_char} <= 0x0008 or
7254                   (0x000E <= $self->{next_char} and
7255                    $self->{next_char} <= 0x001F) or
7256                   (0x007F <= $self->{next_char} and
7257                    $self->{next_char} <= 0x009F) or
7258                   (0xD800 <= $self->{next_char} and
7259                    $self->{next_char} <= 0xDFFF) or
7260                   (0xFDD0 <= $self->{next_char} and
7261                    $self->{next_char} <= 0xFDDF) or
7262                   {
7263                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7264                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7265                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7266                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7267                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7268                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7269                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7270                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7271                    0x10FFFE => 1, 0x10FFFF => 1,
7272                   }->{$self->{next_char}}) {
7273            !!!cp ('i4.1');
7274            !!!parse-error (type => 'control char', level => $self->{must_level});
7275    ## TODO: error type documentation
7276        }        }
7277      };      };
7278      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6515  sub set_inner_html ($$$) { Line 7280  sub set_inner_html ($$$) {
7280            
7281      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7282        my (%opt) = @_;        my (%opt) = @_;
7283        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7284          my $column = $opt{column};
7285          if (defined $opt{token} and defined $opt{token}->{line}) {
7286            $line = $opt{token}->{line};
7287            $column = $opt{token}->{column};
7288          }
7289          warn "Parse error ($opt{type}) at line $line column $column\n";
7290      };      };
7291      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7292        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7293      };      };
7294            
7295      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7313  sub set_inner_html ($$$) {
7313          unless defined $p->{content_model};          unless defined $p->{content_model};
7314          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7315    
7316      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7317          ## TODO: Foreign element OK?
7318    
7319      ## Step 3      ## Step 3
7320      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7324  sub set_inner_html ($$$) {
7324      $doc->append_child ($root);      $doc->append_child ($root);
7325    
7326      ## Step 5 # MUST      ## Step 5 # MUST
7327      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7328    
7329      undef $p->{head_element};      undef $p->{head_element};
7330    
# Line 6598  sub set_inner_html ($$$) { Line 7370  sub set_inner_html ($$$) {
7370      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7371    
7372      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7373    
7374        delete $p->{parse_error}; # delete loop
7375    } else {    } else {
7376      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";
7377    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24