/[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.115 by wakaba, Sun Mar 16 23:53:48 2008 UTC revision 1.150 by wakaba, Sun Jun 1 06:47:08 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    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributename => 'attributeName',
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      filterres => 'filterRes',
226      filterunits => 'filterUnits',
227      glyphref => 'glyphRef',
228      gradienttransform => 'gradientTransform',
229      gradientunits => 'gradientUnits',
230      kernelmatrix => 'kernelMatrix',
231      kernelunitlength => 'kernelUnitLength',
232      keypoints => 'keyPoints',
233      keysplines => 'keySplines',
234      keytimes => 'keyTimes',
235      lengthadjust => 'lengthAdjust',
236      limitingconeangle => 'limitingConeAngle',
237      markerheight => 'markerHeight',
238      markerunits => 'markerUnits',
239      markerwidth => 'markerWidth',
240      maskcontentunits => 'maskContentUnits',
241      maskunits => 'maskUnits',
242      numoctaves => 'numOctaves',
243      pathlength => 'pathLength',
244      patterncontentunits => 'patternContentUnits',
245      patterntransform => 'patternTransform',
246      patternunits => 'patternUnits',
247      pointsatx => 'pointsAtX',
248      pointsaty => 'pointsAtY',
249      pointsatz => 'pointsAtZ',
250      preservealpha => 'preserveAlpha',
251      preserveaspectratio => 'preserveAspectRatio',
252      primitiveunits => 'primitiveUnits',
253      refx => 'refX',
254      refy => 'refY',
255      repeatcount => 'repeatCount',
256      repeatdur => 'repeatDur',
257      requiredextensions => 'requiredExtensions',
258      requiredfeatures => 'requiredFeatures',
259      specularconstant => 'specularConstant',
260      specularexponent => 'specularExponent',
261      spreadmethod => 'spreadMethod',
262      startoffset => 'startOffset',
263      stddeviation => 'stdDeviation',
264      stitchtiles => 'stitchTiles',
265      surfacescale => 'surfaceScale',
266      systemlanguage => 'systemLanguage',
267      tablevalues => 'tableValues',
268      targetx => 'targetX',
269      targety => 'targetY',
270      textlength => 'textLength',
271      viewbox => 'viewBox',
272      viewtarget => 'viewTarget',
273      xchannelselector => 'xChannelSelector',
274      ychannelselector => 'yChannelSelector',
275      zoomandpan => 'zoomAndPan',
276  };  };
277    
278    my $foreign_attr_xname = {
279      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
280      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
281      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
282      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
283      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
284      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
285      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
286      'xml:base' => [$XML_NS, ['xml', 'base']],
287      'xml:lang' => [$XML_NS, ['xml', 'lang']],
288      'xml:space' => [$XML_NS, ['xml', 'space']],
289      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
290      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
291    };
292    
293    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
294    
295  my $c1_entity_char = {  my $c1_entity_char = {
296    0x80 => 0x20AC,    0x80 => 0x20AC,
297    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 327  my $c1_entity_char = {
327    0x9F => 0x0178,    0x9F => 0x0178,
328  }; # $c1_entity_char  }; # $c1_entity_char
329    
 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  
   
330  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
331      my $self = shift;
332      my $charset_name = shift;
333      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
334      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
335    } # parse_byte_string
336    
337    sub parse_byte_stream ($$$$;$) {
338    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
339    my $charset = shift;    my $charset_name = shift;
340    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
341    
342    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
343      my $self = shift;      my (%opt) = @_;
344      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
345      my $token = shift;    };
346      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
347      ## TODO: normalize charset name  
348      ## HTML5 encoding sniffing algorithm
349      require Message::Charset::Info;
350      my $charset;
351      my $buffer;
352      my ($char_stream, $e_status);
353    
354      ## "Change the encoding" algorithm:    SNIFFING: {
355    
356      ## Step 1          ## Step 1
357      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
358        $charset = 'utf-8';        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
359    
360          ## ISSUE: Unsupported encoding is not ignored according to the spec.
361          ($char_stream, $e_status) = $charset->get_decode_handle
362              ($byte_stream, allow_error_reporting => 1,
363               allow_fallback => 1);
364          if ($char_stream) {
365            $self->{confident} = 1;
366            last SNIFFING;
367          } else {
368            ## TODO: unsupported error
369          }
370      }      }
371    
372      ## Step 2      ## Step 2
373      if (defined $self->{input_encoding} and      my $byte_buffer = '';
374          $self->{input_encoding} eq $charset) {      for (1..1024) {
375          my $char = $byte_stream->getc;
376          last unless defined $char;
377          $byte_buffer .= $char;
378        } ## TODO: timeout
379    
380        ## Step 3
381        if ($byte_buffer =~ /^\xFE\xFF/) {
382          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
383          ($char_stream, $e_status) = $charset->get_decode_handle
384              ($byte_stream, allow_error_reporting => 1,
385               allow_fallback => 1, byte_buffer => \$byte_buffer);
386        $self->{confident} = 1;        $self->{confident} = 1;
387        return;        last SNIFFING;
388        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
389          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
390          ($char_stream, $e_status) = $charset->get_decode_handle
391              ($byte_stream, allow_error_reporting => 1,
392               allow_fallback => 1, byte_buffer => \$byte_buffer);
393          $self->{confident} = 1;
394          last SNIFFING;
395        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
396          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
397          ($char_stream, $e_status) = $charset->get_decode_handle
398              ($byte_stream, allow_error_reporting => 1,
399               allow_fallback => 1, byte_buffer => \$byte_buffer);
400          $self->{confident} = 1;
401          last SNIFFING;
402      }      }
403    
404      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
405          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
406    
407      ## Step 3      ## Step 5
408      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
409    
410      ## Step 4      ## Step 6
411      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
412        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
413            ($byte_buffer);
414        if (defined $charset_name) {
415          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
416    
417          ## ISSUE: Unsupported encoding is not ignored according to the spec.
418          require Whatpm::Charset::DecodeHandle;
419          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
420              ($byte_stream);
421          ($char_stream, $e_status) = $charset->get_decode_handle
422              ($buffer, allow_error_reporting => 1,
423               allow_fallback => 1, byte_buffer => \$byte_buffer);
424          if ($char_stream) {
425            $buffer->{buffer} = $byte_buffer;
426            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
427                            value => $charset_name,
428                            level => $self->{info_level},
429                            line => 1, column => 1);
430            $self->{confident} = 0;
431            last SNIFFING;
432          }
433        }
434    
435        ## Step 7: default
436        ## TODO: Make this configurable.
437        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
438            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
439            ## detectable in the step 6.
440        require Whatpm::Charset::DecodeHandle;
441        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
442            ($byte_stream);
443        ($char_stream, $e_status)
444            = $charset->get_decode_handle ($buffer,
445                                           allow_error_reporting => 1,
446                                           allow_fallback => 1,
447                                           byte_buffer => \$byte_buffer);
448        $buffer->{buffer} = $byte_buffer;
449        !!!parse-error (type => 'sniffing:default', ## TODO: type name
450                        value => 'windows-1252',
451                        level => $self->{info_level},
452                        line => 1, column => 1);
453        $self->{confident} = 0;
454      } # SNIFFING
455    
456      $self->{input_encoding} = $charset->get_iana_name;
457      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
458        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
459                        value => $self->{input_encoding},
460                        level => $self->{unsupported_level},
461                        line => 1, column => 1);
462      } elsif (not ($e_status &
463                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
464        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      }
469    
470      $self->{change_encoding} = sub {
471        my $self = shift;
472        $charset_name = shift;
473        my $token = shift;
474    
475        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
476        ($char_stream, $e_status) = $charset->get_decode_handle
477            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
478             byte_buffer => \ $buffer->{buffer});
479        
480        if ($char_stream) { # if supported
481          ## "Change the encoding" algorithm:
482    
483          ## Step 1    
484          if ($charset->{category} &
485              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
486            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
487            ($char_stream, $e_status) = $charset->get_decode_handle
488                ($byte_stream,
489                 byte_buffer => \ $buffer->{buffer});
490          }
491          $charset_name = $charset->get_iana_name;
492          
493          ## Step 2
494          if (defined $self->{input_encoding} and
495              $self->{input_encoding} eq $charset_name) {
496            !!!parse-error (type => 'charset label:matching', ## TODO: type
497                            value => $charset_name,
498                            level => $self->{info_level});
499            $self->{confident} = 1;
500            return;
501          }
502    
503          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
504              ':'.$charset_name, level => 'w', token => $token);
505          
506          ## Step 3
507          # if (can) {
508            ## change the encoding on the fly.
509            #$self->{confident} = 1;
510            #return;
511          # }
512          
513          ## Step 4
514          throw Whatpm::HTML::RestartParser ();
515        }
516    }; # $self->{change_encoding}    }; # $self->{change_encoding}
517    
518      my $char_onerror = sub {
519        my (undef, $type, %opt) = @_;
520        !!!parse-error (%opt, type => $type,
521                        line => $self->{line}, column => $self->{column} + 1);
522        if ($opt{octets}) {
523          ${$opt{octets}} = "\x{FFFD}"; # relacement character
524        }
525      };
526      $char_stream->onerror ($char_onerror);
527    
528    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
529    my $return;    my $return;
530    try {    try {
531      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
532    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
533      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
534      $s = \ (Encode::decode ($charset, $$bytes_s));      
535      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
536        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
537          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
538                          value => $self->{input_encoding},
539                          level => $self->{unsupported_level},
540                          line => 1, column => 1);
541        } elsif (not ($e_status &
542                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
543          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
544                          value => $self->{input_encoding},
545                          level => $self->{unsupported_level},
546                          line => 1, column => 1);
547        }
548      $self->{confident} = 1;      $self->{confident} = 1;
549      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
550        $return = $self->parse_char_stream ($char_stream, @args);
551    };    };
552    return $return;    return $return;
553  } # parse_byte_string  } # parse_byte_stream
554    
555  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
556  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 163  sub parse_byte_string ($$$$;$) { Line 561  sub parse_byte_string ($$$$;$) {
561  ## 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
562  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
563    
564  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
565      my $self = shift;
566      require utf8;
567      my $s = ref $_[0] ? $_[0] : \($_[0]);
568      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
569      return $self->parse_char_stream ($input, @_[1..$#_]);
570    } # parse_char_string
571    *parse_string = \&parse_char_string;
572    
573  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
574    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
575    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
576    $self->{document} = $_[1];    $self->{document} = $_[1];
577    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
578    
# Line 186  sub parse_string ($$$;$) { Line 591  sub parse_string ($$$;$) {
591      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
592      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
593    
594      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
595      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
596          $char = $self->{next_next_char};
597          delete $self->{next_next_char};
598        } else {
599          $char = $input->getc;
600        }
601        $self->{next_char} = -1 and return unless defined $char;
602        $self->{next_char} = ord $char;
603    
604      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
605          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
606      $self->{column}++;      $self->{column}++;
607            
608      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
609          !!!cp ('j1');
610        $self->{line}++;        $self->{line}++;
611        $self->{column} = 0;        $self->{column} = 0;
612      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
613        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
614          my $next = $input->getc;
615          if (defined $next and $next ne "\x0A") {
616            $self->{next_next_char} = $next;
617          }
618        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
619        $self->{line}++;        $self->{line}++;
620        $self->{column} = 0;        $self->{column} = 0;
621      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
622          !!!cp ('j3');
623        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
624      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
625          !!!cp ('j4');
626        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
627        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
628        } elsif ($self->{next_char} <= 0x0008 or
629                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
630                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
631                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
632                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
633                 {
634                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
635                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
636                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
637                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
638                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
639                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
640                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
641                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
642                  0x10FFFE => 1, 0x10FFFF => 1,
643                 }->{$self->{next_char}}) {
644          !!!cp ('j5');
645          !!!parse-error (type => 'control char', level => $self->{must_level});
646    ## TODO: error type documentation
647      }      }
648    };    };
649    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 667  sub parse_string ($$$;$) {
667    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
668    
669    return $self->{document};    return $self->{document};
670  } # parse_string  } # parse_char_stream
671    
672  sub new ($) {  sub new ($) {
673    my $class = shift;    my $class = shift;
674    my $self = bless {}, $class;    my $self = bless {
675        must_level => 'm',
676        should_level => 's',
677        good_level => 'w',
678        warn_level => 'w',
679        info_level => 'i',
680        unsupported_level => 'u',
681      }, $class;
682    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
683      $self->{next_char} = -1;      $self->{next_char} = -1;
684    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 740  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
740  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
741  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
742  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
743    sub SELF_CLOSING_START_TAG_STATE () { 34 }
744    sub CDATA_BLOCK_STATE () { 35 }
745    
746  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
747  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 759  sub ROW_IMS ()        { 0b10000000 }
759  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
760  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
761  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
762    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
763        ## NOTE: "in foreign content" insertion mode is special; it is combined
764        ## with the secondary insertion mode.  In this parser, they are stored
765        ## together in the bit-or'ed form.
766    
767  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
768    
# Line 348  sub _initialize_tokenizer ($) { Line 799  sub _initialize_tokenizer ($) {
799    undef $self->{current_attribute};    undef $self->{current_attribute};
800    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
801    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
802      delete $self->{self_closing};
803    $self->{char} = [];    $self->{char} = [];
804    # $self->{next_char}    # $self->{next_char}
805    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 820  sub _initialize_tokenizer ($) {
820  ##        ->{value}  ##        ->{value}
821  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
822  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
823    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
824    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
825    ##     while the token is pushed back to the stack.
826    
827  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
828    
# Line 394  sub _initialize_tokenizer ($) { Line 849  sub _initialize_tokenizer ($) {
849    
850  sub _get_next_token ($) {  sub _get_next_token ($) {
851    my $self = shift;    my $self = shift;
852    
853      if ($self->{self_closing}) {
854        !!!parse-error (type => 'nestc', token => $self->{current_token});
855        ## NOTE: The |self_closing| flag is only set by start tag token.
856        ## In addition, when a start tag token is emitted, it is always set to
857        ## |current_token|.
858        delete $self->{self_closing};
859      }
860    
861    if (@{$self->{token}}) {    if (@{$self->{token}}) {
862        $self->{self_closing} = $self->{token}->[0]->{self_closing};
863      return shift @{$self->{token}};      return shift @{$self->{token}};
864    }    }
865    
# Line 466  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931        # Anything else        # Anything else
932        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
933                     data => chr $self->{next_char},                     data => chr $self->{next_char},
934                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
935                      };
936        ## Stay in the data state        ## Stay in the data state
937        !!!next-input-character;        !!!next-input-character;
938    
# Line 486  sub _get_next_token ($) { Line 952  sub _get_next_token ($) {
952        unless (defined $token) {        unless (defined $token) {
953          !!!cp (13);          !!!cp (13);
954          !!!emit ({type => CHARACTER_TOKEN, data => '&',          !!!emit ({type => CHARACTER_TOKEN, data => '&',
955                    line => $l, column => $c});                    line => $l, column => $c,
956                     });
957        } else {        } else {
958          !!!cp (14);          !!!cp (14);
959          !!!emit ($token);          !!!emit ($token);
# Line 507  sub _get_next_token ($) { Line 974  sub _get_next_token ($) {
974    
975            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
976                      line => $self->{line_prev},                      line => $self->{line_prev},
977                      column => $self->{column_prev}});                      column => $self->{column_prev},
978                       });
979    
980            redo A;            redo A;
981          }          }
# Line 553  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021    
1022            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1023                      line => $self->{line_prev},                      line => $self->{line_prev},
1024                      column => $self->{column_prev}});                      column => $self->{column_prev},
1025                       });
1026    
1027            redo A;            redo A;
1028          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1033  sub _get_next_token ($) {
1033            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1034            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1035                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1036                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1037                                       };
1038            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1039            redo A;            redo A;
1040          } else {          } else {
1041            !!!cp (23);            !!!cp (23);
1042            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1043                              line => $self->{line_prev},
1044                              column => $self->{column_prev});
1045            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1046            ## reconsume            ## reconsume
1047    
1048            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1049                      line => $self->{line_prev},                      line => $self->{line_prev},
1050                      column => $self->{column_prev}});                      column => $self->{column_prev},
1051                       });
1052    
1053            redo A;            redo A;
1054          }          }
# Line 604  sub _get_next_token ($) { Line 1077  sub _get_next_token ($) {
1077                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1078    
1079                !!!emit ({type => CHARACTER_TOKEN, data => '</',                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1080                          line => $l, column => $c});                          line => $l, column => $c,
1081                           });
1082        
1083                redo A;                redo A;
1084              }              }
# Line 624  sub _get_next_token ($) { Line 1098  sub _get_next_token ($) {
1098              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1099              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1100              !!!emit ({type => CHARACTER_TOKEN, data => '</',              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1101                        line => $l, column => $c});                        line => $l, column => $c,
1102                         });
1103              redo A;              redo A;
1104            } else {            } else {
1105              !!!cp (27);              !!!cp (27);
# Line 638  sub _get_next_token ($) { Line 1113  sub _get_next_token ($) {
1113            # next-input-character is already done            # next-input-character is already done
1114            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1115            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1116                      line => $l, column => $c});                      line => $l, column => $c,
1117                       });
1118            redo A;            redo A;
1119          }          }
1120        }        }
# Line 677  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153          # reconsume          # reconsume
1154    
1155          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1156                    line => $l, column => $c});                    line => $l, column => $c,
1157                     });
1158    
1159          redo A;          redo A;
1160        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1163  sub _get_next_token ($) {
1163          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1164          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1165                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1166                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1167                                     };
1168          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1169          redo A;          redo A;
1170        }        }
# Line 703  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1182          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1183            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1184            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1185          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1186            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1212  sub _get_next_token ($) {
1212          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1213          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1214            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1215            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1216          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1217            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232    
1233          redo A;          redo A;
1234        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1235            !!!cp (42);
1236            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1237          !!!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  
1238          redo A;          redo A;
1239        } else {        } else {
1240          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1258          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1259            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1260            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1261          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1262            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1278  sub _get_next_token ($) {
1278        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1279                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1280          !!!cp (49);          !!!cp (49);
1281          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1282                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1283                   value => '',
1284                   line => $self->{line}, column => $self->{column}};
1285          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1286          !!!next-input-character;          !!!next-input-character;
1287          redo A;          redo A;
1288        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1289            !!!cp (50);
1290            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1291          !!!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  
1292          redo A;          redo A;
1293        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1294          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1295          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1296            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1297            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1298          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1299            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323          } else {          } else {
1324            !!!cp (56);            !!!cp (56);
1325          }          }
1326          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1327                                value => ''};              = {name => chr ($self->{next_char}),
1328                   value => '',
1329                   line => $self->{line}, column => $self->{column}};
1330          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1336  sub _get_next_token ($) {
1336          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1337              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1338            !!!cp (57);            !!!cp (57);
1339            !!!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});
1340            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1341          } else {          } else {
1342            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1365  sub _get_next_token ($) {
1365          $before_leave->();          $before_leave->();
1366          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1367            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1368            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1369          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1370            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1389  sub _get_next_token ($) {
1389          !!!next-input-character;          !!!next-input-character;
1390          redo A;          redo A;
1391        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1392            !!!cp (64);
1393          $before_leave->();          $before_leave->();
1394            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1395          !!!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  
1396          redo A;          redo A;
1397        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1398          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1399          $before_leave->();          $before_leave->();
1400          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1401            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1402            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1403          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1404            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1449  sub _get_next_token ($) {
1449        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1450          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1451            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1452            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1453          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1454            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1471  sub _get_next_token ($) {
1471        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1472                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1473          !!!cp (76);          !!!cp (76);
1474          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1475                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1476                   value => '',
1477                   line => $self->{line}, column => $self->{column}};
1478          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1482            !!!cp (77);
1483            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1484          !!!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  
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1487          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1488          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1489            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1490            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1491          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1492            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1081  sub _get_next_token ($) { Line 1508  sub _get_next_token ($) {
1508          redo A;          redo A;
1509        } else {        } else {
1510          !!!cp (82);          !!!cp (82);
1511          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1512                                value => ''};              = {name => chr ($self->{next_char}),
1513                   value => '',
1514                   line => $self->{line}, column => $self->{column}};
1515          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;                  redo A;        
# Line 1115  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1567  sub _get_next_token ($) {
1567          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1568          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1569            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1570            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1571          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1572            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1189  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1615          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1616            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1618          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1619            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1233  sub _get_next_token ($) { Line 1656  sub _get_next_token ($) {
1656          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1657          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1658            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1659            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1660          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1661            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1280  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1702          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1703            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1705          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1706            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1305  sub _get_next_token ($) { Line 1724  sub _get_next_token ($) {
1724          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1725          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1726            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1727            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1728          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1729            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1794  sub _get_next_token ($) {
1794        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1795          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1796            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1797            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1798          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1799            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1814  sub _get_next_token ($) {
1814    
1815          redo A;          redo A;
1816        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1817            !!!cp (122);
1818            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1819          !!!next-input-character;          !!!next-input-character;
1820          if ($self->{next_char} == 0x003E and # >          redo A;
1821              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1822              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1823            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1824            !!!cp (122);            !!!cp (122.3);
1825            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1826            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1827              if ($self->{current_token}->{attributes}) {
1828                !!!cp (122.1);
1829                !!!parse-error (type => 'end tag attribute');
1830              } else {
1831                ## NOTE: This state should never be reached.
1832                !!!cp (122.2);
1833              }
1834          } else {          } else {
1835            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1836          }          }
1837          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1838          # next-input-character is already done          ## Reconsume.
1839            !!!emit ($self->{current_token}); # start tag or end tag
1840          redo A;          redo A;
1841        } else {        } else {
1842          !!!cp (124);          !!!cp ('124.1');
1843          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1844          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1845          ## reconsume          ## reconsume
1846          redo A;          redo A;
1847        }        }
1848        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1849          if ($self->{next_char} == 0x003E) { # >
1850            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1851              !!!cp ('124.2');
1852              !!!parse-error (type => 'nestc', token => $self->{current_token});
1853              ## TODO: Different type than slash in start tag
1854              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1855              if ($self->{current_token}->{attributes}) {
1856                !!!cp ('124.4');
1857                !!!parse-error (type => 'end tag attribute');
1858              } else {
1859                !!!cp ('124.5');
1860              }
1861              ## TODO: Test |<title></title/>|
1862            } else {
1863              !!!cp ('124.3');
1864              $self->{self_closing} = 1;
1865            }
1866    
1867            $self->{state} = DATA_STATE;
1868            !!!next-input-character;
1869    
1870            !!!emit ($self->{current_token}); # start tag or end tag
1871    
1872            redo A;
1873          } elsif ($self->{next_char} == -1) {
1874            !!!parse-error (type => 'unclosed tag');
1875            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1876              !!!cp (124.7);
1877              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1878            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1879              if ($self->{current_token}->{attributes}) {
1880                !!!cp (124.5);
1881                !!!parse-error (type => 'end tag attribute');
1882              } else {
1883                ## NOTE: This state should never be reached.
1884                !!!cp (124.6);
1885              }
1886            } else {
1887              die "$0: $self->{current_token}->{type}: Unknown token type";
1888            }
1889            $self->{state} = DATA_STATE;
1890            ## Reconsume.
1891            !!!emit ($self->{current_token}); # start tag or end tag
1892            redo A;
1893          } else {
1894            !!!cp ('124.4');
1895            !!!parse-error (type => 'nestc');
1896            ## TODO: This error type is wrong.
1897            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1898            ## Reconsume.
1899            redo A;
1900          }
1901      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1902        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1903                
# Line 1466  sub _get_next_token ($) { Line 1944  sub _get_next_token ($) {
1944          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1945            !!!cp (127);            !!!cp (127);
1946            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1947                                      line => $l, column => $c};                                      line => $l, column => $c,
1948                                       };
1949            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1950            !!!next-input-character;            !!!next-input-character;
1951            redo A;            redo A;
# Line 1504  sub _get_next_token ($) { Line 1983  sub _get_next_token ($) {
1983                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1984                      $self->{current_token} = {type => DOCTYPE_TOKEN,                      $self->{current_token} = {type => DOCTYPE_TOKEN,
1985                                                quirks => 1,                                                quirks => 1,
1986                                                line => $l, column => $c};                                                line => $l, column => $c,
1987                                                 };
1988                      !!!next-input-character;                      !!!next-input-character;
1989                      redo A;                      redo A;
1990                    } else {                    } else {
# Line 1525  sub _get_next_token ($) { Line 2005  sub _get_next_token ($) {
2005          } else {          } else {
2006            !!!cp (135);            !!!cp (135);
2007          }          }
2008          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2009                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2010                   $self->{next_char} == 0x005B) { # [
2011            !!!next-input-character;
2012            push @next_char, $self->{next_char};
2013            if ($self->{next_char} == 0x0043) { # C
2014              !!!next-input-character;
2015              push @next_char, $self->{next_char};
2016              if ($self->{next_char} == 0x0044) { # D
2017                !!!next-input-character;
2018                push @next_char, $self->{next_char};
2019                if ($self->{next_char} == 0x0041) { # A
2020                  !!!next-input-character;
2021                  push @next_char, $self->{next_char};
2022                  if ($self->{next_char} == 0x0054) { # T
2023                    !!!next-input-character;
2024                    push @next_char, $self->{next_char};
2025                    if ($self->{next_char} == 0x0041) { # A
2026                      !!!next-input-character;
2027                      push @next_char, $self->{next_char};
2028                      if ($self->{next_char} == 0x005B) { # [
2029                        !!!cp (135.1);
2030                        $self->{state} = CDATA_BLOCK_STATE;
2031                        !!!next-input-character;
2032                        redo A;
2033                      } else {
2034                        !!!cp (135.2);
2035                      }
2036                    } else {
2037                      !!!cp (135.3);
2038                    }
2039                  } else {
2040                    !!!cp (135.4);                
2041                  }
2042                } else {
2043                  !!!cp (135.5);
2044                }
2045              } else {
2046                !!!cp (135.6);
2047              }
2048            } else {
2049              !!!cp (135.7);
2050            }
2051        } else {        } else {
2052          !!!cp (136);          !!!cp (136);
2053        }        }
# Line 1534  sub _get_next_token ($) { Line 2057  sub _get_next_token ($) {
2057        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2058        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2059        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2060                                  line => $l, column => $c};                                  line => $l, column => $c,
2061                                   };
2062        redo A;        redo A;
2063                
2064        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 2207  sub _get_next_token ($) { Line 2731  sub _get_next_token ($) {
2731        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2732          !!!cp (217);          !!!cp (217);
2733          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2734          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2735          ## reconsume          ## reconsume
2736    
# Line 2248  sub _get_next_token ($) { Line 2771  sub _get_next_token ($) {
2771          !!!next-input-character;          !!!next-input-character;
2772          redo A;          redo A;
2773        }        }
2774        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2775          my $s = '';
2776          
2777          my ($l, $c) = ($self->{line}, $self->{column});
2778    
2779          CS: while ($self->{next_char} != -1) {
2780            if ($self->{next_char} == 0x005D) { # ]
2781              !!!next-input-character;
2782              if ($self->{next_char} == 0x005D) { # ]
2783                !!!next-input-character;
2784                MDC: {
2785                  if ($self->{next_char} == 0x003E) { # >
2786                    !!!cp (221.1);
2787                    !!!next-input-character;
2788                    last CS;
2789                  } elsif ($self->{next_char} == 0x005D) { # ]
2790                    !!!cp (221.2);
2791                    $s .= ']';
2792                    !!!next-input-character;
2793                    redo MDC;
2794                  } else {
2795                    !!!cp (221.3);
2796                    $s .= ']]';
2797                    #
2798                  }
2799                } # MDC
2800              } else {
2801                !!!cp (221.4);
2802                $s .= ']';
2803                #
2804              }
2805            } else {
2806              !!!cp (221.5);
2807              #
2808            }
2809            $s .= chr $self->{next_char};
2810            !!!next-input-character;
2811          } # CS
2812    
2813          $self->{state} = DATA_STATE;
2814          ## next-input-character done or EOF, which is reconsumed.
2815    
2816          if (length $s) {
2817            !!!cp (221.6);
2818            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2819                      line => $l, column => $c});
2820          } else {
2821            !!!cp (221.7);
2822          }
2823    
2824          redo A;
2825    
2826          ## ISSUE: "text tokens" in spec.
2827          ## TODO: Streaming support
2828      } else {      } else {
2829        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2830      }      }
# Line 2332  sub _tokenize_attempt_to_consume_an_enti Line 2909  sub _tokenize_attempt_to_consume_an_enti
2909          }          }
2910    
2911          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2912                  has_reference => 1, line => $l, column => $c};                  has_reference => 1,
2913                    line => $l, column => $c,
2914                   };
2915        } # X        } # X
2916      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2917               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2375  sub _tokenize_attempt_to_consume_an_enti Line 2954  sub _tokenize_attempt_to_consume_an_enti
2954        }        }
2955                
2956        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2957                line => $l, column => $c};                line => $l, column => $c,
2958                 };
2959      } else {      } else {
2960        !!!cp (1019);        !!!cp (1019);
2961        !!!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 2975  sub _tokenize_attempt_to_consume_an_enti
2975      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2976      our $EntityChar;      our $EntityChar;
2977    
2978      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2979             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2980             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2981               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2429  sub _tokenize_attempt_to_consume_an_enti Line 3009  sub _tokenize_attempt_to_consume_an_enti
3009      if ($match > 0) {      if ($match > 0) {
3010        !!!cp (1023);        !!!cp (1023);
3011        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3012                line => $l, column => $c};                line => $l, column => $c,
3013                 };
3014      } elsif ($match < 0) {      } elsif ($match < 0) {
3015        !!!parse-error (type => 'no refc', line => $l, column => $c);        !!!parse-error (type => 'no refc', line => $l, column => $c);
3016        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3017          !!!cp (1024);          !!!cp (1024);
3018          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3019                  line => $l, column => $c};                  line => $l, column => $c,
3020                   };
3021        } else {        } else {
3022          !!!cp (1025);          !!!cp (1025);
3023          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3024                  line => $l, column => $c};                  line => $l, column => $c,
3025                   };
3026        }        }
3027      } else {      } else {
3028        !!!cp (1026);        !!!cp (1026);
3029        !!!parse-error (type => 'bare ero', line => $l, column => $c);        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3030        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3031        return {type => CHARACTER_TOKEN, data => '&'.$value,        return {type => CHARACTER_TOKEN, data => '&'.$value,
3032                line => $l, column => $c};                line => $l, column => $c,
3033                 };
3034      }      }
3035    } else {    } else {
3036      !!!cp (1027);      !!!cp (1027);
# Line 2533  sub _tree_construction_initial ($) { Line 3117  sub _tree_construction_initial ($) {
3117                
3118        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3119          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3120          ## NOTE: Default value for both |public_id| and |system_id| attributes
3121          ## are empty strings, so that we don't set any value in missing cases.
3122        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3123            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3124        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2547  sub _tree_construction_initial ($) { Line 3133  sub _tree_construction_initial ($) {
3133        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3134          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3135          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3136          if ({          my $prefix = [
3137            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3138            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3139            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3140            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3141            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3142            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3143            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3144            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3145            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3146            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3147            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3148            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3149            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3150            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3151            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3152            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3153            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3154            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3155            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3156            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3157            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3158            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3159            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3160            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3161            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3162            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3163            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3164            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3165            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3168            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3169            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3170            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3171            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3172            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3173            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3174            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3175            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3176            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3177            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3178            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3179            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3180            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3181            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3182            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3183            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3184            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3185            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3186            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3187            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3188            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3189            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3190            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3191            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3192            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3193            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3194            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3195            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3196            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3197            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3198            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3199            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3200            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3201            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3202            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3203            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3204            !!!cp ('t5');            !!!cp ('t5');
3205            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3206          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3207                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3208            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3209              !!!cp ('t6');              !!!cp ('t6');
3210              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2632  sub _tree_construction_initial ($) { Line 3212  sub _tree_construction_initial ($) {
3212              !!!cp ('t7');              !!!cp ('t7');
3213              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3214            }            }
3215          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3216                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3217            !!!cp ('t8');            !!!cp ('t8');
3218            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3219          } else {          } else {
# Line 2646  sub _tree_construction_initial ($) { Line 3226  sub _tree_construction_initial ($) {
3226          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3227          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3228          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3229            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3230              ## marked as quirks.
3231            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3232            !!!cp ('t11');            !!!cp ('t11');
3233          } else {          } else {
# Line 2669  sub _tree_construction_initial ($) { Line 3250  sub _tree_construction_initial ($) {
3250        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3251        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3252        ## reprocess        ## reprocess
3253          !!!ack-later;
3254        return;        return;
3255      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3256        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 3331  sub _tree_construction_root_element ($)
3331        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3332          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3333            my $root_element;            my $root_element;
3334            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3335            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3336            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3337                  [$root_element, $el_category->{html}];
3338    
3339            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3340              !!!cp ('t24');              !!!cp ('t24');
3341              $self->{application_cache_selection}              $self->{application_cache_selection}
3342                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3343              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3344                ## According to Hixie (#whatwg 2008-03-19), it should be
3345                ## resolved against the base URI of the document in HTML
3346                ## or xml:base of the element in XHTML.
3347            } else {            } else {
3348              !!!cp ('t25');              !!!cp ('t25');
3349              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3350            }            }
3351    
3352              !!!nack ('t25c');
3353    
3354            !!!next-token;            !!!next-token;
3355            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3356          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3367  sub _tree_construction_root_element ($)
3367          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3368        }        }
3369    
3370      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3371        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3372      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3373      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3374    
3375      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3376    
3377      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3378        !!!ack-later;
3379      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3380    
3381      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3399  sub _reset_insertion_mode ($) {
3399        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3400          $last = 1;          $last = 1;
3401          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3402            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3403                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3404              !!!cp ('t27');          } else {
3405              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3406          }          }
3407        }        }
3408              
3409        ## Step 4..13        ## Step 4..14
3410        my $new_mode = {        my $new_mode;
3411          if ($node->[1] & FOREIGN_EL) {
3412            !!!cp ('t28.1');
3413            ## NOTE: Strictly spaking, the line below only applies to MathML and
3414            ## SVG elements.  Currently the HTML syntax supports only MathML and
3415            ## SVG elements as foreigners.
3416            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3417          } elsif ($node->[1] & TABLE_CELL_EL) {
3418            if ($last) {
3419              !!!cp ('t28.2');
3420              #
3421            } else {
3422              !!!cp ('t28.3');
3423              $new_mode = IN_CELL_IM;
3424            }
3425          } else {
3426            !!!cp ('t28.4');
3427            $new_mode = {
3428                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3429                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3430                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3431                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3432                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3433                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2837  sub _reset_insertion_mode ($) { Line 3438  sub _reset_insertion_mode ($) {
3438                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3439                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3440                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3441                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3442          }
3443        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3444                
3445        ## Step 14        ## Step 15
3446        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3447          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3448            !!!cp ('t29');            !!!cp ('t29');
3449            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3457  sub _reset_insertion_mode ($) {
3457          !!!cp ('t31');          !!!cp ('t31');
3458        }        }
3459                
3460        ## Step 15        ## Step 16
3461        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3462                
3463        ## Step 16        ## Step 17
3464        $i--;        $i--;
3465        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3466                
3467        ## Step 17        ## Step 18
3468        redo S3;        redo S3;
3469      } # S3      } # S3
3470    
# Line 2974  sub _tree_construction_main ($) { Line 3576  sub _tree_construction_main ($) {
3576      ## Step 1      ## Step 1
3577      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3578      my $el;      my $el;
3579      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3580    
3581      ## Step 2      ## Step 2
3582      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3587  sub _tree_construction_main ($) {
3587    
3588      ## Step 4      ## Step 4
3589      my $text = '';      my $text = '';
3590        !!!nack ('t40.1');
3591      !!!next-token;      !!!next-token;
3592      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3593        !!!cp ('t40');        !!!cp ('t40');
# Line 3024  sub _tree_construction_main ($) { Line 3627  sub _tree_construction_main ($) {
3627    
3628    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3629      my $script_el;      my $script_el;
3630      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3631      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3632    
3633      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3634      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3635            
3636      my $text = '';      my $text = '';
3637        !!!nack ('t45.1');
3638      !!!next-token;      !!!next-token;
3639      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3640        !!!cp ('t45');        !!!cp ('t45');
# Line 3088  sub _tree_construction_main ($) { Line 3692  sub _tree_construction_main ($) {
3692        my $formatting_element;        my $formatting_element;
3693        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3694        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3695          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3696              !!!cp ('t52');
3697              last AFE;
3698            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3699                         eq $tag_name) {
3700            !!!cp ('t51');            !!!cp ('t51');
3701            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3702            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3703            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3704          }          }
3705        } # AFE        } # AFE
3706        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3123  sub _tree_construction_main ($) { Line 3728  sub _tree_construction_main ($) {
3728              !!!next-token;              !!!next-token;
3729              return;              return;
3730            }            }
3731          } 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]}) {  
3732            !!!cp ('t56');            !!!cp ('t56');
3733            $in_scope = 0;            $in_scope = 0;
3734          }          }
# Line 3141  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743        }        }
3744        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3745          !!!cp ('t58');          !!!cp ('t58');
3746          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3747                            value => $self->{open_elements}->[-1]->[0]
3748                                ->manakai_local_name,
3749                          token => $end_tag_token);                          token => $end_tag_token);
3750        }        }
3751                
# Line 3150  sub _tree_construction_main ($) { Line 3754  sub _tree_construction_main ($) {
3754        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3755        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3756          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3757          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3758              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3759              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3760               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3761            !!!cp ('t59');            !!!cp ('t59');
3762            $furthest_block = $node;            $furthest_block = $node;
3763            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843        } # S7          } # S7  
3844                
3845        ## Step 8        ## Step 8
3846        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3847          my $foster_parent_element;          my $foster_parent_element;
3848          my $next_sibling;          my $next_sibling;
3849                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3850                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3851                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3852                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3853                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 3920  sub _tree_construction_main ($) {
3920    
3921    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3922      my $child = shift;      my $child = shift;
3923      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]}) {  
3924        # MUST        # MUST
3925        my $foster_parent_element;        my $foster_parent_element;
3926        my $next_sibling;        my $next_sibling;
3927                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3928                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3929                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3930                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3931                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 3950  sub _tree_construction_main ($) {
3950      }      }
3951    }; # $insert_to_foster    }; # $insert_to_foster
3952    
3953    B: {    B: while (1) {
3954      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3955        !!!cp ('t73');        !!!cp ('t73');
3956        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3957        ## Ignore the token        ## Ignore the token
3958        ## Stay in the phase        ## Stay in the phase
3959        !!!next-token;        !!!next-token;
3960        redo B;        next B;
3961      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3962               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3963        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3383  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3984          }          }
3985        }        }
3986          !!!nack ('t84.1');
3987        !!!next-token;        !!!next-token;
3988        redo B;        next B;
3989      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3990        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3991        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 3999  sub _tree_construction_main ($) {
3999          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4000        }        }
4001        !!!next-token;        !!!next-token;
4002        redo B;        next B;
4003      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4004          if ($token->{type} == CHARACTER_TOKEN) {
4005            !!!cp ('t87.1');
4006            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4007            !!!next-token;
4008            next B;
4009          } elsif ($token->{type} == START_TAG_TOKEN) {
4010            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4011                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4012                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4013                ($token->{tag_name} eq 'svg' and
4014                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4015              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4016              !!!cp ('t87.2');
4017              #
4018            } elsif ({
4019                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4020                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4021                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4022                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4023                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4024                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4025                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4026                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4027                     }->{$token->{tag_name}}) {
4028              !!!cp ('t87.2');
4029              !!!parse-error (type => 'not closed',
4030                              value => $self->{open_elements}->[-1]->[0]
4031                                  ->manakai_local_name,
4032                              token => $token);
4033    
4034              pop @{$self->{open_elements}}
4035                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4036    
4037              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4038              ## Reprocess.
4039              next B;
4040            } else {
4041              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4042              my $tag_name = $token->{tag_name};
4043              if ($nsuri eq $SVG_NS) {
4044                $tag_name = {
4045                   altglyph => 'altGlyph',
4046                   altglyphdef => 'altGlyphDef',
4047                   altglyphitem => 'altGlyphItem',
4048                   animatecolor => 'animateColor',
4049                   animatemotion => 'animateMotion',
4050                   animatetransform => 'animateTransform',
4051                   clippath => 'clipPath',
4052                   feblend => 'feBlend',
4053                   fecolormatrix => 'feColorMatrix',
4054                   fecomponenttransfer => 'feComponentTransfer',
4055                   fecomposite => 'feComposite',
4056                   feconvolvematrix => 'feConvolveMatrix',
4057                   fediffuselighting => 'feDiffuseLighting',
4058                   fedisplacementmap => 'feDisplacementMap',
4059                   fedistantlight => 'feDistantLight',
4060                   feflood => 'feFlood',
4061                   fefunca => 'feFuncA',
4062                   fefuncb => 'feFuncB',
4063                   fefuncg => 'feFuncG',
4064                   fefuncr => 'feFuncR',
4065                   fegaussianblur => 'feGaussianBlur',
4066                   feimage => 'feImage',
4067                   femerge => 'feMerge',
4068                   femergenode => 'feMergeNode',
4069                   femorphology => 'feMorphology',
4070                   feoffset => 'feOffset',
4071                   fepointlight => 'fePointLight',
4072                   fespecularlighting => 'feSpecularLighting',
4073                   fespotlight => 'feSpotLight',
4074                   fetile => 'feTile',
4075                   feturbulence => 'feTurbulence',
4076                   foreignobject => 'foreignObject',
4077                   glyphref => 'glyphRef',
4078                   lineargradient => 'linearGradient',
4079                   radialgradient => 'radialGradient',
4080                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4081                   textpath => 'textPath',  
4082                }->{$tag_name} || $tag_name;
4083              }
4084    
4085              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4086    
4087              ## "adjust foreign attributes" - done in insert-element-f
4088    
4089              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4090    
4091              if ($self->{self_closing}) {
4092                pop @{$self->{open_elements}};
4093                !!!ack ('t87.3');
4094              } else {
4095                !!!cp ('t87.4');
4096              }
4097    
4098              !!!next-token;
4099              next B;
4100            }
4101          } elsif ($token->{type} == END_TAG_TOKEN) {
4102            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4103            !!!cp ('t87.5');
4104            #
4105          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4106            !!!cp ('t87.6');
4107            !!!parse-error (type => 'not closed',
4108                            value => $self->{open_elements}->[-1]->[0]
4109                                ->manakai_local_name,
4110                            token => $token);
4111    
4112            pop @{$self->{open_elements}}
4113                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4114    
4115            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4116            ## Reprocess.
4117            next B;
4118          } else {
4119            die "$0: $token->{type}: Unknown token type";        
4120          }
4121        }
4122    
4123        if ($self->{insertion_mode} & HEAD_IMS) {
4124        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4125          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4126            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4130  sub _tree_construction_main ($) {
4130              !!!cp ('t88.1');              !!!cp ('t88.1');
4131              ## Ignore the token.              ## Ignore the token.
4132              !!!next-token;              !!!next-token;
4133              redo B;              next B;
4134            }            }
4135            unless (length $token->{data}) {            unless (length $token->{data}) {
4136              !!!cp ('t88');              !!!cp ('t88');
4137              !!!next-token;              !!!next-token;
4138              redo B;              next B;
4139            }            }
4140          }          }
4141    
4142          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4143            !!!cp ('t89');            !!!cp ('t89');
4144            ## As if <head>            ## As if <head>
4145            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4146            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4147            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4148                  [$self->{head_element}, $el_category->{head}];
4149    
4150            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4151            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3449  sub _tree_construction_main ($) { Line 4171  sub _tree_construction_main ($) {
4171            !!!cp ('t92');            !!!cp ('t92');
4172          }          }
4173    
4174              ## "after head" insertion mode          ## "after head" insertion mode
4175              ## As if <body>          ## As if <body>
4176              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4177              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4178              ## reprocess          ## reprocess
4179              redo B;          next B;
4180            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4181              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4182                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4183                  !!!cp ('t93');              !!!cp ('t93');
4184                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4185                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4186                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4187                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4188                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4189                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4190                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4191                  !!!cp ('t94');              !!!next-token;
4192                  #              next B;
4193                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4194                  !!!cp ('t95');              !!!cp ('t93.2');
4195                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4196                  ## Ignore the token              ## Ignore the token
4197                  !!!next-token;              !!!nack ('t93.3');
4198                  redo B;              !!!next-token;
4199                }              next B;
4200              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4201                !!!cp ('t96');              !!!cp ('t95');
4202                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4203                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4204                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4205                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4206                next B;
4207              }
4208            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4209              !!!cp ('t96');
4210              ## As if <head>
4211              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4212              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4213              push @{$self->{open_elements}},
4214                  [$self->{head_element}, $el_category->{head}];
4215    
4216                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4217                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4218              } else {          } else {
4219                !!!cp ('t97');            !!!cp ('t97');
4220              }          }
4221    
4222              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4223                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3505  sub _tree_construction_main ($) { Line 4236  sub _tree_construction_main ($) {
4236                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4237                  !!!cp ('t100');                  !!!cp ('t100');
4238                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4239                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4240                        [$self->{head_element}, $el_category->{head}];
4241                } else {                } else {
4242                  !!!cp ('t101');                  !!!cp ('t101');
4243                }                }
4244                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4245                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4246                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4247                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4248                  !!!nack ('t101.1');
4249                !!!next-token;                !!!next-token;
4250                redo B;                next B;
4251              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4252                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4253                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4254                  !!!cp ('t102');                  !!!cp ('t102');
4255                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4256                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4257                        [$self->{head_element}, $el_category->{head}];
4258                } else {                } else {
4259                  !!!cp ('t103');                  !!!cp ('t103');
4260                }                }
4261                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4262                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4263                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4264                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4265                  !!!ack ('t103.1');
4266                !!!next-token;                !!!next-token;
4267                redo B;                next B;
4268              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4269                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4270                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4271                  !!!cp ('t104');                  !!!cp ('t104');
4272                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4273                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4274                        [$self->{head_element}, $el_category->{head}];
4275                } else {                } else {
4276                  !!!cp ('t105');                  !!!cp ('t105');
4277                }                }
4278                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4279                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.
4280    
4281                unless ($self->{confident}) {                unless ($self->{confident}) {
4282                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4283                    !!!cp ('t106');                    !!!cp ('t106');
4284                      ## NOTE: Whether the encoding is supported or not is handled
4285                      ## in the {change_encoding} callback.
4286                    $self->{change_encoding}                    $self->{change_encoding}
4287                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4288                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4292  sub _tree_construction_main ($) {
4292                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4293                                                 ->{has_reference});                                                 ->{has_reference});
4294                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4295                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4296                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4297                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4298                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4299                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4300                      !!!cp ('t107');                      !!!cp ('t107');
4301                        ## NOTE: Whether the encoding is supported or not is handled
4302                        ## in the {change_encoding} callback.
4303                      $self->{change_encoding}                      $self->{change_encoding}
4304                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4305                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4330  sub _tree_construction_main ($) {
4330    
4331                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4332                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4333                  !!!ack ('t110.1');
4334                !!!next-token;                !!!next-token;
4335                redo B;                next B;
4336              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4337                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4338                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3605  sub _tree_construction_main ($) { Line 4345  sub _tree_construction_main ($) {
4345                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4346                  !!!cp ('t112');                  !!!cp ('t112');
4347                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4348                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4349                        [$self->{head_element}, $el_category->{head}];
4350                } else {                } else {
4351                  !!!cp ('t113');                  !!!cp ('t113');
4352                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
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 'style') {              } elsif ($token->{tag_name} eq 'style' or
4362                         $token->{tag_name} eq 'noframes') {
4363                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4364                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4365                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4366                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                  !!!cp ('t114');                  !!!cp ('t114');
4368                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4369                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4370                        [$self->{head_element}, $el_category->{head}];
4371                } else {                } else {
4372                  !!!cp ('t115');                  !!!cp ('t115');
4373                }                }
4374                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4375                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4376                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4377                redo B;                next B;
4378              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4379                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4380                  !!!cp ('t116');                  !!!cp ('t116');
4381                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4382                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4383                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4384                    !!!nack ('t116.1');
4385                  !!!next-token;                  !!!next-token;
4386                  redo B;                  next B;
4387                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4388                  !!!cp ('t117');                  !!!cp ('t117');
4389                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4390                  ## Ignore the token                  ## Ignore the token
4391                    !!!nack ('t117.1');
4392                  !!!next-token;                  !!!next-token;
4393                  redo B;                  next B;
4394                } else {                } else {
4395                  !!!cp ('t118');                  !!!cp ('t118');
4396                  #                  #
# Line 3662  sub _tree_construction_main ($) { Line 4407  sub _tree_construction_main ($) {
4407                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4408                  !!!cp ('t120');                  !!!cp ('t120');
4409                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4410                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4411                        [$self->{head_element}, $el_category->{head}];
4412                } else {                } else {
4413                  !!!cp ('t121');                  !!!cp ('t121');
4414                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417                $script_start_tag->();                $script_start_tag->();
4418                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4419                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4420                redo B;                next B;
4421              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4422                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4423                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3695  sub _tree_construction_main ($) { Line 4441  sub _tree_construction_main ($) {
4441                }                }
4442    
4443                ## "after head" insertion mode                ## "after head" insertion mode
4444                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4446                  !!!cp ('t126');                  !!!cp ('t126');
4447                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3705  sub _tree_construction_main ($) { Line 4451  sub _tree_construction_main ($) {
4451                } else {                } else {
4452                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4453                }                }
4454                  !!!nack ('t127.1');
4455                !!!next-token;                !!!next-token;
4456                redo B;                next B;
4457              } else {              } else {
4458                !!!cp ('t128');                !!!cp ('t128');
4459                #                #
# Line 3735  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482    
4483              ## "after head" insertion mode              ## "after head" insertion mode
4484              ## As if <body>              ## As if <body>
4485              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4486              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4487              ## reprocess              ## reprocess
4488              redo B;              !!!ack-later;
4489                next B;
4490            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4491              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4492                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4493                  !!!cp ('t132');                  !!!cp ('t132');
4494                  ## As if <head>                  ## As if <head>
4495                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4496                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4497                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4498                        [$self->{head_element}, $el_category->{head}];
4499    
4500                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4506                  !!!cp ('t133');                  !!!cp ('t133');
4507                  ## As if </noscript>                  ## As if </noscript>
# Line 3763  sub _tree_construction_main ($) { Line 4512  sub _tree_construction_main ($) {
4512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4513                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4514                  !!!next-token;                  !!!next-token;
4515                  redo B;                  next B;
4516                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4517                  !!!cp ('t134');                  !!!cp ('t134');
4518                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4519                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4520                  !!!next-token;                  !!!next-token;
4521                  redo B;                  next B;
4522                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4523                    !!!cp ('t134.1');
4524                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4525                    ## Ignore the token
4526                    !!!next-token;
4527                    next B;
4528                } else {                } else {
4529                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4530                }                }
4531              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4532                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3780  sub _tree_construction_main ($) { Line 4534  sub _tree_construction_main ($) {
4534                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4535                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4536                  !!!next-token;                  !!!next-token;
4537                  redo B;                  next B;
4538                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4539                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4540                  !!!cp ('t137');                  !!!cp ('t137');
4541                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4542                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4543                  !!!next-token;                  !!!next-token;
4544                  redo B;                  next B;
4545                } else {                } else {
4546                  !!!cp ('t138');                  !!!cp ('t138');
4547                  #                  #
# Line 3794  sub _tree_construction_main ($) { Line 4549  sub _tree_construction_main ($) {
4549              } elsif ({              } elsif ({
4550                        body => 1, html => 1,                        body => 1, html => 1,
4551                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4552                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4553                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4554                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head');  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4555                  !!!cp ('t140');                  !!!cp ('t140');
4556                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4557                  ## Ignore the token                  ## Ignore the token
4558                  !!!next-token;                  !!!next-token;
4559                  redo B;                  next B;
4560                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4561                    !!!cp ('t140.1');
4562                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4563                    ## Ignore the token
4564                    !!!next-token;
4565                    next B;
4566                } else {                } else {
4567                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4568                }                }
4569                              } elsif ($token->{tag_name} eq 'p') {
4570                #                !!!cp ('t142');
4571              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4572                        p => 1, br => 1,                ## Ignore the token
4573                       }->{$token->{tag_name}}) {                !!!next-token;
4574                  next B;
4575                } elsif ($token->{tag_name} eq 'br') {
4576                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4577                  !!!cp ('t142');                  !!!cp ('t142.2');
4578                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4579                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4580                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4581                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4582      
4583                    ## Reprocess in the "after head" insertion mode...
4584                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4585                    !!!cp ('t143.2');
4586                    ## As if </head>
4587                    pop @{$self->{open_elements}};
4588                    $self->{insertion_mode} = AFTER_HEAD_IM;
4589      
4590                    ## Reprocess in the "after head" insertion mode...
4591                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4592                    !!!cp ('t143.3');
4593                    ## ISSUE: Two parse errors for <head><noscript></br>
4594                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4595                    ## As if </noscript>
4596                    pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4598    
4599                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4600                } else {                  ## As if </head>
4601                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4602                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4603    
4604                #                  ## Reprocess in the "after head" insertion mode...
4605              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4606                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4607                  #                  #
4608                } else {                } else {
4609                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4610                }                }
4611    
4612                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4613                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4614                  ## Ignore the token
4615                  !!!next-token;
4616                  next B;
4617                } else {
4618                  !!!cp ('t145');
4619                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4620                  ## Ignore the token
4621                  !!!next-token;
4622                  next B;
4623              }              }
4624    
4625              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3867  sub _tree_construction_main ($) { Line 4645  sub _tree_construction_main ($) {
4645                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4646                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4647                !!!next-token;                !!!next-token;
4648                redo B;                next B;
4649              } else {              } else {
4650                !!!cp ('t149');                !!!cp ('t149');
4651              }              }
4652    
4653              ## "after head" insertion mode              ## "after head" insertion mode
4654              ## As if <body>              ## As if <body>
4655              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4656              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4657              ## reprocess              ## reprocess
4658              redo B;              next B;
4659        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4660          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4661            !!!cp ('t149.1');            !!!cp ('t149.1');
4662    
4663            ## NOTE: As if <head>            ## NOTE: As if <head>
4664            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4665            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4666                ($self->{head_element});                ($self->{head_element});
4667            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4668              #    [$self->{head_element}, $el_category->{head}];
4669            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4670            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4671    
# Line 3927  sub _tree_construction_main ($) { Line 4706  sub _tree_construction_main ($) {
4706          }          }
4707    
4708          ## NOTE: As if <body>          ## NOTE: As if <body>
4709          !!!insert-element ('body');          !!!insert-element ('body',, $token);
4710          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4711          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4712          redo B;          next B;
4713        } else {        } else {
4714          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4715        }        }
# Line 3945  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4725    
4726              !!!next-token;              !!!next-token;
4727              redo B;              next B;
4728            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4729              if ({              if ({
4730                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 4734  sub _tree_construction_main ($) {
4734                  ## have an element in table scope                  ## have an element in table scope
4735                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4736                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4737                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4738                      !!!cp ('t151');                      !!!cp ('t151');
4739    
4740                      ## Close the cell                      ## Close the cell
4741                      !!!back-token; # <?>                      !!!back-token; # <x>
4742                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4743                                  tag_name => $node->[0]->manakai_local_name,
4744                                line => $token->{line},                                line => $token->{line},
4745                                column => $token->{column}};                                column => $token->{column}};
4746                      redo B;                      next B;
4747                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4748                      !!!cp ('t152');                      !!!cp ('t152');
4749                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4750                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4755  sub _tree_construction_main ($) {
4755                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4756                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4757                  ## Ignore the token                  ## Ignore the token
4758                    !!!nack ('t153.1');
4759                  !!!next-token;                  !!!next-token;
4760                  redo B;                  next B;
4761                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4762                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4763                                    
# Line 3988  sub _tree_construction_main ($) { Line 4767  sub _tree_construction_main ($) {
4767                  INSCOPE: {                  INSCOPE: {
4768                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4769                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4770                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4771                        !!!cp ('t155');                        !!!cp ('t155');
4772                        $i = $_;                        $i = $_;
4773                        last INSCOPE;                        last INSCOPE;
4774                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4775                        !!!cp ('t156');                        !!!cp ('t156');
4776                        last;                        last;
4777                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4781  sub _tree_construction_main ($) {
4781                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4782                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4783                    ## Ignore the token                    ## Ignore the token
4784                      !!!nack ('t157.1');
4785                    !!!next-token;                    !!!next-token;
4786                    redo B;                    next B;
4787                  } # INSCOPE                  } # INSCOPE
4788                                    
4789                  ## generate implied end tags                  ## generate implied end tags
4790                  while ({                  while ($self->{open_elements}->[-1]->[1]
4791                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4792                    !!!cp ('t158');                    !!!cp ('t158');
4793                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4794                  }                  }
4795    
4796                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4797                    !!!cp ('t159');                    !!!cp ('t159');
4798                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4799                                      value => $self->{open_elements}->[-1]->[0]
4800                                          ->manakai_local_name,
4801                                      token => $token);
4802                  } else {                  } else {
4803                    !!!cp ('t160');                    !!!cp ('t160');
4804                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 4810  sub _tree_construction_main ($) {
4810                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4811                                    
4812                  ## reprocess                  ## reprocess
4813                  redo B;                  !!!ack-later;
4814                    next B;
4815                } else {                } else {
4816                  !!!cp ('t161');                  !!!cp ('t161');
4817                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 4827  sub _tree_construction_main ($) {
4827                  my $i;                  my $i;
4828                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4829                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4830                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4831                      !!!cp ('t163');                      !!!cp ('t163');
4832                      $i = $_;                      $i = $_;
4833                      last INSCOPE;                      last INSCOPE;
4834                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4835                      !!!cp ('t164');                      !!!cp ('t164');
4836                      last INSCOPE;                      last INSCOPE;
4837                    }                    }
# Line 4062  sub _tree_construction_main ($) { Line 4841  sub _tree_construction_main ($) {
4841                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4842                      ## Ignore the token                      ## Ignore the token
4843                      !!!next-token;                      !!!next-token;
4844                      redo B;                      next B;
4845                    }                    }
4846                                    
4847                  ## generate implied end tags                  ## generate implied end tags
4848                  while ({                  while ($self->{open_elements}->[-1]->[1]
4849                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4850                    !!!cp ('t166');                    !!!cp ('t166');
4851                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4852                  }                  }
4853    
4854                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4855                            ne $token->{tag_name}) {
4856                    !!!cp ('t167');                    !!!cp ('t167');
4857                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4858                                      value => $self->{open_elements}->[-1]->[0]
4859                                          ->manakai_local_name,
4860                                      token => $token);
4861                  } else {                  } else {
4862                    !!!cp ('t168');                    !!!cp ('t168');
4863                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4870                                    
4871                  !!!next-token;                  !!!next-token;
4872                  redo B;                  next B;
4873                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4874                  !!!cp ('t169');                  !!!cp ('t169');
4875                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4876                  ## Ignore the token                  ## Ignore the token
4877                  !!!next-token;                  !!!next-token;
4878                  redo B;                  next B;
4879                } else {                } else {
4880                  !!!cp ('t170');                  !!!cp ('t170');
4881                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 4887  sub _tree_construction_main ($) {
4887                  INSCOPE: {                  INSCOPE: {
4888                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4889                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4890                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4891                        !!!cp ('t171');                        !!!cp ('t171');
4892                        $i = $_;                        $i = $_;
4893                        last INSCOPE;                        last INSCOPE;
4894                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4895                        !!!cp ('t172');                        !!!cp ('t172');
4896                        last;                        last;
4897                      }                      }
# Line 4122  sub _tree_construction_main ($) { Line 4902  sub _tree_construction_main ($) {
4902                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4903                    ## Ignore the token                    ## Ignore the token
4904                    !!!next-token;                    !!!next-token;
4905                    redo B;                    next B;
4906                  } # INSCOPE                  } # INSCOPE
4907                                    
4908                  ## generate implied end tags                  ## generate implied end tags
4909                  while ({                  while ($self->{open_elements}->[-1]->[1]
4910                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4911                    !!!cp ('t174');                    !!!cp ('t174');
4912                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4913                  }                  }
4914                                    
4915                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4916                    !!!cp ('t175');                    !!!cp ('t175');
4917                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4918                                      value => $self->{open_elements}->[-1]->[0]
4919                                          ->manakai_local_name,
4920                                      token => $token);
4921                  } else {                  } else {
4922                    !!!cp ('t176');                    !!!cp ('t176');
4923                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 4929  sub _tree_construction_main ($) {
4929                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4930                                    
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  next B;
4933                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4934                  !!!cp ('t177');                  !!!cp ('t177');
4935                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4936                  ## Ignore the token                  ## Ignore the token
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  next B;
4939                } else {                } else {
4940                  !!!cp ('t178');                  !!!cp ('t178');
4941                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 4951  sub _tree_construction_main ($) {
4951                INSCOPE: {                INSCOPE: {
4952                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4953                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4954                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4955                      !!!cp ('t179');                      !!!cp ('t179');
4956                      $i = $_;                      $i = $_;
4957    
4958                      ## Close the cell                      ## Close the cell
4959                      !!!back-token; # </?>                      !!!back-token; # </x>
4960                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4961                                line => $token->{line},                                line => $token->{line},
4962                                column => $token->{column}};                                column => $token->{column}};
4963                      redo B;                      next B;
4964                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4965                      !!!cp ('t180');                      !!!cp ('t180');
4966                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4967                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4968                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4969                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4970                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4971                      !!!cp ('t181');                      !!!cp ('t181');
4972                      last;                      last;
# Line 4198  sub _tree_construction_main ($) { Line 4978  sub _tree_construction_main ($) {
4978                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4979                  ## Ignore the token                  ## Ignore the token
4980                  !!!next-token;                  !!!next-token;
4981                  redo B;                  next B;
4982                } # INSCOPE                } # INSCOPE
4983              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4984                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4209  sub _tree_construction_main ($) { Line 4989  sub _tree_construction_main ($) {
4989                my $i;                my $i;
4990                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4991                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4992                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4993                    !!!cp ('t184');                    !!!cp ('t184');
4994                    $i = $_;                    $i = $_;
4995                    last INSCOPE;                    last INSCOPE;
4996                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4997                    !!!cp ('t185');                    !!!cp ('t185');
4998                    last INSCOPE;                    last INSCOPE;
4999                  }                  }
# Line 4225  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5004                  ## Ignore the token                  ## Ignore the token
5005                  !!!next-token;                  !!!next-token;
5006                  redo B;                  next B;
5007                }                }
5008                                
5009                ## generate implied end tags                ## generate implied end tags
5010                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5011                  !!!cp ('t187');                  !!!cp ('t187');
5012                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5013                }                }
5014    
5015                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5016                  !!!cp ('t188');                  !!!cp ('t188');
5017                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5018                                    value => $self->{open_elements}->[-1]->[0]
5019                                        ->manakai_local_name,
5020                                    token => $token);
5021                } else {                } else {
5022                  !!!cp ('t189');                  !!!cp ('t189');
5023                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5030    
5031                ## reprocess                ## reprocess
5032                redo B;                next B;
5033              } elsif ({              } elsif ({
5034                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5035                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4259  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5039                  ## Ignore the token                  ## Ignore the token
5040                  !!!next-token;                  !!!next-token;
5041                  redo B;                  next B;
5042                } else {                } else {
5043                  !!!cp ('t191');                  !!!cp ('t191');
5044                  #                  #
# Line 4273  sub _tree_construction_main ($) { Line 5052  sub _tree_construction_main ($) {
5052                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5053                ## Ignore the token                ## Ignore the token
5054                !!!next-token;                !!!next-token;
5055                redo B;                next B;
5056              } else {              } else {
5057                !!!cp ('t193');                !!!cp ('t193');
5058                #                #
5059              }              }
5060        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5061          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5062            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]}) {  
5063              !!!cp ('t75');              !!!cp ('t75');
5064              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5065              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 5083  sub _tree_construction_main ($) {
5083            unless (length $token->{data}) {            unless (length $token->{data}) {
5084              !!!cp ('t194');              !!!cp ('t194');
5085              !!!next-token;              !!!next-token;
5086              redo B;              next B;
5087            } else {            } else {
5088              !!!cp ('t195');              !!!cp ('t195');
5089            }            }
# Line 4321  sub _tree_construction_main ($) { Line 5097  sub _tree_construction_main ($) {
5097              ## result in a new Text node.              ## result in a new Text node.
5098              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5099                            
5100              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]}) {  
5101                # MUST                # MUST
5102                my $foster_parent_element;                my $foster_parent_element;
5103                my $next_sibling;                my $next_sibling;
5104                my $prev_sibling;                my $prev_sibling;
5105                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5106                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5107                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5108                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5109                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5138  sub _tree_construction_main ($) {
5138          }          }
5139                            
5140          !!!next-token;          !!!next-token;
5141          redo B;          next B;
5142        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5143              if ({              if ({
5144                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4373  sub _tree_construction_main ($) { Line 5146  sub _tree_construction_main ($) {
5146                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5147                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5148                  ## Clear back to table context                  ## Clear back to table context
5149                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5150                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5151                    !!!cp ('t201');                    !!!cp ('t201');
5152                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5153                  }                  }
5154                                    
5155                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5156                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5157                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5158                }                }
# Line 4391  sub _tree_construction_main ($) { Line 5164  sub _tree_construction_main ($) {
5164                  }                  }
5165                                    
5166                  ## Clear back to table body context                  ## Clear back to table body context
5167                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5168                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5169                    !!!cp ('t203');                    !!!cp ('t203');
5170                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5171                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4402  sub _tree_construction_main ($) { Line 5174  sub _tree_construction_main ($) {
5174                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5175                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5176                    !!!cp ('t204');                    !!!cp ('t204');
5177                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5178                      !!!nack ('t204');
5179                    !!!next-token;                    !!!next-token;
5180                    redo B;                    next B;
5181                  } else {                  } else {
5182                    !!!cp ('t205');                    !!!cp ('t205');
5183                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5184                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5185                  }                  }
5186                } else {                } else {
# Line 4415  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188                }                }
5189    
5190                ## Clear back to table row context                ## Clear back to table row context
5191                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5192                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5193                  !!!cp ('t207');                  !!!cp ('t207');
5194                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5195                }                }
5196                                
5197                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5198                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5199    
5200                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5201                                
5202                  !!!nack ('t207.1');
5203                !!!next-token;                !!!next-token;
5204                redo B;                next B;
5205              } elsif ({              } elsif ({
5206                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5207                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5213  sub _tree_construction_main ($) {
5213                  my $i;                  my $i;
5214                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5215                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5216                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5217                      !!!cp ('t208');                      !!!cp ('t208');
5218                      $i = $_;                      $i = $_;
5219                      last INSCOPE;                      last INSCOPE;
5220                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5221                      !!!cp ('t209');                      !!!cp ('t209');
5222                      last INSCOPE;                      last INSCOPE;
5223                    }                    }
5224                  } # INSCOPE                  } # INSCOPE
5225                  unless (defined $i) {                  unless (defined $i) {
5226                   !!!cp ('t210');                    !!!cp ('t210');
5227  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5228                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5229                    ## Ignore the token                    ## Ignore the token
5230                      !!!nack ('t210.1');
5231                    !!!next-token;                    !!!next-token;
5232                    redo B;                    next B;
5233                  }                  }
5234                                    
5235                  ## Clear back to table row context                  ## Clear back to table row context
5236                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5237                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5238                    !!!cp ('t211');                    !!!cp ('t211');
5239                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5240                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5245  sub _tree_construction_main ($) {
5245                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5246                    !!!cp ('t212');                    !!!cp ('t212');
5247                    ## reprocess                    ## reprocess
5248                    redo B;                    !!!ack-later;
5249                      next B;
5250                  } else {                  } else {
5251                    !!!cp ('t213');                    !!!cp ('t213');
5252                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                  my $i;                  my $i;
5259                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5260                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5261                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5262                      !!!cp ('t214');                      !!!cp ('t214');
5263                      $i = $_;                      $i = $_;
5264                      last INSCOPE;                      last INSCOPE;
5265                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5266                      !!!cp ('t215');                      !!!cp ('t215');
5267                      last INSCOPE;                      last INSCOPE;
5268                    }                    }
# Line 4507  sub _tree_construction_main ($) { Line 5272  sub _tree_construction_main ($) {
5272  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5273                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5274                    ## Ignore the token                    ## Ignore the token
5275                      !!!nack ('t216.1');
5276                    !!!next-token;                    !!!next-token;
5277                    redo B;                    next B;
5278                  }                  }
5279    
5280                  ## Clear back to table body context                  ## Clear back to table body context
5281                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5282                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5283                    !!!cp ('t217');                    !!!cp ('t217');
5284                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5285                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301    
5302                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5303                  ## Clear back to table context                  ## Clear back to table context
5304                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5305                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5306                    !!!cp ('t219');                    !!!cp ('t219');
5307                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5308                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5309                  }                  }
5310                                    
5311                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5312                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5313                  ## reprocess                  ## reprocess
5314                  redo B;                  !!!ack-later;
5315                    next B;
5316                } elsif ({                } elsif ({
5317                          caption => 1,                          caption => 1,
5318                          colgroup => 1,                          colgroup => 1,
5319                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5320                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5321                  ## Clear back to table context                  ## Clear back to table context
5322                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5323                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5324                    !!!cp ('t220');                    !!!cp ('t220');
5325                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5326                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4563  sub _tree_construction_main ($) { Line 5329  sub _tree_construction_main ($) {
5329                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5330                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5331                                    
5332                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5333                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5334                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5335                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4572  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5339                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5340                  !!!next-token;                  !!!next-token;
5341                  redo B;                  !!!nack ('t220.1');
5342                    next B;
5343                } else {                } else {
5344                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5345                }                }
5346              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5347                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5348                                  value => $self->{open_elements}->[-1]->[0]
5349                                      ->manakai_local_name,
5350                                  token => $token);
5351    
5352                ## As if </table>                ## As if </table>
5353                ## have a table element in table scope                ## have a table element in table scope
5354                my $i;                my $i;
5355                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5356                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5357                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5358                    !!!cp ('t221');                    !!!cp ('t221');
5359                    $i = $_;                    $i = $_;
5360                    last INSCOPE;                    last INSCOPE;
5361                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5362                    !!!cp ('t222');                    !!!cp ('t222');
5363                    last INSCOPE;                    last INSCOPE;
5364                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5368  sub _tree_construction_main ($) {
5368  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5369                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5370                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5371                    !!!nack ('t223.1');
5372                  !!!next-token;                  !!!next-token;
5373                  redo B;                  next B;
5374                }                }
5375                                
5376  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5377                ## generate implied end tags                ## generate implied end tags
5378                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5379                  !!!cp ('t224');                  !!!cp ('t224');
5380                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5381                }                }
5382    
5383                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5384                  !!!cp ('t225');                  !!!cp ('t225');
5385  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5386                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5387                                    value => $self->{open_elements}->[-1]->[0]
5388                                        ->manakai_local_name,
5389                                    token => $token);
5390                } else {                } else {
5391                  !!!cp ('t226');                  !!!cp ('t226');
5392                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5396  sub _tree_construction_main ($) {
5396    
5397                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5398    
5399                ## reprocess            ## reprocess
5400                redo B;            !!!ack-later;
5401              next B;
5402          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5403            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5404              !!!cp ('t227.8');              !!!cp ('t227.8');
5405              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5406              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5407              redo B;              next B;
5408            } else {            } else {
5409              !!!cp ('t227.7');              !!!cp ('t227.7');
5410              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5414  sub _tree_construction_main ($) {
5414              !!!cp ('t227.6');              !!!cp ('t227.6');
5415              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5416              $script_start_tag->();              $script_start_tag->();
5417              redo B;              next B;
5418            } else {            } else {
5419              !!!cp ('t227.5');              !!!cp ('t227.5');
5420              #              #
# Line 4657  sub _tree_construction_main ($) { Line 5427  sub _tree_construction_main ($) {
5427                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5428                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5429    
5430                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5431    
5432                  ## TODO: form element pointer                  ## TODO: form element pointer
5433    
5434                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5435    
5436                  !!!next-token;                  !!!next-token;
5437                  redo B;                  !!!ack ('t227.2.1');
5438                    next B;
5439                } else {                } else {
5440                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5441                  #                  #
# Line 4693  sub _tree_construction_main ($) { Line 5464  sub _tree_construction_main ($) {
5464                my $i;                my $i;
5465                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5466                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5467                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5468                    !!!cp ('t228');                    !!!cp ('t228');
5469                    $i = $_;                    $i = $_;
5470                    last INSCOPE;                    last INSCOPE;
5471                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5472                    !!!cp ('t229');                    !!!cp ('t229');
5473                    last INSCOPE;                    last INSCOPE;
5474                  }                  }
# Line 4708  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477                  !!!cp ('t230');                  !!!cp ('t230');
5478                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5479                  ## Ignore the token                  ## Ignore the token
5480                    !!!nack ('t230.1');
5481                  !!!next-token;                  !!!next-token;
5482                  redo B;                  next B;
5483                } else {                } else {
5484                  !!!cp ('t232');                  !!!cp ('t232');
5485                }                }
5486    
5487                ## Clear back to table row context                ## Clear back to table row context
5488                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5489                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5490                  !!!cp ('t231');                  !!!cp ('t231');
5491  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5492                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5495  sub _tree_construction_main ($) {
5495                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5496                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5497                !!!next-token;                !!!next-token;
5498                redo B;                !!!nack ('t231.1');
5499                  next B;
5500              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5501                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5502                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5504  sub _tree_construction_main ($) {
5504                  my $i;                  my $i;
5505                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5506                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5507                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5508                      !!!cp ('t233');                      !!!cp ('t233');
5509                      $i = $_;                      $i = $_;
5510                      last INSCOPE;                      last INSCOPE;
5511                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5512                      !!!cp ('t234');                      !!!cp ('t234');
5513                      last INSCOPE;                      last INSCOPE;
5514                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5519                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5520                    ## Ignore the token                    ## Ignore the token
5521                      !!!nack ('t236.1');
5522                    !!!next-token;                    !!!next-token;
5523                    redo B;                    next B;
5524                  }                  }
5525                                    
5526                  ## Clear back to table row context                  ## Clear back to table row context
5527                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5528                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5529                    !!!cp ('t236');                    !!!cp ('t236');
5530  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5531                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5541  sub _tree_construction_main ($) {
5541                  my $i;                  my $i;
5542                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5543                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5544                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5545                      !!!cp ('t237');                      !!!cp ('t237');
5546                      $i = $_;                      $i = $_;
5547                      last INSCOPE;                      last INSCOPE;
5548                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5549                      !!!cp ('t238');                      !!!cp ('t238');
5550                      last INSCOPE;                      last INSCOPE;
5551                    }                    }
# Line 4790  sub _tree_construction_main ($) { Line 5554  sub _tree_construction_main ($) {
5554                    !!!cp ('t239');                    !!!cp ('t239');
5555                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5556                    ## Ignore the token                    ## Ignore the token
5557                      !!!nack ('t239.1');
5558                    !!!next-token;                    !!!next-token;
5559                    redo B;                    next B;
5560                  }                  }
5561                                    
5562                  ## Clear back to table body context                  ## Clear back to table body context
5563                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5564                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5565                    !!!cp ('t240');                    !!!cp ('t240');
5566                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5567                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5587  sub _tree_construction_main ($) {
5587                my $i;                my $i;
5588                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5589                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5590                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5591                    !!!cp ('t241');                    !!!cp ('t241');
5592                    $i = $_;                    $i = $_;
5593                    last INSCOPE;                    last INSCOPE;
5594                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5595                    !!!cp ('t242');                    !!!cp ('t242');
5596                    last INSCOPE;                    last INSCOPE;
5597                  }                  }
# Line 4838  sub _tree_construction_main ($) { Line 5600  sub _tree_construction_main ($) {
5600                  !!!cp ('t243');                  !!!cp ('t243');
5601                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5602                  ## Ignore the token                  ## Ignore the token
5603                    !!!nack ('t243.1');
5604                  !!!next-token;                  !!!next-token;
5605                  redo B;                  next B;
5606                }                }
5607                                    
5608                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5612                                
5613                !!!next-token;                !!!next-token;
5614                redo B;                next B;
5615              } elsif ({              } elsif ({
5616                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5617                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 5621  sub _tree_construction_main ($) {
5621                  my $i;                  my $i;
5622                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5623                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5624                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5625                      !!!cp ('t247');                      !!!cp ('t247');
5626                      $i = $_;                      $i = $_;
5627                      last INSCOPE;                      last INSCOPE;
5628                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5629                      !!!cp ('t248');                      !!!cp ('t248');
5630                      last INSCOPE;                      last INSCOPE;
5631                    }                    }
# Line 4873  sub _tree_construction_main ($) { Line 5634  sub _tree_construction_main ($) {
5634                      !!!cp ('t249');                      !!!cp ('t249');
5635                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5636                      ## Ignore the token                      ## Ignore the token
5637                        !!!nack ('t249.1');
5638                      !!!next-token;                      !!!next-token;
5639                      redo B;                      next B;
5640                    }                    }
5641                                    
5642                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 5644  sub _tree_construction_main ($) {
5644                  my $i;                  my $i;
5645                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5646                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5647                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5648                      !!!cp ('t250');                      !!!cp ('t250');
5649                      $i = $_;                      $i = $_;
5650                      last INSCOPE;                      last INSCOPE;
5651                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5652                      !!!cp ('t251');                      !!!cp ('t251');
5653                      last INSCOPE;                      last INSCOPE;
5654                    }                    }
# Line 4897  sub _tree_construction_main ($) { Line 5657  sub _tree_construction_main ($) {
5657                      !!!cp ('t252');                      !!!cp ('t252');
5658                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5659                      ## Ignore the token                      ## Ignore the token
5660                        !!!nack ('t252.1');
5661                      !!!next-token;                      !!!next-token;
5662                      redo B;                      next B;
5663                    }                    }
5664                                    
5665                  ## Clear back to table row context                  ## Clear back to table row context
5666                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5667                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5668                    !!!cp ('t253');                    !!!cp ('t253');
5669  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5670                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 5679  sub _tree_construction_main ($) {
5679                my $i;                my $i;
5680                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5681                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5682                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5683                    !!!cp ('t254');                    !!!cp ('t254');
5684                    $i = $_;                    $i = $_;
5685                    last INSCOPE;                    last INSCOPE;
5686                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5687                    !!!cp ('t255');                    !!!cp ('t255');
5688                    last INSCOPE;                    last INSCOPE;
5689                  }                  }
# Line 4934  sub _tree_construction_main ($) { Line 5692  sub _tree_construction_main ($) {
5692                  !!!cp ('t256');                  !!!cp ('t256');
5693                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5694                  ## Ignore the token                  ## Ignore the token
5695                    !!!nack ('t256.1');
5696                  !!!next-token;                  !!!next-token;
5697                  redo B;                  next B;
5698                }                }
5699    
5700                ## Clear back to table body context                ## Clear back to table body context
5701                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5702                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5703                  !!!cp ('t257');                  !!!cp ('t257');
5704  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5705                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 5707  sub _tree_construction_main ($) {
5707    
5708                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5709                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5710                  !!!nack ('t257.1');
5711                !!!next-token;                !!!next-token;
5712                redo B;                next B;
5713              } elsif ({              } elsif ({
5714                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5715                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5716                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5717                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5718                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5719                !!!cp ('t258');            !!!cp ('t258');
5720                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5721                ## Ignore the token            ## Ignore the token
5722                !!!next-token;            !!!nack ('t258.1');
5723                redo B;             !!!next-token;
5724              next B;
5725          } else {          } else {
5726            !!!cp ('t259');            !!!cp ('t259');
5727            !!!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 5730  sub _tree_construction_main ($) {
5730            #            #
5731          }          }
5732        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5733          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5734                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5735            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5736            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 5752  sub _tree_construction_main ($) {
5752                unless (length $token->{data}) {                unless (length $token->{data}) {
5753                  !!!cp ('t260');                  !!!cp ('t260');
5754                  !!!next-token;                  !!!next-token;
5755                  redo B;                  next B;
5756                }                }
5757              }              }
5758                            
# Line 5001  sub _tree_construction_main ($) { Line 5761  sub _tree_construction_main ($) {
5761            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5762              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5763                !!!cp ('t262');                !!!cp ('t262');
5764                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5765                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5766                  !!!ack ('t262.1');
5767                !!!next-token;                !!!next-token;
5768                redo B;                next B;
5769              } else {              } else {
5770                !!!cp ('t263');                !!!cp ('t263');
5771                #                #
5772              }              }
5773            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5774              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5775                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5776                  !!!cp ('t264');                  !!!cp ('t264');
5777                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5778                  ## Ignore the token                  ## Ignore the token
5779                  !!!next-token;                  !!!next-token;
5780                  redo B;                  next B;
5781                } else {                } else {
5782                  !!!cp ('t265');                  !!!cp ('t265');
5783                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5784                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5785                  !!!next-token;                  !!!next-token;
5786                  redo B;                              next B;            
5787                }                }
5788              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5789                !!!cp ('t266');                !!!cp ('t266');
5790                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5791                ## Ignore the token                ## Ignore the token
5792                !!!next-token;                !!!next-token;
5793                redo B;                next B;
5794              } else {              } else {
5795                !!!cp ('t267');                !!!cp ('t267');
5796                #                #
5797              }              }
5798        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5799          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5800              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5801            !!!cp ('t270.2');            !!!cp ('t270.2');
5802            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 5807  sub _tree_construction_main ($) {
5807            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5808            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5809            ## Reprocess.            ## Reprocess.
5810            redo B;            next B;
5811          }          }
5812        } else {        } else {
5813          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5814        }        }
5815    
5816            ## As if </colgroup>            ## As if </colgroup>
5817            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5818              !!!cp ('t269');              !!!cp ('t269');
5819  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5820              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5821              ## Ignore the token              ## Ignore the token
5822                !!!nack ('t269.1');
5823              !!!next-token;              !!!next-token;
5824              redo B;              next B;
5825            } else {            } else {
5826              !!!cp ('t270');              !!!cp ('t270');
5827              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5828              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5829                !!!ack-later;
5830              ## reprocess              ## reprocess
5831              redo B;              next B;
5832            }            }
5833      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5834        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5835          !!!cp ('t271');          !!!cp ('t271');
5836          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5837          !!!next-token;          !!!next-token;
5838          redo B;          next B;
5839        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5840              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5841                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5842                  !!!cp ('t272');              !!!cp ('t272');
5843                  ## As if </option>              ## As if </option>
5844                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5845                } else {            } else {
5846                  !!!cp ('t273');              !!!cp ('t273');
5847                }            }
5848    
5849                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5850                !!!next-token;            !!!nack ('t273.1');
5851                redo B;            !!!next-token;
5852              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5853                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5854                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5855                  ## As if </option>              !!!cp ('t274');
5856                  pop @{$self->{open_elements}};              ## As if </option>
5857                } else {              pop @{$self->{open_elements}};
5858                  !!!cp ('t275');            } else {
5859                }              !!!cp ('t275');
5860              }
5861    
5862                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5863                  !!!cp ('t276');              !!!cp ('t276');
5864                  ## As if </optgroup>              ## As if </optgroup>
5865                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5866                } else {            } else {
5867                  !!!cp ('t277');              !!!cp ('t277');
5868                }            }
5869    
5870                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5871                !!!next-token;            !!!nack ('t277.1');
5872                redo B;            !!!next-token;
5873          } elsif ($token->{tag_name} eq 'select' or            next B;
5874                   $token->{tag_name} eq 'input' or          } elsif ({
5875                       select => 1, input => 1, textarea => 1,
5876                     }->{$token->{tag_name}} or
5877                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5878                    {                    {
5879                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5118  sub _tree_construction_main ($) { Line 5884  sub _tree_construction_main ($) {
5884            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5885            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5886            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5887                ## have an element in table scope            ## have an element in table scope
5888                my $i;            my $i;
5889                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5891                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5892                    !!!cp ('t278');                !!!cp ('t278');
5893                    $i = $_;                $i = $_;
5894                    last INSCOPE;                last INSCOPE;
5895                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5896                            table => 1, html => 1,                !!!cp ('t279');
5897                           }->{$node->[1]}) {                last INSCOPE;
5898                    !!!cp ('t279');              }
5899                    last INSCOPE;            } # INSCOPE
5900                  }            unless (defined $i) {
5901                } # INSCOPE              !!!cp ('t280');
5902                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5903                  !!!cp ('t280');              ## Ignore the token
5904                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5905                  ## Ignore the token              !!!next-token;
5906                  !!!next-token;              next B;
5907                  redo B;            }
               }  
5908                                
5909                !!!cp ('t281');            !!!cp ('t281');
5910                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5911    
5912                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5913    
5914            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5915              !!!cp ('t281.2');              !!!nack ('t281.2');
5916              !!!next-token;              !!!next-token;
5917              redo B;              next B;
5918            } else {            } else {
5919              !!!cp ('t281.1');              !!!cp ('t281.1');
5920                !!!ack-later;
5921              ## Reprocess the token.              ## Reprocess the token.
5922              redo B;              next B;
5923            }            }
5924          } else {          } else {
5925            !!!cp ('t282');            !!!cp ('t282');
5926            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5927            ## Ignore the token            ## Ignore the token
5928              !!!nack ('t282.1');
5929            !!!next-token;            !!!next-token;
5930            redo B;            next B;
5931          }          }
5932        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5933              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5934                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5935                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5936                  !!!cp ('t283');              !!!cp ('t283');
5937                  ## As if </option>              ## As if </option>
5938                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5939                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5940                  !!!cp ('t284');              !!!cp ('t284');
5941                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5942                } else {            } else {
5943                  !!!cp ('t285');              !!!cp ('t285');
5944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                  ## Ignore the token              ## Ignore the token
5946                }            }
5947                !!!next-token;            !!!nack ('t285.1');
5948                redo B;            !!!next-token;
5949              } elsif ($token->{tag_name} eq 'option') {            next B;
5950                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5951                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                  pop @{$self->{open_elements}};              !!!cp ('t286');
5953                } else {              pop @{$self->{open_elements}};
5954                  !!!cp ('t287');            } else {
5955                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5956                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5957                }              ## Ignore the token
5958                !!!next-token;            }
5959                redo B;            !!!nack ('t287.1');
5960              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5961                ## have an element in table scope            next B;
5962                my $i;          } elsif ($token->{tag_name} eq 'select') {
5963                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5964                  my $node = $self->{open_elements}->[$_];            my $i;
5965                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5966                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5967                    $i = $_;              if ($node->[1] & SELECT_EL) {
5968                    last INSCOPE;                !!!cp ('t288');
5969                  } elsif ({                $i = $_;
5970                            table => 1, html => 1,                last INSCOPE;
5971                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5972                    !!!cp ('t289');                !!!cp ('t289');
5973                    last INSCOPE;                last INSCOPE;
5974                  }              }
5975                } # INSCOPE            } # INSCOPE
5976                unless (defined $i) {            unless (defined $i) {
5977                  !!!cp ('t290');              !!!cp ('t290');
5978                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5979                  ## Ignore the token              ## Ignore the token
5980                  !!!next-token;              !!!nack ('t290.1');
5981                  redo B;              !!!next-token;
5982                }              next B;
5983              }
5984                                
5985                !!!cp ('t291');            !!!cp ('t291');
5986                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5987    
5988                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5989    
5990                !!!next-token;            !!!nack ('t291.1');
5991                redo B;            !!!next-token;
5992              next B;
5993          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5994                   {                   {
5995                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5996                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5997                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5998  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5999                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6000                                
6001                ## have an element in table scope            ## have an element in table scope
6002                my $i;            my $i;
6003                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6004                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6005                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6006                    !!!cp ('t292');                !!!cp ('t292');
6007                    $i = $_;                $i = $_;
6008                    last INSCOPE;                last INSCOPE;
6009                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6010                            table => 1, html => 1,                !!!cp ('t293');
6011                           }->{$node->[1]}) {                last INSCOPE;
6012                    !!!cp ('t293');              }
6013                    last INSCOPE;            } # INSCOPE
6014                  }            unless (defined $i) {
6015                } # INSCOPE              !!!cp ('t294');
6016                unless (defined $i) {              ## Ignore the token
6017                  !!!cp ('t294');              !!!nack ('t294.1');
6018                  ## Ignore the token              !!!next-token;
6019                  !!!next-token;              next B;
6020                  redo B;            }
               }  
6021                                
6022                ## As if </select>            ## As if </select>
6023                ## have an element in table scope            ## have an element in table scope
6024                undef $i;            undef $i;
6025                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6026                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6027                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6028                    !!!cp ('t295');                !!!cp ('t295');
6029                    $i = $_;                $i = $_;
6030                    last INSCOPE;                last INSCOPE;
6031                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6032  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6033                    !!!cp ('t296');                !!!cp ('t296');
6034                    last INSCOPE;                last INSCOPE;
6035                  }              }
6036                } # INSCOPE            } # INSCOPE
6037                unless (defined $i) {            unless (defined $i) {
6038                  !!!cp ('t297');              !!!cp ('t297');
6039  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6040                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6041                  ## Ignore the </select> token              ## Ignore the </select> token
6042                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6043                  redo B;              !!!next-token; ## TODO: ok?
6044                }              next B;
6045              }
6046                                
6047                !!!cp ('t298');            !!!cp ('t298');
6048                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6049    
6050                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6051    
6052                ## reprocess            !!!ack-later;
6053                redo B;            ## reprocess
6054              next B;
6055          } else {          } else {
6056            !!!cp ('t299');            !!!cp ('t299');
6057            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6058            ## Ignore the token            ## Ignore the token
6059              !!!nack ('t299.3');
6060            !!!next-token;            !!!next-token;
6061            redo B;            next B;
6062          }          }
6063        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6064          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6065                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6066            !!!cp ('t299.1');            !!!cp ('t299.1');
6067            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 6086  sub _tree_construction_main ($) {
6086            unless (length $token->{data}) {            unless (length $token->{data}) {
6087              !!!cp ('t300');              !!!cp ('t300');
6088              !!!next-token;              !!!next-token;
6089              redo B;              next B;
6090            }            }
6091          }          }
6092                    
# Line 5335  sub _tree_construction_main ($) { Line 6104  sub _tree_construction_main ($) {
6104    
6105          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6106          ## reprocess          ## reprocess
6107          redo B;          next B;
6108        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6109          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6110            !!!cp ('t303');            !!!cp ('t303');
# Line 5350  sub _tree_construction_main ($) { Line 6119  sub _tree_construction_main ($) {
6119          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6120    
6121          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6122            !!!ack-later;
6123          ## reprocess          ## reprocess
6124          redo B;          next B;
6125        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6126          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6127            !!!cp ('t305');            !!!cp ('t305');
# Line 5370  sub _tree_construction_main ($) { Line 6140  sub _tree_construction_main ($) {
6140              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6141              ## Ignore the token              ## Ignore the token
6142              !!!next-token;              !!!next-token;
6143              redo B;              next B;
6144            } else {            } else {
6145              !!!cp ('t308');              !!!cp ('t308');
6146              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6147              !!!next-token;              !!!next-token;
6148              redo B;              next B;
6149            }            }
6150          } else {          } else {
6151            !!!cp ('t309');            !!!cp ('t309');
# Line 5383  sub _tree_construction_main ($) { Line 6153  sub _tree_construction_main ($) {
6153    
6154            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6155            ## reprocess            ## reprocess
6156            redo B;            next B;
6157          }          }
6158        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6159          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6170  sub _tree_construction_main ($) {
6170            unless (length $token->{data}) {            unless (length $token->{data}) {
6171              !!!cp ('t310');              !!!cp ('t310');
6172              !!!next-token;              !!!next-token;
6173              redo B;              next B;
6174            }            }
6175          }          }
6176                    
# Line 5428  sub _tree_construction_main ($) { Line 6198  sub _tree_construction_main ($) {
6198              !!!cp ('t315');              !!!cp ('t315');
6199              !!!next-token;              !!!next-token;
6200            }            }
6201            redo B;            next B;
6202          }          }
6203                    
6204          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5446  sub _tree_construction_main ($) { Line 6216  sub _tree_construction_main ($) {
6216          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6217              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6218            !!!cp ('t318');            !!!cp ('t318');
6219            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220              !!!nack ('t318.1');
6221            !!!next-token;            !!!next-token;
6222            redo B;            next B;
6223          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6224                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6225            !!!cp ('t319');            !!!cp ('t319');
6226            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6227            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6228              !!!ack ('t319.1');
6229            !!!next-token;            !!!next-token;
6230            redo B;            next B;
6231          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6232            !!!cp ('t320');            !!!cp ('t320');
6233            ## NOTE: As if in body.            ## NOTE: As if in head.
6234            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6235            redo B;            next B;
6236          } else {          } else {
6237            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6238              !!!cp ('t321');              !!!cp ('t321');
# Line 5470  sub _tree_construction_main ($) { Line 6242  sub _tree_construction_main ($) {
6242              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6243            }            }
6244            ## Ignore the token            ## Ignore the token
6245              !!!nack ('t322.1');
6246            !!!next-token;            !!!next-token;
6247            redo B;            next B;
6248          }          }
6249        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6250          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5486  sub _tree_construction_main ($) { Line 6259  sub _tree_construction_main ($) {
6259    
6260          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6261              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6262            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6264              !!!cp ('t325');              !!!cp ('t325');
6265              !!!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 6272  sub _tree_construction_main ($) {
6272            }            }
6273    
6274            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6275                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6276              !!!cp ('t327');              !!!cp ('t327');
6277              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6278            } else {            } else {
6279              !!!cp ('t328');              !!!cp ('t328');
6280            }            }
6281            redo B;            next B;
6282          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6283                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6284            !!!cp ('t329');            !!!cp ('t329');
6285            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6286            !!!next-token;            !!!next-token;
6287            redo B;            next B;
6288          } else {          } else {
6289            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290              !!!cp ('t330');              !!!cp ('t330');
# Line 5522  sub _tree_construction_main ($) { Line 6295  sub _tree_construction_main ($) {
6295            }            }
6296            ## Ignore the token            ## Ignore the token
6297            !!!next-token;            !!!next-token;
6298            redo B;            next B;
6299          }          }
6300        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6301          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6302                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6303            !!!cp ('t331.1');            !!!cp ('t331.1');
6304            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6323  sub _tree_construction_main ($) {
6323          !!!cp ('t332');          !!!cp ('t332');
6324          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6325          $script_start_tag->();          $script_start_tag->();
6326          redo B;          next B;
6327        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6328          !!!cp ('t333');          !!!cp ('t333');
6329          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6330          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6331          redo B;          next B;
6332        } elsif ({        } elsif ({
6333                  base => 1, link => 1,                  base => 1, link => 1,
6334                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6335          !!!cp ('t334');          !!!cp ('t334');
6336          ## 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
6337          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6339            !!!ack ('t334.1');
6340          !!!next-token;          !!!next-token;
6341          redo B;          next B;
6342        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6343          ## 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
6344          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6345          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.
6346    
6347          unless ($self->{confident}) {          unless ($self->{confident}) {
6348            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6349              !!!cp ('t335');              !!!cp ('t335');
6350                ## NOTE: Whether the encoding is supported or not is handled
6351                ## in the {change_encoding} callback.
6352              $self->{change_encoding}              $self->{change_encoding}
6353                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6354                            
# Line 5581  sub _tree_construction_main ($) { Line 6357  sub _tree_construction_main ($) {
6357                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6358                                           ->{has_reference});                                           ->{has_reference});
6359            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6360              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6361                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6362                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6363                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6364                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6365                !!!cp ('t336');                !!!cp ('t336');
6366                  ## NOTE: Whether the encoding is supported or not is handled
6367                  ## in the {change_encoding} callback.
6368                $self->{change_encoding}                $self->{change_encoding}
6369                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6370                $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 6390  sub _tree_construction_main ($) {
6390            }            }
6391          }          }
6392    
6393            !!!ack ('t338.1');
6394          !!!next-token;          !!!next-token;
6395          redo B;          next B;
6396        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6397          !!!cp ('t341');          !!!cp ('t341');
6398          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6399          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6400          redo B;          next B;
6401        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6402          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6403                                
6404          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6405              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6406            !!!cp ('t342');            !!!cp ('t342');
6407            ## Ignore the token            ## Ignore the token
6408          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6416  sub _tree_construction_main ($) {
6416              }              }
6417            }            }
6418          }          }
6419            !!!nack ('t343.1');
6420          !!!next-token;          !!!next-token;
6421          redo B;          next B;
6422        } elsif ({        } elsif ({
6423                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6424                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6433  sub _tree_construction_main ($) {
6433            !!!cp ('t350');            !!!cp ('t350');
6434            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6435            ## Ignore the token            ## Ignore the token
6436              !!!nack ('t350.1');
6437            !!!next-token;            !!!next-token;
6438            redo B;            next B;
6439          }          }
6440    
6441          ## has a p element in scope          ## has a p element in scope
6442          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6443            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6444              !!!cp ('t344');              !!!cp ('t344');
6445              !!!back-token;              !!!back-token; # <form>
6446              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6447                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6448              redo B;              next B;
6449            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6450              !!!cp ('t345');              !!!cp ('t345');
6451              last INSCOPE;              last INSCOPE;
6452            }            }
6453          } # INSCOPE          } # INSCOPE
6454                        
6455          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6457              !!!nack ('t346.1');
6458            !!!next-token;            !!!next-token;
6459            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6460              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6471  sub _tree_construction_main ($) {
6471            !!!cp ('t347.1');            !!!cp ('t347.1');
6472            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6473    
6474              !!!nack ('t347.2');
6475            !!!next-token;            !!!next-token;
6476          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6477            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6479  sub _tree_construction_main ($) {
6479                        
6480            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6481    
6482              !!!nack ('t382.1');
6483            !!!next-token;            !!!next-token;
6484          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6485            !!!cp ('t386');            !!!cp ('t386');
6486            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6487                    
6488              !!!nack ('t386.1');
6489            !!!next-token;            !!!next-token;
6490          } else {          } else {
6491            !!!cp ('t347');            !!!nack ('t347.1');
6492            !!!next-token;            !!!next-token;
6493          }          }
6494          redo B;          next B;
6495        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6496          ## has a p element in scope          ## has a p element in scope
6497          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6498            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6499              !!!cp ('t353');              !!!cp ('t353');
6500              !!!back-token;              !!!back-token; # <x>
6501              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6502                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6503              redo B;              next B;
6504            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6505              !!!cp ('t354');              !!!cp ('t354');
6506              last INSCOPE;              last INSCOPE;
6507            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6515  sub _tree_construction_main ($) {
6515                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6516          LI: {          LI: {
6517            ## Step 2            ## Step 2
6518            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6519              if ($i != -1) {              if ($i != -1) {
6520                !!!cp ('t355');                !!!cp ('t355');
6521                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6522                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6523                                      ->manakai_local_name,
6524                                  token => $token);
6525              } else {              } else {
6526                !!!cp ('t356');                !!!cp ('t356');
6527              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6532  sub _tree_construction_main ($) {
6532            }            }
6533                        
6534            ## Step 3            ## Step 3
6535            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6536                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6537                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6538                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6539                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6540                  not ($node->[1] & DIV_EL)) {
6541              !!!cp ('t358');              !!!cp ('t358');
6542              last LI;              last LI;
6543            }            }
# Line 5768  sub _tree_construction_main ($) { Line 6549  sub _tree_construction_main ($) {
6549            redo LI;            redo LI;
6550          } # LI          } # LI
6551                        
6552          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6553            !!!nack ('t359.1');
6554          !!!next-token;          !!!next-token;
6555          redo B;          next B;
6556        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6557          ## has a p element in scope          ## has a p element in scope
6558          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6559            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6560              !!!cp ('t367');              !!!cp ('t367');
6561              !!!back-token;              !!!back-token; # <plaintext>
6562              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6563                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6564              redo B;              next B;
6565            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6566              !!!cp ('t368');              !!!cp ('t368');
6567              last INSCOPE;              last INSCOPE;
6568            }            }
6569          } # INSCOPE          } # INSCOPE
6570                        
6571          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6572                        
6573          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6574                        
6575            !!!nack ('t368.1');
6576          !!!next-token;          !!!next-token;
6577          redo B;          next B;
6578        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6579          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6580            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6581            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6582              !!!cp ('t371');              !!!cp ('t371');
6583              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6584                            
6585              !!!back-token;              !!!back-token; # <a>
6586              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6587                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6588              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5830  sub _tree_construction_main ($) { Line 6610  sub _tree_construction_main ($) {
6610                        
6611          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6612    
6613          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6614          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6615    
6616            !!!nack ('t374.1');
6617          !!!next-token;          !!!next-token;
6618          redo B;          next B;
6619        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6620          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6621    
6622          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6623          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6624            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6625            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6626              !!!cp ('t376');              !!!cp ('t376');
6627              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6628              !!!back-token;              !!!back-token; # <nobr>
6629              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6630                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6631              redo B;              next B;
6632            } 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]}) {  
6633              !!!cp ('t377');              !!!cp ('t377');
6634              last INSCOPE;              last INSCOPE;
6635            }            }
6636          } # INSCOPE          } # INSCOPE
6637                    
6638          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6639          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6640                    
6641            !!!nack ('t377.1');
6642          !!!next-token;          !!!next-token;
6643          redo B;          next B;
6644        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6645          ## has a button element in scope          ## has a button element in scope
6646          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6647            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6648            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6649              !!!cp ('t378');              !!!cp ('t378');
6650              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6651              !!!back-token;              !!!back-token; # <button>
6652              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6653                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6654              redo B;              next B;
6655            } 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]}) {  
6656              !!!cp ('t379');              !!!cp ('t379');
6657              last INSCOPE;              last INSCOPE;
6658            }            }
# Line 5884  sub _tree_construction_main ($) { Line 6660  sub _tree_construction_main ($) {
6660                        
6661          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6662                        
6663          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6664    
6665          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6666    
6667          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6668    
6669            !!!nack ('t379.1');
6670          !!!next-token;          !!!next-token;
6671          redo B;          next B;
6672        } elsif ({        } elsif ({
6673                  xmp => 1,                  xmp => 1,
6674                  iframe => 1,                  iframe => 1,
6675                  noembed => 1,                  noembed => 1,
6676                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6677                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6678                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6679          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5907  sub _tree_construction_main ($) { Line 6684  sub _tree_construction_main ($) {
6684          }          }
6685          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6686          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6687          redo B;          next B;
6688        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6689          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6690                    
6691          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6692            !!!cp ('t389');            !!!cp ('t389');
6693            ## Ignore the token            ## Ignore the token
6694              !!!nack ('t389'); ## NOTE: Not acknowledged.
6695            !!!next-token;            !!!next-token;
6696            redo B;            next B;
6697          } else {          } else {
6698              !!!ack ('t391.1');
6699    
6700            my $at = $token->{attributes};            my $at = $token->{attributes};
6701            my $form_attrs;            my $form_attrs;
6702            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5938  sub _tree_construction_main ($) { Line 6718  sub _tree_construction_main ($) {
6718            if ($prompt_attr) {            if ($prompt_attr) {
6719              !!!cp ('t390');              !!!cp ('t390');
6720              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6721                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
6722                              };
6723            } else {            } else {
6724              !!!cp ('t391');              !!!cp ('t391');
6725              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6726                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
6727                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
6728                              }; # SHOULD
6729              ## TODO: make this configurable              ## TODO: make this configurable
6730            }            }
6731            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 6740  sub _tree_construction_main ($) {
6740                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6741                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6742                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6743            !!!back-token (@tokens);            !!!back-token (@tokens);
6744            redo B;            !!!next-token;
6745              next B;
6746          }          }
6747        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6748          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6749          my $el;          my $el;
6750          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6751                    
6752          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6753          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 6756  sub _tree_construction_main ($) {
6756          $insert->($el);          $insert->($el);
6757                    
6758          my $text = '';          my $text = '';
6759            !!!nack ('t392.1');
6760          !!!next-token;          !!!next-token;
6761          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6762            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6007  sub _tree_construction_main ($) { Line 6790  sub _tree_construction_main ($) {
6790            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6791          }          }
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794          } elsif ($token->{tag_name} eq 'math' or
6795                   $token->{tag_name} eq 'svg') {
6796            $reconstruct_active_formatting_elements->($insert_to_current);
6797    
6798            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6799    
6800            ## "adjust foreign attributes" - done in insert-element-f
6801            
6802            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6803            
6804            if ($self->{self_closing}) {
6805              pop @{$self->{open_elements}};
6806              !!!ack ('t398.1');
6807            } else {
6808              !!!cp ('t398.2');
6809              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6810              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6811              ## mode, "in body" (not "in foreign content") secondary insertion
6812              ## mode, maybe.
6813            }
6814    
6815            !!!next-token;
6816            next B;
6817        } elsif ({        } elsif ({
6818                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6819                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6017  sub _tree_construction_main ($) { Line 6823  sub _tree_construction_main ($) {
6823          !!!cp ('t401');          !!!cp ('t401');
6824          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6825          ## Ignore the token          ## Ignore the token
6826            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6827          !!!next-token;          !!!next-token;
6828          redo B;          next B;
6829                    
6830          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6831        } else {        } else {
# Line 6033  sub _tree_construction_main ($) { Line 6840  sub _tree_construction_main ($) {
6840          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
6841          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6842                    
6843          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6844    
6845          if ({          if ({
6846               applet => 1, marquee => 1, object => 1,               applet => 1, marquee => 1, object => 1,
6847              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6848            !!!cp ('t380');            !!!cp ('t380');
6849            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6850              !!!nack ('t380.1');
6851          } elsif ({          } elsif ({
6852                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6853                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 6855  sub _tree_construction_main ($) {
6855                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6856            !!!cp ('t375');            !!!cp ('t375');
6857            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6858              !!!nack ('t375.1');
6859          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6860            !!!cp ('t388');            !!!cp ('t388');
6861            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6862            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6863              !!!ack ('t388.2');
6864          } elsif ({          } elsif ({
6865                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6866                    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 6868  sub _tree_construction_main ($) {
6868                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6869            !!!cp ('t388.1');            !!!cp ('t388.1');
6870            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6871              !!!ack ('t388.3');
6872          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6873            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6874                    
# Line 6070  sub _tree_construction_main ($) { Line 6881  sub _tree_construction_main ($) {
6881              !!!cp ('t400.2');              !!!cp ('t400.2');
6882              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6883            }            }
6884              !!!nack ('t400.3');
6885          } else {          } else {
6886            !!!cp ('t402');            !!!nack ('t402');
6887          }          }
6888                    
6889          !!!next-token;          !!!next-token;
6890          redo B;          next B;
6891        }        }
6892      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6893        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 6895  sub _tree_construction_main ($) {
6895          my $i;          my $i;
6896          INSCOPE: {          INSCOPE: {
6897            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6898              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6899                !!!cp ('t405');                !!!cp ('t405');
6900                $i = $_;                $i = $_;
6901                last INSCOPE;                last INSCOPE;
6902              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6903                !!!cp ('t405.1');                !!!cp ('t405.1');
6904                last;                last;
6905              }              }
# Line 6100  sub _tree_construction_main ($) { Line 6909  sub _tree_construction_main ($) {
6909                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6910            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6911            !!!next-token;            !!!next-token;
6912            redo B;            next B;
6913          } # INSCOPE          } # INSCOPE
6914    
6915          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6916            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]}) {  
6917              !!!cp ('t403');              !!!cp ('t403');
6918              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6919                                value => $_->[0]->manakai_local_name,
6920                                token => $token);
6921              last;              last;
6922            } else {            } else {
6923              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 6926  sub _tree_construction_main ($) {
6926    
6927          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6928          !!!next-token;          !!!next-token;
6929          redo B;          next B;
6930        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6931          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
6932            ## up-to-date, though it has same effect as speced.
6933            if (@{$self->{open_elements}} > 1 and
6934                $self->{open_elements}->[1]->[1] & BODY_EL) {
6935            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6936            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6937              !!!cp ('t406');              !!!cp ('t406');
6938              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6939                                value => $self->{open_elements}->[1]->[0]
6940                                    ->manakai_local_name,
6941                                token => $token);
6942            } else {            } else {
6943              !!!cp ('t407');              !!!cp ('t407');
6944            }            }
6945            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6946            ## reprocess            ## reprocess
6947            redo B;            next B;
6948          } else {          } else {
6949            !!!cp ('t408');            !!!cp ('t408');
6950            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6951            ## Ignore the token            ## Ignore the token
6952            !!!next-token;            !!!next-token;
6953            redo B;            next B;
6954          }          }
6955        } elsif ({        } elsif ({
6956                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 6963  sub _tree_construction_main ($) {
6963          my $i;          my $i;
6964          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6965            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6966            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6967              !!!cp ('t410');              !!!cp ('t410');
6968              $i = $_;              $i = $_;
6969              last INSCOPE;              last INSCOPE;
6970            } 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]}) {  
6971              !!!cp ('t411');              !!!cp ('t411');
6972              last INSCOPE;              last INSCOPE;
6973            }            }
# Line 6173  sub _tree_construction_main ($) { Line 6983  sub _tree_construction_main ($) {
6983                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6984                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6985                    p => 1,                    p => 1,
6986                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6987              !!!cp ('t409');              !!!cp ('t409');
6988              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6989            }            }
6990    
6991            ## Step 2.            ## Step 2.
6992            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6993                      ne $token->{tag_name}) {
6994              !!!cp ('t412');              !!!cp ('t412');
6995              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6996                                value => $self->{open_elements}->[-1]->[0]
6997                                    ->manakai_local_name,
6998                                token => $token);
6999            } else {            } else {
7000              !!!cp ('t414');              !!!cp ('t414');
7001            }            }
# Line 6196  sub _tree_construction_main ($) { Line 7010  sub _tree_construction_main ($) {
7010                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7011          }          }
7012          !!!next-token;          !!!next-token;
7013          redo B;          next B;
7014        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7015          undef $self->{form_element};          undef $self->{form_element};
7016    
# Line 6204  sub _tree_construction_main ($) { Line 7018  sub _tree_construction_main ($) {
7018          my $i;          my $i;
7019          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7020            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7021            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7022              !!!cp ('t418');              !!!cp ('t418');
7023              $i = $_;              $i = $_;
7024              last INSCOPE;              last INSCOPE;
7025            } 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]}) {  
7026              !!!cp ('t419');              !!!cp ('t419');
7027              last INSCOPE;              last INSCOPE;
7028            }            }
# Line 6222  sub _tree_construction_main ($) { Line 7033  sub _tree_construction_main ($) {
7033            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7034          } else {          } else {
7035            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7036            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7037              !!!cp ('t417');              !!!cp ('t417');
7038              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7039            }            }
7040                        
7041            ## Step 2.            ## Step 2.
7042            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7043                      ne $token->{tag_name}) {
7044              !!!cp ('t417.1');              !!!cp ('t417.1');
7045              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7046                                value => $self->{open_elements}->[-1]->[0]
7047                                    ->manakai_local_name,
7048                                token => $token);
7049            } else {            } else {
7050              !!!cp ('t420');              !!!cp ('t420');
7051            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 7055  sub _tree_construction_main ($) {
7055          }          }
7056    
7057          !!!next-token;          !!!next-token;
7058          redo B;          next B;
7059        } elsif ({        } elsif ({
7060                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7061                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 7063  sub _tree_construction_main ($) {
7063          my $i;          my $i;
7064          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7065            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7066            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7067              !!!cp ('t423');              !!!cp ('t423');
7068              $i = $_;              $i = $_;
7069              last INSCOPE;              last INSCOPE;
7070            } 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]}) {  
7071              !!!cp ('t424');              !!!cp ('t424');
7072              last INSCOPE;              last INSCOPE;
7073            }            }
# Line 6270  sub _tree_construction_main ($) { Line 7078  sub _tree_construction_main ($) {
7078            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7079          } else {          } else {
7080            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7081            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7082              !!!cp ('t422');              !!!cp ('t422');
7083              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7084            }            }
7085                        
7086            ## Step 2.            ## Step 2.
7087            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7088                      ne $token->{tag_name}) {
7089              !!!cp ('t425');              !!!cp ('t425');
7090              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7091            } else {            } else {
# Line 6290  sub _tree_construction_main ($) { Line 7097  sub _tree_construction_main ($) {
7097          }          }
7098                    
7099          !!!next-token;          !!!next-token;
7100          redo B;          next B;
7101        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7102          ## has an element in scope          ## has an element in scope
7103          my $i;          my $i;
7104          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7105            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7106            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7107              !!!cp ('t410.1');              !!!cp ('t410.1');
7108              $i = $_;              $i = $_;
7109              last INSCOPE;              last INSCOPE;
7110            } 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]}) {  
7111              !!!cp ('t411.1');              !!!cp ('t411.1');
7112              last INSCOPE;              last INSCOPE;
7113            }            }
7114          } # INSCOPE          } # INSCOPE
7115    
7116          if (defined $i) {          if (defined $i) {
7117            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7118                      ne $token->{tag_name}) {
7119              !!!cp ('t412.1');              !!!cp ('t412.1');
7120              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7121                                value => $self->{open_elements}->[-1]->[0]
7122                                    ->manakai_local_name,
7123                                token => $token);
7124            } else {            } else {
7125              !!!cp ('t414.1');              !!!cp ('t414.1');
7126            }            }
# Line 6325  sub _tree_construction_main ($) { Line 7133  sub _tree_construction_main ($) {
7133            !!!cp ('t415.1');            !!!cp ('t415.1');
7134            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7135            my $el;            my $el;
7136            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7137            $insert->($el);            $insert->($el);
7138            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7139          }          }
7140    
7141          !!!next-token;          !!!next-token;
7142          redo B;          next B;
7143        } elsif ({        } elsif ({
7144                  a => 1,                  a => 1,
7145                  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 7148  sub _tree_construction_main ($) {
7148                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7149          !!!cp ('t427');          !!!cp ('t427');
7150          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7151          redo B;          next B;
7152        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7153          !!!cp ('t428');          !!!cp ('t428');
7154          !!!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 7157  sub _tree_construction_main ($) {
7157          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7158                    
7159          my $el;          my $el;
7160          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7161          $insert->($el);          $insert->($el);
7162                    
7163          ## Ignore the token.          ## Ignore the token.
7164          !!!next-token;          !!!next-token;
7165          redo B;          next B;
7166        } elsif ({        } elsif ({
7167                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7168                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6371  sub _tree_construction_main ($) { Line 7179  sub _tree_construction_main ($) {
7179          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7180          ## Ignore the token          ## Ignore the token
7181          !!!next-token;          !!!next-token;
7182          redo B;          next B;
7183                    
7184          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7185                    
# Line 6382  sub _tree_construction_main ($) { Line 7190  sub _tree_construction_main ($) {
7190    
7191          ## Step 2          ## Step 2
7192          S2: {          S2: {
7193            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7194              ## Step 1              ## Step 1
7195              ## generate implied end tags              ## generate implied end tags
7196              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7197                !!!cp ('t430');                !!!cp ('t430');
7198                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7199                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7200              }              }
7201                    
7202              ## Step 2              ## Step 2
7203              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7204                        ne $token->{tag_name}) {
7205                !!!cp ('t431');                !!!cp ('t431');
7206                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7207                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7208                                  value => $self->{open_elements}->[-1]->[0]
7209                                      ->manakai_local_name,
7210                                  token => $token);
7211              } else {              } else {
7212                !!!cp ('t432');                !!!cp ('t432');
7213              }              }
# Line 6409  sub _tree_construction_main ($) { Line 7219  sub _tree_construction_main ($) {
7219              last S2;              last S2;
7220            } else {            } else {
7221              ## Step 3              ## Step 3
7222              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7223                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7224                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7225                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7226                !!!cp ('t433');                !!!cp ('t433');
7227                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7228                ## Ignore the token                ## Ignore the token
# Line 6430  sub _tree_construction_main ($) { Line 7240  sub _tree_construction_main ($) {
7240            ## Step 5;            ## Step 5;
7241            redo S2;            redo S2;
7242          } # S2          } # S2
7243          redo B;          next B;
7244        }        }
7245      }      }
7246      redo B;      next B;
7247      } continue { # B
7248        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7249          ## NOTE: The code below is executed in cases where it does not have
7250          ## to be, but it it is harmless even in those cases.
7251          ## has an element in scope
7252          INSCOPE: {
7253            for (reverse 0..$#{$self->{open_elements}}) {
7254              my $node = $self->{open_elements}->[$_];
7255              if ($node->[1] & FOREIGN_EL) {
7256                last INSCOPE;
7257              } elsif ($node->[1] & SCOPING_EL) {
7258                last;
7259              }
7260            }
7261            
7262            ## NOTE: No foreign element in scope.
7263            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7264          } # INSCOPE
7265        }
7266    } # B    } # B
7267    
7268    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6479  sub set_inner_html ($$$) { Line 7308  sub set_inner_html ($$$) {
7308    
7309      ## Step 8 # MUST      ## Step 8 # MUST
7310      my $i = 0;      my $i = 0;
7311      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7312      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7313      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7314        my $self = shift;        my $self = shift;
7315    
# Line 6489  sub set_inner_html ($$$) { Line 7318  sub set_inner_html ($$$) {
7318    
7319        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7320        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7321        $column++;  
7322          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7323          $p->{column}++;
7324    
7325        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7326          $line++;          $p->{line}++;
7327          $column = 0;          $p->{column} = 0;
7328          !!!cp ('i1');          !!!cp ('i1');
7329        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7330          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7331          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7332          $line++;          $p->{line}++;
7333          $column = 0;          $p->{column} = 0;
7334          !!!cp ('i2');          !!!cp ('i2');
7335        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7336          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7339  sub set_inner_html ($$$) {
7339          !!!cp ('i4');          !!!cp ('i4');
7340          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7341          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7342          } elsif ($self->{next_char} <= 0x0008 or
7343                   (0x000E <= $self->{next_char} and
7344                    $self->{next_char} <= 0x001F) or
7345                   (0x007F <= $self->{next_char} and
7346                    $self->{next_char} <= 0x009F) or
7347                   (0xD800 <= $self->{next_char} and
7348                    $self->{next_char} <= 0xDFFF) or
7349                   (0xFDD0 <= $self->{next_char} and
7350                    $self->{next_char} <= 0xFDDF) or
7351                   {
7352                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7353                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7354                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7355                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7356                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7357                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7358                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7359                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7360                    0x10FFFE => 1, 0x10FFFF => 1,
7361                   }->{$self->{next_char}}) {
7362            !!!cp ('i4.1');
7363            !!!parse-error (type => 'control char', level => $self->{must_level});
7364    ## TODO: error type documentation
7365        }        }
7366      };      };
7367      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6515  sub set_inner_html ($$$) { Line 7369  sub set_inner_html ($$$) {
7369            
7370      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7371        my (%opt) = @_;        my (%opt) = @_;
7372        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7373          my $column = $opt{column};
7374          if (defined $opt{token} and defined $opt{token}->{line}) {
7375            $line = $opt{token}->{line};
7376            $column = $opt{token}->{column};
7377          }
7378          warn "Parse error ($opt{type}) at line $line column $column\n";
7379      };      };
7380      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7381        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7382      };      };
7383            
7384      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7402  sub set_inner_html ($$$) {
7402          unless defined $p->{content_model};          unless defined $p->{content_model};
7403          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7404    
7405      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7406          ## TODO: Foreign element OK?
7407    
7408      ## Step 3      ## Step 3
7409      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7413  sub set_inner_html ($$$) {
7413      $doc->append_child ($root);      $doc->append_child ($root);
7414    
7415      ## Step 5 # MUST      ## Step 5 # MUST
7416      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7417    
7418      undef $p->{head_element};      undef $p->{head_element};
7419    
# Line 6598  sub set_inner_html ($$$) { Line 7459  sub set_inner_html ($$$) {
7459      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7460    
7461      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7462    
7463        delete $p->{parse_error}; # delete loop
7464    } else {    } else {
7465      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";
7466    }    }

Legend:
Removed from v.1.115  
changed lines
  Added in v.1.150

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24