/[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.120 by wakaba, Thu Mar 20 03:57:00 2008 UTC revision 1.154 by wakaba, Sat Aug 16 07:35:23 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 = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
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      my $token = shift;    };
366      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
367      ## TODO: normalize charset name  
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      ## "Change the encoding" algorithm:    SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380      ## Step 1            ## ISSUE: Unsupported encoding is not ignored according to the spec.
381      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        ($char_stream, $e_status) = $charset->get_decode_handle
382        $charset = 'utf-8';            ($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', token => $token);      ## 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 163  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 186  sub parse_string ($$$;$) { Line 621  sub parse_string ($$$;$) {
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          $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})      ($self->{line_prev}, $self->{column_prev})
635          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
636      $self->{column}++;      $self->{column}++;
637            
638      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639          !!!cp ('j1');
640        $self->{line}++;        $self->{line}++;
641        $self->{column} = 0;        $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        $self->{line}++;        $self->{line}++;
650        $self->{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 229  sub parse_string ($$$;$) { Line 702  sub parse_string ($$$;$) {
702    delete $self->{parse_error}; # remove loop    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 295  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 312  sub ROW_IMS ()        { 0b10000000 } Line 792  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 }  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 348  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 368  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 394  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 574  sub _get_next_token ($) { Line 1072  sub _get_next_token ($) {
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    
# Line 765  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 829  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
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');
# Line 889  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}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!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 942  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');
# Line 1042  sub _get_next_token ($) { Line 1512  sub _get_next_token ($) {
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');
# Line 1388  sub _get_next_token ($) { Line 1847  sub _get_next_token ($) {
1847    
1848          redo A;          redo A;
1849        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1850            !!!cp (122);
1851            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1852          !!!next-input-character;          !!!next-input-character;
1853          if ($self->{next_char} == 0x003E and # >          redo A;
1854              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1855              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1856            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1857            !!!cp (122);            !!!cp (122.3);
1858            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1859            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              if ($self->{current_token}->{attributes}) {
1861                !!!cp (122.1);
1862                !!!parse-error (type => 'end tag attribute');
1863              } else {
1864                ## NOTE: This state should never be reached.
1865                !!!cp (122.2);
1866              }
1867          } else {          } else {
1868            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1869          }          }
1870          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1871          # next-input-character is already done          ## Reconsume.
1872            !!!emit ($self->{current_token}); # start tag or end tag
1873          redo A;          redo A;
1874        } else {        } else {
1875          !!!cp (124);          !!!cp ('124.1');
1876          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1877          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1878          ## reconsume          ## reconsume
1879          redo A;          redo A;
1880        }        }
1881        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1882          if ($self->{next_char} == 0x003E) { # >
1883            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1884              !!!cp ('124.2');
1885              !!!parse-error (type => 'nestc', token => $self->{current_token});
1886              ## TODO: Different type than slash in start tag
1887              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp ('124.4');
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                !!!cp ('124.5');
1893              }
1894              ## TODO: Test |<title></title/>|
1895            } else {
1896              !!!cp ('124.3');
1897              $self->{self_closing} = 1;
1898            }
1899    
1900            $self->{state} = DATA_STATE;
1901            !!!next-input-character;
1902    
1903            !!!emit ($self->{current_token}); # start tag or end tag
1904    
1905            redo A;
1906          } elsif ($self->{next_char} == -1) {
1907            !!!parse-error (type => 'unclosed tag');
1908            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1909              !!!cp (124.7);
1910              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1911            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1912              if ($self->{current_token}->{attributes}) {
1913                !!!cp (124.5);
1914                !!!parse-error (type => 'end tag attribute');
1915              } else {
1916                ## NOTE: This state should never be reached.
1917                !!!cp (124.6);
1918              }
1919            } else {
1920              die "$0: $self->{current_token}->{type}: Unknown token type";
1921            }
1922            $self->{state} = DATA_STATE;
1923            ## Reconsume.
1924            !!!emit ($self->{current_token}); # start tag or end tag
1925            redo A;
1926          } else {
1927            !!!cp ('124.4');
1928            !!!parse-error (type => 'nestc');
1929            ## TODO: This error type is wrong.
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            ## Reconsume.
1932            redo A;
1933          }
1934      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1935        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1936                
# Line 1516  sub _get_next_token ($) { Line 2038  sub _get_next_token ($) {
2038          } else {          } else {
2039            !!!cp (135);            !!!cp (135);
2040          }          }
2041          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2042                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2043                   $self->{next_char} == 0x005B) { # [
2044            !!!next-input-character;
2045            push @next_char, $self->{next_char};
2046            if ($self->{next_char} == 0x0043) { # C
2047              !!!next-input-character;
2048              push @next_char, $self->{next_char};
2049              if ($self->{next_char} == 0x0044) { # D
2050                !!!next-input-character;
2051                push @next_char, $self->{next_char};
2052                if ($self->{next_char} == 0x0041) { # A
2053                  !!!next-input-character;
2054                  push @next_char, $self->{next_char};
2055                  if ($self->{next_char} == 0x0054) { # T
2056                    !!!next-input-character;
2057                    push @next_char, $self->{next_char};
2058                    if ($self->{next_char} == 0x0041) { # A
2059                      !!!next-input-character;
2060                      push @next_char, $self->{next_char};
2061                      if ($self->{next_char} == 0x005B) { # [
2062                        !!!cp (135.1);
2063                        $self->{state} = CDATA_BLOCK_STATE;
2064                        !!!next-input-character;
2065                        redo A;
2066                      } else {
2067                        !!!cp (135.2);
2068                      }
2069                    } else {
2070                      !!!cp (135.3);
2071                    }
2072                  } else {
2073                    !!!cp (135.4);                
2074                  }
2075                } else {
2076                  !!!cp (135.5);
2077                }
2078              } else {
2079                !!!cp (135.6);
2080              }
2081            } else {
2082              !!!cp (135.7);
2083            }
2084        } else {        } else {
2085          !!!cp (136);          !!!cp (136);
2086        }        }
# Line 2115  sub _get_next_token ($) { Line 2680  sub _get_next_token ($) {
2680          redo A;          redo A;
2681        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2682          !!!cp (208);          !!!cp (208);
2683          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2684    
2685          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2686          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2716  sub _get_next_token ($) {
2716          redo A;          redo A;
2717        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2718          !!!cp (212);          !!!cp (212);
2719          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2722          !!!next-input-character;          !!!next-input-character;
# Line 2199  sub _get_next_token ($) { Line 2764  sub _get_next_token ($) {
2764        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2765          !!!cp (217);          !!!cp (217);
2766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2768          ## reconsume          ## reconsume
2769    
# Line 2240  sub _get_next_token ($) { Line 2804  sub _get_next_token ($) {
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2808          my $s = '';
2809          
2810          my ($l, $c) = ($self->{line}, $self->{column});
2811    
2812          CS: while ($self->{next_char} != -1) {
2813            if ($self->{next_char} == 0x005D) { # ]
2814              !!!next-input-character;
2815              if ($self->{next_char} == 0x005D) { # ]
2816                !!!next-input-character;
2817                MDC: {
2818                  if ($self->{next_char} == 0x003E) { # >
2819                    !!!cp (221.1);
2820                    !!!next-input-character;
2821                    last CS;
2822                  } elsif ($self->{next_char} == 0x005D) { # ]
2823                    !!!cp (221.2);
2824                    $s .= ']';
2825                    !!!next-input-character;
2826                    redo MDC;
2827                  } else {
2828                    !!!cp (221.3);
2829                    $s .= ']]';
2830                    #
2831                  }
2832                } # MDC
2833              } else {
2834                !!!cp (221.4);
2835                $s .= ']';
2836                #
2837              }
2838            } else {
2839              !!!cp (221.5);
2840              #
2841            }
2842            $s .= chr $self->{next_char};
2843            !!!next-input-character;
2844          } # CS
2845    
2846          $self->{state} = DATA_STATE;
2847          ## next-input-character done or EOF, which is reconsumed.
2848    
2849          if (length $s) {
2850            !!!cp (221.6);
2851            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2852                      line => $l, column => $c});
2853          } else {
2854            !!!cp (221.7);
2855          }
2856    
2857          redo A;
2858    
2859          ## ISSUE: "text tokens" in spec.
2860          ## TODO: Streaming support
2861      } else {      } else {
2862        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2863      }      }
# Line 2307  sub _tokenize_attempt_to_consume_an_enti Line 2925  sub _tokenize_attempt_to_consume_an_enti
2925    
2926          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2927            !!!cp (1008);            !!!cp (1008);
2928            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2929                              text => (sprintf 'U+%04X', $code),
2930                              line => $l, column => $c);
2931            $code = 0xFFFD;            $code = 0xFFFD;
2932          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2933            !!!cp (1009);            !!!cp (1009);
2934            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
2935                              text => (sprintf 'U-%08X', $code),
2936                              line => $l, column => $c);
2937            $code = 0xFFFD;            $code = 0xFFFD;
2938          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2939            !!!cp (1010);            !!!cp (1010);
# Line 2319  sub _tokenize_attempt_to_consume_an_enti Line 2941  sub _tokenize_attempt_to_consume_an_enti
2941            $code = 0x000A;            $code = 0x000A;
2942          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2943            !!!cp (1011);            !!!cp (1011);
2944            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2945            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2946          }          }
2947    
# Line 2352  sub _tokenize_attempt_to_consume_an_enti Line 2974  sub _tokenize_attempt_to_consume_an_enti
2974    
2975        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2976          !!!cp (1015);          !!!cp (1015);
2977          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2978                            text => (sprintf 'U+%04X', $code),
2979                            line => $l, column => $c);
2980          $code = 0xFFFD;          $code = 0xFFFD;
2981        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2982          !!!cp (1016);          !!!cp (1016);
2983          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
2984                            text => (sprintf 'U-%08X', $code),
2985                            line => $l, column => $c);
2986          $code = 0xFFFD;          $code = 0xFFFD;
2987        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2988          !!!cp (1017);          !!!cp (1017);
2989          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
2990                            line => $l, column => $c);
2991          $code = 0x000A;          $code = 0x000A;
2992        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2993          !!!cp (1018);          !!!cp (1018);
2994          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
2995                            text => (sprintf 'U+%04X', $code),
2996                            line => $l, column => $c);
2997          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2998        }        }
2999                
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 3019  sub _tokenize_attempt_to_consume_an_enti
3019      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3020      our $EntityChar;      our $EntityChar;
3021    
3022      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3023             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3024             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3025               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2462  sub _initialize_tree_constructor ($) { Line 3091  sub _initialize_tree_constructor ($) {
3091    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3092    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3093    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3094      $self->{document}->set_user_data (manakai_source_line => 1);
3095      $self->{document}->set_user_data (manakai_source_column => 1);
3096  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3097    
3098  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2532  sub _tree_construction_initial ($) { Line 3163  sub _tree_construction_initial ($) {
3163                
3164        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3165          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3166          ## NOTE: Default value for both |public_id| and |system_id| attributes
3167          ## are empty strings, so that we don't set any value in missing cases.
3168        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3169            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3170        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3179  sub _tree_construction_initial ($) {
3179        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3180          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3181          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3182          if ({          my $prefix = [
3183            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3184            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3185            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3186            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3187            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3188            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3189            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3190            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3191            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3192            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3193            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3194            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3195            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3196            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3197            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3198            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3199            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3200            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3201            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3202            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3203            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3204            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3205            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3206            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3207            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3208            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3209            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3210            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3211            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3212            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3213            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3214            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3215            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3216            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3217            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3218            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3219            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3220            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3221            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3222            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3223            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3224            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3225            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3226            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3227            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3228            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3229            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3230            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3231            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3232            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3233            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3234            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3235            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3236            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3237            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3238            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3239            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3240            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3241            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3242            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3243            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3244            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3245            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3246            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3247            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3248            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3249            "-//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}) {  
3250            !!!cp ('t5');            !!!cp ('t5');
3251            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3252          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3253                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3254            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3255              !!!cp ('t6');              !!!cp ('t6');
3256              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3258  sub _tree_construction_initial ($) {
3258              !!!cp ('t7');              !!!cp ('t7');
3259              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3260            }            }
3261          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3262                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3263            !!!cp ('t8');            !!!cp ('t8');
3264            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3265          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3272  sub _tree_construction_initial ($) {
3272          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3273          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3274          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") {
3275            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3276              ## marked as quirks.
3277            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3278            !!!cp ('t11');            !!!cp ('t11');
3279          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3296  sub _tree_construction_initial ($) {
3296        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3297        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3298        ## reprocess        ## reprocess
3299          !!!ack-later;
3300        return;        return;
3301      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3302        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2748  sub _tree_construction_root_element ($) Line 3377  sub _tree_construction_root_element ($)
3377        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3378          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3379            my $root_element;            my $root_element;
3380            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3381            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3382            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3383                  [$root_element, $el_category->{html}];
3384    
3385            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3386              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3395  sub _tree_construction_root_element ($)
3395              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3396            }            }
3397    
3398              !!!nack ('t25c');
3399    
3400            !!!next-token;            !!!next-token;
3401            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3402          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3413  sub _tree_construction_root_element ($)
3413          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3414        }        }
3415    
3416      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3417        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3418      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3419      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3420    
3421      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3422    
3423      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3424        !!!ack-later;
3425      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3426    
3427      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3445  sub _reset_insertion_mode ($) {
3445        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3446          $last = 1;          $last = 1;
3447          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3448            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3449                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3450              !!!cp ('t27');          } else {
3451              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3452          }          }
3453        }        }
3454              
3455        ## Step 4..13        ## Step 4..14
3456        my $new_mode = {        my $new_mode;
3457          if ($node->[1] & FOREIGN_EL) {
3458            !!!cp ('t28.1');
3459            ## NOTE: Strictly spaking, the line below only applies to MathML and
3460            ## SVG elements.  Currently the HTML syntax supports only MathML and
3461            ## SVG elements as foreigners.
3462            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3463          } elsif ($node->[1] & TABLE_CELL_EL) {
3464            if ($last) {
3465              !!!cp ('t28.2');
3466              #
3467            } else {
3468              !!!cp ('t28.3');
3469              $new_mode = IN_CELL_IM;
3470            }
3471          } else {
3472            !!!cp ('t28.4');
3473            $new_mode = {
3474                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3475                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3476                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3477                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3478                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3479                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3484  sub _reset_insertion_mode ($) {
3484                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3485                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3486                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3487                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3488          }
3489        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3490                
3491        ## Step 14        ## Step 15
3492        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3493          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3494            !!!cp ('t29');            !!!cp ('t29');
3495            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3503  sub _reset_insertion_mode ($) {
3503          !!!cp ('t31');          !!!cp ('t31');
3504        }        }
3505                
3506        ## Step 15        ## Step 16
3507        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3508                
3509        ## Step 16        ## Step 17
3510        $i--;        $i--;
3511        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3512                
3513        ## Step 17        ## Step 18
3514        redo S3;        redo S3;
3515      } # S3      } # S3
3516    
# Line 2976  sub _tree_construction_main ($) { Line 3622  sub _tree_construction_main ($) {
3622      ## Step 1      ## Step 1
3623      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3624      my $el;      my $el;
3625      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3626    
3627      ## Step 2      ## Step 2
3628      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3633  sub _tree_construction_main ($) {
3633    
3634      ## Step 4      ## Step 4
3635      my $text = '';      my $text = '';
3636        !!!nack ('t40.1');
3637      !!!next-token;      !!!next-token;
3638      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3639        !!!cp ('t40');        !!!cp ('t40');
# Line 3013  sub _tree_construction_main ($) { Line 3660  sub _tree_construction_main ($) {
3660        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3661        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3662          !!!cp ('t43');          !!!cp ('t43');
3663          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3664        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3665          !!!cp ('t44');          !!!cp ('t44');
3666          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3667        } else {        } else {
3668          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3669        }        }
# Line 3026  sub _tree_construction_main ($) { Line 3673  sub _tree_construction_main ($) {
3673    
3674    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3675      my $script_el;      my $script_el;
3676      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3677      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3678    
3679      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3680      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3681            
3682      my $text = '';      my $text = '';
3683        !!!nack ('t45.1');
3684      !!!next-token;      !!!next-token;
3685      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3686        !!!cp ('t45');        !!!cp ('t45');
# Line 3052  sub _tree_construction_main ($) { Line 3700  sub _tree_construction_main ($) {
3700        ## Ignore the token        ## Ignore the token
3701      } else {      } else {
3702        !!!cp ('t48');        !!!cp ('t48');
3703        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3704        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3705        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3706      }      }
# Line 3090  sub _tree_construction_main ($) { Line 3738  sub _tree_construction_main ($) {
3738        my $formatting_element;        my $formatting_element;
3739        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3740        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3741          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3742              !!!cp ('t52');
3743              last AFE;
3744            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3745                         eq $tag_name) {
3746            !!!cp ('t51');            !!!cp ('t51');
3747            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3748            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3749            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3750          }          }
3751        } # AFE        } # AFE
3752        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3753          !!!cp ('t53');          !!!cp ('t53');
3754          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3755          ## Ignore the token          ## Ignore the token
3756          !!!next-token;          !!!next-token;
3757          return;          return;
# Line 3119  sub _tree_construction_main ($) { Line 3768  sub _tree_construction_main ($) {
3768              last INSCOPE;              last INSCOPE;
3769            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3770              !!!cp ('t55');              !!!cp ('t55');
3771              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3772                                text => $token->{tag_name},
3773                              token => $end_tag_token);                              token => $end_tag_token);
3774              ## Ignore the token              ## Ignore the token
3775              !!!next-token;              !!!next-token;
3776              return;              return;
3777            }            }
3778          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3779            !!!cp ('t56');            !!!cp ('t56');
3780            $in_scope = 0;            $in_scope = 0;
3781          }          }
3782        } # INSCOPE        } # INSCOPE
3783        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3784          !!!cp ('t57');          !!!cp ('t57');
3785          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3786                            text => $token->{tag_name},
3787                          token => $end_tag_token);                          token => $end_tag_token);
3788          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3789          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3143  sub _tree_construction_main ($) { Line 3791  sub _tree_construction_main ($) {
3791        }        }
3792        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3793          !!!cp ('t58');          !!!cp ('t58');
3794          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3795                            text => $self->{open_elements}->[-1]->[0]
3796                                ->manakai_local_name,
3797                          token => $end_tag_token);                          token => $end_tag_token);
3798        }        }
3799                
# Line 3152  sub _tree_construction_main ($) { Line 3802  sub _tree_construction_main ($) {
3802        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3803        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3804          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3805          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3806              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3807              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3808               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3809            !!!cp ('t59');            !!!cp ('t59');
3810            $furthest_block = $node;            $furthest_block = $node;
3811            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3891  sub _tree_construction_main ($) {
3891        } # S7          } # S7  
3892                
3893        ## Step 8        ## Step 8
3894        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3895          my $foster_parent_element;          my $foster_parent_element;
3896          my $next_sibling;          my $next_sibling;
3897                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3898                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3899                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3900                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3901                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3968  sub _tree_construction_main ($) {
3968    
3969    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3970      my $child = shift;      my $child = shift;
3971      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]}) {  
3972        # MUST        # MUST
3973        my $foster_parent_element;        my $foster_parent_element;
3974        my $next_sibling;        my $next_sibling;
3975                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3976                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3977                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3978                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3979                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 3998  sub _tree_construction_main ($) {
3998      }      }
3999    }; # $insert_to_foster    }; # $insert_to_foster
4000    
4001    B: {    B: while (1) {
4002      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4003        !!!cp ('t73');        !!!cp ('t73');
4004        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4005        ## Ignore the token        ## Ignore the token
4006        ## Stay in the phase        ## Stay in the phase
4007        !!!next-token;        !!!next-token;
4008        redo B;        next B;
4009      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4010               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4011        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4012          !!!cp ('t79');          !!!cp ('t79');
4013          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4014          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4015        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4016          !!!cp ('t80');          !!!cp ('t80');
4017          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4018          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4019        } else {        } else {
4020          !!!cp ('t81');          !!!cp ('t81');
# Line 3385  sub _tree_construction_main ($) { Line 4031  sub _tree_construction_main ($) {
4031               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4032          }          }
4033        }        }
4034          !!!nack ('t84.1');
4035        !!!next-token;        !!!next-token;
4036        redo B;        next B;
4037      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4038        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4039        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4047  sub _tree_construction_main ($) {
4047          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4048        }        }
4049        !!!next-token;        !!!next-token;
4050        redo B;        next B;
4051      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4052          if ($token->{type} == CHARACTER_TOKEN) {
4053            !!!cp ('t87.1');
4054            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4055            !!!next-token;
4056            next B;
4057          } elsif ($token->{type} == START_TAG_TOKEN) {
4058            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4059                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4060                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4061                ($token->{tag_name} eq 'svg' and
4062                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4063              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4064              !!!cp ('t87.2');
4065              #
4066            } elsif ({
4067                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4068                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4069                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4070                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4071                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4072                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4073                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4074                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4075                     }->{$token->{tag_name}}) {
4076              !!!cp ('t87.2');
4077              !!!parse-error (type => 'not closed',
4078                              text => $self->{open_elements}->[-1]->[0]
4079                                  ->manakai_local_name,
4080                              token => $token);
4081    
4082              pop @{$self->{open_elements}}
4083                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4084    
4085              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4086              ## Reprocess.
4087              next B;
4088            } else {
4089              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4090              my $tag_name = $token->{tag_name};
4091              if ($nsuri eq $SVG_NS) {
4092                $tag_name = {
4093                   altglyph => 'altGlyph',
4094                   altglyphdef => 'altGlyphDef',
4095                   altglyphitem => 'altGlyphItem',
4096                   animatecolor => 'animateColor',
4097                   animatemotion => 'animateMotion',
4098                   animatetransform => 'animateTransform',
4099                   clippath => 'clipPath',
4100                   feblend => 'feBlend',
4101                   fecolormatrix => 'feColorMatrix',
4102                   fecomponenttransfer => 'feComponentTransfer',
4103                   fecomposite => 'feComposite',
4104                   feconvolvematrix => 'feConvolveMatrix',
4105                   fediffuselighting => 'feDiffuseLighting',
4106                   fedisplacementmap => 'feDisplacementMap',
4107                   fedistantlight => 'feDistantLight',
4108                   feflood => 'feFlood',
4109                   fefunca => 'feFuncA',
4110                   fefuncb => 'feFuncB',
4111                   fefuncg => 'feFuncG',
4112                   fefuncr => 'feFuncR',
4113                   fegaussianblur => 'feGaussianBlur',
4114                   feimage => 'feImage',
4115                   femerge => 'feMerge',
4116                   femergenode => 'feMergeNode',
4117                   femorphology => 'feMorphology',
4118                   feoffset => 'feOffset',
4119                   fepointlight => 'fePointLight',
4120                   fespecularlighting => 'feSpecularLighting',
4121                   fespotlight => 'feSpotLight',
4122                   fetile => 'feTile',
4123                   feturbulence => 'feTurbulence',
4124                   foreignobject => 'foreignObject',
4125                   glyphref => 'glyphRef',
4126                   lineargradient => 'linearGradient',
4127                   radialgradient => 'radialGradient',
4128                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4129                   textpath => 'textPath',  
4130                }->{$tag_name} || $tag_name;
4131              }
4132    
4133              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4134    
4135              ## "adjust foreign attributes" - done in insert-element-f
4136    
4137              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4138    
4139              if ($self->{self_closing}) {
4140                pop @{$self->{open_elements}};
4141                !!!ack ('t87.3');
4142              } else {
4143                !!!cp ('t87.4');
4144              }
4145    
4146              !!!next-token;
4147              next B;
4148            }
4149          } elsif ($token->{type} == END_TAG_TOKEN) {
4150            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4151            !!!cp ('t87.5');
4152            #
4153          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4154            !!!cp ('t87.6');
4155            !!!parse-error (type => 'not closed',
4156                            text => $self->{open_elements}->[-1]->[0]
4157                                ->manakai_local_name,
4158                            token => $token);
4159    
4160            pop @{$self->{open_elements}}
4161                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4162    
4163            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4164            ## Reprocess.
4165            next B;
4166          } else {
4167            die "$0: $token->{type}: Unknown token type";        
4168          }
4169        }
4170    
4171        if ($self->{insertion_mode} & HEAD_IMS) {
4172        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4173          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4174            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4178  sub _tree_construction_main ($) {
4178              !!!cp ('t88.1');              !!!cp ('t88.1');
4179              ## Ignore the token.              ## Ignore the token.
4180              !!!next-token;              !!!next-token;
4181              redo B;              next B;
4182            }            }
4183            unless (length $token->{data}) {            unless (length $token->{data}) {
4184              !!!cp ('t88');              !!!cp ('t88');
4185              !!!next-token;              !!!next-token;
4186              redo B;              next B;
4187            }            }
4188          }          }
4189    
4190          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4191            !!!cp ('t89');            !!!cp ('t89');
4192            ## As if <head>            ## As if <head>
4193            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4194            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4195            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4196                  [$self->{head_element}, $el_category->{head}];
4197    
4198            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4199            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3435  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203            !!!cp ('t90');            !!!cp ('t90');
4204            ## As if </noscript>            ## As if </noscript>
4205            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4206            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4207                        
4208            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4209            ## As if </head>            ## As if </head>
# Line 3451  sub _tree_construction_main ($) { Line 4219  sub _tree_construction_main ($) {
4219            !!!cp ('t92');            !!!cp ('t92');
4220          }          }
4221    
4222              ## "after head" insertion mode          ## "after head" insertion mode
4223              ## As if <body>          ## As if <body>
4224              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4225              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4226              ## reprocess          ## reprocess
4227              redo B;          next B;
4228            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4229              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4230                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4231                  !!!cp ('t93');              !!!cp ('t93');
4232                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4233                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4234                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4235                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4236                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4237                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4238                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4239                  !!!cp ('t94');              !!!next-token;
4240                  #              next B;
4241                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4242                  !!!cp ('t95');              !!!cp ('t93.2');
4243                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4244                  ## Ignore the token                              token => $token);
4245                  !!!next-token;              ## Ignore the token
4246                  redo B;              !!!nack ('t93.3');
4247                }              !!!next-token;
4248              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4249                !!!cp ('t96');            } else {
4250                ## As if <head>              !!!cp ('t95');
4251                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4252                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4253                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4254                !!!nack ('t95.1');
4255                !!!next-token;
4256                next B;
4257              }
4258            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4259              !!!cp ('t96');
4260              ## As if <head>
4261              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4262              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4263              push @{$self->{open_elements}},
4264                  [$self->{head_element}, $el_category->{head}];
4265    
4266                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4267                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4268              } else {          } else {
4269                !!!cp ('t97');            !!!cp ('t97');
4270              }          }
4271    
4272              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4273                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4274                  !!!cp ('t98');                  !!!cp ('t98');
4275                  ## As if </noscript>                  ## As if </noscript>
4276                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4277                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4278                                    token => $token);
4279                                
4280                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4281                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3506  sub _tree_construction_main ($) { Line 4286  sub _tree_construction_main ($) {
4286                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4287                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4288                  !!!cp ('t100');                  !!!cp ('t100');
4289                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4290                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4291                    push @{$self->{open_elements}},
4292                        [$self->{head_element}, $el_category->{head}];
4293                } else {                } else {
4294                  !!!cp ('t101');                  !!!cp ('t101');
4295                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4297  sub _tree_construction_main ($) {
4297                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4298                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4299                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4300                  !!!nack ('t101.1');
4301                !!!next-token;                !!!next-token;
4302                redo B;                next B;
4303              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4304                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4305                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4306                  !!!cp ('t102');                  !!!cp ('t102');
4307                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4308                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4309                    push @{$self->{open_elements}},
4310                        [$self->{head_element}, $el_category->{head}];
4311                } else {                } else {
4312                  !!!cp ('t103');                  !!!cp ('t103');
4313                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4316                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4317                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4318                  !!!ack ('t103.1');
4319                !!!next-token;                !!!next-token;
4320                redo B;                next B;
4321              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4322                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4323                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4324                  !!!cp ('t104');                  !!!cp ('t104');
4325                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4326                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4327                    push @{$self->{open_elements}},
4328                        [$self->{head_element}, $el_category->{head}];
4329                } else {                } else {
4330                  !!!cp ('t105');                  !!!cp ('t105');
4331                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4333  sub _tree_construction_main ($) {
4333                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.
4334    
4335                unless ($self->{confident}) {                unless ($self->{confident}) {
4336                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4337                    !!!cp ('t106');                    !!!cp ('t106');
4338                      ## NOTE: Whether the encoding is supported or not is handled
4339                      ## in the {change_encoding} callback.
4340                    $self->{change_encoding}                    $self->{change_encoding}
4341                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4342                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4346  sub _tree_construction_main ($) {
4346                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4347                                                 ->{has_reference});                                                 ->{has_reference});
4348                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4349                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4350                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4351                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4352                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4353                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4354                      !!!cp ('t107');                      !!!cp ('t107');
4355                        ## NOTE: Whether the encoding is supported or not is handled
4356                        ## in the {change_encoding} callback.
4357                      $self->{change_encoding}                      $self->{change_encoding}
4358                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4359                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4384  sub _tree_construction_main ($) {
4384    
4385                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4386                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4387                  !!!ack ('t110.1');
4388                !!!next-token;                !!!next-token;
4389                redo B;                next B;
4390              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4391                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4392                  !!!cp ('t111');                  !!!cp ('t111');
4393                  ## As if </noscript>                  ## As if </noscript>
4394                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4395                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4396                                    token => $token);
4397                                
4398                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4399                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4400                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4401                  !!!cp ('t112');                  !!!cp ('t112');
4402                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4403                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4404                    push @{$self->{open_elements}},
4405                        [$self->{head_element}, $el_category->{head}];
4406                } else {                } else {
4407                  !!!cp ('t113');                  !!!cp ('t113');
4408                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4413  sub _tree_construction_main ($) {
4413                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4414                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4415                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4416                redo B;                next B;
4417              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4418                         $token->{tag_name} eq 'noframes') {
4419                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4420                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4421                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4422                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4423                  !!!cp ('t114');                  !!!cp ('t114');
4424                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4425                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4426                    push @{$self->{open_elements}},
4427                        [$self->{head_element}, $el_category->{head}];
4428                } else {                } else {
4429                  !!!cp ('t115');                  !!!cp ('t115');
4430                }                }
4431                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4432                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4433                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4434                redo B;                next B;
4435              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4436                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4437                  !!!cp ('t116');                  !!!cp ('t116');
4438                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4439                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4440                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4441                    !!!nack ('t116.1');
4442                  !!!next-token;                  !!!next-token;
4443                  redo B;                  next B;
4444                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4445                  !!!cp ('t117');                  !!!cp ('t117');
4446                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4447                                    token => $token);
4448                  ## Ignore the token                  ## Ignore the token
4449                    !!!nack ('t117.1');
4450                  !!!next-token;                  !!!next-token;
4451                  redo B;                  next B;
4452                } else {                } else {
4453                  !!!cp ('t118');                  !!!cp ('t118');
4454                  #                  #
# Line 3657  sub _tree_construction_main ($) { Line 4458  sub _tree_construction_main ($) {
4458                  !!!cp ('t119');                  !!!cp ('t119');
4459                  ## As if </noscript>                  ## As if </noscript>
4460                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4461                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4462                                    token => $token);
4463                                
4464                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4465                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4466                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4467                  !!!cp ('t120');                  !!!cp ('t120');
4468                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4469                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4470                    push @{$self->{open_elements}},
4471                        [$self->{head_element}, $el_category->{head}];
4472                } else {                } else {
4473                  !!!cp ('t121');                  !!!cp ('t121');
4474                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4477  sub _tree_construction_main ($) {
4477                $script_start_tag->();                $script_start_tag->();
4478                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4479                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4480                redo B;                next B;
4481              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4482                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4483                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4484                  !!!cp ('t122');                  !!!cp ('t122');
4485                  ## As if </noscript>                  ## As if </noscript>
4486                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4487                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4488                                    text => $token->{tag_name}, token => $token);
4489                                    
4490                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4491                  ## As if </head>                  ## As if </head>
# Line 3707  sub _tree_construction_main ($) { Line 4512  sub _tree_construction_main ($) {
4512                } else {                } else {
4513                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4514                }                }
4515                  !!!nack ('t127.1');
4516                !!!next-token;                !!!next-token;
4517                redo B;                next B;
4518              } else {              } else {
4519                !!!cp ('t128');                !!!cp ('t128');
4520                #                #
# Line 3718  sub _tree_construction_main ($) { Line 4524  sub _tree_construction_main ($) {
4524                !!!cp ('t129');                !!!cp ('t129');
4525                ## As if </noscript>                ## As if </noscript>
4526                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4527                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4528                                  text => $token->{tag_name}, token => $token);
4529                                
4530                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4531                ## As if </head>                ## As if </head>
# Line 3740  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4548              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4549              ## reprocess              ## reprocess
4550              redo B;              !!!ack-later;
4551                next B;
4552            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4553              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4554                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4555                  !!!cp ('t132');                  !!!cp ('t132');
4556                  ## As if <head>                  ## As if <head>
4557                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4558                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4559                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4560                        [$self->{head_element}, $el_category->{head}];
4561    
4562                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4563                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4564                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4565                  !!!next-token;                  !!!next-token;
4566                  redo B;                  next B;
4567                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4568                  !!!cp ('t133');                  !!!cp ('t133');
4569                  ## As if </noscript>                  ## As if </noscript>
4570                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4571                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4572                                    text => 'head', token => $token);
4573                                    
4574                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4575                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4576                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4577                  !!!next-token;                  !!!next-token;
4578                  redo B;                  next B;
4579                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4580                  !!!cp ('t134');                  !!!cp ('t134');
4581                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4582                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4583                  !!!next-token;                  !!!next-token;
4584                  redo B;                  next B;
4585                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4586                    !!!cp ('t134.1');
4587                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4588                                    token => $token);
4589                    ## Ignore the token
4590                    !!!next-token;
4591                    next B;
4592                } else {                } else {
4593                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4594                }                }
4595              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4596                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4598  sub _tree_construction_main ($) {
4598                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4599                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4600                  !!!next-token;                  !!!next-token;
4601                  redo B;                  next B;
4602                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4603                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4604                  !!!cp ('t137');                  !!!cp ('t137');
4605                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4606                                    text => 'noscript', token => $token);
4607                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4608                  !!!next-token;                  !!!next-token;
4609                  redo B;                  next B;
4610                } else {                } else {
4611                  !!!cp ('t138');                  !!!cp ('t138');
4612                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4614  sub _tree_construction_main ($) {
4614              } elsif ({              } elsif ({
4615                        body => 1, html => 1,                        body => 1, html => 1,
4616                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4617                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4618                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4619                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head',, $token);  
                 $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) {  
4620                  !!!cp ('t140');                  !!!cp ('t140');
4621                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4622                                    text => $token->{tag_name}, token => $token);
4623                    ## Ignore the token
4624                    !!!next-token;
4625                    next B;
4626                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4627                    !!!cp ('t140.1');
4628                    !!!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                } else {                } else {
4634                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4635                }                }
4636                              } elsif ($token->{tag_name} eq 'p') {
4637                #                !!!cp ('t142');
4638              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4639                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4640                       }->{$token->{tag_name}}) {                ## Ignore the token
4641                  !!!next-token;
4642                  next B;
4643                } elsif ($token->{tag_name} eq 'br') {
4644                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4645                  !!!cp ('t142');                  !!!cp ('t142.2');
4646                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4647                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4648                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4649                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4650      
4651                    ## Reprocess in the "after head" insertion mode...
4652                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4653                    !!!cp ('t143.2');
4654                    ## As if </head>
4655                    pop @{$self->{open_elements}};
4656                    $self->{insertion_mode} = AFTER_HEAD_IM;
4657      
4658                    ## Reprocess in the "after head" insertion mode...
4659                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4660                    !!!cp ('t143.3');
4661                    ## ISSUE: Two parse errors for <head><noscript></br>
4662                    !!!parse-error (type => 'unmatched end tag',
4663                                    text => 'br', token => $token);
4664                    ## As if </noscript>
4665                    pop @{$self->{open_elements}};
4666                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4667    
4668                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4669                } else {                  ## As if </head>
4670                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4671                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4672    
4673                #                  ## Reprocess in the "after head" insertion mode...
4674              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4675                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4676                  #                  #
4677                } else {                } else {
4678                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4679                }                }
4680    
4681                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4682                  !!!parse-error (type => 'unmatched end tag',
4683                                  text => 'br', token => $token);
4684                  ## Ignore the token
4685                  !!!next-token;
4686                  next B;
4687                } else {
4688                  !!!cp ('t145');
4689                  !!!parse-error (type => 'unmatched end tag',
4690                                  text => $token->{tag_name}, token => $token);
4691                  ## Ignore the token
4692                  !!!next-token;
4693                  next B;
4694              }              }
4695    
4696              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4697                !!!cp ('t146');                !!!cp ('t146');
4698                ## As if </noscript>                ## As if </noscript>
4699                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4700                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4701                                  text => $token->{tag_name}, token => $token);
4702                                
4703                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4704                ## As if </head>                ## As if </head>
# Line 3866  sub _tree_construction_main ($) { Line 4714  sub _tree_construction_main ($) {
4714              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4715  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4716                !!!cp ('t148');                !!!cp ('t148');
4717                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4718                                  text => $token->{tag_name}, token => $token);
4719                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4720                !!!next-token;                !!!next-token;
4721                redo B;                next B;
4722              } else {              } else {
4723                !!!cp ('t149');                !!!cp ('t149');
4724              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4729              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4730              ## reprocess              ## reprocess
4731              redo B;              next B;
4732        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4733          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4734            !!!cp ('t149.1');            !!!cp ('t149.1');
4735    
4736            ## NOTE: As if <head>            ## NOTE: As if <head>
4737            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4738            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4739                ($self->{head_element});                ($self->{head_element});
4740            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4741              #    [$self->{head_element}, $el_category->{head}];
4742            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4743            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4744    
# Line 3932  sub _tree_construction_main ($) { Line 4782  sub _tree_construction_main ($) {
4782          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4783          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4784          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4785          redo B;          next B;
4786        } else {        } else {
4787          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4788        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4797  sub _tree_construction_main ($) {
4797              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4798    
4799              !!!next-token;              !!!next-token;
4800              redo B;              next B;
4801            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4802              if ({              if ({
4803                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4807  sub _tree_construction_main ($) {
4807                  ## have an element in table scope                  ## have an element in table scope
4808                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4809                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4810                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4811                      !!!cp ('t151');                      !!!cp ('t151');
4812    
4813                      ## Close the cell                      ## Close the cell
4814                      !!!back-token; # <?>                      !!!back-token; # <x>
4815                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4816                                  tag_name => $node->[0]->manakai_local_name,
4817                                line => $token->{line},                                line => $token->{line},
4818                                column => $token->{column}};                                column => $token->{column}};
4819                      redo B;                      next B;
4820                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4821                      !!!cp ('t152');                      !!!cp ('t152');
4822                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4823                      last;                      last;
# Line 3977  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826    
4827                  !!!cp ('t153');                  !!!cp ('t153');
4828                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4829                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4830                  ## Ignore the token                  ## Ignore the token
4831                    !!!nack ('t153.1');
4832                  !!!next-token;                  !!!next-token;
4833                  redo B;                  next B;
4834                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4835                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4836                                    token => $token);
4837                                    
4838                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4839                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3990  sub _tree_construction_main ($) { Line 4841  sub _tree_construction_main ($) {
4841                  INSCOPE: {                  INSCOPE: {
4842                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4843                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4844                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4845                        !!!cp ('t155');                        !!!cp ('t155');
4846                        $i = $_;                        $i = $_;
4847                        last INSCOPE;                        last INSCOPE;
4848                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4849                        !!!cp ('t156');                        !!!cp ('t156');
4850                        last;                        last;
4851                      }                      }
# Line 4004  sub _tree_construction_main ($) { Line 4853  sub _tree_construction_main ($) {
4853    
4854                    !!!cp ('t157');                    !!!cp ('t157');
4855                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4856                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4857                    ## Ignore the token                    ## Ignore the token
4858                      !!!nack ('t157.1');
4859                    !!!next-token;                    !!!next-token;
4860                    redo B;                    next B;
4861                  } # INSCOPE                  } # INSCOPE
4862                                    
4863                  ## generate implied end tags                  ## generate implied end tags
4864                  while ({                  while ($self->{open_elements}->[-1]->[1]
4865                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4866                    !!!cp ('t158');                    !!!cp ('t158');
4867                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4868                  }                  }
4869    
4870                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4871                    !!!cp ('t159');                    !!!cp ('t159');
4872                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4873                                      text => $self->{open_elements}->[-1]->[0]
4874                                          ->manakai_local_name,
4875                                      token => $token);
4876                  } else {                  } else {
4877                    !!!cp ('t160');                    !!!cp ('t160');
4878                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4884  sub _tree_construction_main ($) {
4884                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4885                                    
4886                  ## reprocess                  ## reprocess
4887                  redo B;                  !!!ack-later;
4888                    next B;
4889                } else {                } else {
4890                  !!!cp ('t161');                  !!!cp ('t161');
4891                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4901  sub _tree_construction_main ($) {
4901                  my $i;                  my $i;
4902                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4903                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4904                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4905                      !!!cp ('t163');                      !!!cp ('t163');
4906                      $i = $_;                      $i = $_;
4907                      last INSCOPE;                      last INSCOPE;
4908                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4909                      !!!cp ('t164');                      !!!cp ('t164');
4910                      last INSCOPE;                      last INSCOPE;
4911                    }                    }
4912                  } # INSCOPE                  } # INSCOPE
4913                    unless (defined $i) {                    unless (defined $i) {
4914                      !!!cp ('t165');                      !!!cp ('t165');
4915                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
4916                                        text => $token->{tag_name},
4917                                        token => $token);
4918                      ## Ignore the token                      ## Ignore the token
4919                      !!!next-token;                      !!!next-token;
4920                      redo B;                      next B;
4921                    }                    }
4922                                    
4923                  ## generate implied end tags                  ## generate implied end tags
4924                  while ({                  while ($self->{open_elements}->[-1]->[1]
4925                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4926                    !!!cp ('t166');                    !!!cp ('t166');
4927                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4928                  }                  }
4929    
4930                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4931                            ne $token->{tag_name}) {
4932                    !!!cp ('t167');                    !!!cp ('t167');
4933                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4934                                      text => $self->{open_elements}->[-1]->[0]
4935                                          ->manakai_local_name,
4936                                      token => $token);
4937                  } else {                  } else {
4938                    !!!cp ('t168');                    !!!cp ('t168');
4939                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4946                                    
4947                  !!!next-token;                  !!!next-token;
4948                  redo B;                  next B;
4949                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4950                  !!!cp ('t169');                  !!!cp ('t169');
4951                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4952                                    text => $token->{tag_name}, token => $token);
4953                  ## Ignore the token                  ## Ignore the token
4954                  !!!next-token;                  !!!next-token;
4955                  redo B;                  next B;
4956                } else {                } else {
4957                  !!!cp ('t170');                  !!!cp ('t170');
4958                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4964  sub _tree_construction_main ($) {
4964                  INSCOPE: {                  INSCOPE: {
4965                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4966                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4967                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4968                        !!!cp ('t171');                        !!!cp ('t171');
4969                        $i = $_;                        $i = $_;
4970                        last INSCOPE;                        last INSCOPE;
4971                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4972                        !!!cp ('t172');                        !!!cp ('t172');
4973                        last;                        last;
4974                      }                      }
# Line 4121  sub _tree_construction_main ($) { Line 4976  sub _tree_construction_main ($) {
4976    
4977                    !!!cp ('t173');                    !!!cp ('t173');
4978                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
4979                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4980                    ## Ignore the token                    ## Ignore the token
4981                    !!!next-token;                    !!!next-token;
4982                    redo B;                    next B;
4983                  } # INSCOPE                  } # INSCOPE
4984                                    
4985                  ## generate implied end tags                  ## generate implied end tags
4986                  while ({                  while ($self->{open_elements}->[-1]->[1]
4987                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4988                    !!!cp ('t174');                    !!!cp ('t174');
4989                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4990                  }                  }
4991                                    
4992                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4993                    !!!cp ('t175');                    !!!cp ('t175');
4994                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4995                                      text => $self->{open_elements}->[-1]->[0]
4996                                          ->manakai_local_name,
4997                                      token => $token);
4998                  } else {                  } else {
4999                    !!!cp ('t176');                    !!!cp ('t176');
5000                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 5006  sub _tree_construction_main ($) {
5006                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5007                                    
5008                  !!!next-token;                  !!!next-token;
5009                  redo B;                  next B;
5010                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5011                  !!!cp ('t177');                  !!!cp ('t177');
5012                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5013                                    text => $token->{tag_name}, token => $token);
5014                  ## Ignore the token                  ## Ignore the token
5015                  !!!next-token;                  !!!next-token;
5016                  redo B;                  next B;
5017                } else {                } else {
5018                  !!!cp ('t178');                  !!!cp ('t178');
5019                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                INSCOPE: {                INSCOPE: {
5030                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5031                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5032                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5033                      !!!cp ('t179');                      !!!cp ('t179');
5034                      $i = $_;                      $i = $_;
5035    
5036                      ## Close the cell                      ## Close the cell
5037                      !!!back-token; # </?>                      !!!back-token; # </x>
5038                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5039                                line => $token->{line},                                line => $token->{line},
5040                                column => $token->{column}};                                column => $token->{column}};
5041                      redo B;                      next B;
5042                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5043                      !!!cp ('t180');                      !!!cp ('t180');
5044                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5045                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5046                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5047                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5048                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5049                      !!!cp ('t181');                      !!!cp ('t181');
5050                      last;                      last;
# Line 4197  sub _tree_construction_main ($) { Line 5053  sub _tree_construction_main ($) {
5053    
5054                  !!!cp ('t182');                  !!!cp ('t182');
5055                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5056                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5057                  ## Ignore the token                  ## Ignore the token
5058                  !!!next-token;                  !!!next-token;
5059                  redo B;                  next B;
5060                } # INSCOPE                } # INSCOPE
5061              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5062                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5063                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5064                                  token => $token);
5065    
5066                ## As if </caption>                ## As if </caption>
5067                ## have a table element in table scope                ## have a table element in table scope
5068                my $i;                my $i;
5069                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5070                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5071                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5072                    !!!cp ('t184');                    !!!cp ('t184');
5073                    $i = $_;                    $i = $_;
5074                    last INSCOPE;                    last INSCOPE;
5075                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5076                    !!!cp ('t185');                    !!!cp ('t185');
5077                    last INSCOPE;                    last INSCOPE;
5078                  }                  }
5079                } # INSCOPE                } # INSCOPE
5080                unless (defined $i) {                unless (defined $i) {
5081                  !!!cp ('t186');                  !!!cp ('t186');
5082                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5083                                    text => 'caption', token => $token);
5084                  ## Ignore the token                  ## Ignore the token
5085                  !!!next-token;                  !!!next-token;
5086                  redo B;                  next B;
5087                }                }
5088                                
5089                ## generate implied end tags                ## generate implied end tags
5090                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5091                  !!!cp ('t187');                  !!!cp ('t187');
5092                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5093                }                }
5094    
5095                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5096                  !!!cp ('t188');                  !!!cp ('t188');
5097                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5098                                    text => $self->{open_elements}->[-1]->[0]
5099                                        ->manakai_local_name,
5100                                    token => $token);
5101                } else {                } else {
5102                  !!!cp ('t189');                  !!!cp ('t189');
5103                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5110    
5111                ## reprocess                ## reprocess
5112                redo B;                next B;
5113              } elsif ({              } elsif ({
5114                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5115                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5116                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5117                  !!!cp ('t190');                  !!!cp ('t190');
5118                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5119                                    text => $token->{tag_name}, token => $token);
5120                  ## Ignore the token                  ## Ignore the token
5121                  !!!next-token;                  !!!next-token;
5122                  redo B;                  next B;
5123                } else {                } else {
5124                  !!!cp ('t191');                  !!!cp ('t191');
5125                  #                  #
# Line 4272  sub _tree_construction_main ($) { Line 5130  sub _tree_construction_main ($) {
5130                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5131                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5132                !!!cp ('t192');                !!!cp ('t192');
5133                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5134                                  text => $token->{tag_name}, token => $token);
5135                ## Ignore the token                ## Ignore the token
5136                !!!next-token;                !!!next-token;
5137                redo B;                next B;
5138              } else {              } else {
5139                !!!cp ('t193');                !!!cp ('t193');
5140                #                #
5141              }              }
5142        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5143          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5144            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
5145              !!!cp ('t75');              !!!cp ('t75');
5146              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5147              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5165  sub _tree_construction_main ($) {
5165            unless (length $token->{data}) {            unless (length $token->{data}) {
5166              !!!cp ('t194');              !!!cp ('t194');
5167              !!!next-token;              !!!next-token;
5168              redo B;              next B;
5169            } else {            } else {
5170              !!!cp ('t195');              !!!cp ('t195');
5171            }            }
5172          }          }
5173    
5174              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5175    
5176              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5177              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4323  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179              ## result in a new Text node.              ## result in a new Text node.
5180              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5181                            
5182              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]}) {  
5183                # MUST                # MUST
5184                my $foster_parent_element;                my $foster_parent_element;
5185                my $next_sibling;                my $next_sibling;
5186                my $prev_sibling;                my $prev_sibling;
5187                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5188                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5189                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5190                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5191                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220          }          }
5221                            
5222          !!!next-token;          !!!next-token;
5223          redo B;          next B;
5224        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5225              if ({          if ({
5226                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5227                   th => 1, td => 1,               th => 1, td => 1,
5228                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5229                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5230                  ## Clear back to table context              ## Clear back to table context
5231                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5232                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5233                    !!!cp ('t201');                !!!cp ('t201');
5234                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5235                  }              }
5236                                
5237                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5238                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5239                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5240                }            }
5241              
5242                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5243                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5244                    !!!cp ('t202');                !!!cp ('t202');
5245                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5246                  }              }
5247                                    
5248                  ## Clear back to table body context              ## Clear back to table body context
5249                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5250                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5251                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5252                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5253                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5254                    pop @{$self->{open_elements}};              }
                 }  
5255                                    
5256                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5257                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5258                    !!!cp ('t204');                    !!!cp ('t204');
5259                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5260                      !!!nack ('t204');
5261                    !!!next-token;                    !!!next-token;
5262                    redo B;                    next B;
5263                  } else {                  } else {
5264                    !!!cp ('t205');                    !!!cp ('t205');
5265                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5270  sub _tree_construction_main ($) {
5270                }                }
5271    
5272                ## Clear back to table row context                ## Clear back to table row context
5273                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5274                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5275                  !!!cp ('t207');                  !!!cp ('t207');
5276                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5277                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5281  sub _tree_construction_main ($) {
5281    
5282                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5283                                
5284                  !!!nack ('t207.1');
5285                !!!next-token;                !!!next-token;
5286                redo B;                next B;
5287              } elsif ({              } elsif ({
5288                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5289                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5295  sub _tree_construction_main ($) {
5295                  my $i;                  my $i;
5296                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5297                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5298                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5299                      !!!cp ('t208');                      !!!cp ('t208');
5300                      $i = $_;                      $i = $_;
5301                      last INSCOPE;                      last INSCOPE;
5302                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5303                      !!!cp ('t209');                      !!!cp ('t209');
5304                      last INSCOPE;                      last INSCOPE;
5305                    }                    }
5306                  } # INSCOPE                  } # INSCOPE
5307                  unless (defined $i) {                  unless (defined $i) {
5308                   !!!cp ('t210');                    !!!cp ('t210');
5309  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5310                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5311                                      text => $token->{tag_name}, token => $token);
5312                    ## Ignore the token                    ## Ignore the token
5313                      !!!nack ('t210.1');
5314                    !!!next-token;                    !!!next-token;
5315                    redo B;                    next B;
5316                  }                  }
5317                                    
5318                  ## Clear back to table row context                  ## Clear back to table row context
5319                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5320                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5321                    !!!cp ('t211');                    !!!cp ('t211');
5322                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5323                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5328  sub _tree_construction_main ($) {
5328                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5329                    !!!cp ('t212');                    !!!cp ('t212');
5330                    ## reprocess                    ## reprocess
5331                    redo B;                    !!!ack-later;
5332                      next B;
5333                  } else {                  } else {
5334                    !!!cp ('t213');                    !!!cp ('t213');
5335                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
5341                  my $i;                  my $i;
5342                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5343                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5344                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5345                      !!!cp ('t214');                      !!!cp ('t214');
5346                      $i = $_;                      $i = $_;
5347                      last INSCOPE;                      last INSCOPE;
5348                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5349                      !!!cp ('t215');                      !!!cp ('t215');
5350                      last INSCOPE;                      last INSCOPE;
5351                    }                    }
5352                  } # INSCOPE                  } # INSCOPE
5353                  unless (defined $i) {                  unless (defined $i) {
5354                    !!!cp ('t216');                    !!!cp ('t216');
5355  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5356                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5357                                      text => $token->{tag_name}, token => $token);
5358                    ## Ignore the token                    ## Ignore the token
5359                      !!!nack ('t216.1');
5360                    !!!next-token;                    !!!next-token;
5361                    redo B;                    next B;
5362                  }                  }
5363    
5364                  ## Clear back to table body context                  ## Clear back to table body context
5365                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5366                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5367                    !!!cp ('t217');                    !!!cp ('t217');
5368                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5369                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5385  sub _tree_construction_main ($) {
5385    
5386                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5387                  ## Clear back to table context                  ## Clear back to table context
5388                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5389                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5390                    !!!cp ('t219');                    !!!cp ('t219');
5391                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5392                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5395  sub _tree_construction_main ($) {
5395                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5396                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5397                  ## reprocess                  ## reprocess
5398                  redo B;                  !!!ack-later;
5399                    next B;
5400                } elsif ({                } elsif ({
5401                          caption => 1,                          caption => 1,
5402                          colgroup => 1,                          colgroup => 1,
5403                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5404                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5405                  ## Clear back to table context                  ## Clear back to table context
5406                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5407                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5408                    !!!cp ('t220');                    !!!cp ('t220');
5409                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5410                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5422  sub _tree_construction_main ($) {
5422                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5423                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5424                  !!!next-token;                  !!!next-token;
5425                  redo B;                  !!!nack ('t220.1');
5426                    next B;
5427                } else {                } else {
5428                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5429                }                }
5430              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5431                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5432                                  text => $self->{open_elements}->[-1]->[0]
5433                                      ->manakai_local_name,
5434                                  token => $token);
5435    
5436                ## As if </table>                ## As if </table>
5437                ## have a table element in table scope                ## have a table element in table scope
5438                my $i;                my $i;
5439                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5440                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5441                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5442                    !!!cp ('t221');                    !!!cp ('t221');
5443                    $i = $_;                    $i = $_;
5444                    last INSCOPE;                    last INSCOPE;
5445                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5446                    !!!cp ('t222');                    !!!cp ('t222');
5447                    last INSCOPE;                    last INSCOPE;
5448                  }                  }
# Line 4601  sub _tree_construction_main ($) { Line 5450  sub _tree_construction_main ($) {
5450                unless (defined $i) {                unless (defined $i) {
5451                  !!!cp ('t223');                  !!!cp ('t223');
5452  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5453                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5454                                    token => $token);
5455                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5456                    !!!nack ('t223.1');
5457                  !!!next-token;                  !!!next-token;
5458                  redo B;                  next B;
5459                }                }
5460                                
5461  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5462                ## generate implied end tags                ## generate implied end tags
5463                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5464                  !!!cp ('t224');                  !!!cp ('t224');
5465                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5466                }                }
5467    
5468                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5469                  !!!cp ('t225');                  !!!cp ('t225');
5470  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5471                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5472                                    text => $self->{open_elements}->[-1]->[0]
5473                                        ->manakai_local_name,
5474                                    token => $token);
5475                } else {                } else {
5476                  !!!cp ('t226');                  !!!cp ('t226');
5477                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5481  sub _tree_construction_main ($) {
5481    
5482                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5483    
5484                ## reprocess            ## reprocess
5485                redo B;            !!!ack-later;
5486              next B;
5487          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5488            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5489              !!!cp ('t227.8');              !!!cp ('t227.8');
5490              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5491              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5492              redo B;              next B;
5493            } else {            } else {
5494              !!!cp ('t227.7');              !!!cp ('t227.7');
5495              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5499  sub _tree_construction_main ($) {
5499              !!!cp ('t227.6');              !!!cp ('t227.6');
5500              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5501              $script_start_tag->();              $script_start_tag->();
5502              redo B;              next B;
5503            } else {            } else {
5504              !!!cp ('t227.5');              !!!cp ('t227.5');
5505              #              #
# Line 4657  sub _tree_construction_main ($) { Line 5510  sub _tree_construction_main ($) {
5510                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5511                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5512                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5513                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5514                                    text => $token->{tag_name}, token => $token);
5515    
5516                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5517    
# Line 4666  sub _tree_construction_main ($) { Line 5520  sub _tree_construction_main ($) {
5520                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5521    
5522                  !!!next-token;                  !!!next-token;
5523                  redo B;                  !!!ack ('t227.2.1');
5524                    next B;
5525                } else {                } else {
5526                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5527                  #                  #
# Line 4684  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539            #            #
5540          }          }
5541    
5542          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5543                            token => $token);
5544    
5545          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5546          #          #
# Line 4695  sub _tree_construction_main ($) { Line 5551  sub _tree_construction_main ($) {
5551                my $i;                my $i;
5552                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5553                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5554                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5555                    !!!cp ('t228');                    !!!cp ('t228');
5556                    $i = $_;                    $i = $_;
5557                    last INSCOPE;                    last INSCOPE;
5558                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5559                    !!!cp ('t229');                    !!!cp ('t229');
5560                    last INSCOPE;                    last INSCOPE;
5561                  }                  }
5562                } # INSCOPE                } # INSCOPE
5563                unless (defined $i) {                unless (defined $i) {
5564                  !!!cp ('t230');                  !!!cp ('t230');
5565                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5566                                    text => $token->{tag_name}, token => $token);
5567                  ## Ignore the token                  ## Ignore the token
5568                    !!!nack ('t230.1');
5569                  !!!next-token;                  !!!next-token;
5570                  redo B;                  next B;
5571                } else {                } else {
5572                  !!!cp ('t232');                  !!!cp ('t232');
5573                }                }
5574    
5575                ## Clear back to table row context                ## Clear back to table row context
5576                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5577                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5578                  !!!cp ('t231');                  !!!cp ('t231');
5579  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5580                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5583  sub _tree_construction_main ($) {
5583                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5584                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5585                !!!next-token;                !!!next-token;
5586                redo B;                !!!nack ('t231.1');
5587                  next B;
5588              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5589                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5590                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5592  sub _tree_construction_main ($) {
5592                  my $i;                  my $i;
5593                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5594                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5595                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5596                      !!!cp ('t233');                      !!!cp ('t233');
5597                      $i = $_;                      $i = $_;
5598                      last INSCOPE;                      last INSCOPE;
5599                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5600                      !!!cp ('t234');                      !!!cp ('t234');
5601                      last INSCOPE;                      last INSCOPE;
5602                    }                    }
# Line 4750  sub _tree_construction_main ($) { Line 5604  sub _tree_construction_main ($) {
5604                  unless (defined $i) {                  unless (defined $i) {
5605                    !!!cp ('t235');                    !!!cp ('t235');
5606  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5607                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5608                                      text => $token->{type}, token => $token);
5609                    ## Ignore the token                    ## Ignore the token
5610                      !!!nack ('t236.1');
5611                    !!!next-token;                    !!!next-token;
5612                    redo B;                    next B;
5613                  }                  }
5614                                    
5615                  ## Clear back to table row context                  ## Clear back to table row context
5616                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5617                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5618                    !!!cp ('t236');                    !!!cp ('t236');
5619  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5620                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5630  sub _tree_construction_main ($) {
5630                  my $i;                  my $i;
5631                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5632                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5633                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5634                      !!!cp ('t237');                      !!!cp ('t237');
5635                      $i = $_;                      $i = $_;
5636                      last INSCOPE;                      last INSCOPE;
5637                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5638                      !!!cp ('t238');                      !!!cp ('t238');
5639                      last INSCOPE;                      last INSCOPE;
5640                    }                    }
5641                  } # INSCOPE                  } # INSCOPE
5642                  unless (defined $i) {                  unless (defined $i) {
5643                    !!!cp ('t239');                    !!!cp ('t239');
5644                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5645                                      text => $token->{tag_name}, token => $token);
5646                    ## Ignore the token                    ## Ignore the token
5647                      !!!nack ('t239.1');
5648                    !!!next-token;                    !!!next-token;
5649                    redo B;                    next B;
5650                  }                  }
5651                                    
5652                  ## Clear back to table body context                  ## Clear back to table body context
5653                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5654                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5655                    !!!cp ('t240');                    !!!cp ('t240');
5656                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5657                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                my $i;                my $i;
5678                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5679                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5680                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5681                    !!!cp ('t241');                    !!!cp ('t241');
5682                    $i = $_;                    $i = $_;
5683                    last INSCOPE;                    last INSCOPE;
5684                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5685                    !!!cp ('t242');                    !!!cp ('t242');
5686                    last INSCOPE;                    last INSCOPE;
5687                  }                  }
5688                } # INSCOPE                } # INSCOPE
5689                unless (defined $i) {                unless (defined $i) {
5690                  !!!cp ('t243');                  !!!cp ('t243');
5691                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5692                                    text => $token->{tag_name}, token => $token);
5693                  ## Ignore the token                  ## Ignore the token
5694                    !!!nack ('t243.1');
5695                  !!!next-token;                  !!!next-token;
5696                  redo B;                  next B;
5697                }                }
5698                                    
5699                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5702  sub _tree_construction_main ($) {
5702                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5703                                
5704                !!!next-token;                !!!next-token;
5705                redo B;                next B;
5706              } elsif ({              } elsif ({
5707                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5708                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712                  my $i;                  my $i;
5713                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5714                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5715                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5716                      !!!cp ('t247');                      !!!cp ('t247');
5717                      $i = $_;                      $i = $_;
5718                      last INSCOPE;                      last INSCOPE;
5719                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5720                      !!!cp ('t248');                      !!!cp ('t248');
5721                      last INSCOPE;                      last INSCOPE;
5722                    }                    }
5723                  } # INSCOPE                  } # INSCOPE
5724                    unless (defined $i) {                    unless (defined $i) {
5725                      !!!cp ('t249');                      !!!cp ('t249');
5726                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5727                                        text => $token->{tag_name}, token => $token);
5728                      ## Ignore the token                      ## Ignore the token
5729                        !!!nack ('t249.1');
5730                      !!!next-token;                      !!!next-token;
5731                      redo B;                      next B;
5732                    }                    }
5733                                    
5734                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5736  sub _tree_construction_main ($) {
5736                  my $i;                  my $i;
5737                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5738                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5739                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5740                      !!!cp ('t250');                      !!!cp ('t250');
5741                      $i = $_;                      $i = $_;
5742                      last INSCOPE;                      last INSCOPE;
5743                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5744                      !!!cp ('t251');                      !!!cp ('t251');
5745                      last INSCOPE;                      last INSCOPE;
5746                    }                    }
5747                  } # INSCOPE                  } # INSCOPE
5748                    unless (defined $i) {                    unless (defined $i) {
5749                      !!!cp ('t252');                      !!!cp ('t252');
5750                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5751                                        text => 'tr', token => $token);
5752                      ## Ignore the token                      ## Ignore the token
5753                        !!!nack ('t252.1');
5754                      !!!next-token;                      !!!next-token;
5755                      redo B;                      next B;
5756                    }                    }
5757                                    
5758                  ## Clear back to table row context                  ## Clear back to table row context
5759                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5760                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5761                    !!!cp ('t253');                    !!!cp ('t253');
5762  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5763                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5772  sub _tree_construction_main ($) {
5772                my $i;                my $i;
5773                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5774                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5775                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5776                    !!!cp ('t254');                    !!!cp ('t254');
5777                    $i = $_;                    $i = $_;
5778                    last INSCOPE;                    last INSCOPE;
5779                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5780                    !!!cp ('t255');                    !!!cp ('t255');
5781                    last INSCOPE;                    last INSCOPE;
5782                  }                  }
5783                } # INSCOPE                } # INSCOPE
5784                unless (defined $i) {                unless (defined $i) {
5785                  !!!cp ('t256');                  !!!cp ('t256');
5786                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5787                                    text => $token->{tag_name}, token => $token);
5788                  ## Ignore the token                  ## Ignore the token
5789                    !!!nack ('t256.1');
5790                  !!!next-token;                  !!!next-token;
5791                  redo B;                  next B;
5792                }                }
5793    
5794                ## Clear back to table body context                ## Clear back to table body context
5795                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5796                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5797                  !!!cp ('t257');                  !!!cp ('t257');
5798  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5799                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5801  sub _tree_construction_main ($) {
5801    
5802                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5803                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5804                  !!!nack ('t257.1');
5805                !!!next-token;                !!!next-token;
5806                redo B;                next B;
5807              } elsif ({              } elsif ({
5808                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5809                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5810                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5811                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5812                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5813                !!!cp ('t258');            !!!cp ('t258');
5814                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5815                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5816                !!!next-token;            ## Ignore the token
5817                redo B;            !!!nack ('t258.1');
5818               !!!next-token;
5819              next B;
5820          } else {          } else {
5821            !!!cp ('t259');            !!!cp ('t259');
5822            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5823                              text => $token->{tag_name}, token => $token);
5824    
5825            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5826            #            #
5827          }          }
5828        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5829          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5830                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5831            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5832            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5848  sub _tree_construction_main ($) {
5848                unless (length $token->{data}) {                unless (length $token->{data}) {
5849                  !!!cp ('t260');                  !!!cp ('t260');
5850                  !!!next-token;                  !!!next-token;
5851                  redo B;                  next B;
5852                }                }
5853              }              }
5854                            
# Line 5005  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
5859                !!!cp ('t262');                !!!cp ('t262');
5860                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5861                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5862                  !!!ack ('t262.1');
5863                !!!next-token;                !!!next-token;
5864                redo B;                next B;
5865              } else {              } else {
5866                !!!cp ('t263');                !!!cp ('t263');
5867                #                #
5868              }              }
5869            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5870              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5871                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5872                  !!!cp ('t264');                  !!!cp ('t264');
5873                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5874                                    text => 'colgroup', token => $token);
5875                  ## Ignore the token                  ## Ignore the token
5876                  !!!next-token;                  !!!next-token;
5877                  redo B;                  next B;
5878                } else {                } else {
5879                  !!!cp ('t265');                  !!!cp ('t265');
5880                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5881                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5882                  !!!next-token;                  !!!next-token;
5883                  redo B;                              next B;            
5884                }                }
5885              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5886                !!!cp ('t266');                !!!cp ('t266');
5887                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
5888                                  text => 'col', token => $token);
5889                ## Ignore the token                ## Ignore the token
5890                !!!next-token;                !!!next-token;
5891                redo B;                next B;
5892              } else {              } else {
5893                !!!cp ('t267');                !!!cp ('t267');
5894                #                #
5895              }              }
5896        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5897          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5898              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5899            !!!cp ('t270.2');            !!!cp ('t270.2');
5900            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5905  sub _tree_construction_main ($) {
5905            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5906            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5907            ## Reprocess.            ## Reprocess.
5908            redo B;            next B;
5909          }          }
5910        } else {        } else {
5911          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5912        }        }
5913    
5914            ## As if </colgroup>            ## As if </colgroup>
5915            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5916              !!!cp ('t269');              !!!cp ('t269');
5917  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5918              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
5919                                text => 'colgroup', token => $token);
5920              ## Ignore the token              ## Ignore the token
5921                !!!nack ('t269.1');
5922              !!!next-token;              !!!next-token;
5923              redo B;              next B;
5924            } else {            } else {
5925              !!!cp ('t270');              !!!cp ('t270');
5926              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5927              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5928                !!!ack-later;
5929              ## reprocess              ## reprocess
5930              redo B;              next B;
5931            }            }
5932      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5933        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5934          !!!cp ('t271');          !!!cp ('t271');
5935          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5936          !!!next-token;          !!!next-token;
5937          redo B;          next B;
5938        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5939              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5940                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5941                  !!!cp ('t272');              !!!cp ('t272');
5942                  ## As if </option>              ## As if </option>
5943                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5944                } else {            } else {
5945                  !!!cp ('t273');              !!!cp ('t273');
5946                }            }
5947    
5948                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5949                !!!next-token;            !!!nack ('t273.1');
5950                redo B;            !!!next-token;
5951              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5952                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5953                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5954                  ## As if </option>              !!!cp ('t274');
5955                  pop @{$self->{open_elements}};              ## As if </option>
5956                } else {              pop @{$self->{open_elements}};
5957                  !!!cp ('t275');            } else {
5958                }              !!!cp ('t275');
5959              }
5960    
5961                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5962                  !!!cp ('t276');              !!!cp ('t276');
5963                  ## As if </optgroup>              ## As if </optgroup>
5964                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5965                } else {            } else {
5966                  !!!cp ('t277');              !!!cp ('t277');
5967                }            }
5968    
5969                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5970                !!!next-token;            !!!nack ('t277.1');
5971                redo B;            !!!next-token;
5972          } elsif ($token->{tag_name} eq 'select' or            next B;
5973                   $token->{tag_name} eq 'input' or          } elsif ({
5974                       select => 1, input => 1, textarea => 1,
5975                     }->{$token->{tag_name}} or
5976                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5977                    {                    {
5978                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5117  sub _tree_construction_main ($) { Line 5980  sub _tree_construction_main ($) {
5980                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
5981                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
5982            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
5983            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
5984                              token => $token);
5985            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5986            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5987                ## have an element in table scope            ## have an element in table scope
5988                my $i;            my $i;
5989                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5990                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5991                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5992                    !!!cp ('t278');                !!!cp ('t278');
5993                    $i = $_;                $i = $_;
5994                    last INSCOPE;                last INSCOPE;
5995                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5996                            table => 1, html => 1,                !!!cp ('t279');
5997                           }->{$node->[1]}) {                last INSCOPE;
5998                    !!!cp ('t279');              }
5999                    last INSCOPE;            } # INSCOPE
6000                  }            unless (defined $i) {
6001                } # INSCOPE              !!!cp ('t280');
6002                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6003                  !!!cp ('t280');                              text => 'select', token => $token);
6004                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6005                  ## Ignore the token              !!!nack ('t280.1');
6006                  !!!next-token;              !!!next-token;
6007                  redo B;              next B;
6008                }            }
6009                                
6010                !!!cp ('t281');            !!!cp ('t281');
6011                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6012    
6013                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6014    
6015            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6016              !!!cp ('t281.2');              !!!nack ('t281.2');
6017              !!!next-token;              !!!next-token;
6018              redo B;              next B;
6019            } else {            } else {
6020              !!!cp ('t281.1');              !!!cp ('t281.1');
6021                !!!ack-later;
6022              ## Reprocess the token.              ## Reprocess the token.
6023              redo B;              next B;
6024            }            }
6025          } else {          } else {
6026            !!!cp ('t282');            !!!cp ('t282');
6027            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6028                              text => $token->{tag_name}, token => $token);
6029            ## Ignore the token            ## Ignore the token
6030              !!!nack ('t282.1');
6031            !!!next-token;            !!!next-token;
6032            redo B;            next B;
6033          }          }
6034        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6035              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6036                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6037                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6038                  !!!cp ('t283');              !!!cp ('t283');
6039                  ## As if </option>              ## As if </option>
6040                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6041                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6042                  !!!cp ('t284');              !!!cp ('t284');
6043                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6044                } else {            } else {
6045                  !!!cp ('t285');              !!!cp ('t285');
6046                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6047                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6048                }              ## Ignore the token
6049                !!!next-token;            }
6050                redo B;            !!!nack ('t285.1');
6051              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6052                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6053                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6054                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6055                } else {              !!!cp ('t286');
6056                  !!!cp ('t287');              pop @{$self->{open_elements}};
6057                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6058                  ## Ignore the token              !!!cp ('t287');
6059                }              !!!parse-error (type => 'unmatched end tag',
6060                !!!next-token;                              text => $token->{tag_name}, token => $token);
6061                redo B;              ## Ignore the token
6062              } elsif ($token->{tag_name} eq 'select') {            }
6063                ## have an element in table scope            !!!nack ('t287.1');
6064                my $i;            !!!next-token;
6065                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6066                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6067                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6068                    !!!cp ('t288');            my $i;
6069                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6071                  } elsif ({              if ($node->[1] & SELECT_EL) {
6072                            table => 1, html => 1,                !!!cp ('t288');
6073                           }->{$node->[1]}) {                $i = $_;
6074                    !!!cp ('t289');                last INSCOPE;
6075                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6076                  }                !!!cp ('t289');
6077                } # INSCOPE                last INSCOPE;
6078                unless (defined $i) {              }
6079                  !!!cp ('t290');            } # INSCOPE
6080                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6081                  ## Ignore the token              !!!cp ('t290');
6082                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6083                  redo B;                              text => $token->{tag_name}, token => $token);
6084                }              ## Ignore the token
6085                !!!nack ('t290.1');
6086                !!!next-token;
6087                next B;
6088              }
6089                                
6090                !!!cp ('t291');            !!!cp ('t291');
6091                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6092    
6093                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6094    
6095                !!!next-token;            !!!nack ('t291.1');
6096                redo B;            !!!next-token;
6097              next B;
6098          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6099                   {                   {
6100                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6101                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6102                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6103  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6104                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6105                              text => $token->{tag_name}, token => $token);
6106                                
6107                ## have an element in table scope            ## have an element in table scope
6108                my $i;            my $i;
6109                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6110                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6111                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6112                    !!!cp ('t292');                !!!cp ('t292');
6113                    $i = $_;                $i = $_;
6114                    last INSCOPE;                last INSCOPE;
6115                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6116                            table => 1, html => 1,                !!!cp ('t293');
6117                           }->{$node->[1]}) {                last INSCOPE;
6118                    !!!cp ('t293');              }
6119                    last INSCOPE;            } # INSCOPE
6120                  }            unless (defined $i) {
6121                } # INSCOPE              !!!cp ('t294');
6122                unless (defined $i) {              ## Ignore the token
6123                  !!!cp ('t294');              !!!nack ('t294.1');
6124                  ## Ignore the token              !!!next-token;
6125                  !!!next-token;              next B;
6126                  redo B;            }
               }  
6127                                
6128                ## As if </select>            ## As if </select>
6129                ## have an element in table scope            ## have an element in table scope
6130                undef $i;            undef $i;
6131                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6132                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6133                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6134                    !!!cp ('t295');                !!!cp ('t295');
6135                    $i = $_;                $i = $_;
6136                    last INSCOPE;                last INSCOPE;
6137                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6138  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6139                    !!!cp ('t296');                !!!cp ('t296');
6140                    last INSCOPE;                last INSCOPE;
6141                  }              }
6142                } # INSCOPE            } # INSCOPE
6143                unless (defined $i) {            unless (defined $i) {
6144                  !!!cp ('t297');              !!!cp ('t297');
6145  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6146                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6147                  ## Ignore the </select> token                              text => 'select', token => $token);
6148                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6149                  redo B;              !!!nack ('t297.1');
6150                }              !!!next-token; ## TODO: ok?
6151                next B;
6152              }
6153                                
6154                !!!cp ('t298');            !!!cp ('t298');
6155                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6156    
6157                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6158    
6159                ## reprocess            !!!ack-later;
6160                redo B;            ## reprocess
6161              next B;
6162          } else {          } else {
6163            !!!cp ('t299');            !!!cp ('t299');
6164            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6165                              text => $token->{tag_name}, token => $token);
6166            ## Ignore the token            ## Ignore the token
6167              !!!nack ('t299.3');
6168            !!!next-token;            !!!next-token;
6169            redo B;            next B;
6170          }          }
6171        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6172          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6173                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6174            !!!cp ('t299.1');            !!!cp ('t299.1');
6175            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6194  sub _tree_construction_main ($) {
6194            unless (length $token->{data}) {            unless (length $token->{data}) {
6195              !!!cp ('t300');              !!!cp ('t300');
6196              !!!next-token;              !!!next-token;
6197              redo B;              next B;
6198            }            }
6199          }          }
6200                    
6201          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6202            !!!cp ('t301');            !!!cp ('t301');
6203            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6204    
6205            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6206          } else {          } else {
# Line 5333  sub _tree_construction_main ($) { Line 6208  sub _tree_construction_main ($) {
6208          }          }
6209                    
6210          ## "after body" insertion mode          ## "after body" insertion mode
6211          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6212    
6213          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6214          ## reprocess          ## reprocess
6215          redo B;          next B;
6216        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6217          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6218            !!!cp ('t303');            !!!cp ('t303');
6219            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6220                              text => $token->{tag_name}, token => $token);
6221                        
6222            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6223          } else {          } else {
# Line 5349  sub _tree_construction_main ($) { Line 6225  sub _tree_construction_main ($) {
6225          }          }
6226    
6227          ## "after body" insertion mode          ## "after body" insertion mode
6228          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6229                            text => $token->{tag_name}, token => $token);
6230    
6231          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6232            !!!ack-later;
6233          ## reprocess          ## reprocess
6234          redo B;          next B;
6235        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6236          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6237            !!!cp ('t305');            !!!cp ('t305');
6238            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6239                              text => $token->{tag_name}, token => $token);
6240                        
6241            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6242            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5369  sub _tree_construction_main ($) { Line 6248  sub _tree_construction_main ($) {
6248          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6249            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6250              !!!cp ('t307');              !!!cp ('t307');
6251              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6252                                text => 'html', token => $token);
6253              ## Ignore the token              ## Ignore the token
6254              !!!next-token;              !!!next-token;
6255              redo B;              next B;
6256            } else {            } else {
6257              !!!cp ('t308');              !!!cp ('t308');
6258              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6259              !!!next-token;              !!!next-token;
6260              redo B;              next B;
6261            }            }
6262          } else {          } else {
6263            !!!cp ('t309');            !!!cp ('t309');
6264            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6265                              text => $token->{tag_name}, token => $token);
6266    
6267            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6268            ## reprocess            ## reprocess
6269            redo B;            next B;
6270          }          }
6271        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6272          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6283  sub _tree_construction_main ($) {
6283            unless (length $token->{data}) {            unless (length $token->{data}) {
6284              !!!cp ('t310');              !!!cp ('t310');
6285              !!!next-token;              !!!next-token;
6286              redo B;              next B;
6287            }            }
6288          }          }
6289                    
6290          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6291            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6292              !!!cp ('t311');              !!!cp ('t311');
6293              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6294            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6295              !!!cp ('t312');              !!!cp ('t312');
6296              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6297            } else { # "after html frameset"            } else { # "after html frameset"
6298              !!!cp ('t313');              !!!cp ('t313');
6299              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
6300    
6301              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6302              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
6303              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6304            }            }
6305                        
6306            ## Ignore the token.            ## Ignore the token.
# Line 5430  sub _tree_construction_main ($) { Line 6311  sub _tree_construction_main ($) {
6311              !!!cp ('t315');              !!!cp ('t315');
6312              !!!next-token;              !!!next-token;
6313            }            }
6314            redo B;            next B;
6315          }          }
6316                    
6317          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6318        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6319          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6320            !!!cp ('t316');            !!!cp ('t316');
6321            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6322                              text => $token->{tag_name}, token => $token);
6323    
6324            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6325            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5449  sub _tree_construction_main ($) { Line 6331  sub _tree_construction_main ($) {
6331              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6332            !!!cp ('t318');            !!!cp ('t318');
6333            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6334              !!!nack ('t318.1');
6335            !!!next-token;            !!!next-token;
6336            redo B;            next B;
6337          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6338                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6339            !!!cp ('t319');            !!!cp ('t319');
6340            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6341            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6342              !!!ack ('t319.1');
6343            !!!next-token;            !!!next-token;
6344            redo B;            next B;
6345          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6346            !!!cp ('t320');            !!!cp ('t320');
6347            ## NOTE: As if in body.            ## NOTE: As if in head.
6348            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6349            redo B;            next B;
6350          } else {          } else {
6351            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6352              !!!cp ('t321');              !!!cp ('t321');
6353              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6354                                text => $token->{tag_name}, token => $token);
6355            } else {            } else {
6356              !!!cp ('t322');              !!!cp ('t322');
6357              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6358                                text => $token->{tag_name}, token => $token);
6359            }            }
6360            ## Ignore the token            ## Ignore the token
6361              !!!nack ('t322.1');
6362            !!!next-token;            !!!next-token;
6363            redo B;            next B;
6364          }          }
6365        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6366          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6367            !!!cp ('t323');            !!!cp ('t323');
6368            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6369                              text => $token->{tag_name}, token => $token);
6370    
6371            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6372            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5488  sub _tree_construction_main ($) { Line 6376  sub _tree_construction_main ($) {
6376    
6377          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6378              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6379            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6380                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6381              !!!cp ('t325');              !!!cp ('t325');
6382              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6383                                text => $token->{tag_name}, token => $token);
6384              ## Ignore the token              ## Ignore the token
6385              !!!next-token;              !!!next-token;
6386            } else {            } else {
# Line 5501  sub _tree_construction_main ($) { Line 6390  sub _tree_construction_main ($) {
6390            }            }
6391    
6392            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6393                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6394              !!!cp ('t327');              !!!cp ('t327');
6395              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6396            } else {            } else {
6397              !!!cp ('t328');              !!!cp ('t328');
6398            }            }
6399            redo B;            next B;
6400          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6401                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6402            !!!cp ('t329');            !!!cp ('t329');
6403            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6404            !!!next-token;            !!!next-token;
6405            redo B;            next B;
6406          } else {          } else {
6407            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6408              !!!cp ('t330');              !!!cp ('t330');
6409              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6410                                text => $token->{tag_name}, token => $token);
6411            } else {            } else {
6412              !!!cp ('t331');              !!!cp ('t331');
6413              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:/',
6414                                text => $token->{tag_name}, token => $token);
6415            }            }
6416            ## Ignore the token            ## Ignore the token
6417            !!!next-token;            !!!next-token;
6418            redo B;            next B;
6419          }          }
6420        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6421          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6422                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6423            !!!cp ('t331.1');            !!!cp ('t331.1');
6424            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6443  sub _tree_construction_main ($) {
6443          !!!cp ('t332');          !!!cp ('t332');
6444          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6445          $script_start_tag->();          $script_start_tag->();
6446          redo B;          next B;
6447        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6448          !!!cp ('t333');          !!!cp ('t333');
6449          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6450          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6451          redo B;          next B;
6452        } elsif ({        } elsif ({
6453                  base => 1, link => 1,                  base => 1, link => 1,
6454                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6456  sub _tree_construction_main ($) {
6456          ## 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
6457          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6458          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6459            !!!ack ('t334.1');
6460          !!!next-token;          !!!next-token;
6461          redo B;          next B;
6462        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
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}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465          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.
6466    
6467          unless ($self->{confident}) {          unless ($self->{confident}) {
6468            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6469              !!!cp ('t335');              !!!cp ('t335');
6470                ## NOTE: Whether the encoding is supported or not is handled
6471                ## in the {change_encoding} callback.
6472              $self->{change_encoding}              $self->{change_encoding}
6473                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6474                            
# Line 5583  sub _tree_construction_main ($) { Line 6477  sub _tree_construction_main ($) {
6477                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6478                                           ->{has_reference});                                           ->{has_reference});
6479            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6480              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6481                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6482                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6483                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6484                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6485                !!!cp ('t336');                !!!cp ('t336');
6486                  ## NOTE: Whether the encoding is supported or not is handled
6487                  ## in the {change_encoding} callback.
6488                $self->{change_encoding}                $self->{change_encoding}
6489                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6490                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5615  sub _tree_construction_main ($) { Line 6510  sub _tree_construction_main ($) {
6510            }            }
6511          }          }
6512    
6513            !!!ack ('t338.1');
6514          !!!next-token;          !!!next-token;
6515          redo B;          next B;
6516        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6517          !!!cp ('t341');          !!!cp ('t341');
6518          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6519          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6520          redo B;          next B;
6521        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6522          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6523                                
6524          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6525              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6526            !!!cp ('t342');            !!!cp ('t342');
6527            ## Ignore the token            ## Ignore the token
6528          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6536  sub _tree_construction_main ($) {
6536              }              }
6537            }            }
6538          }          }
6539            !!!nack ('t343.1');
6540          !!!next-token;          !!!next-token;
6541          redo B;          next B;
6542        } elsif ({        } elsif ({
6543                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6544                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553            !!!cp ('t350');            !!!cp ('t350');
6554            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6555            ## Ignore the token            ## Ignore the token
6556              !!!nack ('t350.1');
6557            !!!next-token;            !!!next-token;
6558            redo B;            next B;
6559          }          }
6560    
6561          ## has a p element in scope          ## has a p element in scope
6562          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6563            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6564              !!!cp ('t344');              !!!cp ('t344');
6565              !!!back-token;              !!!back-token; # <form>
6566              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6567                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6568              redo B;              next B;
6569            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6570              !!!cp ('t345');              !!!cp ('t345');
6571              last INSCOPE;              last INSCOPE;
6572            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6574  sub _tree_construction_main ($) {
6574                        
6575          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6576          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6577              !!!nack ('t346.1');
6578            !!!next-token;            !!!next-token;
6579            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6580              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6591  sub _tree_construction_main ($) {
6591            !!!cp ('t347.1');            !!!cp ('t347.1');
6592            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6593    
6594              !!!nack ('t347.2');
6595            !!!next-token;            !!!next-token;
6596          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6597            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6599  sub _tree_construction_main ($) {
6599                        
6600            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6601    
6602              !!!nack ('t382.1');
6603            !!!next-token;            !!!next-token;
6604          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6605            !!!cp ('t386');            !!!cp ('t386');
6606            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6607                    
6608              !!!nack ('t386.1');
6609            !!!next-token;            !!!next-token;
6610          } else {          } else {
6611            !!!cp ('t347');            !!!nack ('t347.1');
6612            !!!next-token;            !!!next-token;
6613          }          }
6614          redo B;          next B;
6615        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6616          ## has a p element in scope          ## has a p element in scope
6617          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6618            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6619              !!!cp ('t353');              !!!cp ('t353');
6620              !!!back-token;              !!!back-token; # <x>
6621              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6622                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6623              redo B;              next B;
6624            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6625              !!!cp ('t354');              !!!cp ('t354');
6626              last INSCOPE;              last INSCOPE;
6627            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6635  sub _tree_construction_main ($) {
6635                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6636          LI: {          LI: {
6637            ## Step 2            ## Step 2
6638            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6639              if ($i != -1) {              if ($i != -1) {
6640                !!!cp ('t355');                !!!cp ('t355');
6641                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6642                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6643                                      ->manakai_local_name,
6644                                  token => $token);
6645              } else {              } else {
6646                !!!cp ('t356');                !!!cp ('t356');
6647              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6652  sub _tree_construction_main ($) {
6652            }            }
6653                        
6654            ## Step 3            ## Step 3
6655            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6656                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6657                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6658                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6659                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6660                  not ($node->[1] & DIV_EL)) {
6661              !!!cp ('t358');              !!!cp ('t358');
6662              last LI;              last LI;
6663            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670          } # LI          } # LI
6671                        
6672          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673            !!!nack ('t359.1');
6674          !!!next-token;          !!!next-token;
6675          redo B;          next B;
6676        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6677          ## has a p element in scope          ## has a p element in scope
6678          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6679            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6680              !!!cp ('t367');              !!!cp ('t367');
6681              !!!back-token;              !!!back-token; # <plaintext>
6682              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6683                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6684              redo B;              next B;
6685            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6686              !!!cp ('t368');              !!!cp ('t368');
6687              last INSCOPE;              last INSCOPE;
6688            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6692  sub _tree_construction_main ($) {
6692                        
6693          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6694                        
6695            !!!nack ('t368.1');
6696          !!!next-token;          !!!next-token;
6697          redo B;          next B;
6698        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6699          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6700            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6701            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6702              !!!cp ('t371');              !!!cp ('t371');
6703              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6704                            
6705              !!!back-token;              !!!back-token; # <a>
6706              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6707                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6708              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6733  sub _tree_construction_main ($) {
6733          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6734          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6735    
6736            !!!nack ('t374.1');
6737          !!!next-token;          !!!next-token;
6738          redo B;          next B;
6739        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6740          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6741    
6742          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6743          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6744            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6745            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6746              !!!cp ('t376');              !!!cp ('t376');
6747              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6748              !!!back-token;              !!!back-token; # <nobr>
6749              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6750                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6751              redo B;              next B;
6752            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6753              !!!cp ('t377');              !!!cp ('t377');
6754              last INSCOPE;              last INSCOPE;
6755            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6758  sub _tree_construction_main ($) {
6758          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6759          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6760                    
6761            !!!nack ('t377.1');
6762          !!!next-token;          !!!next-token;
6763          redo B;          next B;
6764        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6765          ## has a button element in scope          ## has a button element in scope
6766          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6767            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6768            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6769              !!!cp ('t378');              !!!cp ('t378');
6770              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6771              !!!back-token;              !!!back-token; # <button>
6772              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6773                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6774              redo B;              next B;
6775            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6776              !!!cp ('t379');              !!!cp ('t379');
6777              last INSCOPE;              last INSCOPE;
6778            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786    
6787          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6788    
6789            !!!nack ('t379.1');
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ({        } elsif ({
6793                  xmp => 1,                  xmp => 1,
6794                  iframe => 1,                  iframe => 1,
6795                  noembed => 1,                  noembed => 1,
6796                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6797                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6798                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6799          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6804  sub _tree_construction_main ($) {
6804          }          }
6805          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6806          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6807          redo B;          next B;
6808        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6809          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6810                    
6811          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6812            !!!cp ('t389');            !!!cp ('t389');
6813            ## Ignore the token            ## Ignore the token
6814              !!!nack ('t389'); ## NOTE: Not acknowledged.
6815            !!!next-token;            !!!next-token;
6816            redo B;            next B;
6817          } else {          } else {
6818              !!!ack ('t391.1');
6819    
6820            my $at = $token->{attributes};            my $at = $token->{attributes};
6821            my $form_attrs;            my $form_attrs;
6822            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6860  sub _tree_construction_main ($) {
6860                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6861                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6862                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6863            !!!back-token (@tokens);            !!!back-token (@tokens);
6864            redo B;            !!!next-token;
6865              next B;
6866          }          }
6867        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6868          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6869          my $el;          my $el;
6870          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6871                    
6872          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6873          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          $insert->($el);          $insert->($el);
6877                    
6878          my $text = '';          my $text = '';
6879            !!!nack ('t392.1');
6880          !!!next-token;          !!!next-token;
6881          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6882            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6008  sub _tree_construction_main ($) { Line 6907  sub _tree_construction_main ($) {
6907            ## Ignore the token            ## Ignore the token
6908          } else {          } else {
6909            !!!cp ('t398');            !!!cp ('t398');
6910            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6911          }          }
6912          !!!next-token;          !!!next-token;
6913            next B;
6914          } elsif ($token->{tag_name} eq 'rt' or
6915                   $token->{tag_name} eq 'rp') {
6916            ## has a |ruby| element in scope
6917            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6918              my $node = $self->{open_elements}->[$_];
6919              if ($node->[1] & RUBY_EL) {
6920                !!!cp ('t398.1');
6921                ## generate implied end tags
6922                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6923                  !!!cp ('t398.2');
6924                  pop @{$self->{open_elements}};
6925                }
6926                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6927                  !!!cp ('t398.3');
6928                  !!!parse-error (type => 'not closed',
6929                                  text => $self->{open_elements}->[-1]->[0]
6930                                      ->manakai_local_name,
6931                                  token => $token);
6932                  pop @{$self->{open_elements}}
6933                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6934                }
6935                last INSCOPE;
6936              } elsif ($node->[1] & SCOPING_EL) {
6937                !!!cp ('t398.4');
6938                last INSCOPE;
6939              }
6940            } # INSCOPE
6941    
6942            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6943    
6944            !!!nack ('t398.5');
6945            !!!next-token;
6946          redo B;          redo B;
6947          } elsif ($token->{tag_name} eq 'math' or
6948                   $token->{tag_name} eq 'svg') {
6949            $reconstruct_active_formatting_elements->($insert_to_current);
6950    
6951            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6952    
6953            ## "adjust foreign attributes" - done in insert-element-f
6954            
6955            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6956            
6957            if ($self->{self_closing}) {
6958              pop @{$self->{open_elements}};
6959              !!!ack ('t398.1');
6960            } else {
6961              !!!cp ('t398.2');
6962              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6963              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6964              ## mode, "in body" (not "in foreign content") secondary insertion
6965              ## mode, maybe.
6966            }
6967    
6968            !!!next-token;
6969            next B;
6970        } elsif ({        } elsif ({
6971                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6972                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6019  sub _tree_construction_main ($) { Line 6974  sub _tree_construction_main ($) {
6974                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6975                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6976          !!!cp ('t401');          !!!cp ('t401');
6977          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
6978                            text => $token->{tag_name}, token => $token);
6979          ## Ignore the token          ## Ignore the token
6980            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6981          !!!next-token;          !!!next-token;
6982          redo B;          next B;
6983                    
6984          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6985        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 7001  sub _tree_construction_main ($) {
7001              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7002            !!!cp ('t380');            !!!cp ('t380');
7003            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7004              !!!nack ('t380.1');
7005          } elsif ({          } elsif ({
7006                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7007                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 7009  sub _tree_construction_main ($) {
7009                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7010            !!!cp ('t375');            !!!cp ('t375');
7011            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7012              !!!nack ('t375.1');
7013          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7014            !!!cp ('t388');            !!!cp ('t388');
7015            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7016            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7017              !!!ack ('t388.2');
7018          } elsif ({          } elsif ({
7019                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7020                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6062  sub _tree_construction_main ($) { Line 7022  sub _tree_construction_main ($) {
7022                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7023            !!!cp ('t388.1');            !!!cp ('t388.1');
7024            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7025              !!!ack ('t388.3');
7026          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7027            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7028                    
# Line 6074  sub _tree_construction_main ($) { Line 7035  sub _tree_construction_main ($) {
7035              !!!cp ('t400.2');              !!!cp ('t400.2');
7036              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7037            }            }
7038              !!!nack ('t400.3');
7039          } else {          } else {
7040            !!!cp ('t402');            !!!nack ('t402');
7041          }          }
7042                    
7043          !!!next-token;          !!!next-token;
7044          redo B;          next B;
7045        }        }
7046      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7047        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 7049  sub _tree_construction_main ($) {
7049          my $i;          my $i;
7050          INSCOPE: {          INSCOPE: {
7051            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7052              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7053                !!!cp ('t405');                !!!cp ('t405');
7054                $i = $_;                $i = $_;
7055                last INSCOPE;                last INSCOPE;
7056              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7057                !!!cp ('t405.1');                !!!cp ('t405.1');
7058                last;                last;
7059              }              }
7060            }            }
7061    
7062            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7063                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7064            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7065            !!!next-token;            !!!next-token;
7066            redo B;            next B;
7067          } # INSCOPE          } # INSCOPE
7068    
7069          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7070            unless ({            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                    th => 1, tr => 1, body => 1, html => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$_->[1]}) {  
7071              !!!cp ('t403');              !!!cp ('t403');
7072              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7073                                text => $_->[0]->manakai_local_name,
7074                                token => $token);
7075              last;              last;
7076            } else {            } else {
7077              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 7080  sub _tree_construction_main ($) {
7080    
7081          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7082          !!!next-token;          !!!next-token;
7083          redo B;          next B;
7084        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7085          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
7086            ## up-to-date, though it has same effect as speced.
7087            if (@{$self->{open_elements}} > 1 and
7088                $self->{open_elements}->[1]->[1] & BODY_EL) {
7089            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7090            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7091              !!!cp ('t406');              !!!cp ('t406');
7092              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7093                                text => $self->{open_elements}->[1]->[0]
7094                                    ->manakai_local_name,
7095                                token => $token);
7096            } else {            } else {
7097              !!!cp ('t407');              !!!cp ('t407');
7098            }            }
7099            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7100            ## reprocess            ## reprocess
7101            redo B;            next B;
7102          } else {          } else {
7103            !!!cp ('t408');            !!!cp ('t408');
7104            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7105                              text => $token->{tag_name}, token => $token);
7106            ## Ignore the token            ## Ignore the token
7107            !!!next-token;            !!!next-token;
7108            redo B;            next B;
7109          }          }
7110        } elsif ({        } elsif ({
7111                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 7118  sub _tree_construction_main ($) {
7118          my $i;          my $i;
7119          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7120            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7121            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7122              !!!cp ('t410');              !!!cp ('t410');
7123              $i = $_;              $i = $_;
7124              last INSCOPE;              last INSCOPE;
7125            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7126              !!!cp ('t411');              !!!cp ('t411');
7127              last INSCOPE;              last INSCOPE;
7128            }            }
# Line 6169  sub _tree_construction_main ($) { Line 7130  sub _tree_construction_main ($) {
7130    
7131          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7132            !!!cp ('t413');            !!!cp ('t413');
7133            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7134                              text => $token->{tag_name}, token => $token);
7135          } else {          } else {
7136            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7137            while ({            while ({
7138                      ## END_TAG_OPTIONAL_EL
7139                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7140                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7141                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7142                    p => 1,                    p => 1,
7143                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7144                      rp => 1,
7145                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7146              !!!cp ('t409');              !!!cp ('t409');
7147              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7148            }            }
7149    
7150            ## Step 2.            ## Step 2.
7151            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7152                      ne $token->{tag_name}) {
7153              !!!cp ('t412');              !!!cp ('t412');
7154              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7155                                text => $self->{open_elements}->[-1]->[0]
7156                                    ->manakai_local_name,
7157                                token => $token);
7158            } else {            } else {
7159              !!!cp ('t414');              !!!cp ('t414');
7160            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7169  sub _tree_construction_main ($) {
7169                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7170          }          }
7171          !!!next-token;          !!!next-token;
7172          redo B;          next B;
7173        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7174          undef $self->{form_element};          undef $self->{form_element};
7175    
# Line 6208  sub _tree_construction_main ($) { Line 7177  sub _tree_construction_main ($) {
7177          my $i;          my $i;
7178          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7179            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7180            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7181              !!!cp ('t418');              !!!cp ('t418');
7182              $i = $_;              $i = $_;
7183              last INSCOPE;              last INSCOPE;
7184            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7185              !!!cp ('t419');              !!!cp ('t419');
7186              last INSCOPE;              last INSCOPE;
7187            }            }
# Line 6223  sub _tree_construction_main ($) { Line 7189  sub _tree_construction_main ($) {
7189    
7190          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7191            !!!cp ('t421');            !!!cp ('t421');
7192            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7193                              text => $token->{tag_name}, token => $token);
7194          } else {          } else {
7195            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7196            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7197              !!!cp ('t417');              !!!cp ('t417');
7198              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7199            }            }
7200                        
7201            ## Step 2.            ## Step 2.
7202            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7203                      ne $token->{tag_name}) {
7204              !!!cp ('t417.1');              !!!cp ('t417.1');
7205              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7206                                text => $self->{open_elements}->[-1]->[0]
7207                                    ->manakai_local_name,
7208                                token => $token);
7209            } else {            } else {
7210              !!!cp ('t420');              !!!cp ('t420');
7211            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7215  sub _tree_construction_main ($) {
7215          }          }
7216    
7217          !!!next-token;          !!!next-token;
7218          redo B;          next B;
7219        } elsif ({        } elsif ({
7220                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7221                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7223  sub _tree_construction_main ($) {
7223          my $i;          my $i;
7224          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7225            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7226            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7227              !!!cp ('t423');              !!!cp ('t423');
7228              $i = $_;              $i = $_;
7229              last INSCOPE;              last INSCOPE;
7230            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7231              !!!cp ('t424');              !!!cp ('t424');
7232              last INSCOPE;              last INSCOPE;
7233            }            }
# Line 6271  sub _tree_construction_main ($) { Line 7235  sub _tree_construction_main ($) {
7235    
7236          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7237            !!!cp ('t425.1');            !!!cp ('t425.1');
7238            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7239                              text => $token->{tag_name}, token => $token);
7240          } else {          } else {
7241            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7242            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7243              !!!cp ('t422');              !!!cp ('t422');
7244              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7245            }            }
7246                        
7247            ## Step 2.            ## Step 2.
7248            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7249                      ne $token->{tag_name}) {
7250              !!!cp ('t425');              !!!cp ('t425');
7251              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7252                                text => $token->{tag_name}, token => $token);
7253            } else {            } else {
7254              !!!cp ('t426');              !!!cp ('t426');
7255            }            }
# Line 6294  sub _tree_construction_main ($) { Line 7259  sub _tree_construction_main ($) {
7259          }          }
7260                    
7261          !!!next-token;          !!!next-token;
7262          redo B;          next B;
7263        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7264          ## has an element in scope          ## has an element in scope
7265          my $i;          my $i;
7266          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7267            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7268            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7269              !!!cp ('t410.1');              !!!cp ('t410.1');
7270              $i = $_;              $i = $_;
7271              last INSCOPE;              last INSCOPE;
7272            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7273              !!!cp ('t411.1');              !!!cp ('t411.1');
7274              last INSCOPE;              last INSCOPE;
7275            }            }
7276          } # INSCOPE          } # INSCOPE
7277    
7278          if (defined $i) {          if (defined $i) {
7279            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7280                      ne $token->{tag_name}) {
7281              !!!cp ('t412.1');              !!!cp ('t412.1');
7282              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7283                                text => $self->{open_elements}->[-1]->[0]
7284                                    ->manakai_local_name,
7285                                token => $token);
7286            } else {            } else {
7287              !!!cp ('t414.1');              !!!cp ('t414.1');
7288            }            }
# Line 6324  sub _tree_construction_main ($) { Line 7290  sub _tree_construction_main ($) {
7290            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7291          } else {          } else {
7292            !!!cp ('t413.1');            !!!cp ('t413.1');
7293            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7294                              text => $token->{tag_name}, token => $token);
7295    
7296            !!!cp ('t415.1');            !!!cp ('t415.1');
7297            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7298            my $el;            my $el;
7299            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7300            $insert->($el);            $insert->($el);
7301            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7302          }          }
7303    
7304          !!!next-token;          !!!next-token;
7305          redo B;          next B;
7306        } elsif ({        } elsif ({
7307                  a => 1,                  a => 1,
7308                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6344  sub _tree_construction_main ($) { Line 7311  sub _tree_construction_main ($) {
7311                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7312          !!!cp ('t427');          !!!cp ('t427');
7313          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7314          redo B;          next B;
7315        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7316          !!!cp ('t428');          !!!cp ('t428');
7317          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7318                            text => 'br', token => $token);
7319    
7320          ## As if <br>          ## As if <br>
7321          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7322                    
7323          my $el;          my $el;
7324          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7325          $insert->($el);          $insert->($el);
7326                    
7327          ## Ignore the token.          ## Ignore the token.
7328          !!!next-token;          !!!next-token;
7329          redo B;          next B;
7330        } elsif ({        } elsif ({
7331                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7332                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6372  sub _tree_construction_main ($) { Line 7340  sub _tree_construction_main ($) {
7340                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7341                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7342          !!!cp ('t429');          !!!cp ('t429');
7343          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7344                            text => $token->{tag_name}, token => $token);
7345          ## Ignore the token          ## Ignore the token
7346          !!!next-token;          !!!next-token;
7347          redo B;          next B;
7348                    
7349          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7350                    
# Line 6386  sub _tree_construction_main ($) { Line 7355  sub _tree_construction_main ($) {
7355    
7356          ## Step 2          ## Step 2
7357          S2: {          S2: {
7358            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7359              ## Step 1              ## Step 1
7360              ## generate implied end tags              ## generate implied end tags
7361              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7362                !!!cp ('t430');                !!!cp ('t430');
7363                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7364                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7365                  ## which seems wrong.
7366                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7367                  $node_i++;
7368              }              }
7369                    
7370              ## Step 2              ## Step 2
7371              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7372                        ne $token->{tag_name}) {
7373                !!!cp ('t431');                !!!cp ('t431');
7374                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7375                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7376                                  text => $self->{open_elements}->[-1]->[0]
7377                                      ->manakai_local_name,
7378                                  token => $token);
7379              } else {              } else {
7380                !!!cp ('t432');                !!!cp ('t432');
7381              }              }
7382                            
7383              ## Step 3              ## Step 3
7384              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7385    
7386              !!!next-token;              !!!next-token;
7387              last S2;              last S2;
7388            } else {            } else {
7389              ## Step 3              ## Step 3
7390              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7391                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7392                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7393                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7394                !!!cp ('t433');                !!!cp ('t433');
7395                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7396                                  text => $token->{tag_name}, token => $token);
7397                ## Ignore the token                ## Ignore the token
7398                !!!next-token;                !!!next-token;
7399                last S2;                last S2;
# Line 6434  sub _tree_construction_main ($) { Line 7409  sub _tree_construction_main ($) {
7409            ## Step 5;            ## Step 5;
7410            redo S2;            redo S2;
7411          } # S2          } # S2
7412          redo B;          next B;
7413        }        }
7414      }      }
7415      redo B;      next B;
7416      } continue { # B
7417        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7418          ## NOTE: The code below is executed in cases where it does not have
7419          ## to be, but it it is harmless even in those cases.
7420          ## has an element in scope
7421          INSCOPE: {
7422            for (reverse 0..$#{$self->{open_elements}}) {
7423              my $node = $self->{open_elements}->[$_];
7424              if ($node->[1] & FOREIGN_EL) {
7425                last INSCOPE;
7426              } elsif ($node->[1] & SCOPING_EL) {
7427                last;
7428              }
7429            }
7430            
7431            ## NOTE: No foreign element in scope.
7432            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7433          } # INSCOPE
7434        }
7435    } # B    } # B
7436    
7437    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6483  sub set_inner_html ($$$) { Line 7477  sub set_inner_html ($$$) {
7477    
7478      ## Step 8 # MUST      ## Step 8 # MUST
7479      my $i = 0;      my $i = 0;
7480      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7481      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7482      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7483        my $self = shift;        my $self = shift;
7484    
# Line 6493  sub set_inner_html ($$$) { Line 7487  sub set_inner_html ($$$) {
7487    
7488        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7489        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7490        $column++;  
7491          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7492          $p->{column}++;
7493    
7494        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7495          $line++;          $p->{line}++;
7496          $column = 0;          $p->{column} = 0;
7497          !!!cp ('i1');          !!!cp ('i1');
7498        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7499          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7500          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7501          $line++;          $p->{line}++;
7502          $column = 0;          $p->{column} = 0;
7503          !!!cp ('i2');          !!!cp ('i2');
7504        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7505          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6512  sub set_inner_html ($$$) { Line 7508  sub set_inner_html ($$$) {
7508          !!!cp ('i4');          !!!cp ('i4');
7509          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7510          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7511          } elsif ($self->{next_char} <= 0x0008 or
7512                   (0x000E <= $self->{next_char} and
7513                    $self->{next_char} <= 0x001F) or
7514                   (0x007F <= $self->{next_char} and
7515                    $self->{next_char} <= 0x009F) or
7516                   (0xD800 <= $self->{next_char} and
7517                    $self->{next_char} <= 0xDFFF) or
7518                   (0xFDD0 <= $self->{next_char} and
7519                    $self->{next_char} <= 0xFDDF) or
7520                   {
7521                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7522                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7523                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7524                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7525                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7526                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7527                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7528                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7529                    0x10FFFE => 1, 0x10FFFF => 1,
7530                   }->{$self->{next_char}}) {
7531            !!!cp ('i4.1');
7532            if ($self->{next_char} < 0x10000) {
7533              !!!parse-error (type => 'control char',
7534                              text => (sprintf 'U+%04X', $self->{next_char}));
7535            } else {
7536              !!!parse-error (type => 'control char',
7537                              text => (sprintf 'U-%08X', $self->{next_char}));
7538            }
7539        }        }
7540      };      };
7541      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6519  sub set_inner_html ($$$) { Line 7543  sub set_inner_html ($$$) {
7543            
7544      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7545        my (%opt) = @_;        my (%opt) = @_;
7546        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7547          my $column = $opt{column};
7548          if (defined $opt{token} and defined $opt{token}->{line}) {
7549            $line = $opt{token}->{line};
7550            $column = $opt{token}->{column};
7551          }
7552          warn "Parse error ($opt{type}) at line $line column $column\n";
7553      };      };
7554      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7555        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7556      };      };
7557            
7558      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6546  sub set_inner_html ($$$) { Line 7576  sub set_inner_html ($$$) {
7576          unless defined $p->{content_model};          unless defined $p->{content_model};
7577          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7578    
7579      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7580          ## TODO: Foreign element OK?
7581    
7582      ## Step 3      ## Step 3
7583      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6556  sub set_inner_html ($$$) { Line 7587  sub set_inner_html ($$$) {
7587      $doc->append_child ($root);      $doc->append_child ($root);
7588    
7589      ## Step 5 # MUST      ## Step 5 # MUST
7590      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7591    
7592      undef $p->{head_element};      undef $p->{head_element};
7593    
# Line 6602  sub set_inner_html ($$$) { Line 7633  sub set_inner_html ($$$) {
7633      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7634    
7635      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7636    
7637        delete $p->{parse_error}; # delete loop
7638    } else {    } else {
7639      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";
7640    }    }

Legend:
Removed from v.1.120  
changed lines
  Added in v.1.154

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24