/[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.135 by wakaba, Sat May 17 07:31: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    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
17    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    img => 1,  
21    embed => 1,  sub A_EL () { 0b1 }
22    param => 1,  sub ADDRESS_EL () { 0b10 }
23    area => 1,  sub BODY_EL () { 0b100 }
24    col => 1,  sub BUTTON_EL () { 0b1000 }
25    input => 1,  sub CAPTION_EL () { 0b10000 }
26    sub DD_EL () { 0b100000 }
27    sub DIV_EL () { 0b1000000 }
28    sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195  };  };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280    };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
334  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
335    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
336    my $charset = shift;    my $charset_name = shift;
337    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
338    my $s;    my $s;
339      
340    if (defined $charset) {    my $onerror = $_[2] || sub {
341      require Encode; ## TODO: decode(utf8) don't delete BOM      my (%opt) = @_;
342      $s = \ (Encode::decode ($charset, $$bytes_s));      warn "Parse error ($opt{type})\n";
343      $self->{input_encoding} = lc $charset; ## TODO: normalize name    };
344      $self->{confident} = 1;    $self->{parse_error} = $onerror; # updated later by parse_char_string
345    } else {  
346      ## TODO: Implement HTML5 detection algorithm    ## HTML5 encoding sniffing algorithm
347      require Message::Charset::Info;
348      my $charset;
349      my ($e, $e_status);
350    
351      SNIFFING: {
352    
353        ## Step 1
354        if (defined $charset_name) {
355          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
356    
357          ## ISSUE: Unsupported encoding is not ignored according to the spec.
358          ($e, $e_status) = $charset->get_perl_encoding
359              (allow_error_reporting => 1,
360               allow_fallback => 1);
361          if ($e) {
362            $self->{confident} = 1;
363            last SNIFFING;
364          }
365        }
366    
367        ## Step 2
368        # wait
369    
370        ## Step 3
371        my $head = substr ($$bytes_s, 0, 3);
372        if ($head =~ /^\xFE\xFF/) {
373          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
374          ($e, $e_status) = $charset->get_perl_encoding
375              (allow_error_reporting => 1,
376               allow_fallback => 1);
377          $self->{confident} = 1;
378          last SNIFFING;
379        } elsif ($head =~ /^\xFF\xFE/) {
380          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
381          ($e, $e_status) = $charset->get_perl_encoding
382              (allow_error_reporting => 1,
383               allow_fallback => 1);
384          $self->{confident} = 1;
385          last SNIFFING;
386        } elsif ($head eq "\xEF\xBB\xBF") {
387          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
388          ($e, $e_status) = $charset->get_perl_encoding
389              (allow_error_reporting => 1,
390               allow_fallback => 1);
391          $self->{confident} = 1;
392          last SNIFFING;
393        }
394    
395        ## Step 4
396        ## TODO: <meta charset>
397    
398        ## Step 5
399        ## TODO: from history
400    
401        ## Step 6
402      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
403      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
404          (substr ($$bytes_s, 0, 1024));          (substr ($$bytes_s, 0, 1024));
405      $charset ||= 'windows-1252';      if (defined $charset_name) {
406      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
407      $self->{input_encoding} = $charset;  
408          ## ISSUE: Unsupported encoding is not ignored according to the spec.
409          ($e, $e_status) = $charset->get_perl_encoding
410              (allow_error_reporting => 1,
411               allow_fallback => 1);
412          if ($e) {
413            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
414                            value => $charset_name,
415                            level => $self->{info_level},
416                            line => 1, column => 1);
417            $self->{confident} = 0;
418            last SNIFFING;
419          }
420        }
421    
422        ## Step 7: default
423        ## TODO: Make this configurable.
424        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
425            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
426            ## detectable in the step 6.
427        ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,
428                                                       allow_fallback => 1);
429        !!!parse-error (type => 'sniffing:default', ## TODO: type name
430                        value => 'windows-1252',
431                        level => $self->{info_level},
432                        line => 1, column => 1);
433      $self->{confident} = 0;      $self->{confident} = 0;
434      } # SNIFFING
435    
436      $self->{input_encoding} = $charset->get_iana_name;
437      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
438        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
439                        value => $e->name,
440                        level => $self->{unsupported_level},
441                        line => 1, column => 1);
442      } elsif (not ($e_status &
443                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
444        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
445                        value => $self->{input_encoding},
446                        level => $self->{unsupported_level},
447                        line => 1, column => 1);
448    }    }
449      $s = \ $e->decode ($$bytes_s);
450    
451    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
452      my $self = shift;      my $self = shift;
453      my $charset = lc shift;      $charset_name = shift;
454      my $token = shift;      my $token = shift;
     ## TODO: if $charset is supported  
     ## TODO: normalize charset name  
455    
456      ## "Change the encoding" algorithm:      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
457        ($e, $e_status) = $charset->get_perl_encoding
458            (allow_error_reporting => 1, allow_fallback => 1);
459        
460        if ($e) { # if supported
461          ## "Change the encoding" algorithm:
462    
463      ## Step 1            ## Step 1    
464      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
465        $charset = 'utf-8';          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
466      }          ($e, $e_status) = $charset->get_perl_encoding;
467          }
468          $charset_name = $charset->get_iana_name;
469          
470          ## Step 2
471          if (defined $self->{input_encoding} and
472              $self->{input_encoding} eq $charset_name) {
473            !!!parse-error (type => 'charset label:matching', ## TODO: type
474                            value => $charset_name,
475                            level => $self->{info_level});
476            $self->{confident} = 1;
477            return;
478          }
479    
480      ## Step 2        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
481      if (defined $self->{input_encoding} and            ':'.$charset_name, level => 'w', token => $token);
482          $self->{input_encoding} eq $charset) {        
483        $self->{confident} = 1;        ## Step 3
484        return;        # if (can) {
485            ## change the encoding on the fly.
486            #$self->{confident} = 1;
487            #return;
488          # }
489          
490          ## Step 4
491          throw Whatpm::HTML::RestartParser ();
492      }      }
   
     !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.  
         ':'.$charset, level => 'w', token => $token);  
   
     ## Step 3  
     # if (can) {  
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
   
     ## Step 4  
     throw Whatpm::HTML::RestartParser (charset => $charset);  
493    }; # $self->{change_encoding}    }; # $self->{change_encoding}
494    
495    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
# Line 145  sub parse_byte_string ($$$$;$) { Line 497  sub parse_byte_string ($$$$;$) {
497    try {    try {
498      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_string ($s, @args);  
499    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
500      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
501      $s = \ (Encode::decode ($charset, $$bytes_s));      
502      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
503        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
504          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
505                          value => $e->name,
506                          level => $self->{unsupported_level},
507                          line => 1, column => 1);
508        } elsif (not ($e_status &
509                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
510          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
511                          value => $self->{input_encoding},
512                          level => $self->{unsupported_level},
513                          line => 1, column => 1);
514        }
515        $s = \ $e->decode ($$bytes_s);
516      $self->{confident} = 1;      $self->{confident} = 1;
517      $return = $self->parse_char_string ($s, @args);      $return = $self->parse_char_string ($s, @args);
518    };    };
# Line 163  sub parse_byte_string ($$$$;$) { Line 528  sub parse_byte_string ($$$$;$) {
528  ## 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
529  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
530    
531  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
532      my $self = shift;
533      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
534      return $self->parse_char_stream ($input, @_[1..$#_]);
535    } # parse_char_string
536    *parse_string = \&parse_char_string;
537    
538  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
539    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
540    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
541    $self->{document} = $_[1];    $self->{document} = $_[1];
542    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
543    
# Line 186  sub parse_string ($$$;$) { Line 556  sub parse_string ($$$;$) {
556      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
557      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
558    
559      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = $input->getc;
560      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
561        $self->{next_char} = ord $char;
562    
563      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
564          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
565      $self->{column}++;      $self->{column}++;
566            
567      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
568          !!!cp ('j1');
569        $self->{line}++;        $self->{line}++;
570        $self->{column} = 0;        $self->{column} = 0;
571      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
572        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
573          my $next = $input->getc;
574          if ($next ne "\x0A") {
575            $input->ungetc ($next);
576          }
577        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
578        $self->{line}++;        $self->{line}++;
579        $self->{column} = 0;        $self->{column} = 0;
580      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
581          !!!cp ('j3');
582        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
583      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
584          !!!cp ('j4');
585        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
586        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
587        } elsif ($self->{next_char} <= 0x0008 or
588                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
589                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
590                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
591                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
592                 {
593                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
594                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
595                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
596                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
597                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
598                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
599                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
600                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
601                  0x10FFFE => 1, 0x10FFFF => 1,
602                 }->{$self->{next_char}}) {
603          !!!cp ('j5');
604          !!!parse-error (type => 'control char', level => $self->{must_level});
605    ## TODO: error type documentation
606      }      }
607    };    };
608    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 229  sub parse_string ($$$;$) { Line 626  sub parse_string ($$$;$) {
626    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
627    
628    return $self->{document};    return $self->{document};
629  } # parse_string  } # parse_char_stream
630    
631  sub new ($) {  sub new ($) {
632    my $class = shift;    my $class = shift;
633    my $self = bless {}, $class;    my $self = bless {
634        must_level => 'm',
635        should_level => 's',
636        good_level => 'w',
637        warn_level => 'w',
638        info_level => 'i',
639        unsupported_level => 'u',
640      }, $class;
641    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
642      $self->{next_char} = -1;      $self->{next_char} = -1;
643    };    };
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 699  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
699  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
700  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
701  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
702    sub SELF_CLOSING_START_TAG_STATE () { 34 }
703    sub CDATA_BLOCK_STATE () { 35 }
704    
705  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
706  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 718  sub ROW_IMS ()        { 0b10000000 }
718  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
719  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
720  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
721    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
722        ## NOTE: "in foreign content" insertion mode is special; it is combined
723        ## with the secondary insertion mode.  In this parser, they are stored
724        ## together in the bit-or'ed form.
725    
726  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
727    
# Line 348  sub _initialize_tokenizer ($) { Line 758  sub _initialize_tokenizer ($) {
758    undef $self->{current_attribute};    undef $self->{current_attribute};
759    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
760    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
761      delete $self->{self_closing};
762    $self->{char} = [];    $self->{char} = [];
763    # $self->{next_char}    # $self->{next_char}
764    !!!next-input-character;    !!!next-input-character;
# Line 368  sub _initialize_tokenizer ($) { Line 779  sub _initialize_tokenizer ($) {
779  ##        ->{value}  ##        ->{value}
780  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
781  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
782    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
783    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
784    ##     while the token is pushed back to the stack.
785    
786    ## ISSUE: "When a DOCTYPE token is created, its
787    ## <i>self-closing flag</i> must be unset (its other state is that it
788    ## be set), and its attributes list must be empty.": Wrong subject?
789    
790  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
791    
# Line 394  sub _initialize_tokenizer ($) { Line 812  sub _initialize_tokenizer ($) {
812    
813  sub _get_next_token ($) {  sub _get_next_token ($) {
814    my $self = shift;    my $self = shift;
815    
816      if ($self->{self_closing}) {
817        !!!parse-error (type => 'nestc', token => $self->{current_token});
818        ## NOTE: The |self_closing| flag is only set by start tag token.
819        ## In addition, when a start tag token is emitted, it is always set to
820        ## |current_token|.
821        delete $self->{self_closing};
822      }
823    
824    if (@{$self->{token}}) {    if (@{$self->{token}}) {
825        $self->{self_closing} = $self->{token}->[0]->{self_closing};
826      return shift @{$self->{token}};      return shift @{$self->{token}};
827    }    }
828    
# Line 765  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193    
1194          redo A;          redo A;
1195        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1196            !!!cp (42);
1197            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1198          !!!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  
1199          redo A;          redo A;
1200        } else {        } else {
1201          !!!cp (44);          !!!cp (44);
# Line 829  sub _get_next_token ($) { Line 1247  sub _get_next_token ($) {
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1250            !!!cp (50);
1251            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1252          !!!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  
1253          redo A;          redo A;
1254        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1255          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 942  sub _get_next_token ($) { Line 1350  sub _get_next_token ($) {
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1353            !!!cp (64);
1354          $before_leave->();          $before_leave->();
1355            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1356          !!!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  
1357          redo A;          redo A;
1358        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1359          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1042  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1443            !!!cp (77);
1444            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1445          !!!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  
1446          redo A;          redo A;
1447        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1448          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
# Line 1388  sub _get_next_token ($) { Line 1775  sub _get_next_token ($) {
1775    
1776          redo A;          redo A;
1777        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1778            !!!cp (122);
1779            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1780          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1781          redo A;          redo A;
1782        } else {        } else {
1783          !!!cp (124);          !!!cp ('124.1');
1784          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1785          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1786          ## reconsume          ## reconsume
1787          redo A;          redo A;
1788        }        }
1789        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1790          if ($self->{next_char} == 0x003E) { # >
1791            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1792              !!!cp ('124.2');
1793              !!!parse-error (type => 'nestc', token => $self->{current_token});
1794              ## TODO: Different type than slash in start tag
1795              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1796              if ($self->{current_token}->{attributes}) {
1797                !!!cp ('124.4');
1798                !!!parse-error (type => 'end tag attribute');
1799              } else {
1800                !!!cp ('124.5');
1801              }
1802              ## TODO: Test |<title></title/>|
1803            } else {
1804              !!!cp ('124.3');
1805              $self->{self_closing} = 1;
1806            }
1807    
1808            $self->{state} = DATA_STATE;
1809            !!!next-input-character;
1810    
1811            !!!emit ($self->{current_token}); # start tag or end tag
1812    
1813            redo A;
1814          } else {
1815            !!!cp ('124.4');
1816            !!!parse-error (type => 'nestc');
1817            ## TODO: This error type is wrong.
1818            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1819            ## Reconsume.
1820            redo A;
1821          }
1822      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1823        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1824                
# Line 1516  sub _get_next_token ($) { Line 1926  sub _get_next_token ($) {
1926          } else {          } else {
1927            !!!cp (135);            !!!cp (135);
1928          }          }
1929          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1930                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1931                   $self->{next_char} == 0x005B) { # [
1932            !!!next-input-character;
1933            push @next_char, $self->{next_char};
1934            if ($self->{next_char} == 0x0043) { # C
1935              !!!next-input-character;
1936              push @next_char, $self->{next_char};
1937              if ($self->{next_char} == 0x0044) { # D
1938                !!!next-input-character;
1939                push @next_char, $self->{next_char};
1940                if ($self->{next_char} == 0x0041) { # A
1941                  !!!next-input-character;
1942                  push @next_char, $self->{next_char};
1943                  if ($self->{next_char} == 0x0054) { # T
1944                    !!!next-input-character;
1945                    push @next_char, $self->{next_char};
1946                    if ($self->{next_char} == 0x0041) { # A
1947                      !!!next-input-character;
1948                      push @next_char, $self->{next_char};
1949                      if ($self->{next_char} == 0x005B) { # [
1950                        !!!cp (135.1);
1951                        $self->{state} = CDATA_BLOCK_STATE;
1952                        !!!next-input-character;
1953                        redo A;
1954                      } else {
1955                        !!!cp (135.2);
1956                      }
1957                    } else {
1958                      !!!cp (135.3);
1959                    }
1960                  } else {
1961                    !!!cp (135.4);                
1962                  }
1963                } else {
1964                  !!!cp (135.5);
1965                }
1966              } else {
1967                !!!cp (135.6);
1968              }
1969            } else {
1970              !!!cp (135.7);
1971            }
1972        } else {        } else {
1973          !!!cp (136);          !!!cp (136);
1974        }        }
# Line 2240  sub _get_next_token ($) { Line 2693  sub _get_next_token ($) {
2693          !!!next-input-character;          !!!next-input-character;
2694          redo A;          redo A;
2695        }        }
2696        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2697          my $s = '';
2698          
2699          my ($l, $c) = ($self->{line}, $self->{column});
2700    
2701          CS: while ($self->{next_char} != -1) {
2702            if ($self->{next_char} == 0x005D) { # ]
2703              !!!next-input-character;
2704              if ($self->{next_char} == 0x005D) { # ]
2705                !!!next-input-character;
2706                MDC: {
2707                  if ($self->{next_char} == 0x003E) { # >
2708                    !!!cp (221.1);
2709                    !!!next-input-character;
2710                    last CS;
2711                  } elsif ($self->{next_char} == 0x005D) { # ]
2712                    !!!cp (221.2);
2713                    $s .= ']';
2714                    !!!next-input-character;
2715                    redo MDC;
2716                  } else {
2717                    !!!cp (221.3);
2718                    $s .= ']]';
2719                    #
2720                  }
2721                } # MDC
2722              } else {
2723                !!!cp (221.4);
2724                $s .= ']';
2725                #
2726              }
2727            } else {
2728              !!!cp (221.5);
2729              #
2730            }
2731            $s .= chr $self->{next_char};
2732            !!!next-input-character;
2733          } # CS
2734    
2735          $self->{state} = DATA_STATE;
2736          ## next-input-character done or EOF, which is reconsumed.
2737    
2738          if (length $s) {
2739            !!!cp (221.6);
2740            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2741                      line => $l, column => $c});
2742          } else {
2743            !!!cp (221.7);
2744          }
2745    
2746          redo A;
2747    
2748          ## ISSUE: "text tokens" in spec.
2749          ## TODO: Streaming support
2750      } else {      } else {
2751        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2752      }      }
# Line 2390  sub _tokenize_attempt_to_consume_an_enti Line 2897  sub _tokenize_attempt_to_consume_an_enti
2897      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2898      our $EntityChar;      our $EntityChar;
2899    
2900      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2901             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2902             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2903               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2532  sub _tree_construction_initial ($) { Line 3039  sub _tree_construction_initial ($) {
3039                
3040        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3041          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3042          ## NOTE: Default value for both |public_id| and |system_id| attributes
3043          ## are empty strings, so that we don't set any value in missing cases.
3044        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3045            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3046        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2668  sub _tree_construction_initial ($) { Line 3177  sub _tree_construction_initial ($) {
3177        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3178        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3179        ## reprocess        ## reprocess
3180          !!!ack-later;
3181        return;        return;
3182      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3183        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 3258  sub _tree_construction_root_element ($)
3258        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3259          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3260            my $root_element;            my $root_element;
3261            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3262            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3263            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3264                  [$root_element, $el_category->{html}];
3265    
3266            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3267              !!!cp ('t24');              !!!cp ('t24');
# Line 2765  sub _tree_construction_root_element ($) Line 3276  sub _tree_construction_root_element ($)
3276              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3277            }            }
3278    
3279              !!!nack ('t25c');
3280    
3281            !!!next-token;            !!!next-token;
3282            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3283          } else {          } else {
# Line 2781  sub _tree_construction_root_element ($) Line 3294  sub _tree_construction_root_element ($)
3294          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3295        }        }
3296    
3297      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3298        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3299      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3300      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3301    
3302      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3303    
3304      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3305        !!!ack-later;
3306      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3307    
3308      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2811  sub _reset_insertion_mode ($) { Line 3326  sub _reset_insertion_mode ($) {
3326        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3327          $last = 1;          $last = 1;
3328          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3329            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3330              !!!cp ('t27');              !!!cp ('t27');
3331              #              #
3332            } else {            } else {
# Line 2822  sub _reset_insertion_mode ($) { Line 3336  sub _reset_insertion_mode ($) {
3336          }          }
3337        }        }
3338            
3339        ## Step 4..13      ## Step 4..14
3340        my $new_mode = {      my $new_mode;
3341        if ($node->[1] & FOREIGN_EL) {
3342          ## NOTE: Strictly spaking, the line below only applies to MathML and
3343          ## SVG elements.  Currently the HTML syntax supports only MathML and
3344          ## SVG elements as foreigners.
3345          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3346          ## ISSUE: What is set as the secondary insertion mode?
3347        } else {
3348          $new_mode = {
3349                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3350                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3351                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2839  sub _reset_insertion_mode ($) { Line 3361  sub _reset_insertion_mode ($) {
3361                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3362                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3363                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3364                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3365        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3366        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3367                
3368        ## Step 14        ## Step 15
3369        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3370          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3371            !!!cp ('t29');            !!!cp ('t29');
3372            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2857  sub _reset_insertion_mode ($) { Line 3380  sub _reset_insertion_mode ($) {
3380          !!!cp ('t31');          !!!cp ('t31');
3381        }        }
3382                
3383        ## Step 15        ## Step 16
3384        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3385                
3386        ## Step 16        ## Step 17
3387        $i--;        $i--;
3388        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3389                
3390        ## Step 17        ## Step 18
3391        redo S3;        redo S3;
3392      } # S3      } # S3
3393    
# Line 2976  sub _tree_construction_main ($) { Line 3499  sub _tree_construction_main ($) {
3499      ## Step 1      ## Step 1
3500      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3501      my $el;      my $el;
3502      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3503    
3504      ## Step 2      ## Step 2
3505      $insert->($el);      $insert->($el);
# Line 2987  sub _tree_construction_main ($) { Line 3510  sub _tree_construction_main ($) {
3510    
3511      ## Step 4      ## Step 4
3512      my $text = '';      my $text = '';
3513        !!!nack ('t40.1');
3514      !!!next-token;      !!!next-token;
3515      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3516        !!!cp ('t40');        !!!cp ('t40');
# Line 3026  sub _tree_construction_main ($) { Line 3550  sub _tree_construction_main ($) {
3550    
3551    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3552      my $script_el;      my $script_el;
3553      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3554      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3555    
3556      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3557      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3558            
3559      my $text = '';      my $text = '';
3560        !!!nack ('t45.1');
3561      !!!next-token;      !!!next-token;
3562      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3563        !!!cp ('t45');        !!!cp ('t45');
# Line 3090  sub _tree_construction_main ($) { Line 3615  sub _tree_construction_main ($) {
3615        my $formatting_element;        my $formatting_element;
3616        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3617        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3618          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3619              !!!cp ('t52');
3620              last AFE;
3621            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3622                         eq $tag_name) {
3623            !!!cp ('t51');            !!!cp ('t51');
3624            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3625            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3626            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3627          }          }
3628        } # AFE        } # AFE
3629        unless (defined $formatting_element) {        unless (defined $formatting_element) {
# Line 3125  sub _tree_construction_main ($) { Line 3651  sub _tree_construction_main ($) {
3651              !!!next-token;              !!!next-token;
3652              return;              return;
3653            }            }
3654          } 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]}) {  
3655            !!!cp ('t56');            !!!cp ('t56');
3656            $in_scope = 0;            $in_scope = 0;
3657          }          }
# Line 3143  sub _tree_construction_main ($) { Line 3666  sub _tree_construction_main ($) {
3666        }        }
3667        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3668          !!!cp ('t58');          !!!cp ('t58');
3669          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
3670                            value => $self->{open_elements}->[-1]->[0]
3671                                ->manakai_local_name,
3672                          token => $end_tag_token);                          token => $end_tag_token);
3673        }        }
3674                
# Line 3152  sub _tree_construction_main ($) { Line 3677  sub _tree_construction_main ($) {
3677        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3678        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3679          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3680          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3681              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3682              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3683               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3684            !!!cp ('t59');            !!!cp ('t59');
3685            $furthest_block = $node;            $furthest_block = $node;
3686            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3241  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766        } # S7          } # S7  
3767                
3768        ## Step 8        ## Step 8
3769        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
3770          my $foster_parent_element;          my $foster_parent_element;
3771          my $next_sibling;          my $next_sibling;
3772                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
3773                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3774                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3775                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3776                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3320  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843    
3844    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3845      my $child = shift;      my $child = shift;
3846      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]}) {  
3847        # MUST        # MUST
3848        my $foster_parent_element;        my $foster_parent_element;
3849        my $next_sibling;        my $next_sibling;
3850                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3851                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3852                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3853                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3854                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3352  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873      }      }
3874    }; # $insert_to_foster    }; # $insert_to_foster
3875    
3876    B: {    B: while (1) {
3877      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3878        !!!cp ('t73');        !!!cp ('t73');
3879        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3880        ## Ignore the token        ## Ignore the token
3881        ## Stay in the phase        ## Stay in the phase
3882        !!!next-token;        !!!next-token;
3883        redo B;        next B;
3884      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3885               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3886        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
# Line 3385  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3907          }          }
3908        }        }
3909          !!!nack ('t84.1');
3910        !!!next-token;        !!!next-token;
3911        redo B;        next B;
3912      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3913        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3914        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3400  sub _tree_construction_main ($) { Line 3922  sub _tree_construction_main ($) {
3922          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3923        }        }
3924        !!!next-token;        !!!next-token;
3925        redo B;        next B;
3926      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3927          if ($token->{type} == CHARACTER_TOKEN) {
3928            !!!cp ('t87.1');
3929            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3930            !!!next-token;
3931            next B;
3932          } elsif ($token->{type} == START_TAG_TOKEN) {
3933            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3934                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3935                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3936                ($token->{tag_name} eq 'svg' and
3937                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3938              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3939              !!!cp ('t87.2');
3940              #
3941            } elsif ({
3942                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3943                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3944                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3945                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3946                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3947                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3948                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3949                      var => 1,
3950                     }->{$token->{tag_name}}) {
3951              !!!cp ('t87.2');
3952              !!!parse-error (type => 'not closed',
3953                              value => $self->{open_elements}->[-1]->[0]
3954                                  ->manakai_local_name,
3955                              token => $token);
3956    
3957              pop @{$self->{open_elements}}
3958                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3959    
3960              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3961              ## Reprocess.
3962              next B;
3963            } else {
3964              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3965              my $tag_name = $token->{tag_name};
3966              if ($nsuri eq $SVG_NS) {
3967                $tag_name = {
3968                   altglyph => 'altGlyph',
3969                   altglyphdef => 'altGlyphDef',
3970                   altglyphitem => 'altGlyphItem',
3971                   animatecolor => 'animateColor',
3972                   animatemotion => 'animateMotion',
3973                   animatetransform => 'animateTransform',
3974                   clippath => 'clipPath',
3975                   feblend => 'feBlend',
3976                   fecolormatrix => 'feColorMatrix',
3977                   fecomponenttransfer => 'feComponentTransfer',
3978                   fecomposite => 'feComposite',
3979                   feconvolvematrix => 'feConvolveMatrix',
3980                   fediffuselighting => 'feDiffuseLighting',
3981                   fedisplacementmap => 'feDisplacementMap',
3982                   fedistantlight => 'feDistantLight',
3983                   feflood => 'feFlood',
3984                   fefunca => 'feFuncA',
3985                   fefuncb => 'feFuncB',
3986                   fefuncg => 'feFuncG',
3987                   fefuncr => 'feFuncR',
3988                   fegaussianblur => 'feGaussianBlur',
3989                   feimage => 'feImage',
3990                   femerge => 'feMerge',
3991                   femergenode => 'feMergeNode',
3992                   femorphology => 'feMorphology',
3993                   feoffset => 'feOffset',
3994                   fepointlight => 'fePointLight',
3995                   fespecularlighting => 'feSpecularLighting',
3996                   fespotlight => 'feSpotLight',
3997                   fetile => 'feTile',
3998                   feturbulence => 'feTurbulence',
3999                   foreignobject => 'foreignObject',
4000                   glyphref => 'glyphRef',
4001                   lineargradient => 'linearGradient',
4002                   radialgradient => 'radialGradient',
4003                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4004                   textpath => 'textPath',  
4005                }->{$tag_name} || $tag_name;
4006              }
4007    
4008              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4009    
4010              ## "adjust foreign attributes" - done in insert-element-f
4011    
4012              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4013    
4014              if ($self->{self_closing}) {
4015                pop @{$self->{open_elements}};
4016                !!!ack ('t87.3');
4017              } else {
4018                !!!cp ('t87.4');
4019              }
4020    
4021              !!!next-token;
4022              next B;
4023            }
4024          } elsif ($token->{type} == END_TAG_TOKEN) {
4025            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4026            !!!cp ('t87.5');
4027            #
4028          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4029            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4030            !!!cp ('t87.6');
4031            #
4032            ## TODO: ...
4033          } else {
4034            die "$0: $token->{type}: Unknown token type";        
4035          }
4036        }
4037    
4038        if ($self->{insertion_mode} & HEAD_IMS) {
4039        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4040          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4041            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3411  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045              !!!cp ('t88.1');              !!!cp ('t88.1');
4046              ## Ignore the token.              ## Ignore the token.
4047              !!!next-token;              !!!next-token;
4048              redo B;              next B;
4049            }            }
4050            unless (length $token->{data}) {            unless (length $token->{data}) {
4051              !!!cp ('t88');              !!!cp ('t88');
4052              !!!next-token;              !!!next-token;
4053              redo B;              next B;
4054            }            }
4055          }          }
4056    
4057          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4058            !!!cp ('t89');            !!!cp ('t89');
4059            ## As if <head>            ## As if <head>
4060            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4061            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4062            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4063                  [$self->{head_element}, $el_category->{head}];
4064    
4065            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4066            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3451  sub _tree_construction_main ($) { Line 4086  sub _tree_construction_main ($) {
4086            !!!cp ('t92');            !!!cp ('t92');
4087          }          }
4088    
4089              ## "after head" insertion mode          ## "after head" insertion mode
4090              ## As if <body>          ## As if <body>
4091              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4092              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4093              ## reprocess          ## reprocess
4094              redo B;          next B;
4095            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4096              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4097                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4098                  !!!cp ('t93');              !!!cp ('t93');
4099                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4100                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4101                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4102                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4103                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4104                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4105                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4106                  !!!cp ('t94');              !!!next-token;
4107                  #              next B;
4108                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4109                  !!!cp ('t95');              !!!cp ('t94');
4110                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              #
4111                  ## Ignore the token            } else {
4112                  !!!next-token;              !!!cp ('t95');
4113                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4114                }              ## Ignore the token
4115              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
4116                !!!cp ('t96');              !!!next-token;
4117                ## As if <head>              next B;
4118                !!!create-element ($self->{head_element}, 'head',, $token);            }
4119                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4120                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
4121              ## As if <head>
4122              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4123              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4124              push @{$self->{open_elements}},
4125                  [$self->{head_element}, $el_category->{head}];
4126    
4127                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4128                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4129              } else {          } else {
4130                !!!cp ('t97');            !!!cp ('t97');
4131              }          }
4132    
4133              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4134                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3507  sub _tree_construction_main ($) { Line 4147  sub _tree_construction_main ($) {
4147                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4148                  !!!cp ('t100');                  !!!cp ('t100');
4149                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4150                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4151                        [$self->{head_element}, $el_category->{head}];
4152                } else {                } else {
4153                  !!!cp ('t101');                  !!!cp ('t101');
4154                }                }
# Line 3515  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4157                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4158                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4159                  !!!nack ('t101.1');
4160                !!!next-token;                !!!next-token;
4161                redo B;                next B;
4162              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4163                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4164                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4165                  !!!cp ('t102');                  !!!cp ('t102');
4166                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4167                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4168                        [$self->{head_element}, $el_category->{head}];
4169                } else {                } else {
4170                  !!!cp ('t103');                  !!!cp ('t103');
4171                }                }
# Line 3530  sub _tree_construction_main ($) { Line 4173  sub _tree_construction_main ($) {
4173                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4174                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4175                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4176                  !!!ack ('t103.1');
4177                !!!next-token;                !!!next-token;
4178                redo B;                next B;
4179              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4180                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4181                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4182                  !!!cp ('t104');                  !!!cp ('t104');
4183                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4184                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4185                        [$self->{head_element}, $el_category->{head}];
4186                } else {                } else {
4187                  !!!cp ('t105');                  !!!cp ('t105');
4188                }                }
# Line 3545  sub _tree_construction_main ($) { Line 4190  sub _tree_construction_main ($) {
4190                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.
4191    
4192                unless ($self->{confident}) {                unless ($self->{confident}) {
4193                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4194                    !!!cp ('t106');                    !!!cp ('t106');
4195                      ## NOTE: Whether the encoding is supported or not is handled
4196                      ## in the {change_encoding} callback.
4197                    $self->{change_encoding}                    $self->{change_encoding}
4198                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4199                           $token);                           $token);
# Line 3556  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4204                                                 ->{has_reference});                                                 ->{has_reference});
4205                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4206                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4207                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4208                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4209                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4210                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4211                      !!!cp ('t107');                      !!!cp ('t107');
4212                        ## NOTE: Whether the encoding is supported or not is handled
4213                        ## in the {change_encoding} callback.
4214                      $self->{change_encoding}                      $self->{change_encoding}
4215                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4216                             $token);                             $token);
# Line 3593  sub _tree_construction_main ($) { Line 4241  sub _tree_construction_main ($) {
4241    
4242                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4243                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4244                  !!!ack ('t110.1');
4245                !!!next-token;                !!!next-token;
4246                redo B;                next B;
4247              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4248                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4249                  !!!cp ('t111');                  !!!cp ('t111');
# Line 3607  sub _tree_construction_main ($) { Line 4256  sub _tree_construction_main ($) {
4256                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!cp ('t112');                  !!!cp ('t112');
4258                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                } else {                } else {
4262                  !!!cp ('t113');                  !!!cp ('t113');
4263                }                }
# Line 3618  sub _tree_construction_main ($) { Line 4268  sub _tree_construction_main ($) {
4268                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4269                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4270                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4271                redo B;                next B;
4272              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4273                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4274                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
# Line 3626  sub _tree_construction_main ($) { Line 4276  sub _tree_construction_main ($) {
4276                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4277                  !!!cp ('t114');                  !!!cp ('t114');
4278                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4279                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4280                        [$self->{head_element}, $el_category->{head}];
4281                } else {                } else {
4282                  !!!cp ('t115');                  !!!cp ('t115');
4283                }                }
4284                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4285                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4286                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4287                redo B;                next B;
4288              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4289                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4290                  !!!cp ('t116');                  !!!cp ('t116');
4291                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4292                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4293                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4294                    !!!nack ('t116.1');
4295                  !!!next-token;                  !!!next-token;
4296                  redo B;                  next B;
4297                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4298                  !!!cp ('t117');                  !!!cp ('t117');
4299                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4300                  ## Ignore the token                  ## Ignore the token
4301                    !!!nack ('t117.1');
4302                  !!!next-token;                  !!!next-token;
4303                  redo B;                  next B;
4304                } else {                } else {
4305                  !!!cp ('t118');                  !!!cp ('t118');
4306                  #                  #
# Line 3664  sub _tree_construction_main ($) { Line 4317  sub _tree_construction_main ($) {
4317                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4318                  !!!cp ('t120');                  !!!cp ('t120');
4319                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4320                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4321                        [$self->{head_element}, $el_category->{head}];
4322                } else {                } else {
4323                  !!!cp ('t121');                  !!!cp ('t121');
4324                }                }
# Line 3673  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                $script_start_tag->();                $script_start_tag->();
4328                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4329                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4330                redo B;                next B;
4331              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4332                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4333                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3707  sub _tree_construction_main ($) { Line 4361  sub _tree_construction_main ($) {
4361                } else {                } else {
4362                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4363                }                }
4364                  !!!nack ('t127.1');
4365                !!!next-token;                !!!next-token;
4366                redo B;                next B;
4367              } else {              } else {
4368                !!!cp ('t128');                !!!cp ('t128');
4369                #                #
# Line 3740  sub _tree_construction_main ($) { Line 4395  sub _tree_construction_main ($) {
4395              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4396              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4397              ## reprocess              ## reprocess
4398              redo B;              !!!ack-later;
4399                next B;
4400            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4401              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4402                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4403                  !!!cp ('t132');                  !!!cp ('t132');
4404                  ## As if <head>                  ## As if <head>
4405                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4406                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4407                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4408                        [$self->{head_element}, $el_category->{head}];
4409    
4410                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4411                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4412                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4413                  !!!next-token;                  !!!next-token;
4414                  redo B;                  next B;
4415                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4416                  !!!cp ('t133');                  !!!cp ('t133');
4417                  ## As if </noscript>                  ## As if </noscript>
# Line 3765  sub _tree_construction_main ($) { Line 4422  sub _tree_construction_main ($) {
4422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4423                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4424                  !!!next-token;                  !!!next-token;
4425                  redo B;                  next B;
4426                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4427                  !!!cp ('t134');                  !!!cp ('t134');
4428                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4429                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4430                  !!!next-token;                  !!!next-token;
4431                  redo B;                  next B;
4432                } else {                } else {
4433                  !!!cp ('t135');                  !!!cp ('t135');
4434                  #                  #
# Line 3782  sub _tree_construction_main ($) { Line 4439  sub _tree_construction_main ($) {
4439                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4440                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4441                  !!!next-token;                  !!!next-token;
4442                  redo B;                  next B;
4443                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4444                  !!!cp ('t137');                  !!!cp ('t137');
4445                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4446                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4447                  !!!next-token;                  !!!next-token;
4448                  redo B;                  next B;
4449                } else {                } else {
4450                  !!!cp ('t138');                  !!!cp ('t138');
4451                  #                  #
# Line 3799  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4457                  !!!cp ('t139');                  !!!cp ('t139');
4458                  ## As if <head>                  ## As if <head>
4459                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4460                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4461                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4462                        [$self->{head_element}, $el_category->{head}];
4463    
4464                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4465                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3810  sub _tree_construction_main ($) { Line 4468  sub _tree_construction_main ($) {
4468                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4469                  ## Ignore the token                  ## Ignore the token
4470                  !!!next-token;                  !!!next-token;
4471                  redo B;                  next B;
4472                } else {                } else {
4473                  !!!cp ('t141');                  !!!cp ('t141');
4474                }                }
# Line 3822  sub _tree_construction_main ($) { Line 4480  sub _tree_construction_main ($) {
4480                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4481                  !!!cp ('t142');                  !!!cp ('t142');
4482                  ## As if <head>                  ## As if <head>
4483                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4484                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4485                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4486                        [$self->{head_element}, $el_category->{head}];
4487    
4488                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4489                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3842  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4502                  ## Ignore the token                  ## Ignore the token
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                }                }
4506              }              }
4507    
# Line 3869  sub _tree_construction_main ($) { Line 4528  sub _tree_construction_main ($) {
4528                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4529                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4530                !!!next-token;                !!!next-token;
4531                redo B;                next B;
4532              } else {              } else {
4533                !!!cp ('t149');                !!!cp ('t149');
4534              }              }
# Line 3879  sub _tree_construction_main ($) { Line 4538  sub _tree_construction_main ($) {
4538              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4539              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4540              ## reprocess              ## reprocess
4541              redo B;              next B;
4542        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4543          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544            !!!cp ('t149.1');            !!!cp ('t149.1');
4545    
4546            ## NOTE: As if <head>            ## NOTE: As if <head>
4547            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4548            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4549                ($self->{head_element});                ($self->{head_element});
4550            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4551              #    [$self->{head_element}, $el_category->{head}];
4552            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4553            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4554    
# Line 3932  sub _tree_construction_main ($) { Line 4592  sub _tree_construction_main ($) {
4592          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4593          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4594          ## NOTE: Reprocess.          ## NOTE: Reprocess.
4595          redo B;          next B;
4596        } else {        } else {
4597          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4598        }        }
# Line 3947  sub _tree_construction_main ($) { Line 4607  sub _tree_construction_main ($) {
4607              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4608    
4609              !!!next-token;              !!!next-token;
4610              redo B;              next B;
4611            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4612              if ({              if ({
4613                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3957  sub _tree_construction_main ($) { Line 4617  sub _tree_construction_main ($) {
4617                  ## have an element in table scope                  ## have an element in table scope
4618                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4619                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4620                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4621                      !!!cp ('t151');                      !!!cp ('t151');
4622    
4623                      ## Close the cell                      ## Close the cell
4624                      !!!back-token; # <?>                      !!!back-token; # <x>
4625                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
4626                                  tag_name => $node->[0]->manakai_local_name,
4627                                line => $token->{line},                                line => $token->{line},
4628                                column => $token->{column}};                                column => $token->{column}};
4629                      redo B;                      next B;
4630                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4631                      !!!cp ('t152');                      !!!cp ('t152');
4632                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
4633                      last;                      last;
# Line 3979  sub _tree_construction_main ($) { Line 4638  sub _tree_construction_main ($) {
4638                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4639                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4640                  ## Ignore the token                  ## Ignore the token
4641                    !!!nack ('t153.1');
4642                  !!!next-token;                  !!!next-token;
4643                  redo B;                  next B;
4644                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4645                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed:caption', token => $token);
4646                                    
# Line 3990  sub _tree_construction_main ($) { Line 4650  sub _tree_construction_main ($) {
4650                  INSCOPE: {                  INSCOPE: {
4651                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4652                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4653                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
4654                        !!!cp ('t155');                        !!!cp ('t155');
4655                        $i = $_;                        $i = $_;
4656                        last INSCOPE;                        last INSCOPE;
4657                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4658                        !!!cp ('t156');                        !!!cp ('t156');
4659                        last;                        last;
4660                      }                      }
# Line 4006  sub _tree_construction_main ($) { Line 4664  sub _tree_construction_main ($) {
4664                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4665                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4666                    ## Ignore the token                    ## Ignore the token
4667                      !!!nack ('t157.1');
4668                    !!!next-token;                    !!!next-token;
4669                    redo B;                    next B;
4670                  } # INSCOPE                  } # INSCOPE
4671                                    
4672                  ## generate implied end tags                  ## generate implied end tags
4673                  while ({                  while ($self->{open_elements}->[-1]->[1]
4674                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4675                    !!!cp ('t158');                    !!!cp ('t158');
4676                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4677                  }                  }
4678    
4679                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4680                    !!!cp ('t159');                    !!!cp ('t159');
4681                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4682                                      value => $self->{open_elements}->[-1]->[0]
4683                                          ->manakai_local_name,
4684                                      token => $token);
4685                  } else {                  } else {
4686                    !!!cp ('t160');                    !!!cp ('t160');
4687                  }                  }
# Line 4032  sub _tree_construction_main ($) { Line 4693  sub _tree_construction_main ($) {
4693                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4694                                    
4695                  ## reprocess                  ## reprocess
4696                  redo B;                  !!!ack-later;
4697                    next B;
4698                } else {                } else {
4699                  !!!cp ('t161');                  !!!cp ('t161');
4700                  #                  #
# Line 4048  sub _tree_construction_main ($) { Line 4710  sub _tree_construction_main ($) {
4710                  my $i;                  my $i;
4711                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4712                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4713                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4714                      !!!cp ('t163');                      !!!cp ('t163');
4715                      $i = $_;                      $i = $_;
4716                      last INSCOPE;                      last INSCOPE;
4717                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4718                      !!!cp ('t164');                      !!!cp ('t164');
4719                      last INSCOPE;                      last INSCOPE;
4720                    }                    }
# Line 4064  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4725                      ## Ignore the token                      ## Ignore the token
4726                      !!!next-token;                      !!!next-token;
4727                      redo B;                      next B;
4728                    }                    }
4729                                    
4730                  ## generate implied end tags                  ## generate implied end tags
4731                  while ({                  while ($self->{open_elements}->[-1]->[1]
4732                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4733                    !!!cp ('t166');                    !!!cp ('t166');
4734                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4735                  }                  }
4736    
4737                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4738                            ne $token->{tag_name}) {
4739                    !!!cp ('t167');                    !!!cp ('t167');
4740                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4741                                      value => $self->{open_elements}->[-1]->[0]
4742                                          ->manakai_local_name,
4743                                      token => $token);
4744                  } else {                  } else {
4745                    !!!cp ('t168');                    !!!cp ('t168');
4746                  }                  }
# Line 4089  sub _tree_construction_main ($) { Line 4752  sub _tree_construction_main ($) {
4752                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4753                                    
4754                  !!!next-token;                  !!!next-token;
4755                  redo B;                  next B;
4756                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4757                  !!!cp ('t169');                  !!!cp ('t169');
4758                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4759                  ## Ignore the token                  ## Ignore the token
4760                  !!!next-token;                  !!!next-token;
4761                  redo B;                  next B;
4762                } else {                } else {
4763                  !!!cp ('t170');                  !!!cp ('t170');
4764                  #                  #
# Line 4107  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                  INSCOPE: {                  INSCOPE: {
4771                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4772                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
4773                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
4774                        !!!cp ('t171');                        !!!cp ('t171');
4775                        $i = $_;                        $i = $_;
4776                        last INSCOPE;                        last INSCOPE;
4777                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
4778                        !!!cp ('t172');                        !!!cp ('t172');
4779                        last;                        last;
4780                      }                      }
# Line 4124  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785                                    value => $token->{tag_name}, token => $token);                                    value => $token->{tag_name}, token => $token);
4786                    ## Ignore the token                    ## Ignore the token
4787                    !!!next-token;                    !!!next-token;
4788                    redo B;                    next B;
4789                  } # INSCOPE                  } # INSCOPE
4790                                    
4791                  ## generate implied end tags                  ## generate implied end tags
4792                  while ({                  while ($self->{open_elements}->[-1]->[1]
4793                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4794                    !!!cp ('t174');                    !!!cp ('t174');
4795                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4796                  }                  }
4797                                    
4798                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4799                    !!!cp ('t175');                    !!!cp ('t175');
4800                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
4801                                      value => $self->{open_elements}->[-1]->[0]
4802                                          ->manakai_local_name,
4803                                      token => $token);
4804                  } else {                  } else {
4805                    !!!cp ('t176');                    !!!cp ('t176');
4806                  }                  }
# Line 4149  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4813                                    
4814                  !!!next-token;                  !!!next-token;
4815                  redo B;                  next B;
4816                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4817                  !!!cp ('t177');                  !!!cp ('t177');
4818                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4819                  ## Ignore the token                  ## Ignore the token
4820                  !!!next-token;                  !!!next-token;
4821                  redo B;                  next B;
4822                } else {                } else {
4823                  !!!cp ('t178');                  !!!cp ('t178');
4824                  #                  #
# Line 4171  sub _tree_construction_main ($) { Line 4834  sub _tree_construction_main ($) {
4834                INSCOPE: {                INSCOPE: {
4835                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
4836                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4837                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4838                      !!!cp ('t179');                      !!!cp ('t179');
4839                      $i = $_;                      $i = $_;
4840    
4841                      ## Close the cell                      ## Close the cell
4842                      !!!back-token; # </?>                      !!!back-token; # </x>
4843                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4844                                line => $token->{line},                                line => $token->{line},
4845                                column => $token->{column}};                                column => $token->{column}};
4846                      redo B;                      next B;
4847                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4848                      !!!cp ('t180');                      !!!cp ('t180');
4849                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
4850                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
4851                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
4852                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4853                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
4854                      !!!cp ('t181');                      !!!cp ('t181');
4855                      last;                      last;
# Line 4200  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                      value => $token->{tag_name}, token => $token);                      value => $token->{tag_name}, token => $token);
4862                  ## Ignore the token                  ## Ignore the token
4863                  !!!next-token;                  !!!next-token;
4864                  redo B;                  next B;
4865                } # INSCOPE                } # INSCOPE
4866              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4867                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
# Line 4211  sub _tree_construction_main ($) { Line 4872  sub _tree_construction_main ($) {
4872                my $i;                my $i;
4873                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4874                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4875                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4876                    !!!cp ('t184');                    !!!cp ('t184');
4877                    $i = $_;                    $i = $_;
4878                    last INSCOPE;                    last INSCOPE;
4879                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4880                    !!!cp ('t185');                    !!!cp ('t185');
4881                    last INSCOPE;                    last INSCOPE;
4882                  }                  }
# Line 4227  sub _tree_construction_main ($) { Line 4886  sub _tree_construction_main ($) {
4886                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4887                  ## Ignore the token                  ## Ignore the token
4888                  !!!next-token;                  !!!next-token;
4889                  redo B;                  next B;
4890                }                }
4891                                
4892                ## generate implied end tags                ## generate implied end tags
4893                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4894                  !!!cp ('t187');                  !!!cp ('t187');
4895                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4896                }                }
4897    
4898                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4899                  !!!cp ('t188');                  !!!cp ('t188');
4900                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
4901                                    value => $self->{open_elements}->[-1]->[0]
4902                                        ->manakai_local_name,
4903                                    token => $token);
4904                } else {                } else {
4905                  !!!cp ('t189');                  !!!cp ('t189');
4906                }                }
# Line 4252  sub _tree_construction_main ($) { Line 4912  sub _tree_construction_main ($) {
4912                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4913    
4914                ## reprocess                ## reprocess
4915                redo B;                next B;
4916              } elsif ({              } elsif ({
4917                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4918                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
# Line 4261  sub _tree_construction_main ($) { Line 4921  sub _tree_construction_main ($) {
4921                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4922                  ## Ignore the token                  ## Ignore the token
4923                  !!!next-token;                  !!!next-token;
4924                  redo B;                  next B;
4925                } else {                } else {
4926                  !!!cp ('t191');                  !!!cp ('t191');
4927                  #                  #
# Line 4275  sub _tree_construction_main ($) { Line 4935  sub _tree_construction_main ($) {
4935                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4936                ## Ignore the token                ## Ignore the token
4937                !!!next-token;                !!!next-token;
4938                redo B;                next B;
4939              } else {              } else {
4940                !!!cp ('t193');                !!!cp ('t193');
4941                #                #
4942              }              }
4943        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4944          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
4945            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]}) {  
4946              !!!cp ('t75');              !!!cp ('t75');
4947              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
4948              last;              last;
# Line 4309  sub _tree_construction_main ($) { Line 4966  sub _tree_construction_main ($) {
4966            unless (length $token->{data}) {            unless (length $token->{data}) {
4967              !!!cp ('t194');              !!!cp ('t194');
4968              !!!next-token;              !!!next-token;
4969              redo B;              next B;
4970            } else {            } else {
4971              !!!cp ('t195');              !!!cp ('t195');
4972            }            }
# Line 4323  sub _tree_construction_main ($) { Line 4980  sub _tree_construction_main ($) {
4980              ## result in a new Text node.              ## result in a new Text node.
4981              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4982                            
4983              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]}) {  
4984                # MUST                # MUST
4985                my $foster_parent_element;                my $foster_parent_element;
4986                my $next_sibling;                my $next_sibling;
4987                my $prev_sibling;                my $prev_sibling;
4988                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4990                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4991                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4992                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4367  sub _tree_construction_main ($) { Line 5021  sub _tree_construction_main ($) {
5021          }          }
5022                            
5023          !!!next-token;          !!!next-token;
5024          redo B;          next B;
5025        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5026              if ({              if ({
5027                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4375  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5030                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5031                  ## Clear back to table context                  ## Clear back to table context
5032                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5033                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5034                    !!!cp ('t201');                    !!!cp ('t201');
5035                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5036                  }                  }
# Line 4393  sub _tree_construction_main ($) { Line 5047  sub _tree_construction_main ($) {
5047                  }                  }
5048                                    
5049                  ## Clear back to table body context                  ## Clear back to table body context
5050                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5051                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5052                    !!!cp ('t203');                    !!!cp ('t203');
5053                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5054                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4405  sub _tree_construction_main ($) { Line 5058  sub _tree_construction_main ($) {
5058                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5059                    !!!cp ('t204');                    !!!cp ('t204');
5060                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5061                      !!!nack ('t204');
5062                    !!!next-token;                    !!!next-token;
5063                    redo B;                    next B;
5064                  } else {                  } else {
5065                    !!!cp ('t205');                    !!!cp ('t205');
5066                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4417  sub _tree_construction_main ($) { Line 5071  sub _tree_construction_main ($) {
5071                }                }
5072    
5073                ## Clear back to table row context                ## Clear back to table row context
5074                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5075                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5076                  !!!cp ('t207');                  !!!cp ('t207');
5077                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5078                }                }
# Line 4429  sub _tree_construction_main ($) { Line 5082  sub _tree_construction_main ($) {
5082    
5083                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5084                                
5085                  !!!nack ('t207.1');
5086                !!!next-token;                !!!next-token;
5087                redo B;                next B;
5088              } elsif ({              } elsif ({
5089                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5090                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4442  sub _tree_construction_main ($) { Line 5096  sub _tree_construction_main ($) {
5096                  my $i;                  my $i;
5097                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5098                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5099                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5100                      !!!cp ('t208');                      !!!cp ('t208');
5101                      $i = $_;                      $i = $_;
5102                      last INSCOPE;                      last INSCOPE;
5103                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5104                      !!!cp ('t209');                      !!!cp ('t209');
5105                      last INSCOPE;                      last INSCOPE;
5106                    }                    }
5107                  } # INSCOPE                  } # INSCOPE
5108                  unless (defined $i) {                  unless (defined $i) {
5109                   !!!cp ('t210');                    !!!cp ('t210');
5110  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5111                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5112                    ## Ignore the token                    ## Ignore the token
5113                      !!!nack ('t210.1');
5114                    !!!next-token;                    !!!next-token;
5115                    redo B;                    next B;
5116                  }                  }
5117                                    
5118                  ## Clear back to table row context                  ## Clear back to table row context
5119                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5120                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5121                    !!!cp ('t211');                    !!!cp ('t211');
5122                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5123                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4479  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5129                    !!!cp ('t212');                    !!!cp ('t212');
5130                    ## reprocess                    ## reprocess
5131                    redo B;                    !!!ack-later;
5132                      next B;
5133                  } else {                  } else {
5134                    !!!cp ('t213');                    !!!cp ('t213');
5135                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4491  sub _tree_construction_main ($) { Line 5141  sub _tree_construction_main ($) {
5141                  my $i;                  my $i;
5142                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5143                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5144                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5145                      !!!cp ('t214');                      !!!cp ('t214');
5146                      $i = $_;                      $i = $_;
5147                      last INSCOPE;                      last INSCOPE;
5148                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5149                      !!!cp ('t215');                      !!!cp ('t215');
5150                      last INSCOPE;                      last INSCOPE;
5151                    }                    }
# Line 4509  sub _tree_construction_main ($) { Line 5155  sub _tree_construction_main ($) {
5155  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
5156                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5157                    ## Ignore the token                    ## Ignore the token
5158                      !!!nack ('t216.1');
5159                    !!!next-token;                    !!!next-token;
5160                    redo B;                    next B;
5161                  }                  }
5162    
5163                  ## Clear back to table body context                  ## Clear back to table body context
5164                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5165                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5166                    !!!cp ('t217');                    !!!cp ('t217');
5167                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5168                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4538  sub _tree_construction_main ($) { Line 5184  sub _tree_construction_main ($) {
5184    
5185                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5186                  ## Clear back to table context                  ## Clear back to table context
5187                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5188                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5189                    !!!cp ('t219');                    !!!cp ('t219');
5190                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5191                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4548  sub _tree_construction_main ($) { Line 5194  sub _tree_construction_main ($) {
5194                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5195                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5196                  ## reprocess                  ## reprocess
5197                  redo B;                  !!!ack-later;
5198                    next B;
5199                } elsif ({                } elsif ({
5200                          caption => 1,                          caption => 1,
5201                          colgroup => 1,                          colgroup => 1,
5202                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5203                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5204                  ## Clear back to table context                  ## Clear back to table context
5205                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5206                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5207                    !!!cp ('t220');                    !!!cp ('t220');
5208                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5209                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4574  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5222                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5223                  !!!next-token;                  !!!next-token;
5224                  redo B;                  !!!nack ('t220.1');
5225                    next B;
5226                } else {                } else {
5227                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5228                }                }
5229              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5230                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5231                                  value => $self->{open_elements}->[-1]->[0]
5232                                      ->manakai_local_name,
5233                                  token => $token);
5234    
5235                ## As if </table>                ## As if </table>
5236                ## have a table element in table scope                ## have a table element in table scope
5237                my $i;                my $i;
5238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5239                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5240                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5241                    !!!cp ('t221');                    !!!cp ('t221');
5242                    $i = $_;                    $i = $_;
5243                    last INSCOPE;                    last INSCOPE;
5244                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5245                    !!!cp ('t222');                    !!!cp ('t222');
5246                    last INSCOPE;                    last INSCOPE;
5247                  }                  }
# Line 4603  sub _tree_construction_main ($) { Line 5251  sub _tree_construction_main ($) {
5251  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5252                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5253                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5254                    !!!nack ('t223.1');
5255                  !!!next-token;                  !!!next-token;
5256                  redo B;                  next B;
5257                }                }
5258                                
5259  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5260                ## generate implied end tags                ## generate implied end tags
5261                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5262                  !!!cp ('t224');                  !!!cp ('t224');
5263                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5264                }                }
5265    
5266                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5267                  !!!cp ('t225');                  !!!cp ('t225');
5268  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5269                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5270                                    value => $self->{open_elements}->[-1]->[0]
5271                                        ->manakai_local_name,
5272                                    token => $token);
5273                } else {                } else {
5274                  !!!cp ('t226');                  !!!cp ('t226');
5275                }                }
# Line 4629  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279    
5280                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5281    
5282                ## reprocess            ## reprocess
5283                redo B;            !!!ack-later;
5284              next B;
5285          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5286            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5287              !!!cp ('t227.8');              !!!cp ('t227.8');
5288              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5289              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5290              redo B;              next B;
5291            } else {            } else {
5292              !!!cp ('t227.7');              !!!cp ('t227.7');
5293              #              #
# Line 4646  sub _tree_construction_main ($) { Line 5297  sub _tree_construction_main ($) {
5297              !!!cp ('t227.6');              !!!cp ('t227.6');
5298              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5299              $script_start_tag->();              $script_start_tag->();
5300              redo B;              next B;
5301            } else {            } else {
5302              !!!cp ('t227.5');              !!!cp ('t227.5');
5303              #              #
# Line 4666  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5318    
5319                  !!!next-token;                  !!!next-token;
5320                  redo B;                  !!!ack ('t227.2.1');
5321                    next B;
5322                } else {                } else {
5323                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5324                  #                  #
# Line 4695  sub _tree_construction_main ($) { Line 5347  sub _tree_construction_main ($) {
5347                my $i;                my $i;
5348                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5349                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5350                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5351                    !!!cp ('t228');                    !!!cp ('t228');
5352                    $i = $_;                    $i = $_;
5353                    last INSCOPE;                    last INSCOPE;
5354                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5355                    !!!cp ('t229');                    !!!cp ('t229');
5356                    last INSCOPE;                    last INSCOPE;
5357                  }                  }
# Line 4710  sub _tree_construction_main ($) { Line 5360  sub _tree_construction_main ($) {
5360                  !!!cp ('t230');                  !!!cp ('t230');
5361                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5362                  ## Ignore the token                  ## Ignore the token
5363                    !!!nack ('t230.1');
5364                  !!!next-token;                  !!!next-token;
5365                  redo B;                  next B;
5366                } else {                } else {
5367                  !!!cp ('t232');                  !!!cp ('t232');
5368                }                }
5369    
5370                ## Clear back to table row context                ## Clear back to table row context
5371                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5372                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5373                  !!!cp ('t231');                  !!!cp ('t231');
5374  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5375                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4728  sub _tree_construction_main ($) { Line 5378  sub _tree_construction_main ($) {
5378                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5379                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5380                !!!next-token;                !!!next-token;
5381                redo B;                !!!nack ('t231.1');
5382                  next B;
5383              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5384                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5385                  ## As if </tr>                  ## As if </tr>
# Line 4736  sub _tree_construction_main ($) { Line 5387  sub _tree_construction_main ($) {
5387                  my $i;                  my $i;
5388                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5389                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5390                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5391                      !!!cp ('t233');                      !!!cp ('t233');
5392                      $i = $_;                      $i = $_;
5393                      last INSCOPE;                      last INSCOPE;
5394                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5395                      !!!cp ('t234');                      !!!cp ('t234');
5396                      last INSCOPE;                      last INSCOPE;
5397                    }                    }
# Line 4752  sub _tree_construction_main ($) { Line 5401  sub _tree_construction_main ($) {
5401  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5402                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5403                    ## Ignore the token                    ## Ignore the token
5404                      !!!nack ('t236.1');
5405                    !!!next-token;                    !!!next-token;
5406                    redo B;                    next B;
5407                  }                  }
5408                                    
5409                  ## Clear back to table row context                  ## Clear back to table row context
5410                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5411                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5412                    !!!cp ('t236');                    !!!cp ('t236');
5413  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5414                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4775  sub _tree_construction_main ($) { Line 5424  sub _tree_construction_main ($) {
5424                  my $i;                  my $i;
5425                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5426                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5427                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5428                      !!!cp ('t237');                      !!!cp ('t237');
5429                      $i = $_;                      $i = $_;
5430                      last INSCOPE;                      last INSCOPE;
5431                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5432                      !!!cp ('t238');                      !!!cp ('t238');
5433                      last INSCOPE;                      last INSCOPE;
5434                    }                    }
# Line 4792  sub _tree_construction_main ($) { Line 5437  sub _tree_construction_main ($) {
5437                    !!!cp ('t239');                    !!!cp ('t239');
5438                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5439                    ## Ignore the token                    ## Ignore the token
5440                      !!!nack ('t239.1');
5441                    !!!next-token;                    !!!next-token;
5442                    redo B;                    next B;
5443                  }                  }
5444                                    
5445                  ## Clear back to table body context                  ## Clear back to table body context
5446                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5447                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5448                    !!!cp ('t240');                    !!!cp ('t240');
5449                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5450                  }                  }
# Line 4825  sub _tree_construction_main ($) { Line 5470  sub _tree_construction_main ($) {
5470                my $i;                my $i;
5471                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5472                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5473                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5474                    !!!cp ('t241');                    !!!cp ('t241');
5475                    $i = $_;                    $i = $_;
5476                    last INSCOPE;                    last INSCOPE;
5477                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5478                    !!!cp ('t242');                    !!!cp ('t242');
5479                    last INSCOPE;                    last INSCOPE;
5480                  }                  }
# Line 4840  sub _tree_construction_main ($) { Line 5483  sub _tree_construction_main ($) {
5483                  !!!cp ('t243');                  !!!cp ('t243');
5484                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5485                  ## Ignore the token                  ## Ignore the token
5486                    !!!nack ('t243.1');
5487                  !!!next-token;                  !!!next-token;
5488                  redo B;                  next B;
5489                }                }
5490                                    
5491                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4850  sub _tree_construction_main ($) { Line 5494  sub _tree_construction_main ($) {
5494                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5495                                
5496                !!!next-token;                !!!next-token;
5497                redo B;                next B;
5498              } elsif ({              } elsif ({
5499                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5500                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4860  sub _tree_construction_main ($) { Line 5504  sub _tree_construction_main ($) {
5504                  my $i;                  my $i;
5505                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5506                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5507                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5508                      !!!cp ('t247');                      !!!cp ('t247');
5509                      $i = $_;                      $i = $_;
5510                      last INSCOPE;                      last INSCOPE;
5511                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5512                      !!!cp ('t248');                      !!!cp ('t248');
5513                      last INSCOPE;                      last INSCOPE;
5514                    }                    }
# Line 4875  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517                      !!!cp ('t249');                      !!!cp ('t249');
5518                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5519                      ## Ignore the token                      ## Ignore the token
5520                        !!!nack ('t249.1');
5521                      !!!next-token;                      !!!next-token;
5522                      redo B;                      next B;
5523                    }                    }
5524                                    
5525                  ## As if </tr>                  ## As if </tr>
# Line 4884  sub _tree_construction_main ($) { Line 5527  sub _tree_construction_main ($) {
5527                  my $i;                  my $i;
5528                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5529                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5530                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5531                      !!!cp ('t250');                      !!!cp ('t250');
5532                      $i = $_;                      $i = $_;
5533                      last INSCOPE;                      last INSCOPE;
5534                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5535                      !!!cp ('t251');                      !!!cp ('t251');
5536                      last INSCOPE;                      last INSCOPE;
5537                    }                    }
# Line 4899  sub _tree_construction_main ($) { Line 5540  sub _tree_construction_main ($) {
5540                      !!!cp ('t252');                      !!!cp ('t252');
5541                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5542                      ## Ignore the token                      ## Ignore the token
5543                        !!!nack ('t252.1');
5544                      !!!next-token;                      !!!next-token;
5545                      redo B;                      next B;
5546                    }                    }
5547                                    
5548                  ## Clear back to table row context                  ## Clear back to table row context
5549                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5550                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5551                    !!!cp ('t253');                    !!!cp ('t253');
5552  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5553                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4921  sub _tree_construction_main ($) { Line 5562  sub _tree_construction_main ($) {
5562                my $i;                my $i;
5563                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5564                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5565                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5566                    !!!cp ('t254');                    !!!cp ('t254');
5567                    $i = $_;                    $i = $_;
5568                    last INSCOPE;                    last INSCOPE;
5569                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5570                    !!!cp ('t255');                    !!!cp ('t255');
5571                    last INSCOPE;                    last INSCOPE;
5572                  }                  }
# Line 4936  sub _tree_construction_main ($) { Line 5575  sub _tree_construction_main ($) {
5575                  !!!cp ('t256');                  !!!cp ('t256');
5576                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5577                  ## Ignore the token                  ## Ignore the token
5578                    !!!nack ('t256.1');
5579                  !!!next-token;                  !!!next-token;
5580                  redo B;                  next B;
5581                }                }
5582    
5583                ## Clear back to table body context                ## Clear back to table body context
5584                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5585                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5586                  !!!cp ('t257');                  !!!cp ('t257');
5587  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
5588                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4951  sub _tree_construction_main ($) { Line 5590  sub _tree_construction_main ($) {
5590    
5591                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5592                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5593                  !!!nack ('t257.1');
5594                !!!next-token;                !!!next-token;
5595                redo B;                next B;
5596              } elsif ({              } elsif ({
5597                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5598                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5599                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5600                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5601                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5602                !!!cp ('t258');            !!!cp ('t258');
5603                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5604                ## Ignore the token            ## Ignore the token
5605                !!!next-token;            !!!nack ('t258.1');
5606                redo B;             !!!next-token;
5607              next B;
5608          } else {          } else {
5609            !!!cp ('t259');            !!!cp ('t259');
5610            !!!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 5613  sub _tree_construction_main ($) {
5613            #            #
5614          }          }
5615        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5616          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5617                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5618            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
5619            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4994  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635                unless (length $token->{data}) {                unless (length $token->{data}) {
5636                  !!!cp ('t260');                  !!!cp ('t260');
5637                  !!!next-token;                  !!!next-token;
5638                  redo B;                  next B;
5639                }                }
5640              }              }
5641                            
# Line 5005  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646                !!!cp ('t262');                !!!cp ('t262');
5647                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5648                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5649                  !!!ack ('t262.1');
5650                !!!next-token;                !!!next-token;
5651                redo B;                next B;
5652              } else {              } else {
5653                !!!cp ('t263');                !!!cp ('t263');
5654                #                #
5655              }              }
5656            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5657              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5658                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5659                  !!!cp ('t264');                  !!!cp ('t264');
5660                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                  !!!next-token;                  !!!next-token;
5663                  redo B;                  next B;
5664                } else {                } else {
5665                  !!!cp ('t265');                  !!!cp ('t265');
5666                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5667                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5668                  !!!next-token;                  !!!next-token;
5669                  redo B;                              next B;            
5670                }                }
5671              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5672                !!!cp ('t266');                !!!cp ('t266');
5673                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5674                ## Ignore the token                ## Ignore the token
5675                !!!next-token;                !!!next-token;
5676                redo B;                next B;
5677              } else {              } else {
5678                !!!cp ('t267');                !!!cp ('t267');
5679                #                #
5680              }              }
5681        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5682          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5683              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
5684            !!!cp ('t270.2');            !!!cp ('t270.2');
5685            ## Stop parsing.            ## Stop parsing.
# Line 5048  sub _tree_construction_main ($) { Line 5690  sub _tree_construction_main ($) {
5690            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
5691            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
5692            ## Reprocess.            ## Reprocess.
5693            redo B;            next B;
5694          }          }
5695        } else {        } else {
5696          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5697        }        }
5698    
5699            ## As if </colgroup>            ## As if </colgroup>
5700            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5701              !!!cp ('t269');              !!!cp ('t269');
5702  ## TODO: Wrong error type?  ## TODO: Wrong error type?
5703              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5704              ## Ignore the token              ## Ignore the token
5705                !!!nack ('t269.1');
5706              !!!next-token;              !!!next-token;
5707              redo B;              next B;
5708            } else {            } else {
5709              !!!cp ('t270');              !!!cp ('t270');
5710              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5711              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5712                !!!ack-later;
5713              ## reprocess              ## reprocess
5714              redo B;              next B;
5715            }            }
5716      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5717        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5718          !!!cp ('t271');          !!!cp ('t271');
5719          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5720          !!!next-token;          !!!next-token;
5721          redo B;          next B;
5722        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5723              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5724                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5725                  !!!cp ('t272');              !!!cp ('t272');
5726                  ## As if </option>              ## As if </option>
5727                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5728                } else {            } else {
5729                  !!!cp ('t273');              !!!cp ('t273');
5730                }            }
5731    
5732                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5733                !!!next-token;            !!!nack ('t273.1');
5734                redo B;            !!!next-token;
5735              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5736                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5737                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5738                  ## As if </option>              !!!cp ('t274');
5739                  pop @{$self->{open_elements}};              ## As if </option>
5740                } else {              pop @{$self->{open_elements}};
5741                  !!!cp ('t275');            } else {
5742                }              !!!cp ('t275');
5743              }
5744    
5745                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5746                  !!!cp ('t276');              !!!cp ('t276');
5747                  ## As if </optgroup>              ## As if </optgroup>
5748                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5749                } else {            } else {
5750                  !!!cp ('t277');              !!!cp ('t277');
5751                }            }
5752    
5753                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5754                !!!next-token;            !!!nack ('t277.1');
5755                redo B;            !!!next-token;
5756              next B;
5757          } elsif ($token->{tag_name} eq 'select' or          } elsif ($token->{tag_name} eq 'select' or
5758                   $token->{tag_name} eq 'input' or                   $token->{tag_name} eq 'input' or
5759                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
# Line 5120  sub _tree_construction_main ($) { Line 5766  sub _tree_construction_main ($) {
5766            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed:select', token => $token);
5767            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
5768            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
5769                ## have an element in table scope            ## have an element in table scope
5770                my $i;            my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5774                    !!!cp ('t278');                !!!cp ('t278');
5775                    $i = $_;                $i = $_;
5776                    last INSCOPE;                last INSCOPE;
5777                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                !!!cp ('t279');
5779                           }->{$node->[1]}) {                last INSCOPE;
5780                    !!!cp ('t279');              }
5781                    last INSCOPE;            } # INSCOPE
5782                  }            unless (defined $i) {
5783                } # INSCOPE              !!!cp ('t280');
5784                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5785                  !!!cp ('t280');              ## Ignore the token
5786                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!nack ('t280.1');
5787                  ## Ignore the token              !!!next-token;
5788                  !!!next-token;              next B;
5789                  redo B;            }
               }  
5790                                
5791                !!!cp ('t281');            !!!cp ('t281');
5792                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5793    
5794                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5795    
5796            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
5797              !!!cp ('t281.2');              !!!nack ('t281.2');
5798              !!!next-token;              !!!next-token;
5799              redo B;              next B;
5800            } else {            } else {
5801              !!!cp ('t281.1');              !!!cp ('t281.1');
5802                !!!ack-later;
5803              ## Reprocess the token.              ## Reprocess the token.
5804              redo B;              next B;
5805            }            }
5806          } else {          } else {
5807            !!!cp ('t282');            !!!cp ('t282');
5808            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5809            ## Ignore the token            ## Ignore the token
5810              !!!nack ('t282.1');
5811            !!!next-token;            !!!next-token;
5812            redo B;            next B;
5813          }          }
5814        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5815              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5816                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5817                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5818                  !!!cp ('t283');              !!!cp ('t283');
5819                  ## As if </option>              ## As if </option>
5820                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5821                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5822                  !!!cp ('t284');              !!!cp ('t284');
5823                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5824                } else {            } else {
5825                  !!!cp ('t285');              !!!cp ('t285');
5826                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5827                  ## Ignore the token              ## Ignore the token
5828                }            }
5829                !!!next-token;            !!!nack ('t285.1');
5830                redo B;            !!!next-token;
5831              } elsif ($token->{tag_name} eq 'option') {            next B;
5832                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5833                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5834                  pop @{$self->{open_elements}};              !!!cp ('t286');
5835                } else {              pop @{$self->{open_elements}};
5836                  !!!cp ('t287');            } else {
5837                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!cp ('t287');
5838                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5839                }              ## Ignore the token
5840                !!!next-token;            }
5841                redo B;            !!!nack ('t287.1');
5842              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5843                ## have an element in table scope            next B;
5844                my $i;          } elsif ($token->{tag_name} eq 'select') {
5845                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5846                  my $node = $self->{open_elements}->[$_];            my $i;
5847                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5848                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5849                    $i = $_;              if ($node->[1] & SELECT_EL) {
5850                    last INSCOPE;                !!!cp ('t288');
5851                  } elsif ({                $i = $_;
5852                            table => 1, html => 1,                last INSCOPE;
5853                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5854                    !!!cp ('t289');                !!!cp ('t289');
5855                    last INSCOPE;                last INSCOPE;
5856                  }              }
5857                } # INSCOPE            } # INSCOPE
5858                unless (defined $i) {            unless (defined $i) {
5859                  !!!cp ('t290');              !!!cp ('t290');
5860                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5861                  ## Ignore the token              ## Ignore the token
5862                  !!!next-token;              !!!nack ('t290.1');
5863                  redo B;              !!!next-token;
5864                }              next B;
5865              }
5866                                
5867                !!!cp ('t291');            !!!cp ('t291');
5868                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5869    
5870                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5871    
5872                !!!next-token;            !!!nack ('t291.1');
5873                redo B;            !!!next-token;
5874              next B;
5875          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5876                   {                   {
5877                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
5878                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5879                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
5880  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5881                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5882                                
5883                ## have an element in table scope            ## have an element in table scope
5884                my $i;            my $i;
5885                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5887                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5888                    !!!cp ('t292');                !!!cp ('t292');
5889                    $i = $_;                $i = $_;
5890                    last INSCOPE;                last INSCOPE;
5891                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892                            table => 1, html => 1,                !!!cp ('t293');
5893                           }->{$node->[1]}) {                last INSCOPE;
5894                    !!!cp ('t293');              }
5895                    last INSCOPE;            } # INSCOPE
5896                  }            unless (defined $i) {
5897                } # INSCOPE              !!!cp ('t294');
5898                unless (defined $i) {              ## Ignore the token
5899                  !!!cp ('t294');              !!!nack ('t294.1');
5900                  ## Ignore the token              !!!next-token;
5901                  !!!next-token;              next B;
5902                  redo B;            }
               }  
5903                                
5904                ## As if </select>            ## As if </select>
5905                ## have an element in table scope            ## have an element in table scope
5906                undef $i;            undef $i;
5907                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5908                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5909                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5910                    !!!cp ('t295');                !!!cp ('t295');
5911                    $i = $_;                $i = $_;
5912                    last INSCOPE;                last INSCOPE;
5913                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5914  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5915                    !!!cp ('t296');                !!!cp ('t296');
5916                    last INSCOPE;                last INSCOPE;
5917                  }              }
5918                } # INSCOPE            } # INSCOPE
5919                unless (defined $i) {            unless (defined $i) {
5920                  !!!cp ('t297');              !!!cp ('t297');
5921  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5922                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5923                  ## Ignore the </select> token              ## Ignore the </select> token
5924                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5925                  redo B;              !!!next-token; ## TODO: ok?
5926                }              next B;
5927              }
5928                                
5929                !!!cp ('t298');            !!!cp ('t298');
5930                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5931    
5932                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5933    
5934                ## reprocess            !!!ack-later;
5935                redo B;            ## reprocess
5936              next B;
5937          } else {          } else {
5938            !!!cp ('t299');            !!!cp ('t299');
5939            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5940            ## Ignore the token            ## Ignore the token
5941              !!!nack ('t299.3');
5942            !!!next-token;            !!!next-token;
5943            redo B;            next B;
5944          }          }
5945        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5946          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5947                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
5948            !!!cp ('t299.1');            !!!cp ('t299.1');
5949            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5319  sub _tree_construction_main ($) { Line 5968  sub _tree_construction_main ($) {
5968            unless (length $token->{data}) {            unless (length $token->{data}) {
5969              !!!cp ('t300');              !!!cp ('t300');
5970              !!!next-token;              !!!next-token;
5971              redo B;              next B;
5972            }            }
5973          }          }
5974                    
# Line 5337  sub _tree_construction_main ($) { Line 5986  sub _tree_construction_main ($) {
5986    
5987          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5988          ## reprocess          ## reprocess
5989          redo B;          next B;
5990        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5991          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5992            !!!cp ('t303');            !!!cp ('t303');
# Line 5352  sub _tree_construction_main ($) { Line 6001  sub _tree_construction_main ($) {
6001          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6002    
6003          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6004            !!!ack-later;
6005          ## reprocess          ## reprocess
6006          redo B;          next B;
6007        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6008          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6009            !!!cp ('t305');            !!!cp ('t305');
# Line 5372  sub _tree_construction_main ($) { Line 6022  sub _tree_construction_main ($) {
6022              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag:html', token => $token);
6023              ## Ignore the token              ## Ignore the token
6024              !!!next-token;              !!!next-token;
6025              redo B;              next B;
6026            } else {            } else {
6027              !!!cp ('t308');              !!!cp ('t308');
6028              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6029              !!!next-token;              !!!next-token;
6030              redo B;              next B;
6031            }            }
6032          } else {          } else {
6033            !!!cp ('t309');            !!!cp ('t309');
# Line 5385  sub _tree_construction_main ($) { Line 6035  sub _tree_construction_main ($) {
6035    
6036            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6037            ## reprocess            ## reprocess
6038            redo B;            next B;
6039          }          }
6040        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6041          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5402  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            unless (length $token->{data}) {            unless (length $token->{data}) {
6053              !!!cp ('t310');              !!!cp ('t310');
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            }            }
6057          }          }
6058                    
# Line 5430  sub _tree_construction_main ($) { Line 6080  sub _tree_construction_main ($) {
6080              !!!cp ('t315');              !!!cp ('t315');
6081              !!!next-token;              !!!next-token;
6082            }            }
6083            redo B;            next B;
6084          }          }
6085                    
6086          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
# Line 5449  sub _tree_construction_main ($) { Line 6099  sub _tree_construction_main ($) {
6099              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6100            !!!cp ('t318');            !!!cp ('t318');
6101            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6102              !!!nack ('t318.1');
6103            !!!next-token;            !!!next-token;
6104            redo B;            next B;
6105          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6106                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6107            !!!cp ('t319');            !!!cp ('t319');
6108            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6109            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6110              !!!ack ('t319.1');
6111            !!!next-token;            !!!next-token;
6112            redo B;            next B;
6113          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6114            !!!cp ('t320');            !!!cp ('t320');
6115            ## NOTE: As if in body.            ## NOTE: As if in body.
6116            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6117            redo B;            next B;
6118          } else {          } else {
6119            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6120              !!!cp ('t321');              !!!cp ('t321');
# Line 5472  sub _tree_construction_main ($) { Line 6124  sub _tree_construction_main ($) {
6124              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6125            }            }
6126            ## Ignore the token            ## Ignore the token
6127              !!!nack ('t322.1');
6128            !!!next-token;            !!!next-token;
6129            redo B;            next B;
6130          }          }
6131        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6132          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
# Line 5488  sub _tree_construction_main ($) { Line 6141  sub _tree_construction_main ($) {
6141    
6142          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6143              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6144            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6145                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6146              !!!cp ('t325');              !!!cp ('t325');
6147              !!!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 6154  sub _tree_construction_main ($) {
6154            }            }
6155    
6156            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6157                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6158              !!!cp ('t327');              !!!cp ('t327');
6159              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6160            } else {            } else {
6161              !!!cp ('t328');              !!!cp ('t328');
6162            }            }
6163            redo B;            next B;
6164          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6165                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6166            !!!cp ('t329');            !!!cp ('t329');
6167            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6168            !!!next-token;            !!!next-token;
6169            redo B;            next B;
6170          } else {          } else {
6171            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6172              !!!cp ('t330');              !!!cp ('t330');
# Line 5524  sub _tree_construction_main ($) { Line 6177  sub _tree_construction_main ($) {
6177            }            }
6178            ## Ignore the token            ## Ignore the token
6179            !!!next-token;            !!!next-token;
6180            redo B;            next B;
6181          }          }
6182        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6183          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6184                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6185            !!!cp ('t331.1');            !!!cp ('t331.1');
6186            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5552  sub _tree_construction_main ($) { Line 6205  sub _tree_construction_main ($) {
6205          !!!cp ('t332');          !!!cp ('t332');
6206          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6207          $script_start_tag->();          $script_start_tag->();
6208          redo B;          next B;
6209        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6210          !!!cp ('t333');          !!!cp ('t333');
6211          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6212          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6213          redo B;          next B;
6214        } elsif ({        } elsif ({
6215                  base => 1, link => 1,                  base => 1, link => 1,
6216                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5565  sub _tree_construction_main ($) { Line 6218  sub _tree_construction_main ($) {
6218          ## 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
6219          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6220          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6221            !!!ack ('t334.1');
6222          !!!next-token;          !!!next-token;
6223          redo B;          next B;
6224        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6225          ## 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
6226          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6227          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.
6228    
6229          unless ($self->{confident}) {          unless ($self->{confident}) {
6230            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6231              !!!cp ('t335');              !!!cp ('t335');
6232                ## NOTE: Whether the encoding is supported or not is handled
6233                ## in the {change_encoding} callback.
6234              $self->{change_encoding}              $self->{change_encoding}
6235                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6236                            
# Line 5583  sub _tree_construction_main ($) { Line 6239  sub _tree_construction_main ($) {
6239                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6240                                           ->{has_reference});                                           ->{has_reference});
6241            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6242              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6243                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6244                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6245                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6246                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6247                !!!cp ('t336');                !!!cp ('t336');
6248                  ## NOTE: Whether the encoding is supported or not is handled
6249                  ## in the {change_encoding} callback.
6250                $self->{change_encoding}                $self->{change_encoding}
6251                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6252                $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 6272  sub _tree_construction_main ($) {
6272            }            }
6273          }          }
6274    
6275            !!!ack ('t338.1');
6276          !!!next-token;          !!!next-token;
6277          redo B;          next B;
6278        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6279          !!!cp ('t341');          !!!cp ('t341');
6280          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6281          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6282          redo B;          next B;
6283        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6284          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body:body', token => $token);
6285                                
6286          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6287              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6288            !!!cp ('t342');            !!!cp ('t342');
6289            ## Ignore the token            ## Ignore the token
6290          } else {          } else {
# Line 5640  sub _tree_construction_main ($) { Line 6298  sub _tree_construction_main ($) {
6298              }              }
6299            }            }
6300          }          }
6301            !!!nack ('t343.1');
6302          !!!next-token;          !!!next-token;
6303          redo B;          next B;
6304        } elsif ({        } elsif ({
6305                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6306                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5656  sub _tree_construction_main ($) { Line 6315  sub _tree_construction_main ($) {
6315            !!!cp ('t350');            !!!cp ('t350');
6316            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6317            ## Ignore the token            ## Ignore the token
6318              !!!nack ('t350.1');
6319            !!!next-token;            !!!next-token;
6320            redo B;            next B;
6321          }          }
6322    
6323          ## has a p element in scope          ## has a p element in scope
6324          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6325            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6326              !!!cp ('t344');              !!!cp ('t344');
6327              !!!back-token;              !!!back-token; # <form>
6328              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6329                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6330              redo B;              next B;
6331            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6332              !!!cp ('t345');              !!!cp ('t345');
6333              last INSCOPE;              last INSCOPE;
6334            }            }
# Line 5679  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336                        
6337          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6339              !!!nack ('t346.1');
6340            !!!next-token;            !!!next-token;
6341            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6342              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5695  sub _tree_construction_main ($) { Line 6353  sub _tree_construction_main ($) {
6353            !!!cp ('t347.1');            !!!cp ('t347.1');
6354            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6355    
6356              !!!nack ('t347.2');
6357            !!!next-token;            !!!next-token;
6358          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6359            !!!cp ('t382');            !!!cp ('t382');
# Line 5702  sub _tree_construction_main ($) { Line 6361  sub _tree_construction_main ($) {
6361                        
6362            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6363    
6364              !!!nack ('t382.1');
6365            !!!next-token;            !!!next-token;
6366          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6367            !!!cp ('t386');            !!!cp ('t386');
6368            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6369                    
6370              !!!nack ('t386.1');
6371            !!!next-token;            !!!next-token;
6372          } else {          } else {
6373            !!!cp ('t347');            !!!nack ('t347.1');
6374            !!!next-token;            !!!next-token;
6375          }          }
6376          redo B;          next B;
6377        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6378          ## has a p element in scope          ## has a p element in scope
6379          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6380            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6381              !!!cp ('t353');              !!!cp ('t353');
6382              !!!back-token;              !!!back-token; # <x>
6383              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6384                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6385              redo B;              next B;
6386            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6387              !!!cp ('t354');              !!!cp ('t354');
6388              last INSCOPE;              last INSCOPE;
6389            }            }
# Line 5739  sub _tree_construction_main ($) { Line 6397  sub _tree_construction_main ($) {
6397                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6398          LI: {          LI: {
6399            ## Step 2            ## Step 2
6400            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6401              if ($i != -1) {              if ($i != -1) {
6402                !!!cp ('t355');                !!!cp ('t355');
6403                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6404                                $self->{open_elements}->[-1]->[1], token => $token);                                value => $self->{open_elements}->[-1]->[0]
6405                                      ->manakai_local_name,
6406                                  token => $token);
6407              } else {              } else {
6408                !!!cp ('t356');                !!!cp ('t356');
6409              }              }
# Line 5754  sub _tree_construction_main ($) { Line 6414  sub _tree_construction_main ($) {
6414            }            }
6415                        
6416            ## Step 3            ## Step 3
6417            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6418                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6419                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6420                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6421                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6422                  not ($node->[1] & DIV_EL)) {
6423              !!!cp ('t358');              !!!cp ('t358');
6424              last LI;              last LI;
6425            }            }
# Line 5771  sub _tree_construction_main ($) { Line 6432  sub _tree_construction_main ($) {
6432          } # LI          } # LI
6433                        
6434          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6435            !!!nack ('t359.1');
6436          !!!next-token;          !!!next-token;
6437          redo B;          next B;
6438        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6439          ## has a p element in scope          ## has a p element in scope
6440          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6441            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6442              !!!cp ('t367');              !!!cp ('t367');
6443              !!!back-token;              !!!back-token; # <plaintext>
6444              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6445                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6446              redo B;              next B;
6447            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6448              !!!cp ('t368');              !!!cp ('t368');
6449              last INSCOPE;              last INSCOPE;
6450            }            }
# Line 5795  sub _tree_construction_main ($) { Line 6454  sub _tree_construction_main ($) {
6454                        
6455          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6456                        
6457            !!!nack ('t368.1');
6458          !!!next-token;          !!!next-token;
6459          redo B;          next B;
6460        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6461          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6462            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6463            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6464              !!!cp ('t371');              !!!cp ('t371');
6465              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6466                            
6467              !!!back-token;              !!!back-token; # <a>
6468              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6469                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6470              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5835  sub _tree_construction_main ($) { Line 6495  sub _tree_construction_main ($) {
6495          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6496          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6497    
6498            !!!nack ('t374.1');
6499          !!!next-token;          !!!next-token;
6500          redo B;          next B;
6501        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6502          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6503    
6504          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6505          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6506            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6507            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6508              !!!cp ('t376');              !!!cp ('t376');
6509              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
6510              !!!back-token;              !!!back-token; # <nobr>
6511              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6512                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6513              redo B;              next B;
6514            } 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]}) {  
6515              !!!cp ('t377');              !!!cp ('t377');
6516              last INSCOPE;              last INSCOPE;
6517            }            }
# Line 5862  sub _tree_construction_main ($) { Line 6520  sub _tree_construction_main ($) {
6520          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6521          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6522                    
6523            !!!nack ('t377.1');
6524          !!!next-token;          !!!next-token;
6525          redo B;          next B;
6526        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6527          ## has a button element in scope          ## has a button element in scope
6528          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6529            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6530            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6531              !!!cp ('t378');              !!!cp ('t378');
6532              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
6533              !!!back-token;              !!!back-token; # <button>
6534              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6535                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6536              redo B;              next B;
6537            } 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]}) {  
6538              !!!cp ('t379');              !!!cp ('t379');
6539              last INSCOPE;              last INSCOPE;
6540            }            }
# Line 5892  sub _tree_construction_main ($) { Line 6548  sub _tree_construction_main ($) {
6548    
6549          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6550    
6551            !!!nack ('t379.1');
6552          !!!next-token;          !!!next-token;
6553          redo B;          next B;
6554        } elsif ({        } elsif ({
6555                  xmp => 1,                  xmp => 1,
6556                  iframe => 1,                  iframe => 1,
# Line 5909  sub _tree_construction_main ($) { Line 6566  sub _tree_construction_main ($) {
6566          }          }
6567          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
6568          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6569          redo B;          next B;
6570        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6571          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
6572                    
6573          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6574            !!!cp ('t389');            !!!cp ('t389');
6575            ## Ignore the token            ## Ignore the token
6576              !!!nack ('t389'); ## NOTE: Not acknowledged.
6577            !!!next-token;            !!!next-token;
6578            redo B;            next B;
6579          } else {          } else {
6580            my $at = $token->{attributes};            my $at = $token->{attributes};
6581            my $form_attrs;            my $form_attrs;
# Line 5962  sub _tree_construction_main ($) { Line 6620  sub _tree_construction_main ($) {
6620                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6621                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6622                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
6623            $token = shift @tokens;            !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6624            !!!back-token (@tokens);            !!!back-token (@tokens);
6625            redo B;            !!!next-token;
6626              next B;
6627          }          }
6628        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6629          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6630          my $el;          my $el;
6631          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6632                    
6633          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6634          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5978  sub _tree_construction_main ($) { Line 6637  sub _tree_construction_main ($) {
6637          $insert->($el);          $insert->($el);
6638                    
6639          my $text = '';          my $text = '';
6640            !!!nack ('t392.1');
6641          !!!next-token;          !!!next-token;
6642          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6643            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6011  sub _tree_construction_main ($) { Line 6671  sub _tree_construction_main ($) {
6671            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6672          }          }
6673          !!!next-token;          !!!next-token;
6674          redo B;          next B;
6675          } elsif ($token->{tag_name} eq 'math' or
6676                   $token->{tag_name} eq 'svg') {
6677            $reconstruct_active_formatting_elements->($insert_to_current);
6678    
6679            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6680    
6681            ## "adjust foreign attributes" - done in insert-element-f
6682            
6683            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6684            
6685            if ($self->{self_closing}) {
6686              pop @{$self->{open_elements}};
6687              !!!ack ('t398.1');
6688            } else {
6689              !!!cp ('t398.2');
6690              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6691              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6692              ## mode, "in body" (not "in foreign content") secondary insertion
6693              ## mode, maybe.
6694            }
6695    
6696            !!!next-token;
6697            next B;
6698        } elsif ({        } elsif ({
6699                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6700                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6021  sub _tree_construction_main ($) { Line 6704  sub _tree_construction_main ($) {
6704          !!!cp ('t401');          !!!cp ('t401');
6705          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6706          ## Ignore the token          ## Ignore the token
6707            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6708          !!!next-token;          !!!next-token;
6709          redo B;          next B;
6710                    
6711          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6712        } else {        } else {
# Line 6044  sub _tree_construction_main ($) { Line 6728  sub _tree_construction_main ($) {
6728              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
6729            !!!cp ('t380');            !!!cp ('t380');
6730            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
6731              !!!nack ('t380.1');
6732          } elsif ({          } elsif ({
6733                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
6734                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6051  sub _tree_construction_main ($) { Line 6736  sub _tree_construction_main ($) {
6736                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6737            !!!cp ('t375');            !!!cp ('t375');
6738            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
6739              !!!nack ('t375.1');
6740          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
6741            !!!cp ('t388');            !!!cp ('t388');
6742            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6743            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6744              !!!ack ('t388.2');
6745          } elsif ({          } elsif ({
6746                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
6747                    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 6749  sub _tree_construction_main ($) {
6749                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6750            !!!cp ('t388.1');            !!!cp ('t388.1');
6751            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6752              !!!ack ('t388.3');
6753          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
6754            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
6755                    
# Line 6074  sub _tree_construction_main ($) { Line 6762  sub _tree_construction_main ($) {
6762              !!!cp ('t400.2');              !!!cp ('t400.2');
6763              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
6764            }            }
6765              !!!nack ('t400.3');
6766          } else {          } else {
6767            !!!cp ('t402');            !!!nack ('t402');
6768          }          }
6769                    
6770          !!!next-token;          !!!next-token;
6771          redo B;          next B;
6772        }        }
6773      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6774        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6087  sub _tree_construction_main ($) { Line 6776  sub _tree_construction_main ($) {
6776          my $i;          my $i;
6777          INSCOPE: {          INSCOPE: {
6778            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
6779              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
6780                !!!cp ('t405');                !!!cp ('t405');
6781                $i = $_;                $i = $_;
6782                last INSCOPE;                last INSCOPE;
6783              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
6784                !!!cp ('t405.1');                !!!cp ('t405.1');
6785                last;                last;
6786              }              }
# Line 6104  sub _tree_construction_main ($) { Line 6790  sub _tree_construction_main ($) {
6790                            value => $token->{tag_name}, token => $token);                            value => $token->{tag_name}, token => $token);
6791            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
6792            !!!next-token;            !!!next-token;
6793            redo B;            next B;
6794          } # INSCOPE          } # INSCOPE
6795    
6796          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
6797            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]}) {  
6798              !!!cp ('t403');              !!!cp ('t403');
6799              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
6800                                value => $_->[0]->manakai_local_name,
6801                                token => $token);
6802              last;              last;
6803            } else {            } else {
6804              !!!cp ('t404');              !!!cp ('t404');
# Line 6123  sub _tree_construction_main ($) { Line 6807  sub _tree_construction_main ($) {
6807    
6808          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
6809          !!!next-token;          !!!next-token;
6810          redo B;          next B;
6811        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6812          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
6813            ## up-to-date, though it has same effect as speced.
6814            if (@{$self->{open_elements}} > 1 and
6815                $self->{open_elements}->[1]->[1] & BODY_EL) {
6816            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6817            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6818              !!!cp ('t406');              !!!cp ('t406');
6819              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6820                                value => $self->{open_elements}->[1]->[0]
6821                                    ->manakai_local_name,
6822                                token => $token);
6823            } else {            } else {
6824              !!!cp ('t407');              !!!cp ('t407');
6825            }            }
6826            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6827            ## reprocess            ## reprocess
6828            redo B;            next B;
6829          } else {          } else {
6830            !!!cp ('t408');            !!!cp ('t408');
6831            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6832            ## Ignore the token            ## Ignore the token
6833            !!!next-token;            !!!next-token;
6834            redo B;            next B;
6835          }          }
6836        } elsif ({        } elsif ({
6837                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6154  sub _tree_construction_main ($) { Line 6844  sub _tree_construction_main ($) {
6844          my $i;          my $i;
6845          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6846            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6847            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6848              !!!cp ('t410');              !!!cp ('t410');
6849              $i = $_;              $i = $_;
6850              last INSCOPE;              last INSCOPE;
6851            } 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]}) {  
6852              !!!cp ('t411');              !!!cp ('t411');
6853              last INSCOPE;              last INSCOPE;
6854            }            }
# Line 6177  sub _tree_construction_main ($) { Line 6864  sub _tree_construction_main ($) {
6864                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6865                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6866                    p => 1,                    p => 1,
6867                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6868              !!!cp ('t409');              !!!cp ('t409');
6869              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6870            }            }
6871    
6872            ## Step 2.            ## Step 2.
6873            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6874                      ne $token->{tag_name}) {
6875              !!!cp ('t412');              !!!cp ('t412');
6876              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6877                                value => $self->{open_elements}->[-1]->[0]
6878                                    ->manakai_local_name,
6879                                token => $token);
6880            } else {            } else {
6881              !!!cp ('t414');              !!!cp ('t414');
6882            }            }
# Line 6200  sub _tree_construction_main ($) { Line 6891  sub _tree_construction_main ($) {
6891                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6892          }          }
6893          !!!next-token;          !!!next-token;
6894          redo B;          next B;
6895        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6896          undef $self->{form_element};          undef $self->{form_element};
6897    
# Line 6208  sub _tree_construction_main ($) { Line 6899  sub _tree_construction_main ($) {
6899          my $i;          my $i;
6900          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6901            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6902            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6903              !!!cp ('t418');              !!!cp ('t418');
6904              $i = $_;              $i = $_;
6905              last INSCOPE;              last INSCOPE;
6906            } 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]}) {  
6907              !!!cp ('t419');              !!!cp ('t419');
6908              last INSCOPE;              last INSCOPE;
6909            }            }
# Line 6226  sub _tree_construction_main ($) { Line 6914  sub _tree_construction_main ($) {
6914            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6915          } else {          } else {
6916            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6917            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6918              !!!cp ('t417');              !!!cp ('t417');
6919              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6920            }            }
6921                        
6922            ## Step 2.            ## Step 2.
6923            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6924                      ne $token->{tag_name}) {
6925              !!!cp ('t417.1');              !!!cp ('t417.1');
6926              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
6927                                value => $self->{open_elements}->[-1]->[0]
6928                                    ->manakai_local_name,
6929                                token => $token);
6930            } else {            } else {
6931              !!!cp ('t420');              !!!cp ('t420');
6932            }              }  
# Line 6246  sub _tree_construction_main ($) { Line 6936  sub _tree_construction_main ($) {
6936          }          }
6937    
6938          !!!next-token;          !!!next-token;
6939          redo B;          next B;
6940        } elsif ({        } elsif ({
6941                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6942                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6254  sub _tree_construction_main ($) { Line 6944  sub _tree_construction_main ($) {
6944          my $i;          my $i;
6945          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6947            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6948              !!!cp ('t423');              !!!cp ('t423');
6949              $i = $_;              $i = $_;
6950              last INSCOPE;              last INSCOPE;
6951            } 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]}) {  
6952              !!!cp ('t424');              !!!cp ('t424');
6953              last INSCOPE;              last INSCOPE;
6954            }            }
# Line 6274  sub _tree_construction_main ($) { Line 6959  sub _tree_construction_main ($) {
6959            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6960          } else {          } else {
6961            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6962            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6963              !!!cp ('t422');              !!!cp ('t422');
6964              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6965            }            }
6966                        
6967            ## Step 2.            ## Step 2.
6968            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6969                      ne $token->{tag_name}) {
6970              !!!cp ('t425');              !!!cp ('t425');
6971              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6972            } else {            } else {
# Line 6294  sub _tree_construction_main ($) { Line 6978  sub _tree_construction_main ($) {
6978          }          }
6979                    
6980          !!!next-token;          !!!next-token;
6981          redo B;          next B;
6982        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
6983          ## has an element in scope          ## has an element in scope
6984          my $i;          my $i;
6985          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6986            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6987            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6988              !!!cp ('t410.1');              !!!cp ('t410.1');
6989              $i = $_;              $i = $_;
6990              last INSCOPE;              last INSCOPE;
6991            } 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]}) {  
6992              !!!cp ('t411.1');              !!!cp ('t411.1');
6993              last INSCOPE;              last INSCOPE;
6994            }            }
6995          } # INSCOPE          } # INSCOPE
6996    
6997          if (defined $i) {          if (defined $i) {
6998            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6999                      ne $token->{tag_name}) {
7000              !!!cp ('t412.1');              !!!cp ('t412.1');
7001              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7002                                value => $self->{open_elements}->[-1]->[0]
7003                                    ->manakai_local_name,
7004                                token => $token);
7005            } else {            } else {
7006              !!!cp ('t414.1');              !!!cp ('t414.1');
7007            }            }
# Line 6329  sub _tree_construction_main ($) { Line 7014  sub _tree_construction_main ($) {
7014            !!!cp ('t415.1');            !!!cp ('t415.1');
7015            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7016            my $el;            my $el;
7017            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7018            $insert->($el);            $insert->($el);
7019            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7020          }          }
7021    
7022          !!!next-token;          !!!next-token;
7023          redo B;          next B;
7024        } elsif ({        } elsif ({
7025                  a => 1,                  a => 1,
7026                  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 7029  sub _tree_construction_main ($) {
7029                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7030          !!!cp ('t427');          !!!cp ('t427');
7031          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7032          redo B;          next B;
7033        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7034          !!!cp ('t428');          !!!cp ('t428');
7035          !!!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 7038  sub _tree_construction_main ($) {
7038          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7039                    
7040          my $el;          my $el;
7041          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7042          $insert->($el);          $insert->($el);
7043                    
7044          ## Ignore the token.          ## Ignore the token.
7045          !!!next-token;          !!!next-token;
7046          redo B;          next B;
7047        } elsif ({        } elsif ({
7048                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7049                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6375  sub _tree_construction_main ($) { Line 7060  sub _tree_construction_main ($) {
7060          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7061          ## Ignore the token          ## Ignore the token
7062          !!!next-token;          !!!next-token;
7063          redo B;          next B;
7064                    
7065          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7066                    
# Line 6386  sub _tree_construction_main ($) { Line 7071  sub _tree_construction_main ($) {
7071    
7072          ## Step 2          ## Step 2
7073          S2: {          S2: {
7074            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7075              ## Step 1              ## Step 1
7076              ## generate implied end tags              ## generate implied end tags
7077              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7078                !!!cp ('t430');                !!!cp ('t430');
7079                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
7080                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7081              }              }
7082                    
7083              ## Step 2              ## Step 2
7084              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7085                        ne $token->{tag_name}) {
7086                !!!cp ('t431');                !!!cp ('t431');
7087                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7088                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7089                                  value => $self->{open_elements}->[-1]->[0]
7090                                      ->manakai_local_name,
7091                                  token => $token);
7092              } else {              } else {
7093                !!!cp ('t432');                !!!cp ('t432');
7094              }              }
# Line 6413  sub _tree_construction_main ($) { Line 7100  sub _tree_construction_main ($) {
7100              last S2;              last S2;
7101            } else {            } else {
7102              ## Step 3              ## Step 3
7103              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7104                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7105                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7106                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7107                !!!cp ('t433');                !!!cp ('t433');
7108                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7109                ## Ignore the token                ## Ignore the token
# Line 6434  sub _tree_construction_main ($) { Line 7121  sub _tree_construction_main ($) {
7121            ## Step 5;            ## Step 5;
7122            redo S2;            redo S2;
7123          } # S2          } # S2
7124          redo B;          next B;
7125        }        }
7126      }      }
7127      redo B;      next B;
7128      } continue { # B
7129        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7130          ## NOTE: The code below is executed in cases where it does not have
7131          ## to be, but it it is harmless even in those cases.
7132          ## has an element in scope
7133          INSCOPE: {
7134            for (reverse 0..$#{$self->{open_elements}}) {
7135              my $node = $self->{open_elements}->[$_];
7136              if ($node->[1] & FOREIGN_EL) {
7137                last INSCOPE;
7138              } elsif ($node->[1] & SCOPING_EL) {
7139                last;
7140              }
7141            }
7142            
7143            ## NOTE: No foreign element in scope.
7144            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7145          } # INSCOPE
7146        }
7147    } # B    } # B
7148    
7149    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6514  sub set_inner_html ($$$) { Line 7220  sub set_inner_html ($$$) {
7220          !!!cp ('i4');          !!!cp ('i4');
7221          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7222          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7223          } elsif ($self->{next_char} <= 0x0008 or
7224                   (0x000E <= $self->{next_char} and
7225                    $self->{next_char} <= 0x001F) or
7226                   (0x007F <= $self->{next_char} and
7227                    $self->{next_char} <= 0x009F) or
7228                   (0xD800 <= $self->{next_char} and
7229                    $self->{next_char} <= 0xDFFF) or
7230                   (0xFDD0 <= $self->{next_char} and
7231                    $self->{next_char} <= 0xFDDF) or
7232                   {
7233                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7234                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7235                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7236                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7237                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7238                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7239                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7240                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7241                    0x10FFFE => 1, 0x10FFFF => 1,
7242                   }->{$self->{next_char}}) {
7243            !!!cp ('i4.1');
7244            !!!parse-error (type => 'control char', level => $self->{must_level});
7245    ## TODO: error type documentation
7246        }        }
7247      };      };
7248      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6554  sub set_inner_html ($$$) { Line 7283  sub set_inner_html ($$$) {
7283          unless defined $p->{content_model};          unless defined $p->{content_model};
7284          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7285    
7286      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7287          ## TODO: Foreign element OK?
7288    
7289      ## Step 3      ## Step 3
7290      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6564  sub set_inner_html ($$$) { Line 7294  sub set_inner_html ($$$) {
7294      $doc->append_child ($root);      $doc->append_child ($root);
7295    
7296      ## Step 5 # MUST      ## Step 5 # MUST
7297      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7298    
7299      undef $p->{head_element};      undef $p->{head_element};
7300    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24