/[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.121 by wakaba, Thu Mar 20 08:04:58 2008 UTC revision 1.149 by wakaba, Sun May 25 08:53:49 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributename => 'attributeName',
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      filterres => 'filterRes',
226      filterunits => 'filterUnits',
227      glyphref => 'glyphRef',
228      gradienttransform => 'gradientTransform',
229      gradientunits => 'gradientUnits',
230      kernelmatrix => 'kernelMatrix',
231      kernelunitlength => 'kernelUnitLength',
232      keypoints => 'keyPoints',
233      keysplines => 'keySplines',
234      keytimes => 'keyTimes',
235      lengthadjust => 'lengthAdjust',
236      limitingconeangle => 'limitingConeAngle',
237      markerheight => 'markerHeight',
238      markerunits => 'markerUnits',
239      markerwidth => 'markerWidth',
240      maskcontentunits => 'maskContentUnits',
241      maskunits => 'maskUnits',
242      numoctaves => 'numOctaves',
243      pathlength => 'pathLength',
244      patterncontentunits => 'patternContentUnits',
245      patterntransform => 'patternTransform',
246      patternunits => 'patternUnits',
247      pointsatx => 'pointsAtX',
248      pointsaty => 'pointsAtY',
249      pointsatz => 'pointsAtZ',
250      preservealpha => 'preserveAlpha',
251      preserveaspectratio => 'preserveAspectRatio',
252      primitiveunits => 'primitiveUnits',
253      refx => 'refX',
254      refy => 'refY',
255      repeatcount => 'repeatCount',
256      repeatdur => 'repeatDur',
257      requiredextensions => 'requiredExtensions',
258      requiredfeatures => 'requiredFeatures',
259      specularconstant => 'specularConstant',
260      specularexponent => 'specularExponent',
261      spreadmethod => 'spreadMethod',
262      startoffset => 'startOffset',
263      stddeviation => 'stdDeviation',
264      stitchtiles => 'stitchTiles',
265      surfacescale => 'surfaceScale',
266      systemlanguage => 'systemLanguage',
267      tablevalues => 'tableValues',
268      targetx => 'targetX',
269      targety => 'targetY',
270      textlength => 'textLength',
271      viewbox => 'viewBox',
272      viewtarget => 'viewTarget',
273      xchannelselector => 'xChannelSelector',
274      ychannelselector => 'yChannelSelector',
275      zoomandpan => 'zoomAndPan',
276  };  };
277    
278    my $foreign_attr_xname = {
279      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
280      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
281      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
282      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
283      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
284      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
285      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
286      'xml:base' => [$XML_NS, ['xml', 'base']],
287      'xml:lang' => [$XML_NS, ['xml', 'lang']],
288      'xml:space' => [$XML_NS, ['xml', 'space']],
289      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
290      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
291    };
292    
293    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
294    
295  my $c1_entity_char = {  my $c1_entity_char = {
296    0x80 => 0x20AC,    0x80 => 0x20AC,
297    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 327  my $c1_entity_char = {
327    0x9F => 0x0178,    0x9F => 0x0178,
328  }; # $c1_entity_char  }; # $c1_entity_char
329    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
330  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
331      my $self = shift;
332      my $charset_name = shift;
333      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
334      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
335    } # parse_byte_string
336    
337    sub parse_byte_stream ($$$$;$) {
338    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
339    my $charset = shift;    my $charset_name = shift;
340    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
341    
342    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
343      my $self = shift;      my (%opt) = @_;
344      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
345      my $token = shift;    };
346      ## TODO: if $charset is supported    $self->{parse_error} = $onerror; # updated later by parse_char_string
347      ## TODO: normalize charset name  
348      ## HTML5 encoding sniffing algorithm
349      require Message::Charset::Info;
350      my $charset;
351      my $buffer;
352      my ($char_stream, $e_status);
353    
354      ## "Change the encoding" algorithm:    SNIFFING: {
355    
356      ## Step 1          ## Step 1
357      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?      if (defined $charset_name) {
358        $charset = 'utf-8';        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
359    
360          ## ISSUE: Unsupported encoding is not ignored according to the spec.
361          ($char_stream, $e_status) = $charset->get_decode_handle
362              ($byte_stream, allow_error_reporting => 1,
363               allow_fallback => 1);
364          if ($char_stream) {
365            $self->{confident} = 1;
366            last SNIFFING;
367          } else {
368            ## TODO: unsupported error
369          }
370      }      }
371    
372      ## Step 2      ## Step 2
373      if (defined $self->{input_encoding} and      my $byte_buffer = '';
374          $self->{input_encoding} eq $charset) {      for (1..1024) {
375          my $char = $byte_stream->getc;
376          last unless defined $char;
377          $byte_buffer .= $char;
378        } ## TODO: timeout
379    
380        ## Step 3
381        if ($byte_buffer =~ /^\xFE\xFF/) {
382          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
383          ($char_stream, $e_status) = $charset->get_decode_handle
384              ($byte_stream, allow_error_reporting => 1,
385               allow_fallback => 1, byte_buffer => \$byte_buffer);
386        $self->{confident} = 1;        $self->{confident} = 1;
387        return;        last SNIFFING;
388        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
389          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
390          ($char_stream, $e_status) = $charset->get_decode_handle
391              ($byte_stream, allow_error_reporting => 1,
392               allow_fallback => 1, byte_buffer => \$byte_buffer);
393          $self->{confident} = 1;
394          last SNIFFING;
395        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
396          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
397          ($char_stream, $e_status) = $charset->get_decode_handle
398              ($byte_stream, allow_error_reporting => 1,
399               allow_fallback => 1, byte_buffer => \$byte_buffer);
400          $self->{confident} = 1;
401          last SNIFFING;
402      }      }
403    
404      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
405          ':'.$charset, level => 'w', token => $token);      ## TODO: <meta charset>
406    
407      ## Step 3      ## Step 5
408      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
409    
410      ## Step 4      ## Step 6
411      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
412        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
413            ($byte_buffer);
414        if (defined $charset_name) {
415          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
416    
417          ## ISSUE: Unsupported encoding is not ignored according to the spec.
418          require Whatpm::Charset::DecodeHandle;
419          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
420              ($byte_stream);
421          ($char_stream, $e_status) = $charset->get_decode_handle
422              ($buffer, allow_error_reporting => 1,
423               allow_fallback => 1, byte_buffer => \$byte_buffer);
424          if ($char_stream) {
425            $buffer->{buffer} = $byte_buffer;
426            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
427                            value => $charset_name,
428                            level => $self->{info_level},
429                            line => 1, column => 1);
430            $self->{confident} = 0;
431            last SNIFFING;
432          }
433        }
434    
435        ## Step 7: default
436        ## TODO: Make this configurable.
437        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
438            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
439            ## detectable in the step 6.
440        require Whatpm::Charset::DecodeHandle;
441        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
442            ($byte_stream);
443        ($char_stream, $e_status)
444            = $charset->get_decode_handle ($buffer,
445                                           allow_error_reporting => 1,
446                                           allow_fallback => 1,
447                                           byte_buffer => \$byte_buffer);
448        $buffer->{buffer} = $byte_buffer;
449        !!!parse-error (type => 'sniffing:default', ## TODO: type name
450                        value => 'windows-1252',
451                        level => $self->{info_level},
452                        line => 1, column => 1);
453        $self->{confident} = 0;
454      } # SNIFFING
455    
456      $self->{input_encoding} = $charset->get_iana_name;
457      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
458        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
459                        value => $self->{input_encoding},
460                        level => $self->{unsupported_level},
461                        line => 1, column => 1);
462      } elsif (not ($e_status &
463                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
464        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      }
469    
470      $self->{change_encoding} = sub {
471        my $self = shift;
472        $charset_name = shift;
473        my $token = shift;
474    
475        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
476        ($char_stream, $e_status) = $charset->get_decode_handle
477            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
478             byte_buffer => \ $buffer->{buffer});
479        
480        if ($char_stream) { # if supported
481          ## "Change the encoding" algorithm:
482    
483          ## Step 1    
484          if ($charset->{category} &
485              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
486            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
487            ($char_stream, $e_status) = $charset->get_decode_handle
488                ($byte_stream,
489                 byte_buffer => \ $buffer->{buffer});
490          }
491          $charset_name = $charset->get_iana_name;
492          
493          ## Step 2
494          if (defined $self->{input_encoding} and
495              $self->{input_encoding} eq $charset_name) {
496            !!!parse-error (type => 'charset label:matching', ## TODO: type
497                            value => $charset_name,
498                            level => $self->{info_level});
499            $self->{confident} = 1;
500            return;
501          }
502    
503          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
504              ':'.$charset_name, level => 'w', token => $token);
505          
506          ## Step 3
507          # if (can) {
508            ## change the encoding on the fly.
509            #$self->{confident} = 1;
510            #return;
511          # }
512          
513          ## Step 4
514          throw Whatpm::HTML::RestartParser ();
515        }
516    }; # $self->{change_encoding}    }; # $self->{change_encoding}
517    
518      my $char_onerror = sub {
519        my (undef, $type, %opt) = @_;
520        !!!parse-error (%opt, type => $type,
521                        line => $self->{line}, column => $self->{column} + 1);
522        if ($opt{octets}) {
523          ${$opt{octets}} = "\x{FFFD}"; # relacement character
524        }
525      };
526      $char_stream->onerror ($char_onerror);
527    
528    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
529    my $return;    my $return;
530    try {    try {
531      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
532    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
533      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
534      $s = \ (Encode::decode ($charset, $$bytes_s));      
535      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
536        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
537          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
538                          value => $self->{input_encoding},
539                          level => $self->{unsupported_level},
540                          line => 1, column => 1);
541        } elsif (not ($e_status &
542                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
543          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
544                          value => $self->{input_encoding},
545                          level => $self->{unsupported_level},
546                          line => 1, column => 1);
547        }
548      $self->{confident} = 1;      $self->{confident} = 1;
549      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
550        $return = $self->parse_char_stream ($char_stream, @args);
551    };    };
552    return $return;    return $return;
553  } # parse_byte_string  } # parse_byte_stream
554    
555  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
556  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 163  sub parse_byte_string ($$$$;$) { Line 561  sub parse_byte_string ($$$$;$) {
561  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
562  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
563    
564  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
565      my $self = shift;
566      require utf8;
567      my $s = ref $_[0] ? $_[0] : \($_[0]);
568      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
569      return $self->parse_char_stream ($input, @_[1..$#_]);
570    } # parse_char_string
571    *parse_string = \&parse_char_string;
572    
573  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
574    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
575    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
576    $self->{document} = $_[1];    $self->{document} = $_[1];
577    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
578    
# Line 186  sub parse_string ($$$;$) { Line 591  sub parse_string ($$$;$) {
591      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
592      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
593    
594      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
595      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
596          $char = $self->{next_next_char};
597          delete $self->{next_next_char};
598        } else {
599          $char = $input->getc;
600        }
601        $self->{next_char} = -1 and return unless defined $char;
602        $self->{next_char} = ord $char;
603    
604      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
605          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
606      $self->{column}++;      $self->{column}++;
607            
608      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
609          !!!cp ('j1');
610        $self->{line}++;        $self->{line}++;
611        $self->{column} = 0;        $self->{column} = 0;
612      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
613        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
614          my $next = $input->getc;
615          if (defined $next and $next ne "\x0A") {
616            $self->{next_next_char} = $next;
617          }
618        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
619        $self->{line}++;        $self->{line}++;
620        $self->{column} = 0;        $self->{column} = 0;
621      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
622          !!!cp ('j3');
623        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
624      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
625          !!!cp ('j4');
626        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
627        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
628        } elsif ($self->{next_char} <= 0x0008 or
629                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
630                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
631                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
632                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
633                 {
634                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
635                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
636                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
637                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
638                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
639                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
640                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
641                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
642                  0x10FFFE => 1, 0x10FFFF => 1,
643                 }->{$self->{next_char}}) {
644          !!!cp ('j5');
645          !!!parse-error (type => 'control char', level => $self->{must_level});
646    ## TODO: error type documentation
647      }      }
648    };    };
649    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 667  sub parse_string ($$$;$) {
667    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
668    
669    return $self->{document};    return $self->{document};
670  } # parse_string  } # parse_char_stream
671    
672  sub new ($) {  sub new ($) {
673    my $class = shift;    my $class = shift;
674    my $self = bless {}, $class;    my $self = bless {
675        must_level => 'm',
676        should_level => 's',
677        good_level => 'w',
678        warn_level => 'w',
679        info_level => 'i',
680        unsupported_level => 'u',
681      }, $class;
682    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
683      $self->{next_char} = -1;      $self->{next_char} = -1;
684    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 740  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
740  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
741  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
742  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
743    sub SELF_CLOSING_START_TAG_STATE () { 34 }
744    sub CDATA_BLOCK_STATE () { 35 }
745    
746  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
747  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 759  sub ROW_IMS ()        { 0b10000000 }
759  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
760  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
761  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
762    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
763        ## NOTE: "in foreign content" insertion mode is special; it is combined
764        ## with the secondary insertion mode.  In this parser, they are stored
765        ## together in the bit-or'ed form.
766    
767  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
768    
# Line 348  sub _initialize_tokenizer ($) { Line 799  sub _initialize_tokenizer ($) {
799    undef $self->{current_attribute};    undef $self->{current_attribute};
800    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
801    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
802      delete $self->{self_closing};
803    $self->{char} = [];    $self->{char} = [];
804    # $self->{next_char}    # $self->{next_char}
805    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 820  sub _initialize_tokenizer ($) {
820  ##        ->{value}  ##        ->{value}
821  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
822  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
823    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
824    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
825    ##     while the token is pushed back to the stack.
826    
827    ## ISSUE: "When a DOCTYPE token is created, its
828    ## <i>self-closing flag</i> must be unset (its other state is that it
829    ## be set), and its attributes list must be empty.": Wrong subject?
830    
831  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
832    
# Line 394  sub _initialize_tokenizer ($) { Line 853  sub _initialize_tokenizer ($) {
853    
854  sub _get_next_token ($) {  sub _get_next_token ($) {
855    my $self = shift;    my $self = shift;
856    
857      if ($self->{self_closing}) {
858        !!!parse-error (type => 'nestc', token => $self->{current_token});
859        ## NOTE: The |self_closing| flag is only set by start tag token.
860        ## In addition, when a start tag token is emitted, it is always set to
861        ## |current_token|.
862        delete $self->{self_closing};
863      }
864    
865    if (@{$self->{token}}) {    if (@{$self->{token}}) {
866        $self->{self_closing} = $self->{token}->[0]->{self_closing};
867      return shift @{$self->{token}};      return shift @{$self->{token}};
868    }    }
869    
# Line 574  sub _get_next_token ($) { Line 1043  sub _get_next_token ($) {
1043            redo A;            redo A;
1044          } else {          } else {
1045            !!!cp (23);            !!!cp (23);
1046            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1047                              line => $self->{line_prev},
1048                              column => $self->{column_prev});
1049            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1050            ## reconsume            ## reconsume
1051    
# Line 765  sub _get_next_token ($) { Line 1236  sub _get_next_token ($) {
1236    
1237          redo A;          redo A;
1238        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1239            !!!cp (42);
1240            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1241          !!!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  
1242          redo A;          redo A;
1243        } else {        } else {
1244          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1290  sub _get_next_token ($) {
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1293            !!!cp (50);
1294            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1295          !!!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  
1296          redo A;          redo A;
1297        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1298          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 942  sub _get_next_token ($) { Line 1393  sub _get_next_token ($) {
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
1395        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1396            !!!cp (64);
1397          $before_leave->();          $before_leave->();
1398            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1399          !!!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  
1400          redo A;          redo A;
1401        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1402          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1483  sub _get_next_token ($) {
1483          !!!next-input-character;          !!!next-input-character;
1484          redo A;          redo A;
1485        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1486            !!!cp (77);
1487            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1488          !!!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  
1489          redo A;          redo A;
1490        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1491          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1388  sub _get_next_token ($) { Line 1818  sub _get_next_token ($) {
1818    
1819          redo A;          redo A;
1820        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1821            !!!cp (122);
1822            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1823          !!!next-input-character;          !!!next-input-character;
1824          if ($self->{next_char} == 0x003E and # >          redo A;
1825              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1826              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1827            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1828            !!!cp (122);            !!!cp (122.3);
1829            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1830            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1831              if ($self->{current_token}->{attributes}) {
1832                !!!cp (122.1);
1833                !!!parse-error (type => 'end tag attribute');
1834              } else {
1835                ## NOTE: This state should never be reached.
1836                !!!cp (122.2);
1837              }
1838          } else {          } else {
1839            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1840          }          }
1841          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1842          # next-input-character is already done          ## Reconsume.
1843            !!!emit ($self->{current_token}); # start tag or end tag
1844          redo A;          redo A;
1845        } else {        } else {
1846          !!!cp (124);          !!!cp ('124.1');
1847          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1848          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1849          ## reconsume          ## reconsume
1850          redo A;          redo A;
1851        }        }
1852        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1853          if ($self->{next_char} == 0x003E) { # >
1854            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1855              !!!cp ('124.2');
1856              !!!parse-error (type => 'nestc', token => $self->{current_token});
1857              ## TODO: Different type than slash in start tag
1858              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1859              if ($self->{current_token}->{attributes}) {
1860                !!!cp ('124.4');
1861                !!!parse-error (type => 'end tag attribute');
1862              } else {
1863                !!!cp ('124.5');
1864              }
1865              ## TODO: Test |<title></title/>|
1866            } else {
1867              !!!cp ('124.3');
1868              $self->{self_closing} = 1;
1869            }
1870    
1871            $self->{state} = DATA_STATE;
1872            !!!next-input-character;
1873    
1874            !!!emit ($self->{current_token}); # start tag or end tag
1875    
1876            redo A;
1877          } elsif ($self->{next_char} == -1) {
1878            !!!parse-error (type => 'unclosed tag');
1879            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1880              !!!cp (124.7);
1881              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1882            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1883              if ($self->{current_token}->{attributes}) {
1884                !!!cp (124.5);
1885                !!!parse-error (type => 'end tag attribute');
1886              } else {
1887                ## NOTE: This state should never be reached.
1888                !!!cp (124.6);
1889              }
1890            } else {
1891              die "$0: $self->{current_token}->{type}: Unknown token type";
1892            }
1893            $self->{state} = DATA_STATE;
1894            ## Reconsume.
1895            !!!emit ($self->{current_token}); # start tag or end tag
1896            redo A;
1897          } else {
1898            !!!cp ('124.4');
1899            !!!parse-error (type => 'nestc');
1900            ## TODO: This error type is wrong.
1901            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1902            ## Reconsume.
1903            redo A;
1904          }
1905      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1906        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1907                
# Line 1516  sub _get_next_token ($) { Line 2009  sub _get_next_token ($) {
2009          } else {          } else {
2010            !!!cp (135);            !!!cp (135);
2011          }          }
2012          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2013                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2014                   $self->{next_char} == 0x005B) { # [
2015            !!!next-input-character;
2016            push @next_char, $self->{next_char};
2017            if ($self->{next_char} == 0x0043) { # C
2018              !!!next-input-character;
2019              push @next_char, $self->{next_char};
2020              if ($self->{next_char} == 0x0044) { # D
2021                !!!next-input-character;
2022                push @next_char, $self->{next_char};
2023                if ($self->{next_char} == 0x0041) { # A
2024                  !!!next-input-character;
2025                  push @next_char, $self->{next_char};
2026                  if ($self->{next_char} == 0x0054) { # T
2027                    !!!next-input-character;
2028                    push @next_char, $self->{next_char};
2029                    if ($self->{next_char} == 0x0041) { # A
2030                      !!!next-input-character;
2031                      push @next_char, $self->{next_char};
2032                      if ($self->{next_char} == 0x005B) { # [
2033                        !!!cp (135.1);
2034                        $self->{state} = CDATA_BLOCK_STATE;
2035                        !!!next-input-character;
2036                        redo A;
2037                      } else {
2038                        !!!cp (135.2);
2039                      }
2040                    } else {
2041                      !!!cp (135.3);
2042                    }
2043                  } else {
2044                    !!!cp (135.4);                
2045                  }
2046                } else {
2047                  !!!cp (135.5);
2048                }
2049              } else {
2050                !!!cp (135.6);
2051              }
2052            } else {
2053              !!!cp (135.7);
2054            }
2055        } else {        } else {
2056          !!!cp (136);          !!!cp (136);
2057        }        }
# Line 2198  sub _get_next_token ($) { Line 2734  sub _get_next_token ($) {
2734          redo A;          redo A;
2735        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2736          !!!cp (217);          !!!cp (217);
         !!!parse-error (type => 'unclosed DOCTYPE');  
2737    
2738          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2739          ## reconsume          ## reconsume
# Line 2240  sub _get_next_token ($) { Line 2775  sub _get_next_token ($) {
2775          !!!next-input-character;          !!!next-input-character;
2776          redo A;          redo A;
2777        }        }
2778        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2779          my $s = '';
2780          
2781          my ($l, $c) = ($self->{line}, $self->{column});
2782    
2783          CS: while ($self->{next_char} != -1) {
2784            if ($self->{next_char} == 0x005D) { # ]
2785              !!!next-input-character;
2786              if ($self->{next_char} == 0x005D) { # ]
2787                !!!next-input-character;
2788                MDC: {
2789                  if ($self->{next_char} == 0x003E) { # >
2790                    !!!cp (221.1);
2791                    !!!next-input-character;
2792                    last CS;
2793                  } elsif ($self->{next_char} == 0x005D) { # ]
2794                    !!!cp (221.2);
2795                    $s .= ']';
2796                    !!!next-input-character;
2797                    redo MDC;
2798                  } else {
2799                    !!!cp (221.3);
2800                    $s .= ']]';
2801                    #
2802                  }
2803                } # MDC
2804              } else {
2805                !!!cp (221.4);
2806                $s .= ']';
2807                #
2808              }
2809            } else {
2810              !!!cp (221.5);
2811              #
2812            }
2813            $s .= chr $self->{next_char};
2814            !!!next-input-character;
2815          } # CS
2816    
2817          $self->{state} = DATA_STATE;
2818          ## next-input-character done or EOF, which is reconsumed.
2819    
2820          if (length $s) {
2821            !!!cp (221.6);
2822            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2823                      line => $l, column => $c});
2824          } else {
2825            !!!cp (221.7);
2826          }
2827    
2828          redo A;
2829    
2830          ## ISSUE: "text tokens" in spec.
2831          ## TODO: Streaming support
2832      } else {      } else {
2833        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2834      }      }
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 2979  sub _tokenize_attempt_to_consume_an_enti
2979      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2980      our $EntityChar;      our $EntityChar;
2981    
2982      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2983             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2984             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2985               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2532  sub _tree_construction_initial ($) { Line 3121  sub _tree_construction_initial ($) {
3121                
3122        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3123          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3124          ## NOTE: Default value for both |public_id| and |system_id| attributes
3125          ## are empty strings, so that we don't set any value in missing cases.
3126        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3127            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3128        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2546  sub _tree_construction_initial ($) { Line 3137  sub _tree_construction_initial ($) {
3137        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3138          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3139          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3140          if ({          my $prefix = [
3141            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3142            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3143            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3144            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3145            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3146            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3147            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3148            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3149            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3150            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3151            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3152            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3153            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3154            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3155            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3156            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3157            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3158            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3159            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3160            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3161            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3162            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3163            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3164            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3165            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3166            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3167            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3168            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3169            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3170            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3171            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3172            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3173            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3174            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3175            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3176            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3177            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3178            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3179            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3180            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3181            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3182            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3183            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3185            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3186            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3187            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3188            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3189            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3190            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3191            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3192            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3193            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3194            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3195            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3196            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3197            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3198            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3199            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3200            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3201            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3202            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3203            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3204            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3205            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3206            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3207            "-//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}) {  
3208            !!!cp ('t5');            !!!cp ('t5');
3209            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3210          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3211                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3212            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3213              !!!cp ('t6');              !!!cp ('t6');
3214              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2631  sub _tree_construction_initial ($) { Line 3216  sub _tree_construction_initial ($) {
3216              !!!cp ('t7');              !!!cp ('t7');
3217              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3218            }            }
3219          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3220                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3221            !!!cp ('t8');            !!!cp ('t8');
3222            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3223          } else {          } else {
# Line 2645  sub _tree_construction_initial ($) { Line 3230  sub _tree_construction_initial ($) {
3230          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3231          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3232          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") {
3233            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3234              ## marked as quirks.
3235            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3236            !!!cp ('t11');            !!!cp ('t11');
3237          } else {          } else {
# Line 2668  sub _tree_construction_initial ($) { Line 3254  sub _tree_construction_initial ($) {
3254        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3255        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3256        ## reprocess        ## reprocess
3257          !!!ack-later;
3258        return;        return;
3259      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3260        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 3335  sub _tree_construction_root_element ($)
3335        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3336          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3337            my $root_element;            my $root_element;
3338            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3339            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3340            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3341                  [$root_element, $el_category->{html}];
3342    
3343            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3344              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3353  sub _tree_construction_root_element ($)
3353              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3354            }            }
3355    
3356              !!!nack ('t25c');
3357    
3358            !!!next-token;            !!!next-token;
3359            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3360          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3371  sub _tree_construction_root_element ($)
3371          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3372        }        }
3373    
3374      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3375        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3376      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3377      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3378    
3379      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3380    
3381      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3382        !!!ack-later;
3383      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3384    
3385      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3403  sub _reset_insertion_mode ($) {
3403        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3404          $last = 1;          $last = 1;
3405          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3406            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3407                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3408              !!!cp ('t27');          } else {
3409              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3410          }          }
3411        }        }
3412              
3413        ## Step 4..13        ## Step 4..14
3414        my $new_mode = {        my $new_mode;
3415          if ($node->[1] & FOREIGN_EL) {
3416            !!!cp ('t28.1');
3417            ## NOTE: Strictly spaking, the line below only applies to MathML and
3418            ## SVG elements.  Currently the HTML syntax supports only MathML and
3419            ## SVG elements as foreigners.
3420            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3421          } elsif ($node->[1] & TABLE_CELL_EL) {
3422            if ($last) {
3423              !!!cp ('t28.2');
3424              #
3425            } else {
3426              !!!cp ('t28.3');
3427              $new_mode = IN_CELL_IM;
3428            }
3429          } else {
3430            !!!cp ('t28.4');
3431            $new_mode = {
3432                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3433                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3434                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3435                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3436                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3437                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2839  sub _reset_insertion_mode ($) { Line 3442  sub _reset_insertion_mode ($) {
3442                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3443                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3444                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3445                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3446          }
3447        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3448                
3449        ## Step 14        ## Step 15
3450        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3451          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3452            !!!cp ('t29');            !!!cp ('t29');
3453            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3461  sub _reset_insertion_mode ($) {
3461          !!!cp ('t31');          !!!cp ('t31');
3462        }        }
3463                
3464        ## Step 15        ## Step 16
3465        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3466                
3467        ## Step 16        ## Step 17
3468        $i--;        $i--;
3469        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3470                
3471        ## Step 17        ## Step 18
3472        redo S3;        redo S3;
3473      } # S3      } # S3
3474    
# Line 2976  sub _tree_construction_main ($) { Line 3580  sub _tree_construction_main ($) {
3580      ## Step 1      ## Step 1
3581      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3582      my $el;      my $el;
3583      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3584    
3585      ## Step 2      ## Step 2
3586      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
3591    
3592      ## Step 4      ## Step 4
3593      my $text = '';      my $text = '';
3594        !!!nack ('t40.1');
3595      !!!next-token;      !!!next-token;
3596      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3597        !!!cp ('t40');        !!!cp ('t40');
# Line 3026  sub _tree_construction_main ($) { Line 3631  sub _tree_construction_main ($) {
3631    
3632    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3633      my $script_el;      my $script_el;
3634      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3635      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3636    
3637      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3638      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3639            
3640      my $text = '';      my $text = '';
3641        !!!nack ('t45.1');
3642      !!!next-token;      !!!next-token;
3643      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3644        !!!cp ('t45');        !!!cp ('t45');
# Line 3090  sub _tree_construction_main ($) { Line 3696  sub _tree_construction_main ($) {
3696        my $formatting_element;        my $formatting_element;
3697        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3698        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3699          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3700              !!!cp ('t52');
3701              last AFE;
3702            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3703                         eq $tag_name) {
3704            !!!cp ('t51');            !!!cp ('t51');
3705            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3706            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3707            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3708          }          }
3709        } # AFE        } # AFE
3710        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3125  sub _tree_construction_main ($) { Line 3732  sub _tree_construction_main ($) {
3732              !!!next-token;              !!!next-token;
3733              return;              return;
3734            }            }
3735          } 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]}) {  
3736            !!!cp ('t56');            !!!cp ('t56');
3737            $in_scope = 0;            $in_scope = 0;
3738          }          }
# Line 3143  sub _tree_construction_main ($) { Line 3747  sub _tree_construction_main ($) {
3747        }        }
3748        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3749          !!!cp ('t58');          !!!cp ('t58');
3750          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3751                            value => $self->{open_elements}->[-1]->[0]
3752                                ->manakai_local_name,
3753                          token => $end_tag_token);                          token => $end_tag_token);
3754        }        }
3755                
# Line 3152  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3759        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3760          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3761          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3762              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3763              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3764               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3765            !!!cp ('t59');            !!!cp ('t59');
3766            $furthest_block = $node;            $furthest_block = $node;
3767            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3847  sub _tree_construction_main ($) {
3847        } # S7          } # S7  
3848                
3849        ## Step 8        ## Step 8
3850        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3851          my $foster_parent_element;          my $foster_parent_element;
3852          my $next_sibling;          my $next_sibling;
3853                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3854                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3855                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3856                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3857                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3924  sub _tree_construction_main ($) {
3924    
3925    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3926      my $child = shift;      my $child = shift;
3927      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]}) {  
3928        # MUST        # MUST
3929        my $foster_parent_element;        my $foster_parent_element;
3930        my $next_sibling;        my $next_sibling;
3931                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3932                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3933                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3934                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3935                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 3954  sub _tree_construction_main ($) {
3954      }      }
3955    }; # $insert_to_foster    }; # $insert_to_foster
3956    
3957    B: {    B: while (1) {
3958      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3959        !!!cp ('t73');        !!!cp ('t73');
3960        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3961        ## Ignore the token        ## Ignore the token
3962        ## Stay in the phase        ## Stay in the phase
3963        !!!next-token;        !!!next-token;
3964        redo B;        next B;
3965      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3966               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3967        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3385  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3988          }          }
3989        }        }
3990          !!!nack ('t84.1');
3991        !!!next-token;        !!!next-token;
3992        redo B;        next B;
3993      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3994        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3995        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4004        }        }
4005        !!!next-token;        !!!next-token;
4006        redo B;        next B;
4007      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4008          if ($token->{type} == CHARACTER_TOKEN) {
4009            !!!cp ('t87.1');
4010            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4011            !!!next-token;
4012            next B;
4013          } elsif ($token->{type} == START_TAG_TOKEN) {
4014            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4015                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4016                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4017                ($token->{tag_name} eq 'svg' and
4018                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4019              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4020              !!!cp ('t87.2');
4021              #
4022            } elsif ({
4023                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4024                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4025                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4026                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4027                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4028                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4029                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4030                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4031                     }->{$token->{tag_name}}) {
4032              !!!cp ('t87.2');
4033              !!!parse-error (type => 'not closed',
4034                              value => $self->{open_elements}->[-1]->[0]
4035                                  ->manakai_local_name,
4036                              token => $token);
4037    
4038              pop @{$self->{open_elements}}
4039                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4040    
4041              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4042              ## Reprocess.
4043              next B;
4044            } else {
4045              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4046              my $tag_name = $token->{tag_name};
4047              if ($nsuri eq $SVG_NS) {
4048                $tag_name = {
4049                   altglyph => 'altGlyph',
4050                   altglyphdef => 'altGlyphDef',
4051                   altglyphitem => 'altGlyphItem',
4052                   animatecolor => 'animateColor',
4053                   animatemotion => 'animateMotion',
4054                   animatetransform => 'animateTransform',
4055                   clippath => 'clipPath',
4056                   feblend => 'feBlend',
4057                   fecolormatrix => 'feColorMatrix',
4058                   fecomponenttransfer => 'feComponentTransfer',
4059                   fecomposite => 'feComposite',
4060                   feconvolvematrix => 'feConvolveMatrix',
4061                   fediffuselighting => 'feDiffuseLighting',
4062                   fedisplacementmap => 'feDisplacementMap',
4063                   fedistantlight => 'feDistantLight',
4064                   feflood => 'feFlood',
4065                   fefunca => 'feFuncA',
4066                   fefuncb => 'feFuncB',
4067                   fefuncg => 'feFuncG',
4068                   fefuncr => 'feFuncR',
4069                   fegaussianblur => 'feGaussianBlur',
4070                   feimage => 'feImage',
4071                   femerge => 'feMerge',
4072                   femergenode => 'feMergeNode',
4073                   femorphology => 'feMorphology',
4074                   feoffset => 'feOffset',
4075                   fepointlight => 'fePointLight',
4076                   fespecularlighting => 'feSpecularLighting',
4077                   fespotlight => 'feSpotLight',
4078                   fetile => 'feTile',
4079                   feturbulence => 'feTurbulence',
4080                   foreignobject => 'foreignObject',
4081                   glyphref => 'glyphRef',
4082                   lineargradient => 'linearGradient',
4083                   radialgradient => 'radialGradient',
4084                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4085                   textpath => 'textPath',  
4086                }->{$tag_name} || $tag_name;
4087              }
4088    
4089              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4090    
4091              ## "adjust foreign attributes" - done in insert-element-f
4092    
4093              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4094    
4095              if ($self->{self_closing}) {
4096                pop @{$self->{open_elements}};
4097                !!!ack ('t87.3');
4098              } else {
4099                !!!cp ('t87.4');
4100              }
4101    
4102              !!!next-token;
4103              next B;
4104            }
4105          } elsif ($token->{type} == END_TAG_TOKEN) {
4106            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4107            !!!cp ('t87.5');
4108            #
4109          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4110            !!!cp ('t87.6');
4111            !!!parse-error (type => 'not closed',
4112                            value => $self->{open_elements}->[-1]->[0]
4113                                ->manakai_local_name,
4114                            token => $token);
4115    
4116            pop @{$self->{open_elements}}
4117                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4118    
4119            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4120            ## Reprocess.
4121            next B;
4122          } else {
4123            die "$0: $token->{type}: Unknown token type";        
4124          }
4125        }
4126    
4127        if ($self->{insertion_mode} & HEAD_IMS) {
4128        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4129          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4130            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4134  sub _tree_construction_main ($) {
4134              !!!cp ('t88.1');              !!!cp ('t88.1');
4135              ## Ignore the token.              ## Ignore the token.
4136              !!!next-token;              !!!next-token;
4137              redo B;              next B;
4138            }            }
4139            unless (length $token->{data}) {            unless (length $token->{data}) {
4140              !!!cp ('t88');              !!!cp ('t88');
4141              !!!next-token;              !!!next-token;
4142              redo B;              next B;
4143            }            }
4144          }          }
4145    
4146          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4147            !!!cp ('t89');            !!!cp ('t89');
4148            ## As if <head>            ## As if <head>
4149            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4150            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4151            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4152                  [$self->{head_element}, $el_category->{head}];
4153    
4154            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4155            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3451  sub _tree_construction_main ($) { Line 4175  sub _tree_construction_main ($) {
4175            !!!cp ('t92');            !!!cp ('t92');
4176          }          }
4177    
4178              ## "after head" insertion mode          ## "after head" insertion mode
4179              ## As if <body>          ## As if <body>
4180              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4181              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4182              ## reprocess          ## reprocess
4183              redo B;          next B;
4184            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4185              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4186                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4187                  !!!cp ('t93');              !!!cp ('t93');
4188                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4189                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4190                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4191                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4192                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4193                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4194                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4195                  !!!cp ('t94');              !!!next-token;
4196                  #              next B;
4197                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4198                  !!!cp ('t95');              !!!cp ('t93.2');
4199                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4200                  ## Ignore the token              ## Ignore the token
4201                  !!!next-token;              !!!nack ('t93.3');
4202                  redo B;              !!!next-token;
4203                }              next B;
4204              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {            } else {
4205                !!!cp ('t96');              !!!cp ('t95');
4206                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4207                !!!create-element ($self->{head_element}, 'head',, $token);              ## Ignore the token
4208                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4209                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4210                next B;
4211              }
4212            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4213              !!!cp ('t96');
4214              ## As if <head>
4215              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4216              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4217              push @{$self->{open_elements}},
4218                  [$self->{head_element}, $el_category->{head}];
4219    
4220                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4221                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4222              } else {          } else {
4223                !!!cp ('t97');            !!!cp ('t97');
4224              }          }
4225    
4226              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4227                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3507  sub _tree_construction_main ($) { Line 4240  sub _tree_construction_main ($) {
4240                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4241                  !!!cp ('t100');                  !!!cp ('t100');
4242                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4243                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4244                        [$self->{head_element}, $el_category->{head}];
4245                } else {                } else {
4246                  !!!cp ('t101');                  !!!cp ('t101');
4247                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4250                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4251                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4252                  !!!nack ('t101.1');
4253                !!!next-token;                !!!next-token;
4254                redo B;                next B;
4255              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4256                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4257                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4258                  !!!cp ('t102');                  !!!cp ('t102');
4259                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4260                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4261                        [$self->{head_element}, $el_category->{head}];
4262                } else {                } else {
4263                  !!!cp ('t103');                  !!!cp ('t103');
4264                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4266  sub _tree_construction_main ($) {
4266                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4267                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4268                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4269                  !!!ack ('t103.1');
4270                !!!next-token;                !!!next-token;
4271                redo B;                next B;
4272              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4273                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4274                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4275                  !!!cp ('t104');                  !!!cp ('t104');
4276                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4277                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4278                        [$self->{head_element}, $el_category->{head}];
4279                } else {                } else {
4280                  !!!cp ('t105');                  !!!cp ('t105');
4281                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4283  sub _tree_construction_main ($) {
4283                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.
4284    
4285                unless ($self->{confident}) {                unless ($self->{confident}) {
4286                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4287                    !!!cp ('t106');                    !!!cp ('t106');
4288                      ## NOTE: Whether the encoding is supported or not is handled
4289                      ## in the {change_encoding} callback.
4290                    $self->{change_encoding}                    $self->{change_encoding}
4291                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4292                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4296  sub _tree_construction_main ($) {
4296                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4297                                                 ->{has_reference});                                                 ->{has_reference});
4298                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4299                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4300                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4301                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4302                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4303                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4304                      !!!cp ('t107');                      !!!cp ('t107');
4305                        ## NOTE: Whether the encoding is supported or not is handled
4306                        ## in the {change_encoding} callback.
4307                      $self->{change_encoding}                      $self->{change_encoding}
4308                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4309                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4334  sub _tree_construction_main ($) {
4334    
4335                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4336                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4337                  !!!ack ('t110.1');
4338                !!!next-token;                !!!next-token;
4339                redo B;                next B;
4340              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4341                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4342                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3607  sub _tree_construction_main ($) { Line 4349  sub _tree_construction_main ($) {
4349                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4350                  !!!cp ('t112');                  !!!cp ('t112');
4351                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4352                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4353                        [$self->{head_element}, $el_category->{head}];
4354                } else {                } else {
4355                  !!!cp ('t113');                  !!!cp ('t113');
4356                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4361  sub _tree_construction_main ($) {
4361                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4362                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4363                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4364                redo B;                next B;
4365              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4366                         $token->{tag_name} eq 'noframes') {
4367                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4368                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4369                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4370                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4371                  !!!cp ('t114');                  !!!cp ('t114');
4372                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4373                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4374                        [$self->{head_element}, $el_category->{head}];
4375                } else {                } else {
4376                  !!!cp ('t115');                  !!!cp ('t115');
4377                }                }
4378                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4379                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4380                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4381                redo B;                next B;
4382              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4383                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4384                  !!!cp ('t116');                  !!!cp ('t116');
4385                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4386                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4387                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4388                    !!!nack ('t116.1');
4389                  !!!next-token;                  !!!next-token;
4390                  redo B;                  next B;
4391                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4392                  !!!cp ('t117');                  !!!cp ('t117');
4393                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4394                  ## Ignore the token                  ## Ignore the token
4395                    !!!nack ('t117.1');
4396                  !!!next-token;                  !!!next-token;
4397                  redo B;                  next B;
4398                } else {                } else {
4399                  !!!cp ('t118');                  !!!cp ('t118');
4400                  #                  #
# Line 3664  sub _tree_construction_main ($) { Line 4411  sub _tree_construction_main ($) {
4411                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4412                  !!!cp ('t120');                  !!!cp ('t120');
4413                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4414                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4415                        [$self->{head_element}, $el_category->{head}];
4416                } else {                } else {
4417                  !!!cp ('t121');                  !!!cp ('t121');
4418                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4421  sub _tree_construction_main ($) {
4421                $script_start_tag->();                $script_start_tag->();
4422                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4423                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4424                redo B;                next B;
4425              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4426                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4427                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3707  sub _tree_construction_main ($) { Line 4455  sub _tree_construction_main ($) {
4455                } else {                } else {
4456                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4457                }                }
4458                  !!!nack ('t127.1');
4459                !!!next-token;                !!!next-token;
4460                redo B;                next B;
4461              } else {              } else {
4462                !!!cp ('t128');                !!!cp ('t128');
4463                #                #
# Line 3740  sub _tree_construction_main ($) { Line 4489  sub _tree_construction_main ($) {
4489              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4490              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4491              ## reprocess              ## reprocess
4492              redo B;              !!!ack-later;
4493                next B;
4494            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4495              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4496                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4497                  !!!cp ('t132');                  !!!cp ('t132');
4498                  ## As if <head>                  ## As if <head>
4499                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4500                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4501                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4502                        [$self->{head_element}, $el_category->{head}];
4503    
4504                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4505                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4506                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4507                  !!!next-token;                  !!!next-token;
4508                  redo B;                  next B;
4509                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4510                  !!!cp ('t133');                  !!!cp ('t133');
4511                  ## As if </noscript>                  ## As if </noscript>
# Line 3765  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4517                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4518                  !!!next-token;                  !!!next-token;
4519                  redo B;                  next B;
4520                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4521                  !!!cp ('t134');                  !!!cp ('t134');
4522                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4523                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4524                  !!!next-token;                  !!!next-token;
4525                  redo B;                  next B;
4526                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4527                    !!!cp ('t134.1');
4528                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4529                    ## Ignore the token
4530                    !!!next-token;
4531                    next B;
4532                } else {                } else {
4533                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4534                }                }
4535              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4536                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3782  sub _tree_construction_main ($) { Line 4538  sub _tree_construction_main ($) {
4538                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4539                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4540                  !!!next-token;                  !!!next-token;
4541                  redo B;                  next B;
4542                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4543                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4544                  !!!cp ('t137');                  !!!cp ('t137');
4545                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4546                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4547                  !!!next-token;                  !!!next-token;
4548                  redo B;                  next B;
4549                } else {                } else {
4550                  !!!cp ('t138');                  !!!cp ('t138');
4551                  #                  #
# Line 3796  sub _tree_construction_main ($) { Line 4553  sub _tree_construction_main ($) {
4553              } elsif ({              } elsif ({
4554                        body => 1, html => 1,                        body => 1, html => 1,
4555                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4556                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4557                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4558                  ## 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) {  
4559                  !!!cp ('t140');                  !!!cp ('t140');
4560                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4561                  ## Ignore the token                  ## Ignore the token
4562                  !!!next-token;                  !!!next-token;
4563                  redo B;                  next B;
4564                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4565                    !!!cp ('t140.1');
4566                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4567                    ## Ignore the token
4568                    !!!next-token;
4569                    next B;
4570                } else {                } else {
4571                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4572                }                }
4573                              } elsif ($token->{tag_name} eq 'p') {
4574                #                !!!cp ('t142');
4575              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4576                        p => 1, br => 1,                ## Ignore the token
4577                       }->{$token->{tag_name}}) {                !!!next-token;
4578                  next B;
4579                } elsif ($token->{tag_name} eq 'br') {
4580                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4581                  !!!cp ('t142');                  !!!cp ('t142.2');
4582                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4583                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4584                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4585                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4586      
4587                    ## Reprocess in the "after head" insertion mode...
4588                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4589                    !!!cp ('t143.2');
4590                    ## As if </head>
4591                    pop @{$self->{open_elements}};
4592                    $self->{insertion_mode} = AFTER_HEAD_IM;
4593      
4594                    ## Reprocess in the "after head" insertion mode...
4595                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4596                    !!!cp ('t143.3');
4597                    ## ISSUE: Two parse errors for <head><noscript></br>
4598                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4599                    ## As if </noscript>
4600                    pop @{$self->{open_elements}};
4601                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4602    
4603                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4604                } else {                  ## As if </head>
4605                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4606                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4607    
4608                #                  ## Reprocess in the "after head" insertion mode...
4609              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4610                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4611                  #                  #
4612                } else {                } else {
4613                  !!!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;  
4614                }                }
4615    
4616                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4617                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4618                  ## Ignore the token
4619                  !!!next-token;
4620                  next B;
4621                } else {
4622                  !!!cp ('t145');
4623                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4624                  ## Ignore the token
4625                  !!!next-token;
4626                  next B;
4627              }              }
4628    
4629              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3869  sub _tree_construction_main ($) { Line 4649  sub _tree_construction_main ($) {
4649                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4650                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4651                !!!next-token;                !!!next-token;
4652                redo B;                next B;
4653              } else {              } else {
4654                !!!cp ('t149');                !!!cp ('t149');
4655              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4659  sub _tree_construction_main ($) {
4659              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4660              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4661              ## reprocess              ## reprocess
4662              redo B;              next B;
4663        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4664          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4665            !!!cp ('t149.1');            !!!cp ('t149.1');
4666    
4667            ## NOTE: As if <head>            ## NOTE: As if <head>
4668            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4669            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4670                ($self->{head_element});                ($self->{head_element});
4671            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4672              #    [$self->{head_element}, $el_category->{head}];
4673            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4674            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4675    
# Line 3932  sub _tree_construction_main ($) { Line 4713  sub _tree_construction_main ($) {
4713          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4714          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4715          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4716          redo B;          next B;
4717        } else {        } else {
4718          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4719        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4729    
4730              !!!next-token;              !!!next-token;
4731              redo B;              next B;
4732            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4733              if ({              if ({
4734                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4738  sub _tree_construction_main ($) {
4738                  ## have an element in table scope                  ## have an element in table scope
4739                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4740                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4741                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4742                      !!!cp ('t151');                      !!!cp ('t151');
4743    
4744                      ## Close the cell                      ## Close the cell
4745                      !!!back-token; # <?>                      !!!back-token; # <x>
4746                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4747                                  tag_name => $node->[0]->manakai_local_name,
4748                                line => $token->{line},                                line => $token->{line},
4749                                column => $token->{column}};                                column => $token->{column}};
4750                      redo B;                      next B;
4751                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4752                      !!!cp ('t152');                      !!!cp ('t152');
4753                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4754                      last;                      last;
# Line 3979  sub _tree_construction_main ($) { Line 4759  sub _tree_construction_main ($) {
4759                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4760                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4761                  ## Ignore the token                  ## Ignore the token
4762                    !!!nack ('t153.1');
4763                  !!!next-token;                  !!!next-token;
4764                  redo B;                  next B;
4765                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4766                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4767                                    
# Line 3990  sub _tree_construction_main ($) { Line 4771  sub _tree_construction_main ($) {
4771                  INSCOPE: {                  INSCOPE: {
4772                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4773                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4774                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4775                        !!!cp ('t155');                        !!!cp ('t155');
4776                        $i = $_;                        $i = $_;
4777                        last INSCOPE;                        last INSCOPE;
4778                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4779                        !!!cp ('t156');                        !!!cp ('t156');
4780                        last;                        last;
4781                      }                      }
# Line 4006  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4786                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4787                    ## Ignore the token                    ## Ignore the token
4788                      !!!nack ('t157.1');
4789                    !!!next-token;                    !!!next-token;
4790                    redo B;                    next B;
4791                  } # INSCOPE                  } # INSCOPE
4792                                    
4793                  ## generate implied end tags                  ## generate implied end tags
4794                  while ({                  while ($self->{open_elements}->[-1]->[1]
4795                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4796                    !!!cp ('t158');                    !!!cp ('t158');
4797                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4798                  }                  }
4799    
4800                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4801                    !!!cp ('t159');                    !!!cp ('t159');
4802                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4803                                      value => $self->{open_elements}->[-1]->[0]
4804                                          ->manakai_local_name,
4805                                      token => $token);
4806                  } else {                  } else {
4807                    !!!cp ('t160');                    !!!cp ('t160');
4808                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4814  sub _tree_construction_main ($) {
4814                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4815                                    
4816                  ## reprocess                  ## reprocess
4817                  redo B;                  !!!ack-later;
4818                    next B;
4819                } else {                } else {
4820                  !!!cp ('t161');                  !!!cp ('t161');
4821                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4831  sub _tree_construction_main ($) {
4831                  my $i;                  my $i;
4832                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4833                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4834                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4835                      !!!cp ('t163');                      !!!cp ('t163');
4836                      $i = $_;                      $i = $_;
4837                      last INSCOPE;                      last INSCOPE;
4838                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4839                      !!!cp ('t164');                      !!!cp ('t164');
4840                      last INSCOPE;                      last INSCOPE;
4841                    }                    }
# Line 4064  sub _tree_construction_main ($) { Line 4845  sub _tree_construction_main ($) {
4845                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4846                      ## Ignore the token                      ## Ignore the token
4847                      !!!next-token;                      !!!next-token;
4848                      redo B;                      next B;
4849                    }                    }
4850                                    
4851                  ## generate implied end tags                  ## generate implied end tags
4852                  while ({                  while ($self->{open_elements}->[-1]->[1]
4853                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4854                    !!!cp ('t166');                    !!!cp ('t166');
4855                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4856                  }                  }
4857    
4858                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4859                            ne $token->{tag_name}) {
4860                    !!!cp ('t167');                    !!!cp ('t167');
4861                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4862                                      value => $self->{open_elements}->[-1]->[0]
4863                                          ->manakai_local_name,
4864                                      token => $token);
4865                  } else {                  } else {
4866                    !!!cp ('t168');                    !!!cp ('t168');
4867                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4873  sub _tree_construction_main ($) {
4873                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4874                                    
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                  !!!cp ('t169');                  !!!cp ('t169');
4879                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4880                  ## Ignore the token                  ## Ignore the token
4881                  !!!next-token;                  !!!next-token;
4882                  redo B;                  next B;
4883                } else {                } else {
4884                  !!!cp ('t170');                  !!!cp ('t170');
4885                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891                  INSCOPE: {                  INSCOPE: {
4892                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4893                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4894                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4895                        !!!cp ('t171');                        !!!cp ('t171');
4896                        $i = $_;                        $i = $_;
4897                        last INSCOPE;                        last INSCOPE;
4898                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4899                        !!!cp ('t172');                        !!!cp ('t172');
4900                        last;                        last;
4901                      }                      }
# Line 4124  sub _tree_construction_main ($) { Line 4906  sub _tree_construction_main ($) {
4906                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4907                    ## Ignore the token                    ## Ignore the token
4908                    !!!next-token;                    !!!next-token;
4909                    redo B;                    next B;
4910                  } # INSCOPE                  } # INSCOPE
4911                                    
4912                  ## generate implied end tags                  ## generate implied end tags
4913                  while ({                  while ($self->{open_elements}->[-1]->[1]
4914                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4915                    !!!cp ('t174');                    !!!cp ('t174');
4916                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4917                  }                  }
4918                                    
4919                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4920                    !!!cp ('t175');                    !!!cp ('t175');
4921                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4922                                      value => $self->{open_elements}->[-1]->[0]
4923                                          ->manakai_local_name,
4924                                      token => $token);
4925                  } else {                  } else {
4926                    !!!cp ('t176');                    !!!cp ('t176');
4927                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 4933  sub _tree_construction_main ($) {
4933                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4934                                    
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4938                  !!!cp ('t177');                  !!!cp ('t177');
4939                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4940                  ## Ignore the token                  ## Ignore the token
4941                  !!!next-token;                  !!!next-token;
4942                  redo B;                  next B;
4943                } else {                } else {
4944                  !!!cp ('t178');                  !!!cp ('t178');
4945                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 4955  sub _tree_construction_main ($) {
4955                INSCOPE: {                INSCOPE: {
4956                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4957                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4958                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4959                      !!!cp ('t179');                      !!!cp ('t179');
4960                      $i = $_;                      $i = $_;
4961    
4962                      ## Close the cell                      ## Close the cell
4963                      !!!back-token; # </?>                      !!!back-token; # </x>
4964                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4965                                line => $token->{line},                                line => $token->{line},
4966                                column => $token->{column}};                                column => $token->{column}};
4967                      redo B;                      next B;
4968                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4969                      !!!cp ('t180');                      !!!cp ('t180');
4970                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4971                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4972                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4973                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4974                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4975                      !!!cp ('t181');                      !!!cp ('t181');
4976                      last;                      last;
# Line 4200  sub _tree_construction_main ($) { Line 4982  sub _tree_construction_main ($) {
4982                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4983                  ## Ignore the token                  ## Ignore the token
4984                  !!!next-token;                  !!!next-token;
4985                  redo B;                  next B;
4986                } # INSCOPE                } # INSCOPE
4987              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4988                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4211  sub _tree_construction_main ($) { Line 4993  sub _tree_construction_main ($) {
4993                my $i;                my $i;
4994                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4995                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4996                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4997                    !!!cp ('t184');                    !!!cp ('t184');
4998                    $i = $_;                    $i = $_;
4999                    last INSCOPE;                    last INSCOPE;
5000                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5001                    !!!cp ('t185');                    !!!cp ('t185');
5002                    last INSCOPE;                    last INSCOPE;
5003                  }                  }
# Line 4227  sub _tree_construction_main ($) { Line 5007  sub _tree_construction_main ($) {
5007                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5008                  ## Ignore the token                  ## Ignore the token
5009                  !!!next-token;                  !!!next-token;
5010                  redo B;                  next B;
5011                }                }
5012                                
5013                ## generate implied end tags                ## generate implied end tags
5014                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5015                  !!!cp ('t187');                  !!!cp ('t187');
5016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5017                }                }
5018    
5019                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5020                  !!!cp ('t188');                  !!!cp ('t188');
5021                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5022                                    value => $self->{open_elements}->[-1]->[0]
5023                                        ->manakai_local_name,
5024                                    token => $token);
5025                } else {                } else {
5026                  !!!cp ('t189');                  !!!cp ('t189');
5027                }                }
# Line 4252  sub _tree_construction_main ($) { Line 5033  sub _tree_construction_main ($) {
5033                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5034    
5035                ## reprocess                ## reprocess
5036                redo B;                next B;
5037              } elsif ({              } elsif ({
5038                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5039                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4261  sub _tree_construction_main ($) { Line 5042  sub _tree_construction_main ($) {
5042                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5043                  ## Ignore the token                  ## Ignore the token
5044                  !!!next-token;                  !!!next-token;
5045                  redo B;                  next B;
5046                } else {                } else {
5047                  !!!cp ('t191');                  !!!cp ('t191');
5048                  #                  #
# Line 4275  sub _tree_construction_main ($) { Line 5056  sub _tree_construction_main ($) {
5056                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5057                ## Ignore the token                ## Ignore the token
5058                !!!next-token;                !!!next-token;
5059                redo B;                next B;
5060              } else {              } else {
5061                !!!cp ('t193');                !!!cp ('t193');
5062                #                #
5063              }              }
5064        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5065          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5066            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]}) {  
5067              !!!cp ('t75');              !!!cp ('t75');
5068              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5069              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 5087  sub _tree_construction_main ($) {
5087            unless (length $token->{data}) {            unless (length $token->{data}) {
5088              !!!cp ('t194');              !!!cp ('t194');
5089              !!!next-token;              !!!next-token;
5090              redo B;              next B;
5091            } else {            } else {
5092              !!!cp ('t195');              !!!cp ('t195');
5093            }            }
# Line 4323  sub _tree_construction_main ($) { Line 5101  sub _tree_construction_main ($) {
5101              ## result in a new Text node.              ## result in a new Text node.
5102              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5103                            
5104              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]}) {  
5105                # MUST                # MUST
5106                my $foster_parent_element;                my $foster_parent_element;
5107                my $next_sibling;                my $next_sibling;
5108                my $prev_sibling;                my $prev_sibling;
5109                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5110                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5111                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5112                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5113                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5142  sub _tree_construction_main ($) {
5142          }          }
5143                            
5144          !!!next-token;          !!!next-token;
5145          redo B;          next B;
5146        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5147              if ({              if ({
5148                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4375  sub _tree_construction_main ($) { Line 5150  sub _tree_construction_main ($) {
5150                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5151                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5152                  ## Clear back to table context                  ## Clear back to table context
5153                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5154                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5155                    !!!cp ('t201');                    !!!cp ('t201');
5156                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5157                  }                  }
# Line 4393  sub _tree_construction_main ($) { Line 5168  sub _tree_construction_main ($) {
5168                  }                  }
5169                                    
5170                  ## Clear back to table body context                  ## Clear back to table body context
5171                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5172                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5173                    !!!cp ('t203');                    !!!cp ('t203');
5174                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5175                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4405  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5180                    !!!cp ('t204');                    !!!cp ('t204');
5181                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5182                      !!!nack ('t204');
5183                    !!!next-token;                    !!!next-token;
5184                    redo B;                    next B;
5185                  } else {                  } else {
5186                    !!!cp ('t205');                    !!!cp ('t205');
5187                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5192  sub _tree_construction_main ($) {
5192                }                }
5193    
5194                ## Clear back to table row context                ## Clear back to table row context
5195                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5196                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5197                  !!!cp ('t207');                  !!!cp ('t207');
5198                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5199                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5203  sub _tree_construction_main ($) {
5203    
5204                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5205                                
5206                  !!!nack ('t207.1');
5207                !!!next-token;                !!!next-token;
5208                redo B;                next B;
5209              } elsif ({              } elsif ({
5210                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5211                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5217  sub _tree_construction_main ($) {
5217                  my $i;                  my $i;
5218                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5219                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5220                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5221                      !!!cp ('t208');                      !!!cp ('t208');
5222                      $i = $_;                      $i = $_;
5223                      last INSCOPE;                      last INSCOPE;
5224                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5225                      !!!cp ('t209');                      !!!cp ('t209');
5226                      last INSCOPE;                      last INSCOPE;
5227                    }                    }
5228                  } # INSCOPE                  } # INSCOPE
5229                  unless (defined $i) {                  unless (defined $i) {
5230                   !!!cp ('t210');                    !!!cp ('t210');
5231  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5232                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5233                    ## Ignore the token                    ## Ignore the token
5234                      !!!nack ('t210.1');
5235                    !!!next-token;                    !!!next-token;
5236                    redo B;                    next B;
5237                  }                  }
5238                                    
5239                  ## Clear back to table row context                  ## Clear back to table row context
5240                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5241                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5242                    !!!cp ('t211');                    !!!cp ('t211');
5243                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5244                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5250                    !!!cp ('t212');                    !!!cp ('t212');
5251                    ## reprocess                    ## reprocess
5252                    redo B;                    !!!ack-later;
5253                      next B;
5254                  } else {                  } else {
5255                    !!!cp ('t213');                    !!!cp ('t213');
5256                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5262  sub _tree_construction_main ($) {
5262                  my $i;                  my $i;
5263                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5264                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5265                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5266                      !!!cp ('t214');                      !!!cp ('t214');
5267                      $i = $_;                      $i = $_;
5268                      last INSCOPE;                      last INSCOPE;
5269                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5270                      !!!cp ('t215');                      !!!cp ('t215');
5271                      last INSCOPE;                      last INSCOPE;
5272                    }                    }
# Line 4509  sub _tree_construction_main ($) { Line 5276  sub _tree_construction_main ($) {
5276  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5277                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5278                    ## Ignore the token                    ## Ignore the token
5279                      !!!nack ('t216.1');
5280                    !!!next-token;                    !!!next-token;
5281                    redo B;                    next B;
5282                  }                  }
5283    
5284                  ## Clear back to table body context                  ## Clear back to table body context
5285                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5286                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5287                    !!!cp ('t217');                    !!!cp ('t217');
5288                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5289                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5305  sub _tree_construction_main ($) {
5305    
5306                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5307                  ## Clear back to table context                  ## Clear back to table context
5308                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5309                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5310                    !!!cp ('t219');                    !!!cp ('t219');
5311                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5312                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5315  sub _tree_construction_main ($) {
5315                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5316                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5317                  ## reprocess                  ## reprocess
5318                  redo B;                  !!!ack-later;
5319                    next B;
5320                } elsif ({                } elsif ({
5321                          caption => 1,                          caption => 1,
5322                          colgroup => 1,                          colgroup => 1,
5323                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5324                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5325                  ## Clear back to table context                  ## Clear back to table context
5326                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5327                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5328                    !!!cp ('t220');                    !!!cp ('t220');
5329                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5330                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5342  sub _tree_construction_main ($) {
5342                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5343                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5344                  !!!next-token;                  !!!next-token;
5345                  redo B;                  !!!nack ('t220.1');
5346                    next B;
5347                } else {                } else {
5348                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5349                }                }
5350              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5351                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5352                                  value => $self->{open_elements}->[-1]->[0]
5353                                      ->manakai_local_name,
5354                                  token => $token);
5355    
5356                ## As if </table>                ## As if </table>
5357                ## have a table element in table scope                ## have a table element in table scope
5358                my $i;                my $i;
5359                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5360                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5361                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5362                    !!!cp ('t221');                    !!!cp ('t221');
5363                    $i = $_;                    $i = $_;
5364                    last INSCOPE;                    last INSCOPE;
5365                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5366                    !!!cp ('t222');                    !!!cp ('t222');
5367                    last INSCOPE;                    last INSCOPE;
5368                  }                  }
# Line 4603  sub _tree_construction_main ($) { Line 5372  sub _tree_construction_main ($) {
5372  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5373                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5374                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5375                    !!!nack ('t223.1');
5376                  !!!next-token;                  !!!next-token;
5377                  redo B;                  next B;
5378                }                }
5379                                
5380  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5381                ## generate implied end tags                ## generate implied end tags
5382                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5383                  !!!cp ('t224');                  !!!cp ('t224');
5384                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5385                }                }
5386    
5387                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5388                  !!!cp ('t225');                  !!!cp ('t225');
5389  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5390                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5391                                    value => $self->{open_elements}->[-1]->[0]
5392                                        ->manakai_local_name,
5393                                    token => $token);
5394                } else {                } else {
5395                  !!!cp ('t226');                  !!!cp ('t226');
5396                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5400  sub _tree_construction_main ($) {
5400    
5401                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5402    
5403                ## reprocess            ## reprocess
5404                redo B;            !!!ack-later;
5405              next B;
5406          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5407            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5408              !!!cp ('t227.8');              !!!cp ('t227.8');
5409              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5410              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5411              redo B;              next B;
5412            } else {            } else {
5413              !!!cp ('t227.7');              !!!cp ('t227.7');
5414              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5418  sub _tree_construction_main ($) {
5418              !!!cp ('t227.6');              !!!cp ('t227.6');
5419              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5420              $script_start_tag->();              $script_start_tag->();
5421              redo B;              next B;
5422            } else {            } else {
5423              !!!cp ('t227.5');              !!!cp ('t227.5');
5424              #              #
# Line 4666  sub _tree_construction_main ($) { Line 5438  sub _tree_construction_main ($) {
5438                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5439    
5440                  !!!next-token;                  !!!next-token;
5441                  redo B;                  !!!ack ('t227.2.1');
5442                    next B;
5443                } else {                } else {
5444                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5445                  #                  #
# Line 4695  sub _tree_construction_main ($) { Line 5468  sub _tree_construction_main ($) {
5468                my $i;                my $i;
5469                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5470                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5471                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5472                    !!!cp ('t228');                    !!!cp ('t228');
5473                    $i = $_;                    $i = $_;
5474                    last INSCOPE;                    last INSCOPE;
5475                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5476                    !!!cp ('t229');                    !!!cp ('t229');
5477                    last INSCOPE;                    last INSCOPE;
5478                  }                  }
# Line 4710  sub _tree_construction_main ($) { Line 5481  sub _tree_construction_main ($) {
5481                  !!!cp ('t230');                  !!!cp ('t230');
5482                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5483                  ## Ignore the token                  ## Ignore the token
5484                    !!!nack ('t230.1');
5485                  !!!next-token;                  !!!next-token;
5486                  redo B;                  next B;
5487                } else {                } else {
5488                  !!!cp ('t232');                  !!!cp ('t232');
5489                }                }
5490    
5491                ## Clear back to table row context                ## Clear back to table row context
5492                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5493                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5494                  !!!cp ('t231');                  !!!cp ('t231');
5495  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5496                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5499  sub _tree_construction_main ($) {
5499                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5500                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5501                !!!next-token;                !!!next-token;
5502                redo B;                !!!nack ('t231.1');
5503                  next B;
5504              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5505                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5506                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                  my $i;                  my $i;
5509                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5511                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5512                      !!!cp ('t233');                      !!!cp ('t233');
5513                      $i = $_;                      $i = $_;
5514                      last INSCOPE;                      last INSCOPE;
5515                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5516                      !!!cp ('t234');                      !!!cp ('t234');
5517                      last INSCOPE;                      last INSCOPE;
5518                    }                    }
# Line 4752  sub _tree_construction_main ($) { Line 5522  sub _tree_construction_main ($) {
5522  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5523                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5524                    ## Ignore the token                    ## Ignore the token
5525                      !!!nack ('t236.1');
5526                    !!!next-token;                    !!!next-token;
5527                    redo B;                    next B;
5528                  }                  }
5529                                    
5530                  ## Clear back to table row context                  ## Clear back to table row context
5531                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5532                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5533                    !!!cp ('t236');                    !!!cp ('t236');
5534  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5535                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5545  sub _tree_construction_main ($) {
5545                  my $i;                  my $i;
5546                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5547                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5548                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5549                      !!!cp ('t237');                      !!!cp ('t237');
5550                      $i = $_;                      $i = $_;
5551                      last INSCOPE;                      last INSCOPE;
5552                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5553                      !!!cp ('t238');                      !!!cp ('t238');
5554                      last INSCOPE;                      last INSCOPE;
5555                    }                    }
# Line 4792  sub _tree_construction_main ($) { Line 5558  sub _tree_construction_main ($) {
5558                    !!!cp ('t239');                    !!!cp ('t239');
5559                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5560                    ## Ignore the token                    ## Ignore the token
5561                      !!!nack ('t239.1');
5562                    !!!next-token;                    !!!next-token;
5563                    redo B;                    next B;
5564                  }                  }
5565                                    
5566                  ## Clear back to table body context                  ## Clear back to table body context
5567                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5568                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5569                    !!!cp ('t240');                    !!!cp ('t240');
5570                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5571                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5591  sub _tree_construction_main ($) {
5591                my $i;                my $i;
5592                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5593                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5594                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5595                    !!!cp ('t241');                    !!!cp ('t241');
5596                    $i = $_;                    $i = $_;
5597                    last INSCOPE;                    last INSCOPE;
5598                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5599                    !!!cp ('t242');                    !!!cp ('t242');
5600                    last INSCOPE;                    last INSCOPE;
5601                  }                  }
# Line 4840  sub _tree_construction_main ($) { Line 5604  sub _tree_construction_main ($) {
5604                  !!!cp ('t243');                  !!!cp ('t243');
5605                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5606                  ## Ignore the token                  ## Ignore the token
5607                    !!!nack ('t243.1');
5608                  !!!next-token;                  !!!next-token;
5609                  redo B;                  next B;
5610                }                }
5611                                    
5612                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5615  sub _tree_construction_main ($) {
5615                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5616                                
5617                !!!next-token;                !!!next-token;
5618                redo B;                next B;
5619              } elsif ({              } elsif ({
5620                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5621                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5625  sub _tree_construction_main ($) {
5625                  my $i;                  my $i;
5626                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5627                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5628                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5629                      !!!cp ('t247');                      !!!cp ('t247');
5630                      $i = $_;                      $i = $_;
5631                      last INSCOPE;                      last INSCOPE;
5632                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5633                      !!!cp ('t248');                      !!!cp ('t248');
5634                      last INSCOPE;                      last INSCOPE;
5635                    }                    }
# Line 4875  sub _tree_construction_main ($) { Line 5638  sub _tree_construction_main ($) {
5638                      !!!cp ('t249');                      !!!cp ('t249');
5639                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5640                      ## Ignore the token                      ## Ignore the token
5641                        !!!nack ('t249.1');
5642                      !!!next-token;                      !!!next-token;
5643                      redo B;                      next B;
5644                    }                    }
5645                                    
5646                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5648  sub _tree_construction_main ($) {
5648                  my $i;                  my $i;
5649                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5651                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5652                      !!!cp ('t250');                      !!!cp ('t250');
5653                      $i = $_;                      $i = $_;
5654                      last INSCOPE;                      last INSCOPE;
5655                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5656                      !!!cp ('t251');                      !!!cp ('t251');
5657                      last INSCOPE;                      last INSCOPE;
5658                    }                    }
# Line 4899  sub _tree_construction_main ($) { Line 5661  sub _tree_construction_main ($) {
5661                      !!!cp ('t252');                      !!!cp ('t252');
5662                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5663                      ## Ignore the token                      ## Ignore the token
5664                        !!!nack ('t252.1');
5665                      !!!next-token;                      !!!next-token;
5666                      redo B;                      next B;
5667                    }                    }
5668                                    
5669                  ## Clear back to table row context                  ## Clear back to table row context
5670                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5671                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5672                    !!!cp ('t253');                    !!!cp ('t253');
5673  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5674                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                my $i;                my $i;
5684                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5685                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5686                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5687                    !!!cp ('t254');                    !!!cp ('t254');
5688                    $i = $_;                    $i = $_;
5689                    last INSCOPE;                    last INSCOPE;
5690                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5691                    !!!cp ('t255');                    !!!cp ('t255');
5692                    last INSCOPE;                    last INSCOPE;
5693                  }                  }
# Line 4936  sub _tree_construction_main ($) { Line 5696  sub _tree_construction_main ($) {
5696                  !!!cp ('t256');                  !!!cp ('t256');
5697                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5698                  ## Ignore the token                  ## Ignore the token
5699                    !!!nack ('t256.1');
5700                  !!!next-token;                  !!!next-token;
5701                  redo B;                  next B;
5702                }                }
5703    
5704                ## Clear back to table body context                ## Clear back to table body context
5705                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5706                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5707                  !!!cp ('t257');                  !!!cp ('t257');
5708  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5709                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711    
5712                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5713                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5714                  !!!nack ('t257.1');
5715                !!!next-token;                !!!next-token;
5716                redo B;                next B;
5717              } elsif ({              } elsif ({
5718                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5719                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5720                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5721                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5722                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5723                !!!cp ('t258');            !!!cp ('t258');
5724                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5725                ## Ignore the token            ## Ignore the token
5726                !!!next-token;            !!!nack ('t258.1');
5727                redo B;             !!!next-token;
5728              next B;
5729          } else {          } else {
5730            !!!cp ('t259');            !!!cp ('t259');
5731            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
# Line 4972  sub _tree_construction_main ($) { Line 5734  sub _tree_construction_main ($) {
5734            #            #
5735          }          }
5736        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5737          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5738                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5739            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5740            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5756  sub _tree_construction_main ($) {
5756                unless (length $token->{data}) {                unless (length $token->{data}) {
5757                  !!!cp ('t260');                  !!!cp ('t260');
5758                  !!!next-token;                  !!!next-token;
5759                  redo B;                  next B;
5760                }                }
5761              }              }
5762                            
# Line 5005  sub _tree_construction_main ($) { Line 5767  sub _tree_construction_main ($) {
5767                !!!cp ('t262');                !!!cp ('t262');
5768                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5769                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5770                  !!!ack ('t262.1');
5771                !!!next-token;                !!!next-token;
5772                redo B;                next B;
5773              } else {              } else {
5774                !!!cp ('t263');                !!!cp ('t263');
5775                #                #
5776              }              }
5777            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5778              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5779                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5780                  !!!cp ('t264');                  !!!cp ('t264');
5781                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5782                  ## Ignore the token                  ## Ignore the token
5783                  !!!next-token;                  !!!next-token;
5784                  redo B;                  next B;
5785                } else {                } else {
5786                  !!!cp ('t265');                  !!!cp ('t265');
5787                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5788                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5789                  !!!next-token;                  !!!next-token;
5790                  redo B;                              next B;            
5791                }                }
5792              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5793                !!!cp ('t266');                !!!cp ('t266');
5794                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5795                ## Ignore the token                ## Ignore the token
5796                !!!next-token;                !!!next-token;
5797                redo B;                next B;
5798              } else {              } else {
5799                !!!cp ('t267');                !!!cp ('t267');
5800                #                #
5801              }              }
5802        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5803          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5804              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5805            !!!cp ('t270.2');            !!!cp ('t270.2');
5806            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5811  sub _tree_construction_main ($) {
5811            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5812            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5813            ## Reprocess.            ## Reprocess.
5814            redo B;            next B;
5815          }          }
5816        } else {        } else {
5817          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5818        }        }
5819    
5820            ## As if </colgroup>            ## As if </colgroup>
5821            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5822              !!!cp ('t269');              !!!cp ('t269');
5823  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5824              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5825              ## Ignore the token              ## Ignore the token
5826                !!!nack ('t269.1');
5827              !!!next-token;              !!!next-token;
5828              redo B;              next B;
5829            } else {            } else {
5830              !!!cp ('t270');              !!!cp ('t270');
5831              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5832              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5833                !!!ack-later;
5834              ## reprocess              ## reprocess
5835              redo B;              next B;
5836            }            }
5837      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5838        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5839          !!!cp ('t271');          !!!cp ('t271');
5840          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5841          !!!next-token;          !!!next-token;
5842          redo B;          next B;
5843        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5844              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5845                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5846                  !!!cp ('t272');              !!!cp ('t272');
5847                  ## As if </option>              ## As if </option>
5848                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5849                } else {            } else {
5850                  !!!cp ('t273');              !!!cp ('t273');
5851                }            }
5852    
5853                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5854                !!!next-token;            !!!nack ('t273.1');
5855                redo B;            !!!next-token;
5856              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5857                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5858                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5859                  ## As if </option>              !!!cp ('t274');
5860                  pop @{$self->{open_elements}};              ## As if </option>
5861                } else {              pop @{$self->{open_elements}};
5862                  !!!cp ('t275');            } else {
5863                }              !!!cp ('t275');
5864              }
5865    
5866                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5867                  !!!cp ('t276');              !!!cp ('t276');
5868                  ## As if </optgroup>              ## As if </optgroup>
5869                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5870                } else {            } else {
5871                  !!!cp ('t277');              !!!cp ('t277');
5872                }            }
5873    
5874                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5875                !!!next-token;            !!!nack ('t277.1');
5876                redo B;            !!!next-token;
5877          } elsif ($token->{tag_name} eq 'select' or            next B;
5878                   $token->{tag_name} eq 'input' or          } elsif ({
5879                       select => 1, input => 1, textarea => 1,
5880                     }->{$token->{tag_name}} or
5881                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5882                    {                    {
5883                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5120  sub _tree_construction_main ($) { Line 5888  sub _tree_construction_main ($) {
5888            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5889            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5890            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5891                ## have an element in table scope            ## have an element in table scope
5892                my $i;            my $i;
5893                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5894                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5895                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5896                    !!!cp ('t278');                !!!cp ('t278');
5897                    $i = $_;                $i = $_;
5898                    last INSCOPE;                last INSCOPE;
5899                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5900                            table => 1, html => 1,                !!!cp ('t279');
5901                           }->{$node->[1]}) {                last INSCOPE;
5902                    !!!cp ('t279');              }
5903                    last INSCOPE;            } # INSCOPE
5904                  }            unless (defined $i) {
5905                } # INSCOPE              !!!cp ('t280');
5906                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5907                  !!!cp ('t280');              ## Ignore the token
5908                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5909                  ## Ignore the token              !!!next-token;
5910                  !!!next-token;              next B;
5911                  redo B;            }
               }  
5912                                
5913                !!!cp ('t281');            !!!cp ('t281');
5914                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5915    
5916                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5917    
5918            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5919              !!!cp ('t281.2');              !!!nack ('t281.2');
5920              !!!next-token;              !!!next-token;
5921              redo B;              next B;
5922            } else {            } else {
5923              !!!cp ('t281.1');              !!!cp ('t281.1');
5924                !!!ack-later;
5925              ## Reprocess the token.              ## Reprocess the token.
5926              redo B;              next B;
5927            }            }
5928          } else {          } else {
5929            !!!cp ('t282');            !!!cp ('t282');
5930            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5931            ## Ignore the token            ## Ignore the token
5932              !!!nack ('t282.1');
5933            !!!next-token;            !!!next-token;
5934            redo B;            next B;
5935          }          }
5936        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5937              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5938                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5939                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5940                  !!!cp ('t283');              !!!cp ('t283');
5941                  ## As if </option>              ## As if </option>
5942                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5943                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5944                  !!!cp ('t284');              !!!cp ('t284');
5945                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5946                } else {            } else {
5947                  !!!cp ('t285');              !!!cp ('t285');
5948                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5949                  ## Ignore the token              ## Ignore the token
5950                }            }
5951                !!!next-token;            !!!nack ('t285.1');
5952                redo B;            !!!next-token;
5953              } elsif ($token->{tag_name} eq 'option') {            next B;
5954                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5955                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5956                  pop @{$self->{open_elements}};              !!!cp ('t286');
5957                } else {              pop @{$self->{open_elements}};
5958                  !!!cp ('t287');            } else {
5959                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5960                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5961                }              ## Ignore the token
5962                !!!next-token;            }
5963                redo B;            !!!nack ('t287.1');
5964              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5965                ## have an element in table scope            next B;
5966                my $i;          } elsif ($token->{tag_name} eq 'select') {
5967                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5968                  my $node = $self->{open_elements}->[$_];            my $i;
5969                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5970                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5971                    $i = $_;              if ($node->[1] & SELECT_EL) {
5972                    last INSCOPE;                !!!cp ('t288');
5973                  } elsif ({                $i = $_;
5974                            table => 1, html => 1,                last INSCOPE;
5975                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5976                    !!!cp ('t289');                !!!cp ('t289');
5977                    last INSCOPE;                last INSCOPE;
5978                  }              }
5979                } # INSCOPE            } # INSCOPE
5980                unless (defined $i) {            unless (defined $i) {
5981                  !!!cp ('t290');              !!!cp ('t290');
5982                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5983                  ## Ignore the token              ## Ignore the token
5984                  !!!next-token;              !!!nack ('t290.1');
5985                  redo B;              !!!next-token;
5986                }              next B;
5987              }
5988                                
5989                !!!cp ('t291');            !!!cp ('t291');
5990                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5991    
5992                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5993    
5994                !!!next-token;            !!!nack ('t291.1');
5995                redo B;            !!!next-token;
5996              next B;
5997          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5998                   {                   {
5999                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6000                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6001                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6002  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6003                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6004                                
6005                ## have an element in table scope            ## have an element in table scope
6006                my $i;            my $i;
6007                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6008                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6009                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6010                    !!!cp ('t292');                !!!cp ('t292');
6011                    $i = $_;                $i = $_;
6012                    last INSCOPE;                last INSCOPE;
6013                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6014                            table => 1, html => 1,                !!!cp ('t293');
6015                           }->{$node->[1]}) {                last INSCOPE;
6016                    !!!cp ('t293');              }
6017                    last INSCOPE;            } # INSCOPE
6018                  }            unless (defined $i) {
6019                } # INSCOPE              !!!cp ('t294');
6020                unless (defined $i) {              ## Ignore the token
6021                  !!!cp ('t294');              !!!nack ('t294.1');
6022                  ## Ignore the token              !!!next-token;
6023                  !!!next-token;              next B;
6024                  redo B;            }
               }  
6025                                
6026                ## As if </select>            ## As if </select>
6027                ## have an element in table scope            ## have an element in table scope
6028                undef $i;            undef $i;
6029                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6030                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6031                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6032                    !!!cp ('t295');                !!!cp ('t295');
6033                    $i = $_;                $i = $_;
6034                    last INSCOPE;                last INSCOPE;
6035                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6036  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6037                    !!!cp ('t296');                !!!cp ('t296');
6038                    last INSCOPE;                last INSCOPE;
6039                  }              }
6040                } # INSCOPE            } # INSCOPE
6041                unless (defined $i) {            unless (defined $i) {
6042                  !!!cp ('t297');              !!!cp ('t297');
6043  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6044                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6045                  ## Ignore the </select> token              ## Ignore the </select> token
6046                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
6047                  redo B;              !!!next-token; ## TODO: ok?
6048                }              next B;
6049              }
6050                                
6051                !!!cp ('t298');            !!!cp ('t298');
6052                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6053    
6054                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6055    
6056                ## reprocess            !!!ack-later;
6057                redo B;            ## reprocess
6058              next B;
6059          } else {          } else {
6060            !!!cp ('t299');            !!!cp ('t299');
6061            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6062            ## Ignore the token            ## Ignore the token
6063              !!!nack ('t299.3');
6064            !!!next-token;            !!!next-token;
6065            redo B;            next B;
6066          }          }
6067        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6068          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6069                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6070            !!!cp ('t299.1');            !!!cp ('t299.1');
6071            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 6090  sub _tree_construction_main ($) {
6090            unless (length $token->{data}) {            unless (length $token->{data}) {
6091              !!!cp ('t300');              !!!cp ('t300');
6092              !!!next-token;              !!!next-token;
6093              redo B;              next B;
6094            }            }
6095          }          }
6096                    
# Line 5337  sub _tree_construction_main ($) { Line 6108  sub _tree_construction_main ($) {
6108    
6109          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6110          ## reprocess          ## reprocess
6111          redo B;          next B;
6112        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6113          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6114            !!!cp ('t303');            !!!cp ('t303');
# Line 5352  sub _tree_construction_main ($) { Line 6123  sub _tree_construction_main ($) {
6123          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6124    
6125          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6126            !!!ack-later;
6127          ## reprocess          ## reprocess
6128          redo B;          next B;
6129        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6130          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6131            !!!cp ('t305');            !!!cp ('t305');
# Line 5372  sub _tree_construction_main ($) { Line 6144  sub _tree_construction_main ($) {
6144              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6145              ## Ignore the token              ## Ignore the token
6146              !!!next-token;              !!!next-token;
6147              redo B;              next B;
6148            } else {            } else {
6149              !!!cp ('t308');              !!!cp ('t308');
6150              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6151              !!!next-token;              !!!next-token;
6152              redo B;              next B;
6153            }            }
6154          } else {          } else {
6155            !!!cp ('t309');            !!!cp ('t309');
# Line 5385  sub _tree_construction_main ($) { Line 6157  sub _tree_construction_main ($) {
6157    
6158            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6159            ## reprocess            ## reprocess
6160            redo B;            next B;
6161          }          }
6162        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6163          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6174  sub _tree_construction_main ($) {
6174            unless (length $token->{data}) {            unless (length $token->{data}) {
6175              !!!cp ('t310');              !!!cp ('t310');
6176              !!!next-token;              !!!next-token;
6177              redo B;              next B;
6178            }            }
6179          }          }
6180                    
# Line 5430  sub _tree_construction_main ($) { Line 6202  sub _tree_construction_main ($) {
6202              !!!cp ('t315');              !!!cp ('t315');
6203              !!!next-token;              !!!next-token;
6204            }            }
6205            redo B;            next B;
6206          }          }
6207                    
6208          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5449  sub _tree_construction_main ($) { Line 6221  sub _tree_construction_main ($) {
6221              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6222            !!!cp ('t318');            !!!cp ('t318');
6223            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6224              !!!nack ('t318.1');
6225            !!!next-token;            !!!next-token;
6226            redo B;            next B;
6227          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6228                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6229            !!!cp ('t319');            !!!cp ('t319');
6230            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6231            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6232              !!!ack ('t319.1');
6233            !!!next-token;            !!!next-token;
6234            redo B;            next B;
6235          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6236            !!!cp ('t320');            !!!cp ('t320');
6237            ## NOTE: As if in body.            ## NOTE: As if in head.
6238            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6239            redo B;            next B;
6240          } else {          } else {
6241            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6242              !!!cp ('t321');              !!!cp ('t321');
# Line 5472  sub _tree_construction_main ($) { Line 6246  sub _tree_construction_main ($) {
6246              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6247            }            }
6248            ## Ignore the token            ## Ignore the token
6249              !!!nack ('t322.1');
6250            !!!next-token;            !!!next-token;
6251            redo B;            next B;
6252          }          }
6253        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6254          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5488  sub _tree_construction_main ($) { Line 6263  sub _tree_construction_main ($) {
6263    
6264          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6265              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6266            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6267                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6268              !!!cp ('t325');              !!!cp ('t325');
6269              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
# Line 5501  sub _tree_construction_main ($) { Line 6276  sub _tree_construction_main ($) {
6276            }            }
6277    
6278            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6279                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6280              !!!cp ('t327');              !!!cp ('t327');
6281              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6282            } else {            } else {
6283              !!!cp ('t328');              !!!cp ('t328');
6284            }            }
6285            redo B;            next B;
6286          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6287                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6288            !!!cp ('t329');            !!!cp ('t329');
6289            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6290            !!!next-token;            !!!next-token;
6291            redo B;            next B;
6292          } else {          } else {
6293            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6294              !!!cp ('t330');              !!!cp ('t330');
# Line 5524  sub _tree_construction_main ($) { Line 6299  sub _tree_construction_main ($) {
6299            }            }
6300            ## Ignore the token            ## Ignore the token
6301            !!!next-token;            !!!next-token;
6302            redo B;            next B;
6303          }          }
6304        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6305          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6306                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6307            !!!cp ('t331.1');            !!!cp ('t331.1');
6308            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6327  sub _tree_construction_main ($) {
6327          !!!cp ('t332');          !!!cp ('t332');
6328          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6329          $script_start_tag->();          $script_start_tag->();
6330          redo B;          next B;
6331        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6332          !!!cp ('t333');          !!!cp ('t333');
6333          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6334          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6335          redo B;          next B;
6336        } elsif ({        } elsif ({
6337                  base => 1, link => 1,                  base => 1, link => 1,
6338                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6340  sub _tree_construction_main ($) {
6340          ## 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
6341          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6342          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6343            !!!ack ('t334.1');
6344          !!!next-token;          !!!next-token;
6345          redo B;          next B;
6346        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6347          ## 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
6348          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6349          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.
6350    
6351          unless ($self->{confident}) {          unless ($self->{confident}) {
6352            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6353              !!!cp ('t335');              !!!cp ('t335');
6354                ## NOTE: Whether the encoding is supported or not is handled
6355                ## in the {change_encoding} callback.
6356              $self->{change_encoding}              $self->{change_encoding}
6357                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6358                            
# Line 5583  sub _tree_construction_main ($) { Line 6361  sub _tree_construction_main ($) {
6361                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6362                                           ->{has_reference});                                           ->{has_reference});
6363            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6364              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6365                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6366                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6367                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6368                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6369                !!!cp ('t336');                !!!cp ('t336');
6370                  ## NOTE: Whether the encoding is supported or not is handled
6371                  ## in the {change_encoding} callback.
6372                $self->{change_encoding}                $self->{change_encoding}
6373                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6374                $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 6394  sub _tree_construction_main ($) {
6394            }            }
6395          }          }
6396    
6397            !!!ack ('t338.1');
6398          !!!next-token;          !!!next-token;
6399          redo B;          next B;
6400        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6401          !!!cp ('t341');          !!!cp ('t341');
6402          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6403          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6404          redo B;          next B;
6405        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6406          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6407                                
6408          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6409              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6410            !!!cp ('t342');            !!!cp ('t342');
6411            ## Ignore the token            ## Ignore the token
6412          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6420  sub _tree_construction_main ($) {
6420              }              }
6421            }            }
6422          }          }
6423            !!!nack ('t343.1');
6424          !!!next-token;          !!!next-token;
6425          redo B;          next B;
6426        } elsif ({        } elsif ({
6427                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6428                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6437  sub _tree_construction_main ($) {
6437            !!!cp ('t350');            !!!cp ('t350');
6438            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6439            ## Ignore the token            ## Ignore the token
6440              !!!nack ('t350.1');
6441            !!!next-token;            !!!next-token;
6442            redo B;            next B;
6443          }          }
6444    
6445          ## has a p element in scope          ## has a p element in scope
6446          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6447            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6448              !!!cp ('t344');              !!!cp ('t344');
6449              !!!back-token;              !!!back-token; # <form>
6450              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6451                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6452              redo B;              next B;
6453            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6454              !!!cp ('t345');              !!!cp ('t345');
6455              last INSCOPE;              last INSCOPE;
6456            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6458  sub _tree_construction_main ($) {
6458                        
6459          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6460          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6461              !!!nack ('t346.1');
6462            !!!next-token;            !!!next-token;
6463            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6464              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6475  sub _tree_construction_main ($) {
6475            !!!cp ('t347.1');            !!!cp ('t347.1');
6476            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6477    
6478              !!!nack ('t347.2');
6479            !!!next-token;            !!!next-token;
6480          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6481            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6483  sub _tree_construction_main ($) {
6483                        
6484            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6485    
6486              !!!nack ('t382.1');
6487            !!!next-token;            !!!next-token;
6488          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6489            !!!cp ('t386');            !!!cp ('t386');
6490            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6491                    
6492              !!!nack ('t386.1');
6493            !!!next-token;            !!!next-token;
6494          } else {          } else {
6495            !!!cp ('t347');            !!!nack ('t347.1');
6496            !!!next-token;            !!!next-token;
6497          }          }
6498          redo B;          next B;
6499        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6500          ## has a p element in scope          ## has a p element in scope
6501          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6502            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6503              !!!cp ('t353');              !!!cp ('t353');
6504              !!!back-token;              !!!back-token; # <x>
6505              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6506                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6507              redo B;              next B;
6508            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6509              !!!cp ('t354');              !!!cp ('t354');
6510              last INSCOPE;              last INSCOPE;
6511            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6519  sub _tree_construction_main ($) {
6519                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6520          LI: {          LI: {
6521            ## Step 2            ## Step 2
6522            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6523              if ($i != -1) {              if ($i != -1) {
6524                !!!cp ('t355');                !!!cp ('t355');
6525                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6526                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6527                                      ->manakai_local_name,
6528                                  token => $token);
6529              } else {              } else {
6530                !!!cp ('t356');                !!!cp ('t356');
6531              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6536  sub _tree_construction_main ($) {
6536            }            }
6537                        
6538            ## Step 3            ## Step 3
6539            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6540                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6541                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6542                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6543                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6544                  not ($node->[1] & DIV_EL)) {
6545              !!!cp ('t358');              !!!cp ('t358');
6546              last LI;              last LI;
6547            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6554  sub _tree_construction_main ($) {
6554          } # LI          } # LI
6555                        
6556          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6557            !!!nack ('t359.1');
6558          !!!next-token;          !!!next-token;
6559          redo B;          next B;
6560        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
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 ('t367');              !!!cp ('t367');
6565              !!!back-token;              !!!back-token; # <plaintext>
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 ('t368');              !!!cp ('t368');
6571              last INSCOPE;              last INSCOPE;
6572            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6576  sub _tree_construction_main ($) {
6576                        
6577          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6578                        
6579            !!!nack ('t368.1');
6580          !!!next-token;          !!!next-token;
6581          redo B;          next B;
6582        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6583          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6584            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6585            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6586              !!!cp ('t371');              !!!cp ('t371');
6587              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6588                            
6589              !!!back-token;              !!!back-token; # <a>
6590              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6591                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6592              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6617  sub _tree_construction_main ($) {
6617          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6618          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6619    
6620            !!!nack ('t374.1');
6621          !!!next-token;          !!!next-token;
6622          redo B;          next B;
6623        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6624          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6625    
6626          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6627          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6628            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6629            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6630              !!!cp ('t376');              !!!cp ('t376');
6631              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6632              !!!back-token;              !!!back-token; # <nobr>
6633              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6634                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6635              redo B;              next B;
6636            } 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]}) {  
6637              !!!cp ('t377');              !!!cp ('t377');
6638              last INSCOPE;              last INSCOPE;
6639            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6642  sub _tree_construction_main ($) {
6642          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6643          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6644                    
6645            !!!nack ('t377.1');
6646          !!!next-token;          !!!next-token;
6647          redo B;          next B;
6648        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6649          ## has a button element in scope          ## has a button element in scope
6650          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6651            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6652            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6653              !!!cp ('t378');              !!!cp ('t378');
6654              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6655              !!!back-token;              !!!back-token; # <button>
6656              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6657                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6658              redo B;              next B;
6659            } 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]}) {  
6660              !!!cp ('t379');              !!!cp ('t379');
6661              last INSCOPE;              last INSCOPE;
6662            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670    
6671          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6672    
6673            !!!nack ('t379.1');
6674          !!!next-token;          !!!next-token;
6675          redo B;          next B;
6676        } elsif ({        } elsif ({
6677                  xmp => 1,                  xmp => 1,
6678                  iframe => 1,                  iframe => 1,
6679                  noembed => 1,                  noembed => 1,
6680                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6681                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6682                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6683          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5909  sub _tree_construction_main ($) { Line 6688  sub _tree_construction_main ($) {
6688          }          }
6689          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6690          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6691          redo B;          next B;
6692        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6693          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6694                    
6695          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6696            !!!cp ('t389');            !!!cp ('t389');
6697            ## Ignore the token            ## Ignore the token
6698              !!!nack ('t389'); ## NOTE: Not acknowledged.
6699            !!!next-token;            !!!next-token;
6700            redo B;            next B;
6701          } else {          } else {
6702              !!!ack ('t391.1');
6703    
6704            my $at = $token->{attributes};            my $at = $token->{attributes};
6705            my $form_attrs;            my $form_attrs;
6706            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5962  sub _tree_construction_main ($) { Line 6744  sub _tree_construction_main ($) {
6744                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6745                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6746                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
6747            !!!back-token (@tokens);            !!!back-token (@tokens);
6748            redo B;            !!!next-token;
6749              next B;
6750          }          }
6751        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6752          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6753          my $el;          my $el;
6754          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6755                    
6756          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6757          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6760  sub _tree_construction_main ($) {
6760          $insert->($el);          $insert->($el);
6761                    
6762          my $text = '';          my $text = '';
6763            !!!nack ('t392.1');
6764          !!!next-token;          !!!next-token;
6765          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6766            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6011  sub _tree_construction_main ($) { Line 6794  sub _tree_construction_main ($) {
6794            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6795          }          }
6796          !!!next-token;          !!!next-token;
6797          redo B;          next B;
6798          } elsif ($token->{tag_name} eq 'math' or
6799                   $token->{tag_name} eq 'svg') {
6800            $reconstruct_active_formatting_elements->($insert_to_current);
6801    
6802            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6803    
6804            ## "adjust foreign attributes" - done in insert-element-f
6805            
6806            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6807            
6808            if ($self->{self_closing}) {
6809              pop @{$self->{open_elements}};
6810              !!!ack ('t398.1');
6811            } else {
6812              !!!cp ('t398.2');
6813              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6814              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6815              ## mode, "in body" (not "in foreign content") secondary insertion
6816              ## mode, maybe.
6817            }
6818    
6819            !!!next-token;
6820            next B;
6821        } elsif ({        } elsif ({
6822                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6823                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6021  sub _tree_construction_main ($) { Line 6827  sub _tree_construction_main ($) {
6827          !!!cp ('t401');          !!!cp ('t401');
6828          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6829          ## Ignore the token          ## Ignore the token
6830            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6831          !!!next-token;          !!!next-token;
6832          redo B;          next B;
6833                    
6834          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6835        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 6851  sub _tree_construction_main ($) {
6851              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6852            !!!cp ('t380');            !!!cp ('t380');
6853            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6854              !!!nack ('t380.1');
6855          } elsif ({          } elsif ({
6856                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6857                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 6859  sub _tree_construction_main ($) {
6859                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6860            !!!cp ('t375');            !!!cp ('t375');
6861            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6862              !!!nack ('t375.1');
6863          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6864            !!!cp ('t388');            !!!cp ('t388');
6865            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6866            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6867              !!!ack ('t388.2');
6868          } elsif ({          } elsif ({
6869                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6870                    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 6872  sub _tree_construction_main ($) {
6872                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6873            !!!cp ('t388.1');            !!!cp ('t388.1');
6874            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6875              !!!ack ('t388.3');
6876          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6877            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6878                    
# Line 6074  sub _tree_construction_main ($) { Line 6885  sub _tree_construction_main ($) {
6885              !!!cp ('t400.2');              !!!cp ('t400.2');
6886              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6887            }            }
6888              !!!nack ('t400.3');
6889          } else {          } else {
6890            !!!cp ('t402');            !!!nack ('t402');
6891          }          }
6892                    
6893          !!!next-token;          !!!next-token;
6894          redo B;          next B;
6895        }        }
6896      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6897        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 6899  sub _tree_construction_main ($) {
6899          my $i;          my $i;
6900          INSCOPE: {          INSCOPE: {
6901            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6902              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6903                !!!cp ('t405');                !!!cp ('t405');
6904                $i = $_;                $i = $_;
6905                last INSCOPE;                last INSCOPE;
6906              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6907                !!!cp ('t405.1');                !!!cp ('t405.1');
6908                last;                last;
6909              }              }
# Line 6104  sub _tree_construction_main ($) { Line 6913  sub _tree_construction_main ($) {
6913                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6914            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6915            !!!next-token;            !!!next-token;
6916            redo B;            next B;
6917          } # INSCOPE          } # INSCOPE
6918    
6919          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6920            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]}) {  
6921              !!!cp ('t403');              !!!cp ('t403');
6922              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6923                                value => $_->[0]->manakai_local_name,
6924                                token => $token);
6925              last;              last;
6926            } else {            } else {
6927              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 6930  sub _tree_construction_main ($) {
6930    
6931          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6932          !!!next-token;          !!!next-token;
6933          redo B;          next B;
6934        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6935          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
6936            ## up-to-date, though it has same effect as speced.
6937            if (@{$self->{open_elements}} > 1 and
6938                $self->{open_elements}->[1]->[1] & BODY_EL) {
6939            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6940            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6941              !!!cp ('t406');              !!!cp ('t406');
6942              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6943                                value => $self->{open_elements}->[1]->[0]
6944                                    ->manakai_local_name,
6945                                token => $token);
6946            } else {            } else {
6947              !!!cp ('t407');              !!!cp ('t407');
6948            }            }
6949            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6950            ## reprocess            ## reprocess
6951            redo B;            next B;
6952          } else {          } else {
6953            !!!cp ('t408');            !!!cp ('t408');
6954            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6955            ## Ignore the token            ## Ignore the token
6956            !!!next-token;            !!!next-token;
6957            redo B;            next B;
6958          }          }
6959        } elsif ({        } elsif ({
6960                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 6967  sub _tree_construction_main ($) {
6967          my $i;          my $i;
6968          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6969            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6970            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6971              !!!cp ('t410');              !!!cp ('t410');
6972              $i = $_;              $i = $_;
6973              last INSCOPE;              last INSCOPE;
6974            } 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]}) {  
6975              !!!cp ('t411');              !!!cp ('t411');
6976              last INSCOPE;              last INSCOPE;
6977            }            }
# Line 6177  sub _tree_construction_main ($) { Line 6987  sub _tree_construction_main ($) {
6987                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6988                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6989                    p => 1,                    p => 1,
6990                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6991              !!!cp ('t409');              !!!cp ('t409');
6992              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6993            }            }
6994    
6995            ## Step 2.            ## Step 2.
6996            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6997                      ne $token->{tag_name}) {
6998              !!!cp ('t412');              !!!cp ('t412');
6999              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7000                                value => $self->{open_elements}->[-1]->[0]
7001                                    ->manakai_local_name,
7002                                token => $token);
7003            } else {            } else {
7004              !!!cp ('t414');              !!!cp ('t414');
7005            }            }
# Line 6200  sub _tree_construction_main ($) { Line 7014  sub _tree_construction_main ($) {
7014                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7015          }          }
7016          !!!next-token;          !!!next-token;
7017          redo B;          next B;
7018        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7019          undef $self->{form_element};          undef $self->{form_element};
7020    
# Line 6208  sub _tree_construction_main ($) { Line 7022  sub _tree_construction_main ($) {
7022          my $i;          my $i;
7023          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7024            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7025            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7026              !!!cp ('t418');              !!!cp ('t418');
7027              $i = $_;              $i = $_;
7028              last INSCOPE;              last INSCOPE;
7029            } 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]}) {  
7030              !!!cp ('t419');              !!!cp ('t419');
7031              last INSCOPE;              last INSCOPE;
7032            }            }
# Line 6226  sub _tree_construction_main ($) { Line 7037  sub _tree_construction_main ($) {
7037            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7038          } else {          } else {
7039            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7040            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7041              !!!cp ('t417');              !!!cp ('t417');
7042              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7043            }            }
7044                        
7045            ## Step 2.            ## Step 2.
7046            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7047                      ne $token->{tag_name}) {
7048              !!!cp ('t417.1');              !!!cp ('t417.1');
7049              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7050                                value => $self->{open_elements}->[-1]->[0]
7051                                    ->manakai_local_name,
7052                                token => $token);
7053            } else {            } else {
7054              !!!cp ('t420');              !!!cp ('t420');
7055            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059          }          }
7060    
7061          !!!next-token;          !!!next-token;
7062          redo B;          next B;
7063        } elsif ({        } elsif ({
7064                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7065                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 7067  sub _tree_construction_main ($) {
7067          my $i;          my $i;
7068          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7069            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7070            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7071              !!!cp ('t423');              !!!cp ('t423');
7072              $i = $_;              $i = $_;
7073              last INSCOPE;              last INSCOPE;
7074            } 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]}) {  
7075              !!!cp ('t424');              !!!cp ('t424');
7076              last INSCOPE;              last INSCOPE;
7077            }            }
# Line 6274  sub _tree_construction_main ($) { Line 7082  sub _tree_construction_main ($) {
7082            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7083          } else {          } else {
7084            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7085            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7086              !!!cp ('t422');              !!!cp ('t422');
7087              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7088            }            }
7089                        
7090            ## Step 2.            ## Step 2.
7091            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7092                      ne $token->{tag_name}) {
7093              !!!cp ('t425');              !!!cp ('t425');
7094              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7095            } else {            } else {
# Line 6294  sub _tree_construction_main ($) { Line 7101  sub _tree_construction_main ($) {
7101          }          }
7102                    
7103          !!!next-token;          !!!next-token;
7104          redo B;          next B;
7105        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7106          ## has an element in scope          ## has an element in scope
7107          my $i;          my $i;
7108          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7109            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7110            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7111              !!!cp ('t410.1');              !!!cp ('t410.1');
7112              $i = $_;              $i = $_;
7113              last INSCOPE;              last INSCOPE;
7114            } 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]}) {  
7115              !!!cp ('t411.1');              !!!cp ('t411.1');
7116              last INSCOPE;              last INSCOPE;
7117            }            }
7118          } # INSCOPE          } # INSCOPE
7119    
7120          if (defined $i) {          if (defined $i) {
7121            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7122                      ne $token->{tag_name}) {
7123              !!!cp ('t412.1');              !!!cp ('t412.1');
7124              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7125                                value => $self->{open_elements}->[-1]->[0]
7126                                    ->manakai_local_name,
7127                                token => $token);
7128            } else {            } else {
7129              !!!cp ('t414.1');              !!!cp ('t414.1');
7130            }            }
# Line 6329  sub _tree_construction_main ($) { Line 7137  sub _tree_construction_main ($) {
7137            !!!cp ('t415.1');            !!!cp ('t415.1');
7138            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7139            my $el;            my $el;
7140            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7141            $insert->($el);            $insert->($el);
7142            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7143          }          }
7144    
7145          !!!next-token;          !!!next-token;
7146          redo B;          next B;
7147        } elsif ({        } elsif ({
7148                  a => 1,                  a => 1,
7149                  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 7152  sub _tree_construction_main ($) {
7152                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7153          !!!cp ('t427');          !!!cp ('t427');
7154          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7155          redo B;          next B;
7156        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7157          !!!cp ('t428');          !!!cp ('t428');
7158          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag:br', token => $token);
# Line 6353  sub _tree_construction_main ($) { Line 7161  sub _tree_construction_main ($) {
7161          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7162                    
7163          my $el;          my $el;
7164          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7165          $insert->($el);          $insert->($el);
7166                    
7167          ## Ignore the token.          ## Ignore the token.
7168          !!!next-token;          !!!next-token;
7169          redo B;          next B;
7170        } elsif ({        } elsif ({
7171                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7172                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6375  sub _tree_construction_main ($) { Line 7183  sub _tree_construction_main ($) {
7183          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7184          ## Ignore the token          ## Ignore the token
7185          !!!next-token;          !!!next-token;
7186          redo B;          next B;
7187                    
7188          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7189                    
# Line 6386  sub _tree_construction_main ($) { Line 7194  sub _tree_construction_main ($) {
7194    
7195          ## Step 2          ## Step 2
7196          S2: {          S2: {
7197            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7198              ## Step 1              ## Step 1
7199              ## generate implied end tags              ## generate implied end tags
7200              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7201                !!!cp ('t430');                !!!cp ('t430');
7202                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7203                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7204              }              }
7205                    
7206              ## Step 2              ## Step 2
7207              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7208                        ne $token->{tag_name}) {
7209                !!!cp ('t431');                !!!cp ('t431');
7210                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7211                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7212                                  value => $self->{open_elements}->[-1]->[0]
7213                                      ->manakai_local_name,
7214                                  token => $token);
7215              } else {              } else {
7216                !!!cp ('t432');                !!!cp ('t432');
7217              }              }
# Line 6413  sub _tree_construction_main ($) { Line 7223  sub _tree_construction_main ($) {
7223              last S2;              last S2;
7224            } else {            } else {
7225              ## Step 3              ## Step 3
7226              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7227                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7228                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7229                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7230                !!!cp ('t433');                !!!cp ('t433');
7231                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7232                ## Ignore the token                ## Ignore the token
# Line 6434  sub _tree_construction_main ($) { Line 7244  sub _tree_construction_main ($) {
7244            ## Step 5;            ## Step 5;
7245            redo S2;            redo S2;
7246          } # S2          } # S2
7247          redo B;          next B;
7248        }        }
7249      }      }
7250      redo B;      next B;
7251      } continue { # B
7252        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7253          ## NOTE: The code below is executed in cases where it does not have
7254          ## to be, but it it is harmless even in those cases.
7255          ## has an element in scope
7256          INSCOPE: {
7257            for (reverse 0..$#{$self->{open_elements}}) {
7258              my $node = $self->{open_elements}->[$_];
7259              if ($node->[1] & FOREIGN_EL) {
7260                last INSCOPE;
7261              } elsif ($node->[1] & SCOPING_EL) {
7262                last;
7263              }
7264            }
7265            
7266            ## NOTE: No foreign element in scope.
7267            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7268          } # INSCOPE
7269        }
7270    } # B    } # B
7271    
7272    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6514  sub set_inner_html ($$$) { Line 7343  sub set_inner_html ($$$) {
7343          !!!cp ('i4');          !!!cp ('i4');
7344          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7345          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7346          } elsif ($self->{next_char} <= 0x0008 or
7347                   (0x000E <= $self->{next_char} and
7348                    $self->{next_char} <= 0x001F) or
7349                   (0x007F <= $self->{next_char} and
7350                    $self->{next_char} <= 0x009F) or
7351                   (0xD800 <= $self->{next_char} and
7352                    $self->{next_char} <= 0xDFFF) or
7353                   (0xFDD0 <= $self->{next_char} and
7354                    $self->{next_char} <= 0xFDDF) or
7355                   {
7356                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7357                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7358                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7359                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7360                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7361                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7362                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7363                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7364                    0x10FFFE => 1, 0x10FFFF => 1,
7365                   }->{$self->{next_char}}) {
7366            !!!cp ('i4.1');
7367            !!!parse-error (type => 'control char', level => $self->{must_level});
7368    ## TODO: error type documentation
7369        }        }
7370      };      };
7371      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6554  sub set_inner_html ($$$) { Line 7406  sub set_inner_html ($$$) {
7406          unless defined $p->{content_model};          unless defined $p->{content_model};
7407          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7408    
7409      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7410          ## TODO: Foreign element OK?
7411    
7412      ## Step 3      ## Step 3
7413      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6564  sub set_inner_html ($$$) { Line 7417  sub set_inner_html ($$$) {
7417      $doc->append_child ($root);      $doc->append_child ($root);
7418    
7419      ## Step 5 # MUST      ## Step 5 # MUST
7420      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7421    
7422      undef $p->{head_element};      undef $p->{head_element};
7423    

Legend:
Removed from v.1.121  
changed lines
  Added in v.1.149

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24