/[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.84 by wakaba, Thu Mar 6 15:23:17 2008 UTC revision 1.157 by wakaba, Sun Aug 31 09:12:30 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    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$) {
358    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
359    my $charset = shift;    my $charset_name = shift;
360    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;  
   }  
361    
362    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
363      my $self = shift;      my (%opt) = @_;
364      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
365      ## TODO: if $charset is supported    };
366      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      ## Step 1        my $charset;
371      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
372        $charset = 'utf-8';    my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390      }      }
391    
392      ## Step 2      ## Step 2
393      if (defined $self->{input_encoding} and      my $byte_buffer = '';
394          $self->{input_encoding} eq $charset) {      for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406        $self->{confident} = 1;        $self->{confident} = 1;
407        return;        last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422      }      }
423    
424      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
425          ':'.$charset, level => 'w');      ## TODO: <meta charset>
426    
427      ## Step 3      ## Step 5
428      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
429    
430      ## Step 4      ## Step 6
431      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543    }; # $self->{change_encoding}    }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
557    my $return;    my $return;
558    try {    try {
559      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
560    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
561      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
562      $s = \ (Encode::decode ($charset, $$bytes_s));      
563      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578      $self->{confident} = 1;      $self->{confident} = 1;
579      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581    };    };
582    return $return;    return $return;
583  } # parse_byte_string  } # parse_byte_stream
584    
585  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 591  sub parse_byte_string ($$$$;$) {
591  ## 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
592  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
593    
594  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
604    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
605    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
608    
# Line 177  sub parse_string ($$$;$) { Line 613  sub parse_string ($$$;$) {
613        if defined $self->{input_encoding};        if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620    
621      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626      $column++;        $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639        $line++;        !!!cp ('j1');
640        $column = 0;        $self->{line}++;
641          $self->{column} = 0;
642      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
643        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
644          my $next = $input->getc;
645          if (defined $next and $next ne "\x0A") {
646            $self->{next_next_char} = $next;
647          }
648        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
649        $line++;        $self->{line}++;
650        $column = 0;        $self->{column} = 0;
651      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
657        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678          } else {
679            !!!parse-error (type => 'control char',
680                            text => (sprintf 'U-%08X', $self->{next_char}));
681          }
682      }      }
683    };    };
684    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 686  sub parse_string ($$$;$) {
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710        level => {must => 'm',
711                  warn => 'w',
712                  info => 'i',
713                  uncertain => 'u'},
714      }, $class;
715    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
716      $self->{next_char} = -1;      $self->{next_char} = -1;
717    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 773  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
773  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
774  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
775  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
776    sub SELF_CLOSING_START_TAG_STATE () { 34 }
777    sub CDATA_BLOCK_STATE () { 35 }
778    
779  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
780  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 791  sub TABLE_IMS ()      { 0b1000000 }
791  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
792  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
793  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
794    sub SELECT_IMS ()     { 0b10000000000 }
795    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
796        ## NOTE: "in foreign content" insertion mode is special; it is combined
797        ## with the secondary insertion mode.  In this parser, they are stored
798        ## together in the bit-or'ed form.
799    
800  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
801    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 818  sub IN_TABLE_IM () { TABLE_IMS }
818  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
819  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
820  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
821  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
822    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
823  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
824    
825  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 832  sub _initialize_tokenizer ($) {
832    undef $self->{current_attribute};    undef $self->{current_attribute};
833    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
834    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
835      delete $self->{self_closing};
836    $self->{char} = [];    $self->{char} = [];
837    # $self->{next_char}    # $self->{next_char}
838    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 853  sub _initialize_tokenizer ($) {
853  ##        ->{value}  ##        ->{value}
854  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
855  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
856    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
857    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
858    ##     while the token is pushed back to the stack.
859    
860  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
861    
# Line 384  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882    
883  sub _get_next_token ($) {  sub _get_next_token ($) {
884    my $self = shift;    my $self = shift;
885    
886      if ($self->{self_closing}) {
887        !!!parse-error (type => 'nestc', token => $self->{current_token});
888        ## NOTE: The |self_closing| flag is only set by start tag token.
889        ## In addition, when a start tag token is emitted, it is always set to
890        ## |current_token|.
891        delete $self->{self_closing};
892      }
893    
894    if (@{$self->{token}}) {    if (@{$self->{token}}) {
895        $self->{self_closing} = $self->{token}->[0]->{self_closing};
896      return shift @{$self->{token}};      return shift @{$self->{token}};
897    }    }
898    
# Line 447  sub _get_next_token ($) { Line 955  sub _get_next_token ($) {
955          #          #
956        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
957          !!!cp (11);          !!!cp (11);
958          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
959                      line => $self->{line}, column => $self->{column}});
960          last A; ## TODO: ok?          last A; ## TODO: ok?
961        } else {        } else {
962          !!!cp (12);          !!!cp (12);
963        }        }
964        # Anything else        # Anything else
965        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
966                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
967                       line => $self->{line}, column => $self->{column},
968                      };
969        ## Stay in the data state        ## Stay in the data state
970        !!!next-input-character;        !!!next-input-character;
971    
# Line 463  sub _get_next_token ($) { Line 974  sub _get_next_token ($) {
974        redo A;        redo A;
975      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
976        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
977    
978          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
979                
980        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
981    
# Line 471  sub _get_next_token ($) { Line 984  sub _get_next_token ($) {
984    
985        unless (defined $token) {        unless (defined $token) {
986          !!!cp (13);          !!!cp (13);
987          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
988                      line => $l, column => $c,
989                     });
990        } else {        } else {
991          !!!cp (14);          !!!cp (14);
992          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 1005  sub _get_next_token ($) {
1005            ## reconsume            ## reconsume
1006            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1007    
1008            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1009                        line => $self->{line_prev},
1010                        column => $self->{column_prev},
1011                       });
1012    
1013            redo A;            redo A;
1014          }          }
# Line 510  sub _get_next_token ($) { Line 1028  sub _get_next_token ($) {
1028            !!!cp (19);            !!!cp (19);
1029            $self->{current_token}            $self->{current_token}
1030              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1031                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1032                   line => $self->{line_prev},
1033                   column => $self->{column_prev}};
1034            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1035            !!!next-input-character;            !!!next-input-character;
1036            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1038  sub _get_next_token ($) {
1038                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1039            !!!cp (20);            !!!cp (20);
1040            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1041                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1042                                        line => $self->{line_prev},
1043                                        column => $self->{column_prev}};
1044            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1045            !!!next-input-character;            !!!next-input-character;
1046            redo A;            redo A;
1047          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1048            !!!cp (21);            !!!cp (21);
1049            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1050                              line => $self->{line_prev},
1051                              column => $self->{column_prev});
1052            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054    
1055            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1062            !!!cp (22);            !!!cp (22);
1063            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1064                              line => $self->{line_prev},
1065                              column => $self->{column_prev});
1066            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1067              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1068                                        line => $self->{line_prev},
1069                                        column => $self->{column_prev},
1070                                       };
1071            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1072            redo A;            redo A;
1073          } else {          } else {
1074            !!!cp (23);            !!!cp (23);
1075            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1076                              line => $self->{line_prev},
1077                              column => $self->{column_prev});
1078            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1079            ## reconsume            ## reconsume
1080    
1081            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1082                        line => $self->{line_prev},
1083                        column => $self->{column_prev},
1084                       });
1085    
1086            redo A;            redo A;
1087          }          }
# Line 551  sub _get_next_token ($) { Line 1089  sub _get_next_token ($) {
1089          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1090        }        }
1091      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1092          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1093        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1094          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1095    
1096            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1097            my @next_char;            my @next_char;
1098            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 1109  sub _get_next_token ($) {
1109                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1110                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1111    
1112                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1113                            line => $l, column => $c,
1114                           });
1115        
1116                redo A;                redo A;
1117              }              }
# Line 588  sub _get_next_token ($) { Line 1130  sub _get_next_token ($) {
1130              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1131              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1132              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1133              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1134                          line => $l, column => $c,
1135                         });
1136              redo A;              redo A;
1137            } else {            } else {
1138              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 1145  sub _get_next_token ($) {
1145            !!!cp (28);            !!!cp (28);
1146            # next-input-character is already done            # next-input-character is already done
1147            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1148            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1149                        line => $l, column => $c,
1150                       });
1151            redo A;            redo A;
1152          }          }
1153        }        }
# Line 609  sub _get_next_token ($) { Line 1155  sub _get_next_token ($) {
1155        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1156            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1157          !!!cp (29);          !!!cp (29);
1158          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1159                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1160                   tag_name => chr ($self->{next_char} + 0x0020),
1161                   line => $l, column => $c};
1162          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1163          !!!next-input-character;          !!!next-input-character;
1164          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1166  sub _get_next_token ($) {
1166                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1167          !!!cp (30);          !!!cp (30);
1168          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1169                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1170                                      line => $l, column => $c};
1171          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1172          !!!next-input-character;          !!!next-input-character;
1173          redo A;          redo A;
1174        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1175          !!!cp (31);          !!!cp (31);
1176          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1177                            line => $self->{line_prev}, ## "<" in "</>"
1178                            column => $self->{column_prev} - 1);
1179          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1180          !!!next-input-character;          !!!next-input-character;
1181          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1185  sub _get_next_token ($) {
1185          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1186          # reconsume          # reconsume
1187    
1188          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1189                      line => $l, column => $c,
1190                     });
1191    
1192          redo A;          redo A;
1193        } else {        } else {
1194          !!!cp (33);          !!!cp (33);
1195          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1196          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1197            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1198                                      line => $self->{line_prev}, # "<" of "</"
1199                                      column => $self->{column_prev} - 1,
1200                                     };
1201          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1202          redo A;          redo A;
1203        }        }
# Line 657  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1215          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1216            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1217            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1218          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1219            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1245  sub _get_next_token ($) {
1245          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1246          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1247            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1248            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1249          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1250            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1265  sub _get_next_token ($) {
1265    
1266          redo A;          redo A;
1267        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1268            !!!cp (42);
1269            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1270          !!!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  
1271          redo A;          redo A;
1272        } else {        } else {
1273          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1290  sub _get_next_token ($) {
1290        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1291          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1292            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1293            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1294          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1295            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1311  sub _get_next_token ($) {
1311        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1312                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1313          !!!cp (49);          !!!cp (49);
1314          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1315                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1316                   value => '',
1317                   line => $self->{line}, column => $self->{column}};
1318          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320          redo A;          redo A;
1321        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1322            !!!cp (50);
1323            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1324          !!!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  
1325          redo A;          redo A;
1326        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1327          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1328          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1329            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1330            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1331          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1332            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1356  sub _get_next_token ($) {
1356          } else {          } else {
1357            !!!cp (56);            !!!cp (56);
1358          }          }
1359          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1360                                value => ''};              = {name => chr ($self->{next_char}),
1361                   value => '',
1362                   line => $self->{line}, column => $self->{column}};
1363          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1370              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1371            !!!cp (57);            !!!cp (57);
1372            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1373            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1374          } else {          } else {
1375            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1422  sub _get_next_token ($) {
1422          !!!next-input-character;          !!!next-input-character;
1423          redo A;          redo A;
1424        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1425            !!!cp (64);
1426          $before_leave->();          $before_leave->();
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!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  
1429          redo A;          redo A;
1430        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          $before_leave->();          $before_leave->();
1433          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1434            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1435            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1436          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1437            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1482  sub _get_next_token ($) {
1482        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1483          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1504  sub _get_next_token ($) {
1504        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1505                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1506          !!!cp (76);          !!!cp (76);
1507          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1508                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1509                   value => '',
1510                   line => $self->{line}, column => $self->{column}};
1511          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1515            !!!cp (77);
1516            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1517          !!!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  
1518          redo A;          redo A;
1519        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1520          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1521          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1522            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1523            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1524          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1525            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1034  sub _get_next_token ($) { Line 1540  sub _get_next_token ($) {
1540    
1541          redo A;          redo A;
1542        } else {        } else {
1543          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1544          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1545                                value => ''};            !!!cp (78);
1546              !!!parse-error (type => 'bad attribute name');
1547            } else {
1548              !!!cp (82);
1549            }
1550            $self->{current_attribute}
1551                = {name => chr ($self->{next_char}),
1552                   value => '',
1553                   line => $self->{line}, column => $self->{column}};
1554          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;                  redo A;        
# Line 1067  sub _get_next_token ($) { Line 1581  sub _get_next_token ($) {
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1584            !!!parse-error (type => 'empty unquoted attribute value');
1585          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1586            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1587            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1588          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1589            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1607  sub _get_next_token ($) {
1607          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1608          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1609            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1610            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1611          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1612            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1654  sub _get_next_token ($) {
1654          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1655          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1656            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1657            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1658          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1659            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1696  sub _get_next_token ($) {
1696          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1697          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1698            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1699            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1700          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1701            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1741  sub _get_next_token ($) {
1741        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1742          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1743            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1744            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1745          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1746            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1764  sub _get_next_token ($) {
1764          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1765          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1766            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1767            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1768          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1769            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1834  sub _get_next_token ($) {
1834        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1835          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1836            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1837            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1838          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1839            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854    
1855          redo A;          redo A;
1856        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1857            !!!cp (122);
1858            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1859          !!!next-input-character;          !!!next-input-character;
1860          if ($self->{next_char} == 0x003E and # >          redo A;
1861              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1862              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1863            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1864            !!!cp (122);            !!!cp (122.3);
1865            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1866            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1867              if ($self->{current_token}->{attributes}) {
1868                !!!cp (122.1);
1869                !!!parse-error (type => 'end tag attribute');
1870              } else {
1871                ## NOTE: This state should never be reached.
1872                !!!cp (122.2);
1873              }
1874          } else {          } else {
1875            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1876          }          }
1877          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1878          # next-input-character is already done          ## Reconsume.
1879            !!!emit ($self->{current_token}); # start tag or end tag
1880          redo A;          redo A;
1881        } else {        } else {
1882          !!!cp (124);          !!!cp ('124.1');
1883          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1884          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1885          ## reconsume          ## reconsume
1886          redo A;          redo A;
1887        }        }
1888        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1889          if ($self->{next_char} == 0x003E) { # >
1890            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1891              !!!cp ('124.2');
1892              !!!parse-error (type => 'nestc', token => $self->{current_token});
1893              ## TODO: Different type than slash in start tag
1894              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1895              if ($self->{current_token}->{attributes}) {
1896                !!!cp ('124.4');
1897                !!!parse-error (type => 'end tag attribute');
1898              } else {
1899                !!!cp ('124.5');
1900              }
1901              ## TODO: Test |<title></title/>|
1902            } else {
1903              !!!cp ('124.3');
1904              $self->{self_closing} = 1;
1905            }
1906    
1907            $self->{state} = DATA_STATE;
1908            !!!next-input-character;
1909    
1910            !!!emit ($self->{current_token}); # start tag or end tag
1911    
1912            redo A;
1913          } elsif ($self->{next_char} == -1) {
1914            !!!parse-error (type => 'unclosed tag');
1915            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1916              !!!cp (124.7);
1917              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1918            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1919              if ($self->{current_token}->{attributes}) {
1920                !!!cp (124.5);
1921                !!!parse-error (type => 'end tag attribute');
1922              } else {
1923                ## NOTE: This state should never be reached.
1924                !!!cp (124.6);
1925              }
1926            } else {
1927              die "$0: $self->{current_token}->{type}: Unknown token type";
1928            }
1929            $self->{state} = DATA_STATE;
1930            ## Reconsume.
1931            !!!emit ($self->{current_token}); # start tag or end tag
1932            redo A;
1933          } else {
1934            !!!cp ('124.4');
1935            !!!parse-error (type => 'nestc');
1936            ## TODO: This error type is wrong.
1937            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1938            ## Reconsume.
1939            redo A;
1940          }
1941      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1942        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1943                
1944        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1945          #my $token = {type => COMMENT_TOKEN, data => ''};
1946    
1947        BC: {        BC: {
1948          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1950  sub _get_next_token ($) {
1950            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1951            !!!next-input-character;            !!!next-input-character;
1952    
1953            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1954    
1955            redo A;            redo A;
1956          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1958  sub _get_next_token ($) {
1958            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1959            ## reconsume            ## reconsume
1960    
1961            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1962    
1963            redo A;            redo A;
1964          } else {          } else {
1965            !!!cp (126);            !!!cp (126);
1966            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1967            !!!next-input-character;            !!!next-input-character;
1968            redo BC;            redo BC;
1969          }          }
# Line 1408  sub _get_next_token ($) { Line 1973  sub _get_next_token ($) {
1973      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1974        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1975    
1976          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1977    
1978        my @next_char;        my @next_char;
1979        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1980                
# Line 1416  sub _get_next_token ($) { Line 1983  sub _get_next_token ($) {
1983          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1984          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1985            !!!cp (127);            !!!cp (127);
1986            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1987                                        line => $l, column => $c,
1988                                       };
1989            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1990            !!!next-input-character;            !!!next-input-character;
1991            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 2021  sub _get_next_token ($) {
2021                      !!!cp (129);                      !!!cp (129);
2022                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
2023                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2024                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2025                                                  quirks => 1,
2026                                                  line => $l, column => $c,
2027                                                 };
2028                      !!!next-input-character;                      !!!next-input-character;
2029                      redo A;                      redo A;
2030                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 2045  sub _get_next_token ($) {
2045          } else {          } else {
2046            !!!cp (135);            !!!cp (135);
2047          }          }
2048          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2049                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2050                   $self->{next_char} == 0x005B) { # [
2051            !!!next-input-character;
2052            push @next_char, $self->{next_char};
2053            if ($self->{next_char} == 0x0043) { # C
2054              !!!next-input-character;
2055              push @next_char, $self->{next_char};
2056              if ($self->{next_char} == 0x0044) { # D
2057                !!!next-input-character;
2058                push @next_char, $self->{next_char};
2059                if ($self->{next_char} == 0x0041) { # A
2060                  !!!next-input-character;
2061                  push @next_char, $self->{next_char};
2062                  if ($self->{next_char} == 0x0054) { # T
2063                    !!!next-input-character;
2064                    push @next_char, $self->{next_char};
2065                    if ($self->{next_char} == 0x0041) { # A
2066                      !!!next-input-character;
2067                      push @next_char, $self->{next_char};
2068                      if ($self->{next_char} == 0x005B) { # [
2069                        !!!cp (135.1);
2070                        $self->{state} = CDATA_BLOCK_STATE;
2071                        !!!next-input-character;
2072                        redo A;
2073                      } else {
2074                        !!!cp (135.2);
2075                      }
2076                    } else {
2077                      !!!cp (135.3);
2078                    }
2079                  } else {
2080                    !!!cp (135.4);                
2081                  }
2082                } else {
2083                  !!!cp (135.5);
2084                }
2085              } else {
2086                !!!cp (135.6);
2087              }
2088            } else {
2089              !!!cp (135.7);
2090            }
2091        } else {        } else {
2092          !!!cp (136);          !!!cp (136);
2093        }        }
# Line 1480  sub _get_next_token ($) { Line 2096  sub _get_next_token ($) {
2096        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2097        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2098        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2099          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2100                                    line => $l, column => $c,
2101                                   };
2102        redo A;        redo A;
2103                
2104        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 2222  sub _get_next_token ($) {
2222          redo A;          redo A;
2223        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2224          !!!cp (152);          !!!cp (152);
2225          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2226                            line => $self->{line_prev},
2227                            column => $self->{column_prev});
2228          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2229          ## Stay in the state          ## Stay in the state
2230          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2240  sub _get_next_token ($) {
2240          redo A;          redo A;
2241        } else {        } else {
2242          !!!cp (154);          !!!cp (154);
2243          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2244                            line => $self->{line_prev},
2245                            column => $self->{column_prev});
2246          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2247          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2248          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2281  sub _get_next_token ($) {
2281          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2282          !!!next-input-character;          !!!next-input-character;
2283    
2284          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2285    
2286          redo A;          redo A;
2287        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2290  sub _get_next_token ($) {
2290          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2291          ## reconsume          ## reconsume
2292    
2293          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2294    
2295          redo A;          redo A;
2296        } else {        } else {
2297          !!!cp (160);          !!!cp (160);
2298          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2299              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2300  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2301          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2302          !!!next-input-character;          !!!next-input-character;
# Line 2067  sub _get_next_token ($) { Line 2687  sub _get_next_token ($) {
2687          redo A;          redo A;
2688        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2689          !!!cp (208);          !!!cp (208);
2690          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2691    
2692          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2693          !!!next-input-character;          !!!next-input-character;
# Line 2103  sub _get_next_token ($) { Line 2723  sub _get_next_token ($) {
2723          redo A;          redo A;
2724        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2725          !!!cp (212);          !!!cp (212);
2726          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2727    
2728          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2729          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2771  sub _get_next_token ($) {
2771        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2772          !!!cp (217);          !!!cp (217);
2773          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2774          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2775          ## reconsume          ## reconsume
2776    
# Line 2192  sub _get_next_token ($) { Line 2811  sub _get_next_token ($) {
2811          !!!next-input-character;          !!!next-input-character;
2812          redo A;          redo A;
2813        }        }
2814        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2815          my $s = '';
2816          
2817          my ($l, $c) = ($self->{line}, $self->{column});
2818    
2819          CS: while ($self->{next_char} != -1) {
2820            if ($self->{next_char} == 0x005D) { # ]
2821              !!!next-input-character;
2822              if ($self->{next_char} == 0x005D) { # ]
2823                !!!next-input-character;
2824                MDC: {
2825                  if ($self->{next_char} == 0x003E) { # >
2826                    !!!cp (221.1);
2827                    !!!next-input-character;
2828                    last CS;
2829                  } elsif ($self->{next_char} == 0x005D) { # ]
2830                    !!!cp (221.2);
2831                    $s .= ']';
2832                    !!!next-input-character;
2833                    redo MDC;
2834                  } else {
2835                    !!!cp (221.3);
2836                    $s .= ']]';
2837                    #
2838                  }
2839                } # MDC
2840              } else {
2841                !!!cp (221.4);
2842                $s .= ']';
2843                #
2844              }
2845            } else {
2846              !!!cp (221.5);
2847              #
2848            }
2849            $s .= chr $self->{next_char};
2850            !!!next-input-character;
2851          } # CS
2852    
2853          $self->{state} = DATA_STATE;
2854          ## next-input-character done or EOF, which is reconsumed.
2855    
2856          if (length $s) {
2857            !!!cp (221.6);
2858            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2859                      line => $l, column => $c});
2860          } else {
2861            !!!cp (221.7);
2862          }
2863    
2864          redo A;
2865    
2866          ## ISSUE: "text tokens" in spec.
2867          ## TODO: Streaming support
2868      } else {      } else {
2869        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2870      }      }
# Line 2203  sub _get_next_token ($) { Line 2876  sub _get_next_token ($) {
2876  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2877    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2878    
2879      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2880    
2881    if ({    if ({
2882         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2883         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2918  sub _tokenize_attempt_to_consume_an_enti
2918            redo X;            redo X;
2919          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2920            !!!cp (1005);            !!!cp (1005);
2921            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2922            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2923            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2924            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2927  sub _tokenize_attempt_to_consume_an_enti
2927            !!!next-input-character;            !!!next-input-character;
2928          } else {          } else {
2929            !!!cp (1007);            !!!cp (1007);
2930            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2931          }          }
2932    
2933          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2934            !!!cp (1008);            !!!cp (1008);
2935            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => 'invalid character reference',
2936                              text => (sprintf 'U+%04X', $code),
2937                              line => $l, column => $c);
2938            $code = 0xFFFD;            $code = 0xFFFD;
2939          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2940            !!!cp (1009);            !!!cp (1009);
2941            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => 'invalid character reference',
2942                              text => (sprintf 'U-%08X', $code),
2943                              line => $l, column => $c);
2944            $code = 0xFFFD;            $code = 0xFFFD;
2945          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2946            !!!cp (1010);            !!!cp (1010);
2947            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2948            $code = 0x000A;            $code = 0x000A;
2949          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2950            !!!cp (1011);            !!!cp (1011);
2951            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2952            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2953          }          }
2954    
2955          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2956                  has_reference => 1};                  has_reference => 1,
2957                    line => $l, column => $c,
2958                   };
2959        } # X        } # X
2960      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2961               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2976  sub _tokenize_attempt_to_consume_an_enti
2976          !!!next-input-character;          !!!next-input-character;
2977        } else {        } else {
2978          !!!cp (1014);          !!!cp (1014);
2979          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2980        }        }
2981    
2982        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2983          !!!cp (1015);          !!!cp (1015);
2984          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
2985                            text => (sprintf 'U+%04X', $code),
2986                            line => $l, column => $c);
2987          $code = 0xFFFD;          $code = 0xFFFD;
2988        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2989          !!!cp (1016);          !!!cp (1016);
2990          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
2991                            text => (sprintf 'U-%08X', $code),
2992                            line => $l, column => $c);
2993          $code = 0xFFFD;          $code = 0xFFFD;
2994        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2995          !!!cp (1017);          !!!cp (1017);
2996          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
2997                            line => $l, column => $c);
2998          $code = 0x000A;          $code = 0x000A;
2999        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3000          !!!cp (1018);          !!!cp (1018);
3001          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3002                            text => (sprintf 'U+%04X', $code),
3003                            line => $l, column => $c);
3004          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3005        }        }
3006                
3007        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3008                  line => $l, column => $c,
3009                 };
3010      } else {      } else {
3011        !!!cp (1019);        !!!cp (1019);
3012        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3013        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
3014        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
3015        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 3026  sub _tokenize_attempt_to_consume_an_enti
3026      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3027      our $EntityChar;      our $EntityChar;
3028    
3029      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3030             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3031             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3032               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 3059  sub _tokenize_attempt_to_consume_an_enti
3059            
3060      if ($match > 0) {      if ($match > 0) {
3061        !!!cp (1023);        !!!cp (1023);
3062        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3063                  line => $l, column => $c,
3064                 };
3065      } elsif ($match < 0) {      } elsif ($match < 0) {
3066        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3067        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3068          !!!cp (1024);          !!!cp (1024);
3069          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3070                    line => $l, column => $c,
3071                   };
3072        } else {        } else {
3073          !!!cp (1025);          !!!cp (1025);
3074          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3075                    line => $l, column => $c,
3076                   };
3077        }        }
3078      } else {      } else {
3079        !!!cp (1026);        !!!cp (1026);
3080        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3081        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3082        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3083                  line => $l, column => $c,
3084                 };
3085      }      }
3086    } else {    } else {
3087      !!!cp (1027);      !!!cp (1027);
3088      ## no characters are consumed      ## no characters are consumed
3089      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3090      return undef;      return undef;
3091    }    }
3092  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2400  sub _initialize_tree_constructor ($) { Line 3098  sub _initialize_tree_constructor ($) {
3098    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3099    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3100    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3101      $self->{document}->set_user_data (manakai_source_line => 1);
3102      $self->{document}->set_user_data (manakai_source_column => 1);
3103  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3104    
3105  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2459  sub _tree_construction_initial ($) { Line 3159  sub _tree_construction_initial ($) {
3159            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3160            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3161          !!!cp ('t1');          !!!cp ('t1');
3162          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3163        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3164          !!!cp ('t2');          !!!cp ('t2');
3165          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3166          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3167        } else {        } else {
3168          !!!cp ('t3');          !!!cp ('t3');
3169        }        }
3170                
3171        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3172          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3173          ## NOTE: Default value for both |public_id| and |system_id| attributes
3174          ## are empty strings, so that we don't set any value in missing cases.
3175        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3176            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3177        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3186  sub _tree_construction_initial ($) {
3186        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3187          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3188          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3189          if ({          my $prefix = [
3190            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3191            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3192            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3193            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3194            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3195            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3196            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3197            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3198            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3199            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3200            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3201            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3202            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3203            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3204            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3205            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3206            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3207            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3208            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3209            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3210            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3211            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3212            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3213            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3214            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3215            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3216            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3217            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3218            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3219            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3220            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3221            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3222            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3223            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3224            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3225            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3226            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3227            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3228            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3229            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3230            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3231            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3232            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3233            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3234            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3235            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3236            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3237            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3238            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3239            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3240            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3241            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3242            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3243            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3244            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3245            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3246            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3247            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3248            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3249            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3250            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3251            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3252            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3253            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3254            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3255            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3256            "-//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}) {  
3257            !!!cp ('t5');            !!!cp ('t5');
3258            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3259          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3260                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3261            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3262              !!!cp ('t6');              !!!cp ('t6');
3263              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3265  sub _tree_construction_initial ($) {
3265              !!!cp ('t7');              !!!cp ('t7');
3266              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3267            }            }
3268          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3269                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3270            !!!cp ('t8');            !!!cp ('t8');
3271            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3272          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3279  sub _tree_construction_initial ($) {
3279          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3280          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3281          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") {
3282            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3283              ## marked as quirks.
3284            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3285            !!!cp ('t11');            !!!cp ('t11');
3286          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3299  sub _tree_construction_initial ($) {
3299                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3300               }->{$token->{type}}) {               }->{$token->{type}}) {
3301        !!!cp ('t14');        !!!cp ('t14');
3302        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3303        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3304        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3305        ## reprocess        ## reprocess
3306          !!!ack-later;
3307        return;        return;
3308      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3309        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3321  sub _tree_construction_initial ($) {
3321          !!!cp ('t17');          !!!cp ('t17');
3322        }        }
3323    
3324        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3325        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3326        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3327        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3350  sub _tree_construction_root_element ($)
3350    B: {    B: {
3351        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3352          !!!cp ('t19');          !!!cp ('t19');
3353          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3354          ## Ignore the token          ## Ignore the token
3355          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3356          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3384  sub _tree_construction_root_element ($)
3384        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3385          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3386            my $root_element;            my $root_element;
3387            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3388            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3389            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3390                  [$root_element, $el_category->{html}];
3391    
3392            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3393              !!!cp ('t24');              !!!cp ('t24');
3394              $self->{application_cache_selection}              $self->{application_cache_selection}
3395                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3396              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3397                ## According to Hixie (#whatwg 2008-03-19), it should be
3398                ## resolved against the base URI of the document in HTML
3399                ## or xml:base of the element in XHTML.
3400            } else {            } else {
3401              !!!cp ('t25');              !!!cp ('t25');
3402              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3403            }            }
3404    
3405              !!!nack ('t25c');
3406    
3407            !!!next-token;            !!!next-token;
3408            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3409          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3420  sub _tree_construction_root_element ($)
3420          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3421        }        }
3422    
3423      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3424        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3425      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3426      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3427    
3428      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3429    
3430      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3431        !!!ack-later;
3432      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3433    
3434      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3449  sub _reset_insertion_mode ($) {
3449            
3450      ## Step 3      ## Step 3
3451      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3452        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3453          $last = 1;          $last = 1;
3454          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3455            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3456                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3457              !!!cp ('t27');          } else {
3458              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3459          }          }
3460        }        }
3461              
3462        ## Step 4..13        ## Step 4..14
3463        my $new_mode = {        my $new_mode;
3464          if ($node->[1] & FOREIGN_EL) {
3465            !!!cp ('t28.1');
3466            ## NOTE: Strictly spaking, the line below only applies to MathML and
3467            ## SVG elements.  Currently the HTML syntax supports only MathML and
3468            ## SVG elements as foreigners.
3469            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3470          } elsif ($node->[1] & TABLE_CELL_EL) {
3471            if ($last) {
3472              !!!cp ('t28.2');
3473              #
3474            } else {
3475              !!!cp ('t28.3');
3476              $new_mode = IN_CELL_IM;
3477            }
3478          } else {
3479            !!!cp ('t28.4');
3480            $new_mode = {
3481                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3482                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3483                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3484                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3485                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3486                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3491  sub _reset_insertion_mode ($) {
3491                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3492                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3493                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3494                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3495          }
3496        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3497                
3498        ## Step 14        ## Step 15
3499        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3500          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3501            !!!cp ('t29');            !!!cp ('t29');
3502            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3510  sub _reset_insertion_mode ($) {
3510          !!!cp ('t31');          !!!cp ('t31');
3511        }        }
3512                
3513        ## Step 15        ## Step 16
3514        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3515                
3516        ## Step 16        ## Step 17
3517        $i--;        $i--;
3518        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3519                
3520        ## Step 17        ## Step 18
3521        redo S3;        redo S3;
3522      } # S3      } # S3
3523    
# Line 2908  sub _tree_construction_main ($) { Line 3621  sub _tree_construction_main ($) {
3621      !!!cp ('t39');      !!!cp ('t39');
3622    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3623    
3624    my $parse_rcdata = sub ($$) {    my $insert;
3625      my ($content_model_flag, $insert) = @_;  
3626      my $parse_rcdata = sub ($) {
3627        my ($content_model_flag) = @_;
3628    
3629      ## Step 1      ## Step 1
3630      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3631      my $el;      my $el;
3632      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3633    
3634      ## Step 2      ## Step 2
3635      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3636    
3637      ## Step 3      ## Step 3
3638      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3640  sub _tree_construction_main ($) {
3640    
3641      ## Step 4      ## Step 4
3642      my $text = '';      my $text = '';
3643        !!!nack ('t40.1');
3644      !!!next-token;      !!!next-token;
3645      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3646        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3663  sub _tree_construction_main ($) {
3663          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3664        !!!cp ('t42');        !!!cp ('t42');
3665        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3666      } else {      } else {
3667        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3668          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3669            !!!cp ('t43');
3670            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3671          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3672            !!!cp ('t44');
3673            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3674          } else {
3675            die "$0: $content_model_flag in parse_rcdata";
3676          }
3677      }      }
3678      !!!next-token;      !!!next-token;
3679    }; # $parse_rcdata    }; # $parse_rcdata
3680    
3681    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3682      my $script_el;      my $script_el;
3683      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3684      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3685    
3686      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3687      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3688            
3689      my $text = '';      my $text = '';
3690        !!!nack ('t45.1');
3691      !!!next-token;      !!!next-token;
3692      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3693        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3707  sub _tree_construction_main ($) {
3707        ## Ignore the token        ## Ignore the token
3708      } else {      } else {
3709        !!!cp ('t48');        !!!cp ('t48');
3710        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3711        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3712        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3713      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730      !!!next-token;      !!!next-token;
3731    }; # $script_start_tag    }; # $script_start_tag
3732    
3733      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3734      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3735      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3736    
3737    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3738      my $tag_name = shift;      my $end_tag_token = shift;
3739        my $tag_name = $end_tag_token->{tag_name};
3740    
3741        ## NOTE: The adoption agency algorithm (AAA).
3742    
3743      FET: {      FET: {
3744        ## Step 1        ## Step 1
3745        my $formatting_element;        my $formatting_element;
3746        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3747        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3748          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3749              !!!cp ('t52');
3750              last AFE;
3751            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3752                         eq $tag_name) {
3753            !!!cp ('t51');            !!!cp ('t51');
3754            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3755            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3756            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3757          }          }
3758        } # AFE        } # AFE
3759        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3760          !!!cp ('t53');          !!!cp ('t53');
3761          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3762          ## Ignore the token          ## Ignore the token
3763          !!!next-token;          !!!next-token;
3764          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3775  sub _tree_construction_main ($) {
3775              last INSCOPE;              last INSCOPE;
3776            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3777              !!!cp ('t55');              !!!cp ('t55');
3778              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
3779                                text => $token->{tag_name},
3780                                token => $end_tag_token);
3781              ## Ignore the token              ## Ignore the token
3782              !!!next-token;              !!!next-token;
3783              return;              return;
3784            }            }
3785          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3786            !!!cp ('t56');            !!!cp ('t56');
3787            $in_scope = 0;            $in_scope = 0;
3788          }          }
3789        } # INSCOPE        } # INSCOPE
3790        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3791          !!!cp ('t57');          !!!cp ('t57');
3792          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
3793                            text => $token->{tag_name},
3794                            token => $end_tag_token);
3795          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3796          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3797          return;          return;
3798        }        }
3799        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3800          !!!cp ('t58');          !!!cp ('t58');
3801          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3802                            text => $self->{open_elements}->[-1]->[0]
3803                                ->manakai_local_name,
3804                            token => $end_tag_token);
3805        }        }
3806                
3807        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3809  sub _tree_construction_main ($) {
3809        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3810        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3811          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3812          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3813              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3814              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3815               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3816            !!!cp ('t59');            !!!cp ('t59');
3817            $furthest_block = $node;            $furthest_block = $node;
3818            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3898  sub _tree_construction_main ($) {
3898        } # S7          } # S7  
3899                
3900        ## Step 8        ## Step 8
3901        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3902            my $foster_parent_element;
3903            my $next_sibling;
3904            OE: for (reverse 0..$#{$self->{open_elements}}) {
3905              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3906                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3907                                 if (defined $parent and $parent->node_type == 1) {
3908                                   !!!cp ('t65.1');
3909                                   $foster_parent_element = $parent;
3910                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3911                                 } else {
3912                                   !!!cp ('t65.2');
3913                                   $foster_parent_element
3914                                     = $self->{open_elements}->[$_ - 1]->[0];
3915                                 }
3916                                 last OE;
3917                               }
3918                             } # OE
3919                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3920                               unless defined $foster_parent_element;
3921            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3922            $open_tables->[-1]->[1] = 1; # tainted
3923          } else {
3924            !!!cp ('t65.3');
3925            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3926          }
3927                
3928        ## Step 9        ## Step 9
3929        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969      } # FET      } # FET
3970    }; # $formatting_end_tag    }; # $formatting_end_tag
3971    
3972    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3973      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3974    }; # $insert_to_current    }; # $insert_to_current
3975    
3976    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3977                         my $child = shift;      my $child = shift;
3978                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3979                              table => 1, tbody => 1, tfoot => 1,        # MUST
3980                              thead => 1, tr => 1,        my $foster_parent_element;
3981                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3982                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3983                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3984                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3985                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3986                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3998  sub _tree_construction_main ($) {
3998                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3999                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4000                             ($child, $next_sibling);                             ($child, $next_sibling);
4001                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4002                           !!!cp ('t72');      } else {
4003                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4004                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4005        }
4006    }; # $insert_to_foster    }; # $insert_to_foster
4007    
4008    my $insert;    B: while (1) {
   
   B: {  
4009      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4010        !!!cp ('t73');        !!!cp ('t73');
4011        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4012        ## Ignore the token        ## Ignore the token
4013        ## Stay in the phase        ## Stay in the phase
4014        !!!next-token;        !!!next-token;
4015        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4016      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4017               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4018        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4019          !!!cp ('t79');          !!!cp ('t79');
4020          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4021          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4022        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4023          !!!cp ('t80');          !!!cp ('t80');
4024          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4025          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4026        } else {        } else {
4027          !!!cp ('t81');          !!!cp ('t81');
4028        }        }
4029    
4030        !!!cp ('t82');        !!!cp ('t82');
4031        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4032        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4033        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4034          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3321  sub _tree_construction_main ($) { Line 4038  sub _tree_construction_main ($) {
4038               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4039          }          }
4040        }        }
4041          !!!nack ('t84.1');
4042        !!!next-token;        !!!next-token;
4043        redo B;        next B;
4044      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4045        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4046        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3336  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4055        }        }
4056        !!!next-token;        !!!next-token;
4057        redo B;        next B;
4058      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4059          if ($token->{type} == CHARACTER_TOKEN) {
4060            !!!cp ('t87.1');
4061            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4062            !!!next-token;
4063            next B;
4064          } elsif ($token->{type} == START_TAG_TOKEN) {
4065            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4066                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4067                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4068                ($token->{tag_name} eq 'svg' and
4069                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4070              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4071              !!!cp ('t87.2');
4072              #
4073            } elsif ({
4074                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4075                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4076                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4077                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4078                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4079                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4080                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4081                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4082                     }->{$token->{tag_name}}) {
4083              !!!cp ('t87.2');
4084              !!!parse-error (type => 'not closed',
4085                              text => $self->{open_elements}->[-1]->[0]
4086                                  ->manakai_local_name,
4087                              token => $token);
4088    
4089              pop @{$self->{open_elements}}
4090                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4091    
4092              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4093              ## Reprocess.
4094              next B;
4095            } else {
4096              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4097              my $tag_name = $token->{tag_name};
4098              if ($nsuri eq $SVG_NS) {
4099                $tag_name = {
4100                   altglyph => 'altGlyph',
4101                   altglyphdef => 'altGlyphDef',
4102                   altglyphitem => 'altGlyphItem',
4103                   animatecolor => 'animateColor',
4104                   animatemotion => 'animateMotion',
4105                   animatetransform => 'animateTransform',
4106                   clippath => 'clipPath',
4107                   feblend => 'feBlend',
4108                   fecolormatrix => 'feColorMatrix',
4109                   fecomponenttransfer => 'feComponentTransfer',
4110                   fecomposite => 'feComposite',
4111                   feconvolvematrix => 'feConvolveMatrix',
4112                   fediffuselighting => 'feDiffuseLighting',
4113                   fedisplacementmap => 'feDisplacementMap',
4114                   fedistantlight => 'feDistantLight',
4115                   feflood => 'feFlood',
4116                   fefunca => 'feFuncA',
4117                   fefuncb => 'feFuncB',
4118                   fefuncg => 'feFuncG',
4119                   fefuncr => 'feFuncR',
4120                   fegaussianblur => 'feGaussianBlur',
4121                   feimage => 'feImage',
4122                   femerge => 'feMerge',
4123                   femergenode => 'feMergeNode',
4124                   femorphology => 'feMorphology',
4125                   feoffset => 'feOffset',
4126                   fepointlight => 'fePointLight',
4127                   fespecularlighting => 'feSpecularLighting',
4128                   fespotlight => 'feSpotLight',
4129                   fetile => 'feTile',
4130                   feturbulence => 'feTurbulence',
4131                   foreignobject => 'foreignObject',
4132                   glyphref => 'glyphRef',
4133                   lineargradient => 'linearGradient',
4134                   radialgradient => 'radialGradient',
4135                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4136                   textpath => 'textPath',  
4137                }->{$tag_name} || $tag_name;
4138              }
4139    
4140              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4141    
4142              ## "adjust foreign attributes" - done in insert-element-f
4143    
4144              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4145    
4146              if ($self->{self_closing}) {
4147                pop @{$self->{open_elements}};
4148                !!!ack ('t87.3');
4149              } else {
4150                !!!cp ('t87.4');
4151              }
4152    
4153              !!!next-token;
4154              next B;
4155            }
4156          } elsif ($token->{type} == END_TAG_TOKEN) {
4157            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4158            !!!cp ('t87.5');
4159            #
4160          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4161            !!!cp ('t87.6');
4162            !!!parse-error (type => 'not closed',
4163                            text => $self->{open_elements}->[-1]->[0]
4164                                ->manakai_local_name,
4165                            token => $token);
4166    
4167            pop @{$self->{open_elements}}
4168                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4169    
4170            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4171            ## Reprocess.
4172            next B;
4173          } else {
4174            die "$0: $token->{type}: Unknown token type";        
4175          }
4176        }
4177    
4178        if ($self->{insertion_mode} & HEAD_IMS) {
4179        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4180          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4181            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182                !!!cp ('t88.2');
4183                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4184              } else {
4185                !!!cp ('t88.1');
4186                ## Ignore the token.
4187                !!!next-token;
4188                next B;
4189              }
4190            unless (length $token->{data}) {            unless (length $token->{data}) {
4191              !!!cp ('t88');              !!!cp ('t88');
4192              !!!next-token;              !!!next-token;
4193              redo B;              next B;
4194            }            }
4195          }          }
4196    
4197          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4198            !!!cp ('t89');            !!!cp ('t89');
4199            ## As if <head>            ## As if <head>
4200            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4201            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4202            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4203                  [$self->{head_element}, $el_category->{head}];
4204    
4205            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4206            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3363  sub _tree_construction_main ($) { Line 4210  sub _tree_construction_main ($) {
4210            !!!cp ('t90');            !!!cp ('t90');
4211            ## As if </noscript>            ## As if </noscript>
4212            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4213            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4214                        
4215            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4216            ## As if </head>            ## As if </head>
# Line 3379  sub _tree_construction_main ($) { Line 4226  sub _tree_construction_main ($) {
4226            !!!cp ('t92');            !!!cp ('t92');
4227          }          }
4228    
4229              ## "after head" insertion mode          ## "after head" insertion mode
4230              ## As if <body>          ## As if <body>
4231              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4232              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4233              ## reprocess          ## reprocess
4234              redo B;          next B;
4235            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4236              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4237                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4238                  !!!cp ('t93');              !!!cp ('t93');
4239                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4240                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4241                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4242                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4243                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4244                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4245                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4246                  !!!cp ('t94');              !!!next-token;
4247                  #              next B;
4248                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4249                  !!!cp ('t95');              !!!cp ('t93.2');
4250                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4251                  ## Ignore the token                              token => $token);
4252                  !!!next-token;              ## Ignore the token
4253                  redo B;              !!!nack ('t93.3');
4254                }              !!!next-token;
4255              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4256                !!!cp ('t96');            } else {
4257                ## As if <head>              !!!cp ('t95');
4258                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4259                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4260                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4261                !!!nack ('t95.1');
4262                !!!next-token;
4263                next B;
4264              }
4265            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4266              !!!cp ('t96');
4267              ## As if <head>
4268              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4269              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4270              push @{$self->{open_elements}},
4271                  [$self->{head_element}, $el_category->{head}];
4272    
4273                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4274                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4275              } else {          } else {
4276                !!!cp ('t97');            !!!cp ('t97');
4277              }          }
4278    
4279              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4280                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4281                  !!!cp ('t98');                  !!!cp ('t98');
4282                  ## As if </noscript>                  ## As if </noscript>
4283                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4284                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4285                                    token => $token);
4286                                
4287                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4288                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3434  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4294                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4295                  !!!cp ('t100');                  !!!cp ('t100');
4296                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4297                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4298                    push @{$self->{open_elements}},
4299                        [$self->{head_element}, $el_category->{head}];
4300                } else {                } else {
4301                  !!!cp ('t101');                  !!!cp ('t101');
4302                }                }
4303                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4304                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4305                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4306                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4307                  !!!nack ('t101.1');
4308                !!!next-token;                !!!next-token;
4309                redo B;                next B;
4310              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4311                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4312                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4313                  !!!cp ('t102');                  !!!cp ('t102');
4314                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4315                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4316                    push @{$self->{open_elements}},
4317                        [$self->{head_element}, $el_category->{head}];
4318                } else {                } else {
4319                  !!!cp ('t103');                  !!!cp ('t103');
4320                }                }
4321                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4322                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4323                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4324                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4325                  !!!ack ('t103.1');
4326                !!!next-token;                !!!next-token;
4327                redo B;                next B;
4328              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4329                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4330                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                  !!!cp ('t104');                  !!!cp ('t104');
4332                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4333                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4334                    push @{$self->{open_elements}},
4335                        [$self->{head_element}, $el_category->{head}];
4336                } else {                } else {
4337                  !!!cp ('t105');                  !!!cp ('t105');
4338                }                }
4339                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                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.
4341    
4342                unless ($self->{confident}) {                unless ($self->{confident}) {
4343                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4344                    !!!cp ('t106');                    !!!cp ('t106');
4345                      ## NOTE: Whether the encoding is supported or not is handled
4346                      ## in the {change_encoding} callback.
4347                    $self->{change_encoding}                    $self->{change_encoding}
4348                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4349                             $token);
4350                                        
4351                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4352                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4353                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4354                                                 ->{has_reference});                                                 ->{has_reference});
4355                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4356                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4357                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4358                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4359                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4360                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4361                      !!!cp ('t107');                      !!!cp ('t107');
4362                        ## NOTE: Whether the encoding is supported or not is handled
4363                        ## in the {change_encoding} callback.
4364                      $self->{change_encoding}                      $self->{change_encoding}
4365                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4366                               $token);
4367                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4368                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4369                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3517  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
4389                  }                  }
4390                }                }
4391    
4392                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4393                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4394                  !!!ack ('t110.1');
4395                !!!next-token;                !!!next-token;
4396                redo B;                next B;
4397              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4398                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4399                  !!!cp ('t111');                  !!!cp ('t111');
4400                  ## As if </noscript>                  ## As if </noscript>
4401                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4402                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4403                                    token => $token);
4404                                
4405                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4406                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4407                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4408                  !!!cp ('t112');                  !!!cp ('t112');
4409                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4410                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4411                    push @{$self->{open_elements}},
4412                        [$self->{head_element}, $el_category->{head}];
4413                } else {                } else {
4414                  !!!cp ('t113');                  !!!cp ('t113');
4415                }                }
# Line 3541  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4418                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4419                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4420                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4421                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4422                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4423                redo B;                next B;
4424              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4425                         $token->{tag_name} eq 'noframes') {
4426                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4427                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4428                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4429                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4430                  !!!cp ('t114');                  !!!cp ('t114');
4431                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4432                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4433                    push @{$self->{open_elements}},
4434                        [$self->{head_element}, $el_category->{head}];
4435                } else {                } else {
4436                  !!!cp ('t115');                  !!!cp ('t115');
4437                }                }
4438                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4439                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4440                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4441                redo B;                next B;
4442              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4443                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4444                  !!!cp ('t116');                  !!!cp ('t116');
4445                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4446                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4447                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4448                    !!!nack ('t116.1');
4449                  !!!next-token;                  !!!next-token;
4450                  redo B;                  next B;
4451                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4452                  !!!cp ('t117');                  !!!cp ('t117');
4453                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4454                                    token => $token);
4455                  ## Ignore the token                  ## Ignore the token
4456                    !!!nack ('t117.1');
4457                  !!!next-token;                  !!!next-token;
4458                  redo B;                  next B;
4459                } else {                } else {
4460                  !!!cp ('t118');                  !!!cp ('t118');
4461                  #                  #
# Line 3584  sub _tree_construction_main ($) { Line 4465  sub _tree_construction_main ($) {
4465                  !!!cp ('t119');                  !!!cp ('t119');
4466                  ## As if </noscript>                  ## As if </noscript>
4467                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4468                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4469                                    token => $token);
4470                                
4471                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4472                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4473                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4474                  !!!cp ('t120');                  !!!cp ('t120');
4475                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4476                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4477                    push @{$self->{open_elements}},
4478                        [$self->{head_element}, $el_category->{head}];
4479                } else {                } else {
4480                  !!!cp ('t121');                  !!!cp ('t121');
4481                }                }
4482    
4483                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4484                $script_start_tag->($insert_to_current);                $script_start_tag->();
4485                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4486                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4487                redo B;                next B;
4488              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4489                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4490                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4491                  !!!cp ('t122');                  !!!cp ('t122');
4492                  ## As if </noscript>                  ## As if </noscript>
4493                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4494                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4495                                    text => $token->{tag_name}, token => $token);
4496                                    
4497                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4498                  ## As if </head>                  ## As if </head>
# Line 3624  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509                }                }
4510    
4511                ## "after head" insertion mode                ## "after head" insertion mode
4512                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4513                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4514                  !!!cp ('t126');                  !!!cp ('t126');
4515                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3634  sub _tree_construction_main ($) { Line 4519  sub _tree_construction_main ($) {
4519                } else {                } else {
4520                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4521                }                }
4522                  !!!nack ('t127.1');
4523                !!!next-token;                !!!next-token;
4524                redo B;                next B;
4525              } else {              } else {
4526                !!!cp ('t128');                !!!cp ('t128');
4527                #                #
# Line 3645  sub _tree_construction_main ($) { Line 4531  sub _tree_construction_main ($) {
4531                !!!cp ('t129');                !!!cp ('t129');
4532                ## As if </noscript>                ## As if </noscript>
4533                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4534                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4535                                  text => $token->{tag_name}, token => $token);
4536                                
4537                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4538                ## As if </head>                ## As if </head>
# Line 3664  sub _tree_construction_main ($) { Line 4551  sub _tree_construction_main ($) {
4551    
4552              ## "after head" insertion mode              ## "after head" insertion mode
4553              ## As if <body>              ## As if <body>
4554              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4555              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4556              ## reprocess              ## reprocess
4557              redo B;              !!!ack-later;
4558                next B;
4559            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4560              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4561                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4562                  !!!cp ('t132');                  !!!cp ('t132');
4563                  ## As if <head>                  ## As if <head>
4564                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4565                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4566                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4567                        [$self->{head_element}, $el_category->{head}];
4568    
4569                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4570                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4571                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4572                  !!!next-token;                  !!!next-token;
4573                  redo B;                  next B;
4574                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4575                  !!!cp ('t133');                  !!!cp ('t133');
4576                  ## As if </noscript>                  ## As if </noscript>
4577                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4578                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4579                                    text => 'head', token => $token);
4580                                    
4581                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4582                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4583                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4584                  !!!next-token;                  !!!next-token;
4585                  redo B;                  next B;
4586                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4587                  !!!cp ('t134');                  !!!cp ('t134');
4588                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4589                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4590                  !!!next-token;                  !!!next-token;
4591                  redo B;                  next B;
4592                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4593                    !!!cp ('t134.1');
4594                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4595                                    token => $token);
4596                    ## Ignore the token
4597                    !!!next-token;
4598                    next B;
4599                } else {                } else {
4600                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4601                }                }
4602              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4603                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3709  sub _tree_construction_main ($) { Line 4605  sub _tree_construction_main ($) {
4605                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4606                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4607                  !!!next-token;                  !!!next-token;
4608                  redo B;                  next B;
4609                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4610                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4611                  !!!cp ('t137');                  !!!cp ('t137');
4612                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4613                                    text => 'noscript', token => $token);
4614                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4615                  !!!next-token;                  !!!next-token;
4616                  redo B;                  next B;
4617                } else {                } else {
4618                  !!!cp ('t138');                  !!!cp ('t138');
4619                  #                  #
# Line 3723  sub _tree_construction_main ($) { Line 4621  sub _tree_construction_main ($) {
4621              } elsif ({              } elsif ({
4622                        body => 1, html => 1,                        body => 1, html => 1,
4623                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4624                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4625                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4626                  ## 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) {  
4627                  !!!cp ('t140');                  !!!cp ('t140');
4628                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4629                                    text => $token->{tag_name}, token => $token);
4630                  ## Ignore the token                  ## Ignore the token
4631                  !!!next-token;                  !!!next-token;
4632                  redo B;                  next B;
4633                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4634                    !!!cp ('t140.1');
4635                    !!!parse-error (type => 'unmatched end tag',
4636                                    text => $token->{tag_name}, token => $token);
4637                    ## Ignore the token
4638                    !!!next-token;
4639                    next B;
4640                } else {                } else {
4641                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4642                }                }
4643                              } elsif ($token->{tag_name} eq 'p') {
4644                #                !!!cp ('t142');
4645              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4646                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4647                       }->{$token->{tag_name}}) {                ## Ignore the token
4648                  !!!next-token;
4649                  next B;
4650                } elsif ($token->{tag_name} eq 'br') {
4651                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4652                  !!!cp ('t142');                  !!!cp ('t142.2');
4653                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4654                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4655                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4656                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4657      
4658                    ## Reprocess in the "after head" insertion mode...
4659                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4660                    !!!cp ('t143.2');
4661                    ## As if </head>
4662                    pop @{$self->{open_elements}};
4663                    $self->{insertion_mode} = AFTER_HEAD_IM;
4664      
4665                    ## Reprocess in the "after head" insertion mode...
4666                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4667                    !!!cp ('t143.3');
4668                    ## ISSUE: Two parse errors for <head><noscript></br>
4669                    !!!parse-error (type => 'unmatched end tag',
4670                                    text => 'br', token => $token);
4671                    ## As if </noscript>
4672                    pop @{$self->{open_elements}};
4673                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4674    
4675                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4676                } else {                  ## As if </head>
4677                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4678                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4679    
4680                #                  ## Reprocess in the "after head" insertion mode...
4681              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4682                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4683                  #                  #
4684                } else {                } else {
4685                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4686                }                }
4687    
4688                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4689                  !!!parse-error (type => 'unmatched end tag',
4690                                  text => 'br', token => $token);
4691                  ## Ignore the token
4692                  !!!next-token;
4693                  next B;
4694                } else {
4695                  !!!cp ('t145');
4696                  !!!parse-error (type => 'unmatched end tag',
4697                                  text => $token->{tag_name}, token => $token);
4698                  ## Ignore the token
4699                  !!!next-token;
4700                  next B;
4701              }              }
4702    
4703              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4704                !!!cp ('t146');                !!!cp ('t146');
4705                ## As if </noscript>                ## As if </noscript>
4706                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4707                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4708                                  text => $token->{tag_name}, token => $token);
4709                                
4710                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4711                ## As if </head>                ## As if </head>
# Line 3793  sub _tree_construction_main ($) { Line 4721  sub _tree_construction_main ($) {
4721              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4722  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4723                !!!cp ('t148');                !!!cp ('t148');
4724                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4725                                  text => $token->{tag_name}, token => $token);
4726                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4727                !!!next-token;                !!!next-token;
4728                redo B;                next B;
4729              } else {              } else {
4730                !!!cp ('t149');                !!!cp ('t149');
4731              }              }
4732    
4733              ## "after head" insertion mode              ## "after head" insertion mode
4734              ## As if <body>              ## As if <body>
4735              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4736              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4737              ## reprocess              ## reprocess
4738              redo B;              next B;
4739            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4740              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4741            }            !!!cp ('t149.1');
4742    
4743              ## NOTE: As if <head>
4744              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4745              $self->{open_elements}->[-1]->[0]->append_child
4746                  ($self->{head_element});
4747              #push @{$self->{open_elements}},
4748              #    [$self->{head_element}, $el_category->{head}];
4749              #$self->{insertion_mode} = IN_HEAD_IM;
4750              ## NOTE: Reprocess.
4751    
4752              ## NOTE: As if </head>
4753              #pop @{$self->{open_elements}};
4754              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4755              ## NOTE: Reprocess.
4756              
4757              #
4758            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4759              !!!cp ('t149.2');
4760    
4761              ## NOTE: As if </head>
4762              pop @{$self->{open_elements}};
4763              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4764              ## NOTE: Reprocess.
4765    
4766              #
4767            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4768              !!!cp ('t149.3');
4769    
4770              !!!parse-error (type => 'in noscript:#eof', token => $token);
4771    
4772              ## As if </noscript>
4773              pop @{$self->{open_elements}};
4774              #$self->{insertion_mode} = IN_HEAD_IM;
4775              ## NOTE: Reprocess.
4776    
4777              ## NOTE: As if </head>
4778              pop @{$self->{open_elements}};
4779              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4780              ## NOTE: Reprocess.
4781    
4782              #
4783            } else {
4784              !!!cp ('t149.4');
4785              #
4786            }
4787    
4788            ## NOTE: As if <body>
4789            !!!insert-element ('body',, $token);
4790            $self->{insertion_mode} = IN_BODY_IM;
4791            ## NOTE: Reprocess.
4792            next B;
4793          } else {
4794            die "$0: $token->{type}: Unknown token type";
4795          }
4796    
4797            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4798      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3821  sub _tree_construction_main ($) { Line 4804  sub _tree_construction_main ($) {
4804              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4805    
4806              !!!next-token;              !!!next-token;
4807              redo B;              next B;
4808            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4809              if ({              if ({
4810                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3829  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4813                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4814                  ## have an element in table scope                  ## have an element in table scope
4815                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4816                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4817                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4818                      !!!cp ('t151');                      !!!cp ('t151');
4819                      $tn = $node->[1];  
4820                      last INSCOPE;                      ## Close the cell
4821                    } elsif ({                      !!!back-token; # <x>
4822                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4823                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4824                                  line => $token->{line},
4825                                  column => $token->{column}};
4826                        next B;
4827                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4828                      !!!cp ('t152');                      !!!cp ('t152');
4829                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4830                        last;
4831                    }                    }
4832                  } # INSCOPE                  }
4833                    unless (defined $tn) {  
4834                      !!!cp ('t153');                  !!!cp ('t153');
4835  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4836                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      text => $token->{tag_name}, token => $token);
4837                      ## Ignore the token                  ## Ignore the token
4838                      !!!next-token;                  !!!nack ('t153.1');
4839                      redo B;                  !!!next-token;
4840                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4841                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4842                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4843                                    token => $token);
4844                                    
4845                  ## As if </caption>                  ## NOTE: As if </caption>.
4846                  ## have a table element in table scope                  ## have a table element in table scope
4847                  my $i;                  my $i;
4848                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4849                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4850                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4851                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4852                      $i = $_;                        !!!cp ('t155');
4853                      last INSCOPE;                        $i = $_;
4854                    } elsif ({                        last INSCOPE;
4855                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4856                             }->{$node->[1]}) {                        !!!cp ('t156');
4857                      !!!cp ('t156');                        last;
4858                      last INSCOPE;                      }
4859                    }                    }
4860    
4861                      !!!cp ('t157');
4862                      !!!parse-error (type => 'start tag not allowed',
4863                                      text => $token->{tag_name}, token => $token);
4864                      ## Ignore the token
4865                      !!!nack ('t157.1');
4866                      !!!next-token;
4867                      next B;
4868                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4869                                    
4870                  ## generate implied end tags                  ## generate implied end tags
4871                  if ({                  while ($self->{open_elements}->[-1]->[1]
4872                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: Maybe the following elements never appear here.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4873                    !!!cp ('t158');                    !!!cp ('t158');
4874                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4875                  }                  }
4876    
4877                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4878                    !!!cp ('t159');                    !!!cp ('t159');
4879                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4880                                      text => $self->{open_elements}->[-1]->[0]
4881                                          ->manakai_local_name,
4882                                      token => $token);
4883                  } else {                  } else {
4884                    !!!cp ('t160');                    !!!cp ('t160');
4885                  }                  }
# Line 3916  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4892                                    
4893                  ## reprocess                  ## reprocess
4894                  redo B;                  !!!ack-later;
4895                    next B;
4896                } else {                } else {
4897                  !!!cp ('t161');                  !!!cp ('t161');
4898                  #                  #
# Line 3932  sub _tree_construction_main ($) { Line 4908  sub _tree_construction_main ($) {
4908                  my $i;                  my $i;
4909                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4910                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4911                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4912                      !!!cp ('t163');                      !!!cp ('t163');
4913                      $i = $_;                      $i = $_;
4914                      last INSCOPE;                      last INSCOPE;
4915                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4916                      !!!cp ('t164');                      !!!cp ('t164');
4917                      last INSCOPE;                      last INSCOPE;
4918                    }                    }
4919                  } # INSCOPE                  } # INSCOPE
4920                    unless (defined $i) {                    unless (defined $i) {
4921                      !!!cp ('t165');                      !!!cp ('t165');
4922                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
4923                                        text => $token->{tag_name},
4924                                        token => $token);
4925                      ## Ignore the token                      ## Ignore the token
4926                      !!!next-token;                      !!!next-token;
4927                      redo B;                      next B;
4928                    }                    }
4929                                    
4930                  ## generate implied end tags                  ## generate implied end tags
4931                  if ({                  while ($self->{open_elements}->[-1]->[1]
4932                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
   
                      ## NOTE: Maybe the following elements never appear here.  
                      tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4933                    !!!cp ('t166');                    !!!cp ('t166');
4934                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4935                  }                  }
4936                    
4937                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4938                            ne $token->{tag_name}) {
4939                    !!!cp ('t167');                    !!!cp ('t167');
4940                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4941                                      text => $self->{open_elements}->[-1]->[0]
4942                                          ->manakai_local_name,
4943                                      token => $token);
4944                  } else {                  } else {
4945                    !!!cp ('t168');                    !!!cp ('t168');
4946                  }                  }
# Line 3982  sub _tree_construction_main ($) { Line 4952  sub _tree_construction_main ($) {
4952                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4953                                    
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4957                  !!!cp ('t169');                  !!!cp ('t169');
4958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4959                                    text => $token->{tag_name}, token => $token);
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  redo B;                  next B;
4963                } else {                } else {
4964                  !!!cp ('t170');                  !!!cp ('t170');
4965                  #                  #
# Line 3997  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4969                  ## have a table element in table scope                  ## have a table element in table scope
4970                  my $i;                  my $i;
4971                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4972                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4973                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4974                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4975                      $i = $_;                        !!!cp ('t171');
4976                      last INSCOPE;                        $i = $_;
4977                    } elsif ({                        last INSCOPE;
4978                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4979                             }->{$node->[1]}) {                        !!!cp ('t172');
4980                      !!!cp ('t172');                        last;
4981                      last INSCOPE;                      }
4982                    }                    }
4983    
4984                      !!!cp ('t173');
4985                      !!!parse-error (type => 'unmatched end tag',
4986                                      text => $token->{tag_name}, token => $token);
4987                      ## Ignore the token
4988                      !!!next-token;
4989                      next B;
4990                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4991                                    
4992                  ## generate implied end tags                  ## generate implied end tags
4993                  if ({                  while ($self->{open_elements}->[-1]->[1]
4994                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: The following elements never appear here, maybe.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4995                    !!!cp ('t174');                    !!!cp ('t174');
4996                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4997                  }                  }
4998                                    
4999                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5000                    !!!cp ('t175');                    !!!cp ('t175');
5001                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5002                                      text => $self->{open_elements}->[-1]->[0]
5003                                          ->manakai_local_name,
5004                                      token => $token);
5005                  } else {                  } else {
5006                    !!!cp ('t176');                    !!!cp ('t176');
5007                  }                  }
# Line 4047  sub _tree_construction_main ($) { Line 5013  sub _tree_construction_main ($) {
5013                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5014                                    
5015                  !!!next-token;                  !!!next-token;
5016                  redo B;                  next B;
5017                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5018                  !!!cp ('t177');                  !!!cp ('t177');
5019                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5020                                    text => $token->{tag_name}, token => $token);
5021                  ## Ignore the token                  ## Ignore the token
5022                  !!!next-token;                  !!!next-token;
5023                  redo B;                  next B;
5024                } else {                } else {
5025                  !!!cp ('t178');                  !!!cp ('t178');
5026                  #                  #
# Line 4066  sub _tree_construction_main ($) { Line 5033  sub _tree_construction_main ($) {
5033                ## have an element in table scope                ## have an element in table scope
5034                my $i;                my $i;
5035                my $tn;                my $tn;
5036                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5037                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5038                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5039                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5040                    $i = $_;                      !!!cp ('t179');
5041                    last INSCOPE;                      $i = $_;
5042                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5043                    !!!cp ('t180');                      ## Close the cell
5044                    $tn = $node->[1];                      !!!back-token; # </x>
5045                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5046                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5047                  } elsif ({                                column => $token->{column}};
5048                            table => 1, html => 1,                      next B;
5049                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5050                    !!!cp ('t181');                      !!!cp ('t180');
5051                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5052                        ## NOTE: There is exactly one |td| or |th| element
5053                        ## in scope in the stack of open elements by definition.
5054                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5055                        ## ISSUE: Can this be reached?
5056                        !!!cp ('t181');
5057                        last;
5058                      }
5059                  }                  }
5060                } # INSCOPE  
               unless (defined $i) {  
5061                  !!!cp ('t182');                  !!!cp ('t182');
5062                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5063                        text => $token->{tag_name}, token => $token);
5064                  ## Ignore the token                  ## Ignore the token
5065                  !!!next-token;                  !!!next-token;
5066                  redo B;                  next B;
5067                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5068              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5069                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5070                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5071                                  token => $token);
5072    
5073                ## As if </caption>                ## As if </caption>
5074                ## have a table element in table scope                ## have a table element in table scope
5075                my $i;                my $i;
5076                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5077                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5078                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5079                    !!!cp ('t184');                    !!!cp ('t184');
5080                    $i = $_;                    $i = $_;
5081                    last INSCOPE;                    last INSCOPE;
5082                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5083                    !!!cp ('t185');                    !!!cp ('t185');
5084                    last INSCOPE;                    last INSCOPE;
5085                  }                  }
5086                } # INSCOPE                } # INSCOPE
5087                unless (defined $i) {                unless (defined $i) {
5088                  !!!cp ('t186');                  !!!cp ('t186');
5089                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5090                                    text => 'caption', token => $token);
5091                  ## Ignore the token                  ## Ignore the token
5092                  !!!next-token;                  !!!next-token;
5093                  redo B;                  next B;
5094                }                }
5095                                
5096                ## generate implied end tags                ## generate implied end tags
5097                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
   
                    ## NOTE: The following elements never appear, maybe.  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5098                  !!!cp ('t187');                  !!!cp ('t187');
5099                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5100                }                }
5101    
5102                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5103                  !!!cp ('t188');                  !!!cp ('t188');
5104                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5105                                    text => $self->{open_elements}->[-1]->[0]
5106                                        ->manakai_local_name,
5107                                    token => $token);
5108                } else {                } else {
5109                  !!!cp ('t189');                  !!!cp ('t189');
5110                }                }
# Line 4157  sub _tree_construction_main ($) { Line 5116  sub _tree_construction_main ($) {
5116                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5117    
5118                ## reprocess                ## reprocess
5119                redo B;                next B;
5120              } elsif ({              } elsif ({
5121                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5122                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5123                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5124                  !!!cp ('t190');                  !!!cp ('t190');
5125                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5126                                    text => $token->{tag_name}, token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                } else {                } else {
5131                  !!!cp ('t191');                  !!!cp ('t191');
5132                  #                  #
# Line 4177  sub _tree_construction_main ($) { Line 5137  sub _tree_construction_main ($) {
5137                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5138                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5139                !!!cp ('t192');                !!!cp ('t192');
5140                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5141                                  text => $token->{tag_name}, token => $token);
5142                ## Ignore the token                ## Ignore the token
5143                !!!next-token;                !!!next-token;
5144                redo B;                next B;
5145              } else {              } else {
5146                !!!cp ('t193');                !!!cp ('t193');
5147                #                #
5148              }              }
5149          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5150            for my $entry (@{$self->{open_elements}}) {
5151              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5152                !!!cp ('t75');
5153                !!!parse-error (type => 'in body:#eof', token => $token);
5154                last;
5155              }
5156            }
5157    
5158            ## Stop parsing.
5159            last B;
5160        } else {        } else {
5161          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5162        }        }
# Line 4193  sub _tree_construction_main ($) { Line 5165  sub _tree_construction_main ($) {
5165        #        #
5166      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5167        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5168              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5169                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5170              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5171                                
5172                unless (length $token->{data}) {            unless (length $token->{data}) {
5173                  !!!cp ('t194');              !!!cp ('t194');
5174                  !!!next-token;              !!!next-token;
5175                  redo B;              next B;
5176                } else {            } else {
5177                  !!!cp ('t195');              !!!cp ('t195');
5178                }            }
5179              }          }
5180    
5181              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5182    
5183              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5184              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186              ## result in a new Text node.              ## result in a new Text node.
5187              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5188                            
5189              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]}) {  
5190                # MUST                # MUST
5191                my $foster_parent_element;                my $foster_parent_element;
5192                my $next_sibling;                my $next_sibling;
5193                my $prev_sibling;                my $prev_sibling;
5194                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5195                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5196                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5197                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5198                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4250  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5221                     $next_sibling);                     $next_sibling);
5222                }                }
5223              } else {            $open_tables->[-1]->[1] = 1; # tainted
5224                !!!cp ('t200');          } else {
5225                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5226              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5227            }
5228                            
5229              !!!next-token;          !!!next-token;
5230              redo B;          next B;
5231        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5232              if ({          if ({
5233                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5234                   th => 1, td => 1,               th => 1, td => 1,
5235                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5236                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5237                  ## Clear back to table context              ## Clear back to table context
5238                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5239                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5240                    !!!cp ('t201');                !!!cp ('t201');
5241                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5242                    pop @{$self->{open_elements}};              }
5243                  }              
5244                                !!!insert-element ('tbody',, $token);
5245                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5246                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5247                  ## reprocess in the "in table body" insertion mode...            }
5248                }            
5249              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5250                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5251                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5252                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5253                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5254                                    
5255                  ## Clear back to table body context              ## Clear back to table body context
5256                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5257                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5258                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5259                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5260                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5261                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
5262                                    
5263                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5264                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5265                    !!!cp ('t204');                    !!!cp ('t204');
5266                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5267                      !!!nack ('t204');
5268                    !!!next-token;                    !!!next-token;
5269                    redo B;                    next B;
5270                  } else {                  } else {
5271                    !!!cp ('t205');                    !!!cp ('t205');
5272                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5273                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5274                  }                  }
5275                } else {                } else {
# Line 4308  sub _tree_construction_main ($) { Line 5277  sub _tree_construction_main ($) {
5277                }                }
5278    
5279                ## Clear back to table row context                ## Clear back to table row context
5280                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5281                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5282                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5283                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5284                }                }
5285                                
5286                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5287                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5288    
5289                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5290                                
5291                  !!!nack ('t207.1');
5292                !!!next-token;                !!!next-token;
5293                redo B;                next B;
5294              } elsif ({              } elsif ({
5295                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5296                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4334  sub _tree_construction_main ($) { Line 5302  sub _tree_construction_main ($) {
5302                  my $i;                  my $i;
5303                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5304                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5305                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5306                      !!!cp ('t208');                      !!!cp ('t208');
5307                      $i = $_;                      $i = $_;
5308                      last INSCOPE;                      last INSCOPE;
5309                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5310                      !!!cp ('t209');                      !!!cp ('t209');
5311                      last INSCOPE;                      last INSCOPE;
5312                    }                    }
5313                  } # INSCOPE                  } # INSCOPE
5314                  unless (defined $i) {                  unless (defined $i) {
5315                   !!!cp ('t210');                    !!!cp ('t210');
5316  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5317                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5318                                      text => $token->{tag_name}, token => $token);
5319                    ## Ignore the token                    ## Ignore the token
5320                      !!!nack ('t210.1');
5321                    !!!next-token;                    !!!next-token;
5322                    redo B;                    next B;
5323                  }                  }
5324                                    
5325                  ## Clear back to table row context                  ## Clear back to table row context
5326                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5327                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5328                    !!!cp ('t211');                    !!!cp ('t211');
5329                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5330                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5331                  }                  }
5332                                    
# Line 4372  sub _tree_construction_main ($) { Line 5335  sub _tree_construction_main ($) {
5335                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5336                    !!!cp ('t212');                    !!!cp ('t212');
5337                    ## reprocess                    ## reprocess
5338                    redo B;                    !!!ack-later;
5339                      next B;
5340                  } else {                  } else {
5341                    !!!cp ('t213');                    !!!cp ('t213');
5342                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5348  sub _tree_construction_main ($) {
5348                  my $i;                  my $i;
5349                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5350                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5351                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5352                      !!!cp ('t214');                      !!!cp ('t214');
5353                      $i = $_;                      $i = $_;
5354                      last INSCOPE;                      last INSCOPE;
5355                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5356                      !!!cp ('t215');                      !!!cp ('t215');
5357                      last INSCOPE;                      last INSCOPE;
5358                    }                    }
5359                  } # INSCOPE                  } # INSCOPE
5360                  unless (defined $i) {                  unless (defined $i) {
5361                    !!!cp ('t216');                    !!!cp ('t216');
5362  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5363                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5364                                      text => $token->{tag_name}, token => $token);
5365                    ## Ignore the token                    ## Ignore the token
5366                      !!!nack ('t216.1');
5367                    !!!next-token;                    !!!next-token;
5368                    redo B;                    next B;
5369                  }                  }
5370    
5371                  ## Clear back to table body context                  ## Clear back to table body context
5372                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5373                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5374                    !!!cp ('t217');                    !!!cp ('t217');
5375                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5376                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5377                  }                  }
5378                                    
# Line 4432  sub _tree_construction_main ($) { Line 5392  sub _tree_construction_main ($) {
5392    
5393                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5394                  ## Clear back to table context                  ## Clear back to table context
5395                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5396                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5397                    !!!cp ('t219');                    !!!cp ('t219');
5398                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5399                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5400                  }                  }
5401                                    
5402                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5403                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5404                  ## reprocess                  ## reprocess
5405                  redo B;                  !!!ack-later;
5406                    next B;
5407                } elsif ({                } elsif ({
5408                          caption => 1,                          caption => 1,
5409                          colgroup => 1,                          colgroup => 1,
5410                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5411                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5412                  ## Clear back to table context                  ## Clear back to table context
5413                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5414                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5415                    !!!cp ('t220');                    !!!cp ('t220');
5416                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5417                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5418                  }                  }
5419                                    
5420                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5421                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5422                                    
5423                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5424                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5425                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5426                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4470  sub _tree_construction_main ($) { Line 5429  sub _tree_construction_main ($) {
5429                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5430                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5431                  !!!next-token;                  !!!next-token;
5432                  redo B;                  !!!nack ('t220.1');
5433                    next B;
5434                } else {                } else {
5435                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5436                }                }
5437              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5438                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5439                                  text => $self->{open_elements}->[-1]->[0]
5440                                      ->manakai_local_name,
5441                                  token => $token);
5442    
5443                ## As if </table>                ## As if </table>
5444                ## have a table element in table scope                ## have a table element in table scope
5445                my $i;                my $i;
5446                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5447                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5448                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5449                    !!!cp ('t221');                    !!!cp ('t221');
5450                    $i = $_;                    $i = $_;
5451                    last INSCOPE;                    last INSCOPE;
5452                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5453                    !!!cp ('t222');                    !!!cp ('t222');
5454                    last INSCOPE;                    last INSCOPE;
5455                  }                  }
# Line 4497  sub _tree_construction_main ($) { Line 5457  sub _tree_construction_main ($) {
5457                unless (defined $i) {                unless (defined $i) {
5458                  !!!cp ('t223');                  !!!cp ('t223');
5459  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5460                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5461                                    token => $token);
5462                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5463                    !!!nack ('t223.1');
5464                  !!!next-token;                  !!!next-token;
5465                  redo B;                  next B;
5466                }                }
5467                                
5468    ## TODO: Followings are removed from the latest spec.
5469                ## generate implied end tags                ## generate implied end tags
5470                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5471                  !!!cp ('t224');                  !!!cp ('t224');
5472                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5473                }                }
5474    
5475                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5476                  !!!cp ('t225');                  !!!cp ('t225');
5477  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5478                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5479                                    text => $self->{open_elements}->[-1]->[0]
5480                                        ->manakai_local_name,
5481                                    token => $token);
5482                } else {                } else {
5483                  !!!cp ('t226');                  !!!cp ('t226');
5484                }                }
5485    
5486                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5487                  pop @{$open_tables};
5488    
5489                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5490    
5491                ## reprocess            ## reprocess
5492                redo B;            !!!ack-later;
5493              next B;
5494            } elsif ($token->{tag_name} eq 'style') {
5495              if (not $open_tables->[-1]->[1]) { # tainted
5496                !!!cp ('t227.8');
5497                ## NOTE: This is a "as if in head" code clone.
5498                $parse_rcdata->(CDATA_CONTENT_MODEL);
5499                next B;
5500              } else {
5501                !!!cp ('t227.7');
5502                #
5503              }
5504            } elsif ($token->{tag_name} eq 'script') {
5505              if (not $open_tables->[-1]->[1]) { # tainted
5506                !!!cp ('t227.6');
5507                ## NOTE: This is a "as if in head" code clone.
5508                $script_start_tag->();
5509                next B;
5510              } else {
5511                !!!cp ('t227.5');
5512                #
5513              }
5514            } elsif ($token->{tag_name} eq 'input') {
5515              if (not $open_tables->[-1]->[1]) { # tainted
5516                if ($token->{attributes}->{type}) { ## TODO: case
5517                  my $type = lc $token->{attributes}->{type}->{value};
5518                  if ($type eq 'hidden') {
5519                    !!!cp ('t227.3');
5520                    !!!parse-error (type => 'in table',
5521                                    text => $token->{tag_name}, token => $token);
5522    
5523                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5524    
5525                    ## TODO: form element pointer
5526    
5527                    pop @{$self->{open_elements}};
5528    
5529                    !!!next-token;
5530                    !!!ack ('t227.2.1');
5531                    next B;
5532                  } else {
5533                    !!!cp ('t227.2');
5534                    #
5535                  }
5536                } else {
5537                  !!!cp ('t227.1');
5538                  #
5539                }
5540              } else {
5541                !!!cp ('t227.4');
5542                #
5543              }
5544          } else {          } else {
5545            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5546            #            #
5547          }          }
5548    
5549            !!!parse-error (type => 'in table', text => $token->{tag_name},
5550                            token => $token);
5551    
5552            $insert = $insert_to_foster;
5553            #
5554        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5555              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5556                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4546  sub _tree_construction_main ($) { Line 5558  sub _tree_construction_main ($) {
5558                my $i;                my $i;
5559                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5560                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5561                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5562                    !!!cp ('t228');                    !!!cp ('t228');
5563                    $i = $_;                    $i = $_;
5564                    last INSCOPE;                    last INSCOPE;
5565                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5566                    !!!cp ('t229');                    !!!cp ('t229');
5567                    last INSCOPE;                    last INSCOPE;
5568                  }                  }
5569                } # INSCOPE                } # INSCOPE
5570                unless (defined $i) {                unless (defined $i) {
5571                  !!!cp ('t230');                  !!!cp ('t230');
5572                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5573                                    text => $token->{tag_name}, token => $token);
5574                  ## Ignore the token                  ## Ignore the token
5575                    !!!nack ('t230.1');
5576                  !!!next-token;                  !!!next-token;
5577                  redo B;                  next B;
5578                } else {                } else {
5579                  !!!cp ('t232');                  !!!cp ('t232');
5580                }                }
5581    
5582                ## Clear back to table row context                ## Clear back to table row context
5583                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5584                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5585                  !!!cp ('t231');                  !!!cp ('t231');
5586  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5587                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5588                }                }
5589    
5590                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5591                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5592                !!!next-token;                !!!next-token;
5593                redo B;                !!!nack ('t231.1');
5594                  next B;
5595              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5596                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5597                  ## As if </tr>                  ## As if </tr>
# Line 4588  sub _tree_construction_main ($) { Line 5599  sub _tree_construction_main ($) {
5599                  my $i;                  my $i;
5600                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5601                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5602                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5603                      !!!cp ('t233');                      !!!cp ('t233');
5604                      $i = $_;                      $i = $_;
5605                      last INSCOPE;                      last INSCOPE;
5606                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5607                      !!!cp ('t234');                      !!!cp ('t234');
5608                      last INSCOPE;                      last INSCOPE;
5609                    }                    }
# Line 4602  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                  unless (defined $i) {                  unless (defined $i) {
5612                    !!!cp ('t235');                    !!!cp ('t235');
5613  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5614                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5615                                      text => $token->{type}, token => $token);
5616                    ## Ignore the token                    ## Ignore the token
5617                      !!!nack ('t236.1');
5618                    !!!next-token;                    !!!next-token;
5619                    redo B;                    next B;
5620                  }                  }
5621                                    
5622                  ## Clear back to table row context                  ## Clear back to table row context
5623                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5624                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5625                    !!!cp ('t236');                    !!!cp ('t236');
5626  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5627                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5628                  }                  }
5629                                    
# Line 4628  sub _tree_construction_main ($) { Line 5637  sub _tree_construction_main ($) {
5637                  my $i;                  my $i;
5638                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5639                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5640                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5641                      !!!cp ('t237');                      !!!cp ('t237');
5642                      $i = $_;                      $i = $_;
5643                      last INSCOPE;                      last INSCOPE;
5644                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5645                      !!!cp ('t238');                      !!!cp ('t238');
5646                      last INSCOPE;                      last INSCOPE;
5647                    }                    }
5648                  } # INSCOPE                  } # INSCOPE
5649                  unless (defined $i) {                  unless (defined $i) {
5650                    !!!cp ('t239');                    !!!cp ('t239');
5651                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5652                                      text => $token->{tag_name}, token => $token);
5653                    ## Ignore the token                    ## Ignore the token
5654                      !!!nack ('t239.1');
5655                    !!!next-token;                    !!!next-token;
5656                    redo B;                    next B;
5657                  }                  }
5658                                    
5659                  ## Clear back to table body context                  ## Clear back to table body context
5660                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5661                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5662                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5663                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5664                  }                  }
5665                                    
# Line 4670  sub _tree_construction_main ($) { Line 5675  sub _tree_construction_main ($) {
5675                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5676                }                }
5677    
5678                  ## NOTE: </table> in the "in table" insertion mode.
5679                  ## When you edit the code fragment below, please ensure that
5680                  ## the code for <table> in the "in table" insertion mode
5681                  ## is synced with it.
5682    
5683                ## have a table element in table scope                ## have a table element in table scope
5684                my $i;                my $i;
5685                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5686                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5687                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5688                    !!!cp ('t241');                    !!!cp ('t241');
5689                    $i = $_;                    $i = $_;
5690                    last INSCOPE;                    last INSCOPE;
5691                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5692                    !!!cp ('t242');                    !!!cp ('t242');
5693                    last INSCOPE;                    last INSCOPE;
5694                  }                  }
5695                } # INSCOPE                } # INSCOPE
5696                unless (defined $i) {                unless (defined $i) {
5697                  !!!cp ('t243');                  !!!cp ('t243');
5698                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5699                                    text => $token->{tag_name}, token => $token);
5700                  ## Ignore the token                  ## Ignore the token
5701                    !!!nack ('t243.1');
5702                  !!!next-token;                  !!!next-token;
5703                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5704                }                }
5705                                    
5706                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5707                  pop @{$open_tables};
5708                                
5709                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5710                                
5711                !!!next-token;                !!!next-token;
5712                redo B;                next B;
5713              } elsif ({              } elsif ({
5714                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5715                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4729  sub _tree_construction_main ($) { Line 5719  sub _tree_construction_main ($) {
5719                  my $i;                  my $i;
5720                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5721                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5722                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5723                      !!!cp ('t247');                      !!!cp ('t247');
5724                      $i = $_;                      $i = $_;
5725                      last INSCOPE;                      last INSCOPE;
5726                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5727                      !!!cp ('t248');                      !!!cp ('t248');
5728                      last INSCOPE;                      last INSCOPE;
5729                    }                    }
5730                  } # INSCOPE                  } # INSCOPE
5731                    unless (defined $i) {                    unless (defined $i) {
5732                      !!!cp ('t249');                      !!!cp ('t249');
5733                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5734                                        text => $token->{tag_name}, token => $token);
5735                      ## Ignore the token                      ## Ignore the token
5736                        !!!nack ('t249.1');
5737                      !!!next-token;                      !!!next-token;
5738                      redo B;                      next B;
5739                    }                    }
5740                                    
5741                  ## As if </tr>                  ## As if </tr>
# Line 4753  sub _tree_construction_main ($) { Line 5743  sub _tree_construction_main ($) {
5743                  my $i;                  my $i;
5744                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5745                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5746                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5747                      !!!cp ('t250');                      !!!cp ('t250');
5748                      $i = $_;                      $i = $_;
5749                      last INSCOPE;                      last INSCOPE;
5750                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5751                      !!!cp ('t251');                      !!!cp ('t251');
5752                      last INSCOPE;                      last INSCOPE;
5753                    }                    }
5754                  } # INSCOPE                  } # INSCOPE
5755                    unless (defined $i) {                    unless (defined $i) {
5756                      !!!cp ('t252');                      !!!cp ('t252');
5757                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
5758                                        text => 'tr', token => $token);
5759                      ## Ignore the token                      ## Ignore the token
5760                        !!!nack ('t252.1');
5761                      !!!next-token;                      !!!next-token;
5762                      redo B;                      next B;
5763                    }                    }
5764                                    
5765                  ## Clear back to table row context                  ## Clear back to table row context
5766                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5767                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5768                    !!!cp ('t253');                    !!!cp ('t253');
5769  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5770                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5771                  }                  }
5772                                    
# Line 4791  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779                my $i;                my $i;
5780                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5782                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5783                    !!!cp ('t254');                    !!!cp ('t254');
5784                    $i = $_;                    $i = $_;
5785                    last INSCOPE;                    last INSCOPE;
5786                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5787                    !!!cp ('t255');                    !!!cp ('t255');
5788                    last INSCOPE;                    last INSCOPE;
5789                  }                  }
5790                } # INSCOPE                } # INSCOPE
5791                unless (defined $i) {                unless (defined $i) {
5792                  !!!cp ('t256');                  !!!cp ('t256');
5793                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5794                                    text => $token->{tag_name}, token => $token);
5795                  ## Ignore the token                  ## Ignore the token
5796                    !!!nack ('t256.1');
5797                  !!!next-token;                  !!!next-token;
5798                  redo B;                  next B;
5799                }                }
5800    
5801                ## Clear back to table body context                ## Clear back to table body context
5802                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5803                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5804                  !!!cp ('t257');                  !!!cp ('t257');
5805  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5806                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5807                }                }
5808    
5809                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5810                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5811                  !!!nack ('t257.1');
5812                !!!next-token;                !!!next-token;
5813                redo B;                next B;
5814              } elsif ({              } elsif ({
5815                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5816                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5817                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5818                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5819                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5820                !!!cp ('t258');            !!!cp ('t258');
5821                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5822                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5823                !!!next-token;            ## Ignore the token
5824                redo B;            !!!nack ('t258.1');
5825               !!!next-token;
5826              next B;
5827          } else {          } else {
5828            !!!cp ('t259');            !!!cp ('t259');
5829            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
5830                              text => $token->{tag_name}, token => $token);
5831    
5832            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5833            #            #
5834          }          }
5835          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5836            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5837                    @{$self->{open_elements}} == 1) { # redundant, maybe
5838              !!!parse-error (type => 'in body:#eof', token => $token);
5839              !!!cp ('t259.1');
5840              #
5841            } else {
5842              !!!cp ('t259.2');
5843              #
5844            }
5845    
5846            ## Stop parsing
5847            last B;
5848        } else {        } else {
5849          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5850        }        }
# Line 4852  sub _tree_construction_main ($) { Line 5855  sub _tree_construction_main ($) {
5855                unless (length $token->{data}) {                unless (length $token->{data}) {
5856                  !!!cp ('t260');                  !!!cp ('t260');
5857                  !!!next-token;                  !!!next-token;
5858                  redo B;                  next B;
5859                }                }
5860              }              }
5861                            
# Line 4861  sub _tree_construction_main ($) { Line 5864  sub _tree_construction_main ($) {
5864            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5865              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5866                !!!cp ('t262');                !!!cp ('t262');
5867                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5868                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5869                  !!!ack ('t262.1');
5870                !!!next-token;                !!!next-token;
5871                redo B;                next B;
5872              } else {              } else {
5873                !!!cp ('t263');                !!!cp ('t263');
5874                #                #
5875              }              }
5876            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5877              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5878                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5879                  !!!cp ('t264');                  !!!cp ('t264');
5880                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
5881                                    text => 'colgroup', token => $token);
5882                  ## Ignore the token                  ## Ignore the token
5883                  !!!next-token;                  !!!next-token;
5884                  redo B;                  next B;
5885                } else {                } else {
5886                  !!!cp ('t265');                  !!!cp ('t265');
5887                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5888                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5889                  !!!next-token;                  !!!next-token;
5890                  redo B;                              next B;            
5891                }                }
5892              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5893                !!!cp ('t266');                !!!cp ('t266');
5894                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
5895                                  text => 'col', token => $token);
5896                ## Ignore the token                ## Ignore the token
5897                !!!next-token;                !!!next-token;
5898                redo B;                next B;
5899              } else {              } else {
5900                !!!cp ('t267');                !!!cp ('t267');
5901                #                #
5902              }              }
5903            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5904              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5905            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5906              !!!cp ('t270.2');
5907              ## Stop parsing.
5908              last B;
5909            } else {
5910              ## NOTE: As if </colgroup>.
5911              !!!cp ('t270.1');
5912              pop @{$self->{open_elements}}; # colgroup
5913              $self->{insertion_mode} = IN_TABLE_IM;
5914              ## Reprocess.
5915              next B;
5916            }
5917          } else {
5918            die "$0: $token->{type}: Unknown token type";
5919          }
5920    
5921            ## As if </colgroup>            ## As if </colgroup>
5922            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5923              !!!cp ('t269');              !!!cp ('t269');
5924              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5925                !!!parse-error (type => 'unmatched end tag',
5926                                text => 'colgroup', token => $token);
5927              ## Ignore the token              ## Ignore the token
5928                !!!nack ('t269.1');
5929              !!!next-token;              !!!next-token;
5930              redo B;              next B;
5931            } else {            } else {
5932              !!!cp ('t270');              !!!cp ('t270');
5933              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5934              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5935                !!!ack-later;
5936              ## reprocess              ## reprocess
5937              redo B;              next B;
5938            }            }
5939      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5940        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5941          !!!cp ('t271');          !!!cp ('t271');
5942          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5943          !!!next-token;          !!!next-token;
5944          redo B;          next B;
5945        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5946              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5947                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5948                  !!!cp ('t272');              !!!cp ('t272');
5949                  ## As if </option>              ## As if </option>
5950                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5951                } else {            } else {
5952                  !!!cp ('t273');              !!!cp ('t273');
5953                }            }
5954    
5955                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5956                !!!next-token;            !!!nack ('t273.1');
5957                redo B;            !!!next-token;
5958              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5959                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5960                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5961                  ## As if </option>              !!!cp ('t274');
5962                  pop @{$self->{open_elements}};              ## As if </option>
5963                } else {              pop @{$self->{open_elements}};
5964                  !!!cp ('t275');            } else {
5965                }              !!!cp ('t275');
5966              }
5967    
5968                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5969                  !!!cp ('t276');              !!!cp ('t276');
5970                  ## As if </optgroup>              ## As if </optgroup>
5971                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5972                } else {            } else {
5973                  !!!cp ('t277');              !!!cp ('t277');
5974                }            }
5975    
5976                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5977                !!!next-token;            !!!nack ('t277.1');
5978                redo B;            !!!next-token;
5979              } elsif ($token->{tag_name} eq 'select') {            next B;
5980  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
5981                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
5982                ## As if </select> instead                   }->{$token->{tag_name}} or
5983                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5984                my $i;                    {
5985                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5986                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5987                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5988                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
5989                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
5990                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
5991                  } elsif ({                            token => $token);
5992                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5993                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5994                    !!!cp ('t279');            ## have an element in table scope
5995                    last INSCOPE;            my $i;
5996                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997                } # INSCOPE              my $node = $self->{open_elements}->[$_];
5998                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
5999                  !!!cp ('t280');                !!!cp ('t278');
6000                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6001                  ## Ignore the token                last INSCOPE;
6002                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6003                  redo B;                !!!cp ('t279');
6004                }                last INSCOPE;
6005                }
6006              } # INSCOPE
6007              unless (defined $i) {
6008                !!!cp ('t280');
6009                !!!parse-error (type => 'unmatched end tag',
6010                                text => 'select', token => $token);
6011                ## Ignore the token
6012                !!!nack ('t280.1');
6013                !!!next-token;
6014                next B;
6015              }
6016                                
6017                !!!cp ('t281');            !!!cp ('t281');
6018                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6019    
6020                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6021    
6022                !!!next-token;            if ($token->{tag_name} eq 'select') {
6023                redo B;              !!!nack ('t281.2');
6024                !!!next-token;
6025                next B;
6026              } else {
6027                !!!cp ('t281.1');
6028                !!!ack-later;
6029                ## Reprocess the token.
6030                next B;
6031              }
6032          } else {          } else {
6033            !!!cp ('t282');            !!!cp ('t282');
6034            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6035                              text => $token->{tag_name}, token => $token);
6036            ## Ignore the token            ## Ignore the token
6037              !!!nack ('t282.1');
6038            !!!next-token;            !!!next-token;
6039            redo B;            next B;
6040          }          }
6041        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6042              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6043                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6044                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6045                  !!!cp ('t283');              !!!cp ('t283');
6046                  ## As if </option>              ## As if </option>
6047                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6048                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6049                  !!!cp ('t284');              !!!cp ('t284');
6050                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6051                } else {            } else {
6052                  !!!cp ('t285');              !!!cp ('t285');
6053                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6054                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6055                }              ## Ignore the token
6056                !!!next-token;            }
6057                redo B;            !!!nack ('t285.1');
6058              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6059                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6060                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6061                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6062                } else {              !!!cp ('t286');
6063                  !!!cp ('t287');              pop @{$self->{open_elements}};
6064                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6065                  ## Ignore the token              !!!cp ('t287');
6066                }              !!!parse-error (type => 'unmatched end tag',
6067                !!!next-token;                              text => $token->{tag_name}, token => $token);
6068                redo B;              ## Ignore the token
6069              } elsif ($token->{tag_name} eq 'select') {            }
6070                ## have an element in table scope            !!!nack ('t287.1');
6071                my $i;            !!!next-token;
6072                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6073                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6074                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6075                    !!!cp ('t288');            my $i;
6076                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6077                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6078                  } elsif ({              if ($node->[1] & SELECT_EL) {
6079                            table => 1, html => 1,                !!!cp ('t288');
6080                           }->{$node->[1]}) {                $i = $_;
6081                    !!!cp ('t289');                last INSCOPE;
6082                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6083                  }                !!!cp ('t289');
6084                } # INSCOPE                last INSCOPE;
6085                unless (defined $i) {              }
6086                  !!!cp ('t290');            } # INSCOPE
6087                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6088                  ## Ignore the token              !!!cp ('t290');
6089                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6090                  redo B;                              text => $token->{tag_name}, token => $token);
6091                }              ## Ignore the token
6092                !!!nack ('t290.1');
6093                !!!next-token;
6094                next B;
6095              }
6096                                
6097                !!!cp ('t291');            !!!cp ('t291');
6098                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6099    
6100                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6101    
6102                !!!next-token;            !!!nack ('t291.1');
6103                redo B;            !!!next-token;
6104              } elsif ({            next B;
6105                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6106                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6107                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6108                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6109                     }->{$token->{tag_name}}) {
6110  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6111                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6112                              text => $token->{tag_name}, token => $token);
6113                                
6114                ## have an element in table scope            ## have an element in table scope
6115                my $i;            my $i;
6116                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6117                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6118                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6119                    !!!cp ('t292');                !!!cp ('t292');
6120                    $i = $_;                $i = $_;
6121                    last INSCOPE;                last INSCOPE;
6122                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6123                            table => 1, html => 1,                !!!cp ('t293');
6124                           }->{$node->[1]}) {                last INSCOPE;
6125                    !!!cp ('t293');              }
6126                    last INSCOPE;            } # INSCOPE
6127                  }            unless (defined $i) {
6128                } # INSCOPE              !!!cp ('t294');
6129                unless (defined $i) {              ## Ignore the token
6130                  !!!cp ('t294');              !!!nack ('t294.1');
6131                  ## Ignore the token              !!!next-token;
6132                  !!!next-token;              next B;
6133                  redo B;            }
               }  
6134                                
6135                ## As if </select>            ## As if </select>
6136                ## have an element in table scope            ## have an element in table scope
6137                undef $i;            undef $i;
6138                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6139                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6140                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6141                    !!!cp ('t295');                !!!cp ('t295');
6142                    $i = $_;                $i = $_;
6143                    last INSCOPE;                last INSCOPE;
6144                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6145  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6146                    !!!cp ('t296');                !!!cp ('t296');
6147                    last INSCOPE;                last INSCOPE;
6148                  }              }
6149                } # INSCOPE            } # INSCOPE
6150                unless (defined $i) {            unless (defined $i) {
6151                  !!!cp ('t297');              !!!cp ('t297');
6152  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6153                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6154                  ## Ignore the </select> token                              text => 'select', token => $token);
6155                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6156                  redo B;              !!!nack ('t297.1');
6157                }              !!!next-token; ## TODO: ok?
6158                next B;
6159              }
6160                                
6161                !!!cp ('t298');            !!!cp ('t298');
6162                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6163    
6164                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6165    
6166                ## reprocess            !!!ack-later;
6167                redo B;            ## reprocess
6168              next B;
6169          } else {          } else {
6170            !!!cp ('t299');            !!!cp ('t299');
6171            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6172                              text => $token->{tag_name}, token => $token);
6173            ## Ignore the token            ## Ignore the token
6174              !!!nack ('t299.3');
6175            !!!next-token;            !!!next-token;
6176            redo B;            next B;
6177            }
6178          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6179            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6180                    @{$self->{open_elements}} == 1) { # redundant, maybe
6181              !!!cp ('t299.1');
6182              !!!parse-error (type => 'in body:#eof', token => $token);
6183            } else {
6184              !!!cp ('t299.2');
6185          }          }
6186    
6187            ## Stop parsing.
6188            last B;
6189        } else {        } else {
6190          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6191        }        }
# Line 5135  sub _tree_construction_main ($) { Line 6201  sub _tree_construction_main ($) {
6201            unless (length $token->{data}) {            unless (length $token->{data}) {
6202              !!!cp ('t300');              !!!cp ('t300');
6203              !!!next-token;              !!!next-token;
6204              redo B;              next B;
6205            }            }
6206          }          }
6207                    
6208          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6209            !!!cp ('t301');            !!!cp ('t301');
6210            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6211    
6212            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6213          } else {          } else {
# Line 5149  sub _tree_construction_main ($) { Line 6215  sub _tree_construction_main ($) {
6215          }          }
6216                    
6217          ## "after body" insertion mode          ## "after body" insertion mode
6218          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6219    
6220          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6221          ## reprocess          ## reprocess
6222          redo B;          next B;
6223        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6224          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6225            !!!cp ('t303');            !!!cp ('t303');
6226            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6227                              text => $token->{tag_name}, token => $token);
6228                        
6229            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6230          } else {          } else {
# Line 5165  sub _tree_construction_main ($) { Line 6232  sub _tree_construction_main ($) {
6232          }          }
6233    
6234          ## "after body" insertion mode          ## "after body" insertion mode
6235          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6236                            text => $token->{tag_name}, token => $token);
6237    
6238          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6239            !!!ack-later;
6240          ## reprocess          ## reprocess
6241          redo B;          next B;
6242        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6243          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6244            !!!cp ('t305');            !!!cp ('t305');
6245            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6246                              text => $token->{tag_name}, token => $token);
6247                        
6248            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6249            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5185  sub _tree_construction_main ($) { Line 6255  sub _tree_construction_main ($) {
6255          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6256            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6257              !!!cp ('t307');              !!!cp ('t307');
6258              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6259                                text => 'html', token => $token);
6260              ## Ignore the token              ## Ignore the token
6261              !!!next-token;              !!!next-token;
6262              redo B;              next B;
6263            } else {            } else {
6264              !!!cp ('t308');              !!!cp ('t308');
6265              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6266              !!!next-token;              !!!next-token;
6267              redo B;              next B;
6268            }            }
6269          } else {          } else {
6270            !!!cp ('t309');            !!!cp ('t309');
6271            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6272                              text => $token->{tag_name}, token => $token);
6273    
6274            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6275            ## reprocess            ## reprocess
6276            redo B;            next B;
6277          }          }
6278          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6279            !!!cp ('t309.2');
6280            ## Stop parsing
6281            last B;
6282        } else {        } else {
6283          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6284        }        }
# Line 5214  sub _tree_construction_main ($) { Line 6290  sub _tree_construction_main ($) {
6290            unless (length $token->{data}) {            unless (length $token->{data}) {
6291              !!!cp ('t310');              !!!cp ('t310');
6292              !!!next-token;              !!!next-token;
6293              redo B;              next B;
6294            }            }
6295          }          }
6296                    
6297          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6298            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6299              !!!cp ('t311');              !!!cp ('t311');
6300              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6301            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6302              !!!cp ('t312');              !!!cp ('t312');
6303              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6304            } else { # "after html frameset"            } else { # "after html frameset"
6305              !!!cp ('t313');              !!!cp ('t313');
6306              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
6307    
6308              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6309              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6310              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6311            }            }
6312                        
6313            ## Ignore the token.            ## Ignore the token.
# Line 5242  sub _tree_construction_main ($) { Line 6318  sub _tree_construction_main ($) {
6318              !!!cp ('t315');              !!!cp ('t315');
6319              !!!next-token;              !!!next-token;
6320            }            }
6321            redo B;            next B;
6322          }          }
6323                    
6324          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6325        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6326          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6327            !!!cp ('t316');            !!!cp ('t316');
6328            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6329                              text => $token->{tag_name}, token => $token);
6330    
6331            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6332            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5260  sub _tree_construction_main ($) { Line 6337  sub _tree_construction_main ($) {
6337          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6338              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6339            !!!cp ('t318');            !!!cp ('t318');
6340            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6341              !!!nack ('t318.1');
6342            !!!next-token;            !!!next-token;
6343            redo B;            next B;
6344          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6345                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6346            !!!cp ('t319');            !!!cp ('t319');
6347            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6348            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6349              !!!ack ('t319.1');
6350            !!!next-token;            !!!next-token;
6351            redo B;            next B;
6352          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6353            !!!cp ('t320');            !!!cp ('t320');
6354            ## NOTE: As if in body.            ## NOTE: As if in head.
6355            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6356            redo B;            next B;
6357          } else {          } else {
6358            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6359              !!!cp ('t321');              !!!cp ('t321');
6360              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6361                                text => $token->{tag_name}, token => $token);
6362            } else {            } else {
6363              !!!cp ('t322');              !!!cp ('t322');
6364              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6365                                text => $token->{tag_name}, token => $token);
6366            }            }
6367            ## Ignore the token            ## Ignore the token
6368              !!!nack ('t322.1');
6369            !!!next-token;            !!!next-token;
6370            redo B;            next B;
6371          }          }
6372        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6373          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6374            !!!cp ('t323');            !!!cp ('t323');
6375            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6376                              text => $token->{tag_name}, token => $token);
6377    
6378            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6379            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5300  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383    
6384          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6385              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6386            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6387                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6388              !!!cp ('t325');              !!!cp ('t325');
6389              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6390                                text => $token->{tag_name}, token => $token);
6391              ## Ignore the token              ## Ignore the token
6392              !!!next-token;              !!!next-token;
6393            } else {            } else {
# Line 5313  sub _tree_construction_main ($) { Line 6397  sub _tree_construction_main ($) {
6397            }            }
6398    
6399            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6400                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6401              !!!cp ('t327');              !!!cp ('t327');
6402              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6403            } else {            } else {
6404              !!!cp ('t328');              !!!cp ('t328');
6405            }            }
6406            redo B;            next B;
6407          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6408                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6409            !!!cp ('t329');            !!!cp ('t329');
6410            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6411            !!!next-token;            !!!next-token;
6412            redo B;            next B;
6413          } else {          } else {
6414            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6415              !!!cp ('t330');              !!!cp ('t330');
6416              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6417                                text => $token->{tag_name}, token => $token);
6418            } else {            } else {
6419              !!!cp ('t331');              !!!cp ('t331');
6420              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/',
6421                                text => $token->{tag_name}, token => $token);
6422            }            }
6423            ## Ignore the token            ## Ignore the token
6424            !!!next-token;            !!!next-token;
6425            redo B;            next B;
6426            }
6427          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6428            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6429                    @{$self->{open_elements}} == 1) { # redundant, maybe
6430              !!!cp ('t331.1');
6431              !!!parse-error (type => 'in body:#eof', token => $token);
6432            } else {
6433              !!!cp ('t331.2');
6434          }          }
6435            
6436            ## Stop parsing
6437            last B;
6438        } else {        } else {
6439          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6440        }        }
# Line 5352  sub _tree_construction_main ($) { Line 6449  sub _tree_construction_main ($) {
6449        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6450          !!!cp ('t332');          !!!cp ('t332');
6451          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6452          $script_start_tag->($insert);          $script_start_tag->();
6453          redo B;          next B;
6454        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6455          !!!cp ('t333');          !!!cp ('t333');
6456          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6457          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6458          redo B;          next B;
6459        } elsif ({        } elsif ({
6460                  base => 1, link => 1,                  base => 1, link => 1,
6461                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6462          !!!cp ('t334');          !!!cp ('t334');
6463          ## 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
6464          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6466            !!!ack ('t334.1');
6467          !!!next-token;          !!!next-token;
6468          redo B;          next B;
6469        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6470          ## 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
6471          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6472          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.
6473    
6474          unless ($self->{confident}) {          unless ($self->{confident}) {
6475            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6476              !!!cp ('t335');              !!!cp ('t335');
6477                ## NOTE: Whether the encoding is supported or not is handled
6478                ## in the {change_encoding} callback.
6479              $self->{change_encoding}              $self->{change_encoding}
6480                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6481                            
6482              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6483                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6484                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6485                                           ->{has_reference});                                           ->{has_reference});
6486            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6487              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6488                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6489                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6490                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6491                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6492                !!!cp ('t336');                !!!cp ('t336');
6493                  ## NOTE: Whether the encoding is supported or not is handled
6494                  ## in the {change_encoding} callback.
6495                $self->{change_encoding}                $self->{change_encoding}
6496                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6497                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6498                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6499                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5416  sub _tree_construction_main ($) { Line 6517  sub _tree_construction_main ($) {
6517            }            }
6518          }          }
6519    
6520            !!!ack ('t338.1');
6521          !!!next-token;          !!!next-token;
6522          redo B;          next B;
6523        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6524          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6525          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6526          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6527            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6528        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6529          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6530                                
6531          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6532              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6533            !!!cp ('t342');            !!!cp ('t342');
6534            ## Ignore the token            ## Ignore the token
6535          } else {          } else {
# Line 5450  sub _tree_construction_main ($) { Line 6543  sub _tree_construction_main ($) {
6543              }              }
6544            }            }
6545          }          }
6546            !!!nack ('t343.1');
6547          !!!next-token;          !!!next-token;
6548          redo B;          next B;
6549        } elsif ({        } elsif ({
6550                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6551                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6552                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6553                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6554                  pre => 1,                  pre => 1, listing => 1,
6555                    form => 1,
6556                    table => 1,
6557                    hr => 1,
6558                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6559            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6560              !!!cp ('t350');
6561              !!!parse-error (type => 'in form:form', token => $token);
6562              ## Ignore the token
6563              !!!nack ('t350.1');
6564              !!!next-token;
6565              next B;
6566            }
6567    
6568          ## has a p element in scope          ## has a p element in scope
6569          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6570            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6571              !!!cp ('t344');              !!!cp ('t344');
6572              !!!back-token;              !!!back-token; # <form>
6573              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6574              redo B;                        line => $token->{line}, column => $token->{column}};
6575            } elsif ({              next B;
6576                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6577              !!!cp ('t345');              !!!cp ('t345');
6578              last INSCOPE;              last INSCOPE;
6579            }            }
6580          } # INSCOPE          } # INSCOPE
6581                        
6582          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6583          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6584              !!!nack ('t346.1');
6585            !!!next-token;            !!!next-token;
6586            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6587              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5488  sub _tree_construction_main ($) { Line 6594  sub _tree_construction_main ($) {
6594            } else {            } else {
6595              !!!cp ('t348');              !!!cp ('t348');
6596            }            }
6597          } else {          } elsif ($token->{tag_name} eq 'form') {
6598            !!!cp ('t347');            !!!cp ('t347.1');
6599              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6600    
6601              !!!nack ('t347.2');
6602            !!!next-token;            !!!next-token;
6603          }          } elsif ($token->{tag_name} eq 'table') {
6604          redo B;            !!!cp ('t382');
6605        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6606          if (defined $self->{form_element}) {            
6607            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6608            !!!parse-error (type => 'in form:form');  
6609            ## Ignore the token            !!!nack ('t382.1');
6610              !!!next-token;
6611            } elsif ($token->{tag_name} eq 'hr') {
6612              !!!cp ('t386');
6613              pop @{$self->{open_elements}};
6614            
6615              !!!nack ('t386.1');
6616            !!!next-token;            !!!next-token;
           redo B;  
6617          } else {          } else {
6618            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6619            !!!next-token;            !!!next-token;
           redo B;  
6620          }          }
6621        } elsif ($token->{tag_name} eq 'li') {          next B;
6622          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6623          ## has a p element in scope          ## has a p element in scope
6624          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6625            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6626              !!!cp ('t353');              !!!cp ('t353');
6627              !!!back-token;              !!!back-token; # <x>
6628              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6629              redo B;                        line => $token->{line}, column => $token->{column}};
6630            } elsif ({              next B;
6631                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6632              !!!cp ('t354');              !!!cp ('t354');
6633              last INSCOPE;              last INSCOPE;
6634            }            }
# Line 5542  sub _tree_construction_main ($) { Line 6637  sub _tree_construction_main ($) {
6637          ## Step 1          ## Step 1
6638          my $i = -1;          my $i = -1;
6639          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6640            my $li_or_dtdd = {li => {li => 1},
6641                              dt => {dt => 1, dd => 1},
6642                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6643          LI: {          LI: {
6644            ## Step 2            ## Step 2
6645            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6646              if ($i != -1) {              if ($i != -1) {
6647                !!!cp ('t355');                !!!cp ('t355');
6648                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6649                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6650                                      ->manakai_local_name,
6651                                  token => $token);
6652              } else {              } else {
6653                !!!cp ('t356');                !!!cp ('t356');
6654              }              }
# Line 5559  sub _tree_construction_main ($) { Line 6659  sub _tree_construction_main ($) {
6659            }            }
6660                        
6661            ## Step 3            ## Step 3
6662            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6663                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6664                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6665                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6666                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6667                  not ($node->[1] & DIV_EL)) {
6668              !!!cp ('t358');              !!!cp ('t358');
6669              last LI;              last LI;
6670            }            }
# Line 5575  sub _tree_construction_main ($) { Line 6676  sub _tree_construction_main ($) {
6676            redo LI;            redo LI;
6677          } # LI          } # LI
6678                        
6679          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6680          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6681          !!!next-token;          !!!next-token;
6682          redo B;          next B;
6683        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6684          ## has a p element in scope          ## has a p element in scope
6685          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6686            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6687              !!!cp ('t367');              !!!cp ('t367');
6688              !!!back-token;              !!!back-token; # <plaintext>
6689              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6690              redo B;                        line => $token->{line}, column => $token->{column}};
6691            } elsif ({              next B;
6692                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6693              !!!cp ('t368');              !!!cp ('t368');
6694              last INSCOPE;              last INSCOPE;
6695            }            }
6696          } # INSCOPE          } # INSCOPE
6697                        
6698          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6699                        
6700          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6701                        
6702            !!!nack ('t368.1');
6703          !!!next-token;          !!!next-token;
6704          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6705        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6706          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6707            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6708            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6709              !!!cp ('t371');              !!!cp ('t371');
6710              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6711                            
6712              !!!back-token;              !!!back-token; # <a>
6713              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6714              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6715                $formatting_end_tag->($token);
6716                            
6717              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6718                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5738  sub _tree_construction_main ($) { Line 6737  sub _tree_construction_main ($) {
6737                        
6738          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6739    
6740          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6741          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6742    
6743            !!!nack ('t374.1');
6744          !!!next-token;          !!!next-token;
6745          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6746        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6747          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6748    
6749          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6750          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6751            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6752            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6753              !!!cp ('t376');              !!!cp ('t376');
6754              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6755              !!!back-token;              !!!back-token; # <nobr>
6756              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6757              redo B;                        line => $token->{line}, column => $token->{column}};
6758            } elsif ({              next B;
6759                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6760              !!!cp ('t377');              !!!cp ('t377');
6761              last INSCOPE;              last INSCOPE;
6762            }            }
6763          } # INSCOPE          } # INSCOPE
6764                    
6765          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6766          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6767                    
6768            !!!nack ('t377.1');
6769          !!!next-token;          !!!next-token;
6770          redo B;          next B;
6771        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6772          ## has a button element in scope          ## has a button element in scope
6773          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6774            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6775            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6776              !!!cp ('t378');              !!!cp ('t378');
6777              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6778              !!!back-token;              !!!back-token; # <button>
6779              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6780              redo B;                        line => $token->{line}, column => $token->{column}};
6781            } elsif ({              next B;
6782                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6783              !!!cp ('t379');              !!!cp ('t379');
6784              last INSCOPE;              last INSCOPE;
6785            }            }
# Line 5803  sub _tree_construction_main ($) { Line 6787  sub _tree_construction_main ($) {
6787                        
6788          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6789                        
6790          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6791          push @$active_formatting_elements, ['#marker', ''];  
6792            ## TODO: associate with $self->{form_element} if defined
6793    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6794          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6795            
6796          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6797          !!!next-token;          !!!next-token;
6798          redo B;          next B;
6799        } elsif ({        } elsif ({
6800                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6801                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6802                  image => 1,                  noembed => 1,
6803                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6804                    noscript => 0, ## TODO: 1 if scripting is enabled
6805                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6806          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6807            !!!cp ('t384');            !!!cp ('t381');
6808            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6809          } else {          } else {
6810            !!!cp ('t385');            !!!cp ('t399');
6811          }          }
6812            ## NOTE: There is an "as if in body" code clone.
6813          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6814          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6815        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6816          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6817                    
6818          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6819            !!!cp ('t389');            !!!cp ('t389');
6820            ## Ignore the token            ## Ignore the token
6821              !!!nack ('t389'); ## NOTE: Not acknowledged.
6822            !!!next-token;            !!!next-token;
6823            redo B;            next B;
6824          } else {          } else {
6825              !!!ack ('t391.1');
6826    
6827            my $at = $token->{attributes};            my $at = $token->{attributes};
6828            my $form_attrs;            my $form_attrs;
6829            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5917  sub _tree_construction_main ($) { Line 6833  sub _tree_construction_main ($) {
6833            delete $at->{prompt};            delete $at->{prompt};
6834            my @tokens = (            my @tokens = (
6835                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6836                           attributes => $form_attrs},                           attributes => $form_attrs,
6837                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6838                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6839                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6840                            {type => START_TAG_TOKEN, tag_name => 'p',
6841                             line => $token->{line}, column => $token->{column}},
6842                            {type => START_TAG_TOKEN, tag_name => 'label',
6843                             line => $token->{line}, column => $token->{column}},
6844                         );                         );
6845            if ($prompt_attr) {            if ($prompt_attr) {
6846              !!!cp ('t390');              !!!cp ('t390');
6847              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6848                               #line => $token->{line}, column => $token->{column},
6849                              };
6850            } else {            } else {
6851              !!!cp ('t391');              !!!cp ('t391');
6852              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6853                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6854                               #line => $token->{line}, column => $token->{column},
6855                              }; # SHOULD
6856              ## TODO: make this configurable              ## TODO: make this configurable
6857            }            }
6858            push @tokens,            push @tokens,
6859                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6860                             line => $token->{line}, column => $token->{column}},
6861                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6862                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6863                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6864                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6865                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6866            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6867                             line => $token->{line}, column => $token->{column}},
6868                            {type => END_TAG_TOKEN, tag_name => 'form',
6869                             line => $token->{line}, column => $token->{column}};
6870            !!!back-token (@tokens);            !!!back-token (@tokens);
6871            redo B;            !!!next-token;
6872              next B;
6873          }          }
6874        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6875          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6876          my $el;          my $el;
6877          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6878                    
6879          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6880          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5954  sub _tree_construction_main ($) { Line 6883  sub _tree_construction_main ($) {
6883          $insert->($el);          $insert->($el);
6884                    
6885          my $text = '';          my $text = '';
6886            !!!nack ('t392.1');
6887          !!!next-token;          !!!next-token;
6888          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6889            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5984  sub _tree_construction_main ($) { Line 6914  sub _tree_construction_main ($) {
6914            ## Ignore the token            ## Ignore the token
6915          } else {          } else {
6916            !!!cp ('t398');            !!!cp ('t398');
6917            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6918          }          }
6919          !!!next-token;          !!!next-token;
6920            next B;
6921          } elsif ($token->{tag_name} eq 'rt' or
6922                   $token->{tag_name} eq 'rp') {
6923            ## has a |ruby| element in scope
6924            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6925              my $node = $self->{open_elements}->[$_];
6926              if ($node->[1] & RUBY_EL) {
6927                !!!cp ('t398.1');
6928                ## generate implied end tags
6929                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6930                  !!!cp ('t398.2');
6931                  pop @{$self->{open_elements}};
6932                }
6933                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6934                  !!!cp ('t398.3');
6935                  !!!parse-error (type => 'not closed',
6936                                  text => $self->{open_elements}->[-1]->[0]
6937                                      ->manakai_local_name,
6938                                  token => $token);
6939                  pop @{$self->{open_elements}}
6940                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6941                }
6942                last INSCOPE;
6943              } elsif ($node->[1] & SCOPING_EL) {
6944                !!!cp ('t398.4');
6945                last INSCOPE;
6946              }
6947            } # INSCOPE
6948    
6949            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6950    
6951            !!!nack ('t398.5');
6952            !!!next-token;
6953          redo B;          redo B;
6954        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6955                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6956          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6957    
6958            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6959    
6960            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6961    
6962            ## "adjust foreign attributes" - done in insert-element-f
6963                    
6964          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6965                    
6966          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6967              pop @{$self->{open_elements}};
6968              !!!ack ('t398.1');
6969            } else {
6970              !!!cp ('t398.2');
6971              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6972              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6973              ## mode, "in body" (not "in foreign content") secondary insertion
6974              ## mode, maybe.
6975            }
6976    
6977          !!!next-token;          !!!next-token;
6978          redo B;          next B;
6979        } elsif ({        } elsif ({
6980                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6981                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6014  sub _tree_construction_main ($) { Line 6983  sub _tree_construction_main ($) {
6983                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6984                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6985          !!!cp ('t401');          !!!cp ('t401');
6986          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
6987                            text => $token->{tag_name}, token => $token);
6988          ## Ignore the token          ## Ignore the token
6989            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6990          !!!next-token;          !!!next-token;
6991          redo B;          next B;
6992                    
6993          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6994        } else {        } else {
6995          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6996              !!!cp ('t384');
6997              !!!parse-error (type => 'image', token => $token);
6998              $token->{tag_name} = 'img';
6999            } else {
7000              !!!cp ('t385');
7001            }
7002    
7003            ## NOTE: There is an "as if <br>" code clone.
7004          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7005                    
7006          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7007    
7008            if ({
7009                 applet => 1, marquee => 1, object => 1,
7010                }->{$token->{tag_name}}) {
7011              !!!cp ('t380');
7012              push @$active_formatting_elements, ['#marker', ''];
7013              !!!nack ('t380.1');
7014            } elsif ({
7015                      b => 1, big => 1, em => 1, font => 1, i => 1,
7016                      s => 1, small => 1, strile => 1,
7017                      strong => 1, tt => 1, u => 1,
7018                     }->{$token->{tag_name}}) {
7019              !!!cp ('t375');
7020              push @$active_formatting_elements, $self->{open_elements}->[-1];
7021              !!!nack ('t375.1');
7022            } elsif ($token->{tag_name} eq 'input') {
7023              !!!cp ('t388');
7024              ## TODO: associate with $self->{form_element} if defined
7025              pop @{$self->{open_elements}};
7026              !!!ack ('t388.2');
7027            } elsif ({
7028                      area => 1, basefont => 1, bgsound => 1, br => 1,
7029                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7030                      #image => 1,
7031                     }->{$token->{tag_name}}) {
7032              !!!cp ('t388.1');
7033              pop @{$self->{open_elements}};
7034              !!!ack ('t388.3');
7035            } elsif ($token->{tag_name} eq 'select') {
7036              ## TODO: associate with $self->{form_element} if defined
7037            
7038              if ($self->{insertion_mode} & TABLE_IMS or
7039                  $self->{insertion_mode} & BODY_TABLE_IMS or
7040                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7041                !!!cp ('t400.1');
7042                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7043              } else {
7044                !!!cp ('t400.2');
7045                $self->{insertion_mode} = IN_SELECT_IM;
7046              }
7047              !!!nack ('t400.3');
7048            } else {
7049              !!!nack ('t402');
7050            }
7051                    
7052          !!!next-token;          !!!next-token;
7053          redo B;          next B;
7054        }        }
7055      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7056        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7057          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7058              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7059            for (@{$self->{open_elements}}) {          INSCOPE: {
7060              unless ({            for (reverse @{$self->{open_elements}}) {
7061                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7062                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7063                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7064                      }->{$_->[1]}) {                last INSCOPE;
7065                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7066                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7067              } else {                last;
               !!!cp ('t404');  
7068              }              }
7069            }            }
7070    
7071            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7072            !!!next-token;                            text => $token->{tag_name}, token => $token);
7073            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7074            !!!next-token;            !!!next-token;
7075            redo B;            next B;
7076            } # INSCOPE
7077    
7078            for (@{$self->{open_elements}}) {
7079              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7080                !!!cp ('t403');
7081                !!!parse-error (type => 'not closed',
7082                                text => $_->[0]->manakai_local_name,
7083                                token => $token);
7084                last;
7085              } else {
7086                !!!cp ('t404');
7087              }
7088          }          }
7089    
7090            $self->{insertion_mode} = AFTER_BODY_IM;
7091            !!!next-token;
7092            next B;
7093        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7094          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
7095            ## up-to-date, though it has same effect as speced.
7096            if (@{$self->{open_elements}} > 1 and
7097                $self->{open_elements}->[1]->[1] & BODY_EL) {
7098            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7099            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7100              !!!cp ('t406');              !!!cp ('t406');
7101              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7102                                text => $self->{open_elements}->[1]->[0]
7103                                    ->manakai_local_name,
7104                                token => $token);
7105            } else {            } else {
7106              !!!cp ('t407');              !!!cp ('t407');
7107            }            }
7108            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7109            ## reprocess            ## reprocess
7110            redo B;            next B;
7111          } else {          } else {
7112            !!!cp ('t408');            !!!cp ('t408');
7113            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7114                              text => $token->{tag_name}, token => $token);
7115            ## Ignore the token            ## Ignore the token
7116            !!!next-token;            !!!next-token;
7117            redo B;            next B;
7118          }          }
7119        } elsif ({        } elsif ({
7120                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7121                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7122                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7123                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7124                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7125                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7126          ## has an element in scope          ## has an element in scope
7127          my $i;          my $i;
7128          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7129            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7130            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7131              !!!cp ('t410');              !!!cp ('t410');
7132              $i = $_;              $i = $_;
7133              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7134            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7135              !!!cp ('t411');              !!!cp ('t411');
7136              last INSCOPE;              last INSCOPE;
7137            }            }
7138          } # INSCOPE          } # INSCOPE
7139            
7140          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7141            if (defined $i) {            !!!cp ('t413');
7142              !!!parse-error (type => 'unmatched end tag',
7143                              text => $token->{tag_name}, token => $token);
7144              ## NOTE: Ignore the token.
7145            } else {
7146              ## Step 1. generate implied end tags
7147              while ({
7148                      ## END_TAG_OPTIONAL_EL
7149                      dd => ($token->{tag_name} ne 'dd'),
7150                      dt => ($token->{tag_name} ne 'dt'),
7151                      li => ($token->{tag_name} ne 'li'),
7152                      p => 1,
7153                      rt => 1,
7154                      rp => 1,
7155                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7156                !!!cp ('t409');
7157                pop @{$self->{open_elements}};
7158              }
7159    
7160              ## Step 2.
7161              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7162                      ne $token->{tag_name}) {
7163              !!!cp ('t412');              !!!cp ('t412');
7164              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7165                                text => $self->{open_elements}->[-1]->[0]
7166                                    ->manakai_local_name,
7167                                token => $token);
7168            } else {            } else {
7169              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7170            }            }
7171          }  
7172                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7173            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7174          } elsif ($token->{tag_name} eq 'p') {  
7175            !!!cp ('t415');            ## Step 4.
7176            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7177            my $el;                if {
7178            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7179            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7180          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7181          !!!next-token;          !!!next-token;
7182          redo B;          next B;
7183        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7184            undef $self->{form_element};
7185    
7186          ## has an element in scope          ## has an element in scope
7187            my $i;
7188          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7189            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7190            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
   
                  ## NOTE: The following elements never appear here, maybe.  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7191              !!!cp ('t418');              !!!cp ('t418');
7192                $i = $_;
7193              last INSCOPE;              last INSCOPE;
7194            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7195              !!!cp ('t419');              !!!cp ('t419');
7196              last INSCOPE;              last INSCOPE;
7197            }            }
7198          } # INSCOPE          } # INSCOPE
7199            
7200          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
7201            !!!cp ('t421');            !!!cp ('t421');
7202            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7203                              text => $token->{tag_name}, token => $token);
7204              ## NOTE: Ignore the token.
7205            } else {
7206              ## Step 1. generate implied end tags
7207              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7208                !!!cp ('t417');
7209                pop @{$self->{open_elements}};
7210              }
7211              
7212              ## Step 2.
7213              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7214                      ne $token->{tag_name}) {
7215                !!!cp ('t417.1');
7216                !!!parse-error (type => 'not closed',
7217                                text => $self->{open_elements}->[-1]->[0]
7218                                    ->manakai_local_name,
7219                                token => $token);
7220              } else {
7221                !!!cp ('t420');
7222              }  
7223              
7224              ## Step 3.
7225              splice @{$self->{open_elements}}, $i;
7226          }          }
7227    
         undef $self->{form_element};  
7228          !!!next-token;          !!!next-token;
7229          redo B;          next B;
7230        } elsif ({        } elsif ({
7231                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7232                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6193  sub _tree_construction_main ($) { Line 7234  sub _tree_construction_main ($) {
7234          my $i;          my $i;
7235          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7236            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7237            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
7238              !!!cp ('t423');              !!!cp ('t423');
7239              $i = $_;              $i = $_;
7240              last INSCOPE;              last INSCOPE;
7241            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7242              !!!cp ('t424');              !!!cp ('t424');
7243              last INSCOPE;              last INSCOPE;
7244            }            }
7245          } # INSCOPE          } # INSCOPE
7246    
7247            unless (defined $i) { # has an element in scope
7248              !!!cp ('t425.1');
7249              !!!parse-error (type => 'unmatched end tag',
7250                              text => $token->{tag_name}, token => $token);
7251              ## NOTE: Ignore the token.
7252            } else {
7253              ## Step 1. generate implied end tags
7254              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7255                !!!cp ('t422');
7256                pop @{$self->{open_elements}};
7257              }
7258              
7259              ## Step 2.
7260              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7261                      ne $token->{tag_name}) {
7262                !!!cp ('t425');
7263                !!!parse-error (type => 'unmatched end tag',
7264                                text => $token->{tag_name}, token => $token);
7265              } else {
7266                !!!cp ('t426');
7267              }
7268    
7269              ## Step 3.
7270              splice @{$self->{open_elements}}, $i;
7271            }
7272                    
7273          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7274            !!!cp ('t425');          next B;
7275            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7276            ## has an element in scope
7277            my $i;
7278            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7279              my $node = $self->{open_elements}->[$_];
7280              if ($node->[1] & P_EL) {
7281                !!!cp ('t410.1');
7282                $i = $_;
7283                last INSCOPE;
7284              } elsif ($node->[1] & SCOPING_EL) {
7285                !!!cp ('t411.1');
7286                last INSCOPE;
7287              }
7288            } # INSCOPE
7289    
7290            if (defined $i) {
7291              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7292                      ne $token->{tag_name}) {
7293                !!!cp ('t412.1');
7294                !!!parse-error (type => 'not closed',
7295                                text => $self->{open_elements}->[-1]->[0]
7296                                    ->manakai_local_name,
7297                                token => $token);
7298              } else {
7299                !!!cp ('t414.1');
7300              }
7301    
7302              splice @{$self->{open_elements}}, $i;
7303          } else {          } else {
7304            !!!cp ('t426');            !!!cp ('t413.1');
7305              !!!parse-error (type => 'unmatched end tag',
7306                              text => $token->{tag_name}, token => $token);
7307    
7308              !!!cp ('t415.1');
7309              ## As if <p>, then reprocess the current token
7310              my $el;
7311              !!!create-element ($el, $HTML_NS, 'p',, $token);
7312              $insert->($el);
7313              ## NOTE: Not inserted into |$self->{open_elements}|.
7314          }          }
7315            
         splice @{$self->{open_elements}}, $i if defined $i;  
7316          !!!next-token;          !!!next-token;
7317          redo B;          next B;
7318        } elsif ({        } elsif ({
7319                  a => 1,                  a => 1,
7320                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6238  sub _tree_construction_main ($) { Line 7322  sub _tree_construction_main ($) {
7322                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7323                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7324          !!!cp ('t427');          !!!cp ('t427');
7325          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7326          redo B;          next B;
7327        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7328          !!!cp ('t428');          !!!cp ('t428');
7329          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7330                            text => 'br', token => $token);
7331    
7332          ## As if <br>          ## As if <br>
7333          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7334                    
7335          my $el;          my $el;
7336          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7337          $insert->($el);          $insert->($el);
7338                    
7339          ## Ignore the token.          ## Ignore the token.
7340          !!!next-token;          !!!next-token;
7341          redo B;          next B;
7342        } elsif ({        } elsif ({
7343                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7344                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6267  sub _tree_construction_main ($) { Line 7352  sub _tree_construction_main ($) {
7352                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7353                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7354          !!!cp ('t429');          !!!cp ('t429');
7355          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7356                            text => $token->{tag_name}, token => $token);
7357          ## Ignore the token          ## Ignore the token
7358          !!!next-token;          !!!next-token;
7359          redo B;          next B;
7360                    
7361          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7362                    
# Line 6281  sub _tree_construction_main ($) { Line 7367  sub _tree_construction_main ($) {
7367    
7368          ## Step 2          ## Step 2
7369          S2: {          S2: {
7370            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7371              ## Step 1              ## Step 1
7372              ## generate implied end tags              ## generate implied end tags
7373              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7374                !!!cp ('t430');                !!!cp ('t430');
7375                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7376                !!!back-token;                ## ISSUE: <ruby><rt></rt> will also take this code path,
7377                $token = {type => END_TAG_TOKEN,                ## which seems wrong.
7378                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                pop @{$self->{open_elements}};
7379                redo B;                $node_i++;
7380              }              }
7381                    
7382              ## Step 2              ## Step 2
7383              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7384                        ne $token->{tag_name}) {
7385                !!!cp ('t431');                !!!cp ('t431');
7386                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7387                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7388                                  text => $self->{open_elements}->[-1]->[0]
7389                                      ->manakai_local_name,
7390                                  token => $token);
7391              } else {              } else {
7392                !!!cp ('t432');                !!!cp ('t432');
7393              }              }
7394                            
7395              ## Step 3              ## Step 3
7396              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7397    
7398              !!!next-token;              !!!next-token;
7399              last S2;              last S2;
7400            } else {            } else {
7401              ## Step 3              ## Step 3
7402              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7403                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7404                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7405                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7406                !!!cp ('t433');                !!!cp ('t433');
7407                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7408                                  text => $token->{tag_name}, token => $token);
7409                ## Ignore the token                ## Ignore the token
7410                !!!next-token;                !!!next-token;
7411                last S2;                last S2;
# Line 6334  sub _tree_construction_main ($) { Line 7421  sub _tree_construction_main ($) {
7421            ## Step 5;            ## Step 5;
7422            redo S2;            redo S2;
7423          } # S2          } # S2
7424          redo B;          next B;
7425        }        }
7426      }      }
7427      redo B;      next B;
7428      } continue { # B
7429        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7430          ## NOTE: The code below is executed in cases where it does not have
7431          ## to be, but it it is harmless even in those cases.
7432          ## has an element in scope
7433          INSCOPE: {
7434            for (reverse 0..$#{$self->{open_elements}}) {
7435              my $node = $self->{open_elements}->[$_];
7436              if ($node->[1] & FOREIGN_EL) {
7437                last INSCOPE;
7438              } elsif ($node->[1] & SCOPING_EL) {
7439                last;
7440              }
7441            }
7442            
7443            ## NOTE: No foreign element in scope.
7444            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7445          } # INSCOPE
7446        }
7447    } # B    } # B
7448    
7449    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6383  sub set_inner_html ($$$) { Line 7489  sub set_inner_html ($$$) {
7489    
7490      ## Step 8 # MUST      ## Step 8 # MUST
7491      my $i = 0;      my $i = 0;
7492      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7493      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7494      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7495        my $self = shift;        my $self = shift;
7496    
# Line 6393  sub set_inner_html ($$$) { Line 7499  sub set_inner_html ($$$) {
7499    
7500        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7501        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7502        $column++;  
7503          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7504          $p->{column}++;
7505    
7506        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7507          $line++;          $p->{line}++;
7508          $column = 0;          $p->{column} = 0;
7509          !!!cp ('i1');          !!!cp ('i1');
7510        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7511          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7512          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7513          $line++;          $p->{line}++;
7514          $column = 0;          $p->{column} = 0;
7515          !!!cp ('i2');          !!!cp ('i2');
7516        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7517          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6412  sub set_inner_html ($$$) { Line 7520  sub set_inner_html ($$$) {
7520          !!!cp ('i4');          !!!cp ('i4');
7521          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7522          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7523          } elsif ($self->{next_char} <= 0x0008 or
7524                   (0x000E <= $self->{next_char} and
7525                    $self->{next_char} <= 0x001F) or
7526                   (0x007F <= $self->{next_char} and
7527                    $self->{next_char} <= 0x009F) or
7528                   (0xD800 <= $self->{next_char} and
7529                    $self->{next_char} <= 0xDFFF) or
7530                   (0xFDD0 <= $self->{next_char} and
7531                    $self->{next_char} <= 0xFDDF) or
7532                   {
7533                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7534                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7535                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7536                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7537                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7538                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7539                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7540                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7541                    0x10FFFE => 1, 0x10FFFF => 1,
7542                   }->{$self->{next_char}}) {
7543            !!!cp ('i4.1');
7544            if ($self->{next_char} < 0x10000) {
7545              !!!parse-error (type => 'control char',
7546                              text => (sprintf 'U+%04X', $self->{next_char}));
7547            } else {
7548              !!!parse-error (type => 'control char',
7549                              text => (sprintf 'U-%08X', $self->{next_char}));
7550            }
7551        }        }
7552      };      };
7553      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6419  sub set_inner_html ($$$) { Line 7555  sub set_inner_html ($$$) {
7555            
7556      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7557        my (%opt) = @_;        my (%opt) = @_;
7558        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7559          my $column = $opt{column};
7560          if (defined $opt{token} and defined $opt{token}->{line}) {
7561            $line = $opt{token}->{line};
7562            $column = $opt{token}->{column};
7563          }
7564          warn "Parse error ($opt{type}) at line $line column $column\n";
7565      };      };
7566      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7567        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7568      };      };
7569            
7570      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6446  sub set_inner_html ($$$) { Line 7588  sub set_inner_html ($$$) {
7588          unless defined $p->{content_model};          unless defined $p->{content_model};
7589          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7590    
7591      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7592          ## TODO: Foreign element OK?
7593    
7594      ## Step 3      ## Step 3
7595      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6456  sub set_inner_html ($$$) { Line 7599  sub set_inner_html ($$$) {
7599      $doc->append_child ($root);      $doc->append_child ($root);
7600    
7601      ## Step 5 # MUST      ## Step 5 # MUST
7602      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7603    
7604      undef $p->{head_element};      undef $p->{head_element};
7605    
# Line 6502  sub set_inner_html ($$$) { Line 7645  sub set_inner_html ($$$) {
7645      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7646    
7647      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7648    
7649        delete $p->{parse_error}; # delete loop
7650    } else {    } else {
7651      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";
7652    }    }

Legend:
Removed from v.1.84  
changed lines
  Added in v.1.157

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24