/[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.82 by wakaba, Wed Mar 5 02:55:08 2008 UTC revision 1.162 by wakaba, Thu Sep 11 09:12:27 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## 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
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 177  sub parse_string ($$$;$) { Line 641  sub parse_string ($$$;$) {
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669          $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672          my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
677        $line++;        $self->{line}++;
678        $column = 0;        $self->{column} = 0;
679      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 714  sub parse_string ($$$;$) {
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738        level => {must => 'm',
739                  should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
745      $self->{next_char} = -1;      $self->{next_char} = -1;
746    };    };
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 802  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
802  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
809  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 820  sub TABLE_IMS ()      { 0b1000000 }
820  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
821  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
822  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
823    sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829    ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
831    ## NOTE: "after after body" insertion mode.
832  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
833    
834    ## NOTE: "after after frameset" insertion mode.
835  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
836    
837  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
838  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
839  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 847  sub IN_TABLE_IM () { TABLE_IMS }
847  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
848  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
849  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
850  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
851    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
852  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
853    
854  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 332  sub _initialize_tokenizer ($) { Line 861  sub _initialize_tokenizer ($) {
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_char}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
# Line 352  sub _initialize_tokenizer ($) { Line 882  sub _initialize_tokenizer ($) {
882  ##        ->{value}  ##        ->{value}
883  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
884  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 378  sub _initialize_tokenizer ($) { Line 911  sub _initialize_tokenizer ($) {
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
# Line 441  sub _get_next_token ($) { Line 984  sub _get_next_token ($) {
984          #          #
985        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
986          !!!cp (11);          !!!cp (11);
987          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
988                      line => $self->{line}, column => $self->{column}});
989          last A; ## TODO: ok?          last A; ## TODO: ok?
990        } else {        } else {
991          !!!cp (12);          !!!cp (12);
992        }        }
993        # Anything else        # Anything else
994        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
996                       line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
# Line 457  sub _get_next_token ($) { Line 1003  sub _get_next_token ($) {
1003        redo A;        redo A;
1004      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1005        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1006    
1007          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1008                
1009        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1010    
# Line 465  sub _get_next_token ($) { Line 1013  sub _get_next_token ($) {
1013    
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!cp (13);          !!!cp (13);
1016          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                      line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020          !!!cp (14);          !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
# Line 484  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034            ## reconsume            ## reconsume
1035            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1036    
1037            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                        line => $self->{line_prev},
1039                        column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
# Line 504  sub _get_next_token ($) { Line 1057  sub _get_next_token ($) {
1057            !!!cp (19);            !!!cp (19);
1058            $self->{current_token}            $self->{current_token}
1059              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1060                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1061                   line => $self->{line_prev},
1062                   column => $self->{column_prev}};
1063            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
# Line 512  sub _get_next_token ($) { Line 1067  sub _get_next_token ($) {
1067                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1068            !!!cp (20);            !!!cp (20);
1069            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1070                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1071                                        line => $self->{line_prev},
1072                                        column => $self->{column_prev}};
1073            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1074            !!!next-input-character;            !!!next-input-character;
1075            redo A;            redo A;
1076          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1077            !!!cp (21);            !!!cp (21);
1078            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1079                              line => $self->{line_prev},
1080                              column => $self->{column_prev});
1081            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083    
1084            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                        line => $self->{line_prev},
1086                        column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1091            !!!cp (22);            !!!cp (22);
1092            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1093                              line => $self->{line_prev},
1094                              column => $self->{column_prev});
1095            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1096              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                        line => $self->{line_prev},
1098                                        column => $self->{column_prev},
1099                                       };
1100            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!cp (23);            !!!cp (23);
1104            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                        line => $self->{line_prev},
1112                        column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
# Line 545  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1119        }        }
1120      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1121          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1122        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1123          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1124    
1125            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1126            my @next_char;            my @next_char;
1127            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 563  sub _get_next_token ($) { Line 1138  sub _get_next_token ($) {
1138                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1139                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1140    
1141                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                            line => $l, column => $c,
1143                           });
1144        
1145                redo A;                redo A;
1146              }              }
# Line 582  sub _get_next_token ($) { Line 1159  sub _get_next_token ($) {
1159              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1162              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163                          line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166            } else {            } else {
1167              !!!cp (27);              !!!cp (27);
# Line 595  sub _get_next_token ($) { Line 1174  sub _get_next_token ($) {
1174            !!!cp (28);            !!!cp (28);
1175            # next-input-character is already done            # next-input-character is already done
1176            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1177            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                        line => $l, column => $c,
1179                       });
1180            redo A;            redo A;
1181          }          }
1182        }        }
# Line 603  sub _get_next_token ($) { Line 1184  sub _get_next_token ($) {
1184        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1185            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1186          !!!cp (29);          !!!cp (29);
1187          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1188                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1189                   tag_name => chr ($self->{next_char} + 0x0020),
1190                   line => $l, column => $c};
1191          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1192          !!!next-input-character;          !!!next-input-character;
1193          redo A;          redo A;
# Line 612  sub _get_next_token ($) { Line 1195  sub _get_next_token ($) {
1195                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1196          !!!cp (30);          !!!cp (30);
1197          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1198                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1199                                      line => $l, column => $c};
1200          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1201          !!!next-input-character;          !!!next-input-character;
1202          redo A;          redo A;
1203        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1204          !!!cp (31);          !!!cp (31);
1205          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1206                            line => $self->{line_prev}, ## "<" in "</>"
1207                            column => $self->{column_prev} - 1);
1208          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
# Line 628  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                      line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
1223          !!!cp (33);          !!!cp (33);
1224          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1225          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1226            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227                                      line => $self->{line_prev}, # "<" of "</"
1228                                      column => $self->{column_prev} - 1,
1229                                     };
1230          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
1231          redo A;          redo A;
1232        }        }
# Line 651  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1244          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 684  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 706  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!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  
1300          redo A;          redo A;
1301        } else {        } else {
1302          !!!cp (44);          !!!cp (44);
# Line 741  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
1319        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 764  sub _get_next_token ($) { Line 1340  sub _get_next_token ($) {
1340        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          !!!cp (49);          !!!cp (49);
1343          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1344                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!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  
1354          redo A;          redo A;
1355        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 819  sub _get_next_token ($) { Line 1385  sub _get_next_token ($) {
1385          } else {          } else {
1386            !!!cp (56);            !!!cp (56);
1387          }          }
1388          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1389                                value => ''};              = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
# Line 830  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!cp (57);            !!!cp (57);
1401            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404            !!!cp (58);            !!!cp (58);
# Line 859  sub _get_next_token ($) { Line 1427  sub _get_next_token ($) {
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432            !!!cp (62);            !!!cp (62);
# Line 885  sub _get_next_token ($) { Line 1451  sub _get_next_token ($) {
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!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  
1458          redo A;          redo A;
1459        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 957  sub _get_next_token ($) { Line 1511  sub _get_next_token ($) {
1511        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 981  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          !!!cp (76);          !!!cp (76);
1536          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1537                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!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  
1547          redo A;          redo A;
1548        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1028  sub _get_next_token ($) { Line 1569  sub _get_next_token ($) {
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1573          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1574                                value => ''};            !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
# Line 1061  sub _get_next_token ($) { Line 1610  sub _get_next_token ($) {
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613            !!!parse-error (type => 'empty unquoted attribute value');
1614          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1088  sub _get_next_token ($) { Line 1636  sub _get_next_token ($) {
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1683  sub _get_next_token ($) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1725  sub _get_next_token ($) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1228  sub _get_next_token ($) { Line 1770  sub _get_next_token ($) {
1770        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1771          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1253  sub _get_next_token ($) { Line 1793  sub _get_next_token ($) {
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1325  sub _get_next_token ($) { Line 1863  sub _get_next_token ($) {
1863        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1864          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1866            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1347  sub _get_next_token ($) { Line 1883  sub _get_next_token ($) {
1883    
1884          redo A;          redo A;
1885        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888          !!!next-input-character;          !!!next-input-character;
1889          if ($self->{next_char} == 0x003E and # >          redo A;
1890              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1891              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1892            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893            !!!cp (122);            !!!cp (122.3);
1894            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903          } else {          } else {
1904            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1905          }          }
1906          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1907          # next-input-character is already done          ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909          redo A;          redo A;
1910        } else {        } else {
1911          !!!cp (124);          !!!cp ('124.1');
1912          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1913          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914          ## reconsume          ## reconsume
1915          redo A;          redo A;
1916        }        }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
1973        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1974          #my $token = {type => COMMENT_TOKEN, data => ''};
1975    
1976        BC: {        BC: {
1977          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1379  sub _get_next_token ($) { Line 1979  sub _get_next_token ($) {
1979            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1980            !!!next-input-character;            !!!next-input-character;
1981    
1982            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1983    
1984            redo A;            redo A;
1985          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1387  sub _get_next_token ($) { Line 1987  sub _get_next_token ($) {
1987            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1988            ## reconsume            ## reconsume
1989    
1990            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;            redo A;
1993          } else {          } else {
1994            !!!cp (126);            !!!cp (126);
1995            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1996            !!!next-input-character;            !!!next-input-character;
1997            redo BC;            redo BC;
1998          }          }
# Line 1402  sub _get_next_token ($) { Line 2002  sub _get_next_token ($) {
2002      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2003        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2004    
2005          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
2006    
2007        my @next_char;        my @next_char;
2008        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
2009                
# Line 1410  sub _get_next_token ($) { Line 2012  sub _get_next_token ($) {
2012          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
2013          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            !!!cp (127);            !!!cp (127);
2015            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                        line => $l, column => $c,
2017                                       };
2018            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
# Line 1446  sub _get_next_token ($) { Line 2050  sub _get_next_token ($) {
2050                      !!!cp (129);                      !!!cp (129);
2051                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
2052                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2053                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                  quirks => 1,
2055                                                  line => $l, column => $c,
2056                                                 };
2057                      !!!next-input-character;                      !!!next-input-character;
2058                      redo A;                      redo A;
2059                    } else {                    } else {
# Line 1466  sub _get_next_token ($) { Line 2074  sub _get_next_token ($) {
2074          } else {          } else {
2075            !!!cp (135);            !!!cp (135);
2076          }          }
2077          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080            !!!next-input-character;
2081            push @next_char, $self->{next_char};
2082            if ($self->{next_char} == 0x0043) { # C
2083              !!!next-input-character;
2084              push @next_char, $self->{next_char};
2085              if ($self->{next_char} == 0x0044) { # D
2086                !!!next-input-character;
2087                push @next_char, $self->{next_char};
2088                if ($self->{next_char} == 0x0041) { # A
2089                  !!!next-input-character;
2090                  push @next_char, $self->{next_char};
2091                  if ($self->{next_char} == 0x0054) { # T
2092                    !!!next-input-character;
2093                    push @next_char, $self->{next_char};
2094                    if ($self->{next_char} == 0x0041) { # A
2095                      !!!next-input-character;
2096                      push @next_char, $self->{next_char};
2097                      if ($self->{next_char} == 0x005B) { # [
2098                        !!!cp (135.1);
2099                        $self->{state} = CDATA_BLOCK_STATE;
2100                        !!!next-input-character;
2101                        redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                      }
2105                    } else {
2106                      !!!cp (135.3);
2107                    }
2108                  } else {
2109                    !!!cp (135.4);                
2110                  }
2111                } else {
2112                  !!!cp (135.5);
2113                }
2114              } else {
2115                !!!cp (135.6);
2116              }
2117            } else {
2118              !!!cp (135.7);
2119            }
2120        } else {        } else {
2121          !!!cp (136);          !!!cp (136);
2122        }        }
# Line 1474  sub _get_next_token ($) { Line 2125  sub _get_next_token ($) {
2125        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2128          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                    line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1597  sub _get_next_token ($) { Line 2251  sub _get_next_token ($) {
2251          redo A;          redo A;
2252        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2253          !!!cp (152);          !!!cp (152);
2254          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2255                            line => $self->{line_prev},
2256                            column => $self->{column_prev});
2257          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2258          ## Stay in the state          ## Stay in the state
2259          !!!next-input-character;          !!!next-input-character;
# Line 1613  sub _get_next_token ($) { Line 2269  sub _get_next_token ($) {
2269          redo A;          redo A;
2270        } else {        } else {
2271          !!!cp (154);          !!!cp (154);
2272          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2273                            line => $self->{line_prev},
2274                            column => $self->{column_prev});
2275          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2276          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2277          !!!next-input-character;          !!!next-input-character;
# Line 1652  sub _get_next_token ($) { Line 2310  sub _get_next_token ($) {
2310          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2311          !!!next-input-character;          !!!next-input-character;
2312    
2313          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2314    
2315          redo A;          redo A;
2316        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1661  sub _get_next_token ($) { Line 2319  sub _get_next_token ($) {
2319          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          !!!cp (160);          !!!cp (160);
2327          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2328              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2329  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2330          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2331          !!!next-input-character;          !!!next-input-character;
# Line 2061  sub _get_next_token ($) { Line 2716  sub _get_next_token ($) {
2716          redo A;          redo A;
2717        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2718          !!!cp (208);          !!!cp (208);
2719          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2722          !!!next-input-character;          !!!next-input-character;
# Line 2097  sub _get_next_token ($) { Line 2752  sub _get_next_token ($) {
2752          redo A;          redo A;
2753        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2754          !!!cp (212);          !!!cp (212);
2755          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2756    
2757          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2758          !!!next-input-character;          !!!next-input-character;
# Line 2145  sub _get_next_token ($) { Line 2800  sub _get_next_token ($) {
2800        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2801          !!!cp (217);          !!!cp (217);
2802          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2803          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2804          ## reconsume          ## reconsume
2805    
# Line 2186  sub _get_next_token ($) { Line 2840  sub _get_next_token ($) {
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2844          my $s = '';
2845          
2846          my ($l, $c) = ($self->{line}, $self->{column});
2847    
2848          CS: while ($self->{next_char} != -1) {
2849            if ($self->{next_char} == 0x005D) { # ]
2850              !!!next-input-character;
2851              if ($self->{next_char} == 0x005D) { # ]
2852                !!!next-input-character;
2853                MDC: {
2854                  if ($self->{next_char} == 0x003E) { # >
2855                    !!!cp (221.1);
2856                    !!!next-input-character;
2857                    last CS;
2858                  } elsif ($self->{next_char} == 0x005D) { # ]
2859                    !!!cp (221.2);
2860                    $s .= ']';
2861                    !!!next-input-character;
2862                    redo MDC;
2863                  } else {
2864                    !!!cp (221.3);
2865                    $s .= ']]';
2866                    #
2867                  }
2868                } # MDC
2869              } else {
2870                !!!cp (221.4);
2871                $s .= ']';
2872                #
2873              }
2874            } else {
2875              !!!cp (221.5);
2876              #
2877            }
2878            $s .= chr $self->{next_char};
2879            !!!next-input-character;
2880          } # CS
2881    
2882          $self->{state} = DATA_STATE;
2883          ## next-input-character done or EOF, which is reconsumed.
2884    
2885          if (length $s) {
2886            !!!cp (221.6);
2887            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2888                      line => $l, column => $c});
2889          } else {
2890            !!!cp (221.7);
2891          }
2892    
2893          redo A;
2894    
2895          ## ISSUE: "text tokens" in spec.
2896          ## TODO: Streaming support
2897      } else {      } else {
2898        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2899      }      }
# Line 2197  sub _get_next_token ($) { Line 2905  sub _get_next_token ($) {
2905  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2906    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2907    
2908      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2909    
2910    if ({    if ({
2911         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2912         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2237  sub _tokenize_attempt_to_consume_an_enti Line 2947  sub _tokenize_attempt_to_consume_an_enti
2947            redo X;            redo X;
2948          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2949            !!!cp (1005);            !!!cp (1005);
2950            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2951            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2952            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2953            return undef;            return undef;
# Line 2246  sub _tokenize_attempt_to_consume_an_enti Line 2956  sub _tokenize_attempt_to_consume_an_enti
2956            !!!next-input-character;            !!!next-input-character;
2957          } else {          } else {
2958            !!!cp (1007);            !!!cp (1007);
2959            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2960          }          }
2961    
2962          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2963            !!!cp (1008);            !!!cp (1008);
2964            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => 'invalid character reference',
2965                              text => (sprintf 'U+%04X', $code),
2966                              line => $l, column => $c);
2967            $code = 0xFFFD;            $code = 0xFFFD;
2968          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2969            !!!cp (1009);            !!!cp (1009);
2970            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => 'invalid character reference',
2971                              text => (sprintf 'U-%08X', $code),
2972                              line => $l, column => $c);
2973            $code = 0xFFFD;            $code = 0xFFFD;
2974          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2975            !!!cp (1010);            !!!cp (1010);
2976            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2977            $code = 0x000A;            $code = 0x000A;
2978          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2979            !!!cp (1011);            !!!cp (1011);
2980            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2981            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2982          }          }
2983    
2984          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2985                  has_reference => 1};                  has_reference => 1,
2986                    line => $l, column => $c,
2987                   };
2988        } # X        } # X
2989      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2990               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2289  sub _tokenize_attempt_to_consume_an_enti Line 3005  sub _tokenize_attempt_to_consume_an_enti
3005          !!!next-input-character;          !!!next-input-character;
3006        } else {        } else {
3007          !!!cp (1014);          !!!cp (1014);
3008          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
3009        }        }
3010    
3011        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3012          !!!cp (1015);          !!!cp (1015);
3013          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3014                            text => (sprintf 'U+%04X', $code),
3015                            line => $l, column => $c);
3016          $code = 0xFFFD;          $code = 0xFFFD;
3017        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3018          !!!cp (1016);          !!!cp (1016);
3019          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3020                            text => (sprintf 'U-%08X', $code),
3021                            line => $l, column => $c);
3022          $code = 0xFFFD;          $code = 0xFFFD;
3023        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3024          !!!cp (1017);          !!!cp (1017);
3025          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3026                            line => $l, column => $c);
3027          $code = 0x000A;          $code = 0x000A;
3028        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3029          !!!cp (1018);          !!!cp (1018);
3030          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3031                            text => (sprintf 'U+%04X', $code),
3032                            line => $l, column => $c);
3033          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3034        }        }
3035                
3036        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3037                  line => $l, column => $c,
3038                 };
3039      } else {      } else {
3040        !!!cp (1019);        !!!cp (1019);
3041        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3042        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
3043        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
3044        return undef;        return undef;
# Line 2330  sub _tokenize_attempt_to_consume_an_enti Line 3055  sub _tokenize_attempt_to_consume_an_enti
3055      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3056      our $EntityChar;      our $EntityChar;
3057    
3058      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3059             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3060             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3061               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2363  sub _tokenize_attempt_to_consume_an_enti Line 3088  sub _tokenize_attempt_to_consume_an_enti
3088            
3089      if ($match > 0) {      if ($match > 0) {
3090        !!!cp (1023);        !!!cp (1023);
3091        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3092                  line => $l, column => $c,
3093                 };
3094      } elsif ($match < 0) {      } elsif ($match < 0) {
3095        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3096        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3097          !!!cp (1024);          !!!cp (1024);
3098          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3099                    line => $l, column => $c,
3100                   };
3101        } else {        } else {
3102          !!!cp (1025);          !!!cp (1025);
3103          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3104                    line => $l, column => $c,
3105                   };
3106        }        }
3107      } else {      } else {
3108        !!!cp (1026);        !!!cp (1026);
3109        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3110        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3111        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3112                  line => $l, column => $c,
3113                 };
3114      }      }
3115    } else {    } else {
3116      !!!cp (1027);      !!!cp (1027);
3117      ## no characters are consumed      ## no characters are consumed
3118      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3119      return undef;      return undef;
3120    }    }
3121  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2394  sub _initialize_tree_constructor ($) { Line 3127  sub _initialize_tree_constructor ($) {
3127    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3128    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3129    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3130      $self->{document}->set_user_data (manakai_source_line => 1);
3131      $self->{document}->set_user_data (manakai_source_column => 1);
3132  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3133    
3134  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2420  sub _construct_tree ($) { Line 3155  sub _construct_tree ($) {
3155        
3156    !!!next-token;    !!!next-token;
3157    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3158    undef $self->{form_element};    undef $self->{form_element};
3159    undef $self->{head_element};    undef $self->{head_element};
3160    $self->{open_elements} = [];    $self->{open_elements} = [];
3161    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3162    
3163      ## NOTE: The "initial" insertion mode.
3164    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3165    
3166      ## NOTE: The "before html" insertion mode.
3167    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3168      $self->{insertion_mode} = BEFORE_HEAD_IM;
3169    
3170      ## NOTE: The "before head" insertion mode and so on.
3171    $self->_tree_construction_main;    $self->_tree_construction_main;
3172  } # _construct_tree  } # _construct_tree
3173    
3174  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3175    my $self = shift;    my $self = shift;
3176    
3177      ## NOTE: "initial" insertion mode
3178    
3179    INITIAL: {    INITIAL: {
3180      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3181        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2440  sub _tree_construction_initial ($) { Line 3183  sub _tree_construction_initial ($) {
3183        ## language.        ## language.
3184        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3185        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3186        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3187        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3188            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3189          !!!cp ('t1');          !!!cp ('t1');
3190          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3191        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3192          !!!cp ('t2');          !!!cp ('t2');
3193          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3194          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3195            if ($token->{public_identifier} eq 'XSLT-compat') {
3196              !!!cp ('t1.2');
3197              !!!parse-error (type => 'XSLT-compat', token => $token,
3198                              level => $self->{level}->{should});
3199            } else {
3200              !!!parse-error (type => 'not HTML5', token => $token);
3201            }
3202        } else {        } else {
3203          !!!cp ('t3');          !!!cp ('t3');
3204            #
3205        }        }
3206                
3207        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3208          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3209          ## NOTE: Default value for both |public_id| and |system_id| attributes
3210          ## are empty strings, so that we don't set any value in missing cases.
3211        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3212            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3213        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2470  sub _tree_construction_initial ($) { Line 3222  sub _tree_construction_initial ($) {
3222        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3223          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3224          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3225          if ({          my $prefix = [
3226            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3227            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3228            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3229            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3230            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3231            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3232            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3233            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3234            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3235            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3236            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3237            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3238            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3239            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3240            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3241            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3242            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3243            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3244            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3245            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3246            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3247            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3248            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3249            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3250            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3251            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3252            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3253            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3254            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3255            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3256            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3257            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3258            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3259            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3260            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3261            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3262            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3263            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3264            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3265            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3266            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3267            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3268            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3269            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3270            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3271            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3272            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3273            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3274            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3275            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3276            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3277            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3278            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3279            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3280            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3281            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3282            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3283            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3284            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3285            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3286            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3287            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3288            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3289            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3290            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3291            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3292            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3293            !!!cp ('t5');            !!!cp ('t5');
3294            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3295          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3296                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3297            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3298              !!!cp ('t6');              !!!cp ('t6');
3299              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2555  sub _tree_construction_initial ($) { Line 3301  sub _tree_construction_initial ($) {
3301              !!!cp ('t7');              !!!cp ('t7');
3302              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3303            }            }
3304          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3305                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3306            !!!cp ('t8');            !!!cp ('t8');
3307            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3308          } else {          } else {
# Line 2569  sub _tree_construction_initial ($) { Line 3315  sub _tree_construction_initial ($) {
3315          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3316          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3317          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3318            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3319              ## marked as quirks.
3320            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3321            !!!cp ('t11');            !!!cp ('t11');
3322          } else {          } else {
# Line 2579  sub _tree_construction_initial ($) { Line 3326  sub _tree_construction_initial ($) {
3326          !!!cp ('t13');          !!!cp ('t13');
3327        }        }
3328                
3329        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3330        !!!next-token;        !!!next-token;
3331        return;        return;
3332      } elsif ({      } elsif ({
# Line 2588  sub _tree_construction_initial ($) { Line 3335  sub _tree_construction_initial ($) {
3335                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3336               }->{$token->{type}}) {               }->{$token->{type}}) {
3337        !!!cp ('t14');        !!!cp ('t14');
3338        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3339        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3340        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3341        ## reprocess        ## reprocess
3342          !!!ack-later;
3343        return;        return;
3344      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3345        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2599  sub _tree_construction_initial ($) { Line 3347  sub _tree_construction_initial ($) {
3347    
3348          unless (length $token->{data}) {          unless (length $token->{data}) {
3349            !!!cp ('t15');            !!!cp ('t15');
3350            ## Stay in the phase            ## Stay in the insertion mode.
3351            !!!next-token;            !!!next-token;
3352            redo INITIAL;            redo INITIAL;
3353          } else {          } else {
# Line 2609  sub _tree_construction_initial ($) { Line 3357  sub _tree_construction_initial ($) {
3357          !!!cp ('t17');          !!!cp ('t17');
3358        }        }
3359    
3360        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3361        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3362        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3363        ## reprocess        ## reprocess
3364        return;        return;
3365      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
# Line 2619  sub _tree_construction_initial ($) { Line 3367  sub _tree_construction_initial ($) {
3367        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3368        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3369                
3370        ## Stay in the phase.        ## Stay in the insertion mode.
3371        !!!next-token;        !!!next-token;
3372        redo INITIAL;        redo INITIAL;
3373      } else {      } else {
# Line 2632  sub _tree_construction_initial ($) { Line 3380  sub _tree_construction_initial ($) {
3380    
3381  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3382    my $self = shift;    my $self = shift;
3383    
3384      ## NOTE: "before html" insertion mode.
3385        
3386    B: {    B: {
3387        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3388          !!!cp ('t19');          !!!cp ('t19');
3389          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3390          ## Ignore the token          ## Ignore the token
3391          ## Stay in the phase          ## Stay in the insertion mode.
3392          !!!next-token;          !!!next-token;
3393          redo B;          redo B;
3394        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3395          !!!cp ('t20');          !!!cp ('t20');
3396          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3397          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3398          ## Stay in the phase          ## Stay in the insertion mode.
3399          !!!next-token;          !!!next-token;
3400          redo B;          redo B;
3401        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2654  sub _tree_construction_root_element ($) Line 3404  sub _tree_construction_root_element ($)
3404    
3405            unless (length $token->{data}) {            unless (length $token->{data}) {
3406              !!!cp ('t21');              !!!cp ('t21');
3407              ## Stay in the phase              ## Stay in the insertion mode.
3408              !!!next-token;              !!!next-token;
3409              redo B;              redo B;
3410            } else {            } else {
# Line 2668  sub _tree_construction_root_element ($) Line 3418  sub _tree_construction_root_element ($)
3418    
3419          #          #
3420        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3421          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3422              $token->{attributes}->{manifest}) {            my $root_element;
3423            !!!cp ('t24');            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3424            $self->{application_cache_selection}            $self->{document}->append_child ($root_element);
3425                 ->($token->{attributes}->{manifest}->{value});            push @{$self->{open_elements}},
3426            ## ISSUE: No relative reference resolution?                [$root_element, $el_category->{html}];
3427    
3428              if ($token->{attributes}->{manifest}) {
3429                !!!cp ('t24');
3430                $self->{application_cache_selection}
3431                    ->($token->{attributes}->{manifest}->{value});
3432                ## ISSUE: Spec is unclear on relative references.
3433                ## According to Hixie (#whatwg 2008-03-19), it should be
3434                ## resolved against the base URI of the document in HTML
3435                ## or xml:base of the element in XHTML.
3436              } else {
3437                !!!cp ('t25');
3438                $self->{application_cache_selection}->(undef);
3439              }
3440    
3441              !!!nack ('t25c');
3442    
3443              !!!next-token;
3444              return; ## Go to the "before head" insertion mode.
3445          } else {          } else {
3446            !!!cp ('t25');            !!!cp ('t25.1');
3447            $self->{application_cache_selection}->(undef);            #
3448          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3449        } elsif ({        } elsif ({
3450                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3451                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3452                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3453          !!!cp ('t26');          !!!cp ('t26');
         $self->{application_cache_selection}->(undef);  
   
         ## ISSUE: There is an issue in the spec  
3454          #          #
3455        } else {        } else {
3456          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3457        }        }
3458    
3459        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3460        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3461        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3462        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3463        #redo B;  
3464        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3465    
3466        ## NOTE: Reprocess the token.
3467        !!!ack-later;
3468        return; ## Go to the "before head" insertion mode.
3469    
3470        ## ISSUE: There is an issue in the spec
3471    } # B    } # B
3472    
3473    die "$0: _tree_construction_root_element: This should never be reached";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2717  sub _reset_insertion_mode ($) { Line 3485  sub _reset_insertion_mode ($) {
3485            
3486      ## Step 3      ## Step 3
3487      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3488        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3489          $last = 1;          $last = 1;
3490          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3491            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3492                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3493              !!!cp ('t27');          } else {
3494              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3495          }          }
3496        }        }
3497              
3498        ## Step 4..13        ## Step 4..14
3499        my $new_mode = {        my $new_mode;
3500          if ($node->[1] & FOREIGN_EL) {
3501            !!!cp ('t28.1');
3502            ## NOTE: Strictly spaking, the line below only applies to MathML and
3503            ## SVG elements.  Currently the HTML syntax supports only MathML and
3504            ## SVG elements as foreigners.
3505            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3506          } elsif ($node->[1] & TABLE_CELL_EL) {
3507            if ($last) {
3508              !!!cp ('t28.2');
3509              #
3510            } else {
3511              !!!cp ('t28.3');
3512              $new_mode = IN_CELL_IM;
3513            }
3514          } else {
3515            !!!cp ('t28.4');
3516            $new_mode = {
3517                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3518                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3519                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3520                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3521                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3522                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2751  sub _reset_insertion_mode ($) { Line 3527  sub _reset_insertion_mode ($) {
3527                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3528                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3529                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3530                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3531          }
3532        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3533                
3534        ## Step 14        ## Step 15
3535        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3536          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3537            !!!cp ('t29');            !!!cp ('t29');
3538            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2769  sub _reset_insertion_mode ($) { Line 3546  sub _reset_insertion_mode ($) {
3546          !!!cp ('t31');          !!!cp ('t31');
3547        }        }
3548                
3549        ## Step 15        ## Step 16
3550        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3551                
3552        ## Step 16        ## Step 17
3553        $i--;        $i--;
3554        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3555                
3556        ## Step 17        ## Step 18
3557        redo S3;        redo S3;
3558      } # S3      } # S3
3559    
# Line 2880  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657      !!!cp ('t39');      !!!cp ('t39');
3658    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3659    
3660    my $parse_rcdata = sub ($$) {    my $insert;
3661      my ($content_model_flag, $insert) = @_;  
3662      my $parse_rcdata = sub ($) {
3663        my ($content_model_flag) = @_;
3664    
3665      ## Step 1      ## Step 1
3666      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3667      my $el;      my $el;
3668      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3669    
3670      ## Step 2      ## Step 2
3671      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3672    
3673      ## Step 3      ## Step 3
3674      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2897  sub _tree_construction_main ($) { Line 3676  sub _tree_construction_main ($) {
3676    
3677      ## Step 4      ## Step 4
3678      my $text = '';      my $text = '';
3679        !!!nack ('t40.1');
3680      !!!next-token;      !!!next-token;
3681      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3682        !!!cp ('t40');        !!!cp ('t40');
# Line 2919  sub _tree_construction_main ($) { Line 3699  sub _tree_construction_main ($) {
3699          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3700        !!!cp ('t42');        !!!cp ('t42');
3701        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3702      } else {      } else {
3703        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3704          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3705            !!!cp ('t43');
3706            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3707          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3708            !!!cp ('t44');
3709            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3710          } else {
3711            die "$0: $content_model_flag in parse_rcdata";
3712          }
3713      }      }
3714      !!!next-token;      !!!next-token;
3715    }; # $parse_rcdata    }; # $parse_rcdata
3716    
3717    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3718      my $script_el;      my $script_el;
3719      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3720      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3721    
3722      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3723      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3724            
3725      my $text = '';      my $text = '';
3726        !!!nack ('t45.1');
3727      !!!next-token;      !!!next-token;
3728      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3729        !!!cp ('t45');        !!!cp ('t45');
# Line 2960  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743        ## Ignore the token        ## Ignore the token
3744      } else {      } else {
3745        !!!cp ('t48');        !!!cp ('t48');
3746        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3747        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3748        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3749      }      }
# Line 2983  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766      !!!next-token;      !!!next-token;
3767    }; # $script_start_tag    }; # $script_start_tag
3768    
3769      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3770      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3771      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3772    
3773    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3774      my $tag_name = shift;      my $end_tag_token = shift;
3775        my $tag_name = $end_tag_token->{tag_name};
3776    
3777        ## NOTE: The adoption agency algorithm (AAA).
3778    
3779      FET: {      FET: {
3780        ## Step 1        ## Step 1
3781        my $formatting_element;        my $formatting_element;
3782        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3783        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3784          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3785              !!!cp ('t52');
3786              last AFE;
3787            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3788                         eq $tag_name) {
3789            !!!cp ('t51');            !!!cp ('t51');
3790            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3791            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3792            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3793          }          }
3794        } # AFE        } # AFE
3795        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3796          !!!cp ('t53');          !!!cp ('t53');
3797          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3798          ## Ignore the token          ## Ignore the token
3799          !!!next-token;          !!!next-token;
3800          return;          return;
# Line 3020  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811              last INSCOPE;              last INSCOPE;
3812            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3813              !!!cp ('t55');              !!!cp ('t55');
3814              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
3815                                text => $token->{tag_name},
3816                                token => $end_tag_token);
3817              ## Ignore the token              ## Ignore the token
3818              !!!next-token;              !!!next-token;
3819              return;              return;
3820            }            }
3821          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3822            !!!cp ('t56');            !!!cp ('t56');
3823            $in_scope = 0;            $in_scope = 0;
3824          }          }
3825        } # INSCOPE        } # INSCOPE
3826        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3827          !!!cp ('t57');          !!!cp ('t57');
3828          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
3829                            text => $token->{tag_name},
3830                            token => $end_tag_token);
3831          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3832          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3833          return;          return;
3834        }        }
3835        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3836          !!!cp ('t58');          !!!cp ('t58');
3837          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3838                            text => $self->{open_elements}->[-1]->[0]
3839                                ->manakai_local_name,
3840                            token => $end_tag_token);
3841        }        }
3842                
3843        ## Step 2        ## Step 2
# Line 3050  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3846        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3847          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3848          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3849              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3850              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3851               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3852            !!!cp ('t59');            !!!cp ('t59');
3853            $furthest_block = $node;            $furthest_block = $node;
3854            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3139  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934        } # S7          } # S7  
3935                
3936        ## Step 8        ## Step 8
3937        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3938            my $foster_parent_element;
3939            my $next_sibling;
3940            OE: for (reverse 0..$#{$self->{open_elements}}) {
3941              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3942                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3943                                 if (defined $parent and $parent->node_type == 1) {
3944                                   !!!cp ('t65.1');
3945                                   $foster_parent_element = $parent;
3946                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3947                                 } else {
3948                                   !!!cp ('t65.2');
3949                                   $foster_parent_element
3950                                     = $self->{open_elements}->[$_ - 1]->[0];
3951                                 }
3952                                 last OE;
3953                               }
3954                             } # OE
3955                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3956                               unless defined $foster_parent_element;
3957            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3958            $open_tables->[-1]->[1] = 1; # tainted
3959          } else {
3960            !!!cp ('t65.3');
3961            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3962          }
3963                
3964        ## Step 9        ## Step 9
3965        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3185  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005      } # FET      } # FET
4006    }; # $formatting_end_tag    }; # $formatting_end_tag
4007    
4008    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4009      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4010    }; # $insert_to_current    }; # $insert_to_current
4011    
4012    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4013                         my $child = shift;      my $child = shift;
4014                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4015                              table => 1, tbody => 1, tfoot => 1,        # MUST
4016                              thead => 1, tr => 1,        my $foster_parent_element;
4017                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4018                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4019                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
4020                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4021                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4022                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3217  sub _tree_construction_main ($) { Line 4034  sub _tree_construction_main ($) {
4034                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4035                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4036                             ($child, $next_sibling);                             ($child, $next_sibling);
4037                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4038                           !!!cp ('t72');      } else {
4039                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4040                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4041        }
4042    }; # $insert_to_foster    }; # $insert_to_foster
4043    
4044    my $insert;    B: while (1) {
   
   B: {  
4045      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4046        !!!cp ('t73');        !!!cp ('t73');
4047        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4048        ## Ignore the token        ## Ignore the token
4049        ## Stay in the phase        ## Stay in the phase
4050        !!!next-token;        !!!next-token;
4051        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4052      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4053               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4054        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4055          !!!cp ('t79');          !!!cp ('t79');
4056          ## Turn into the main phase          !!!parse-error (type => 'after html', text => 'html', token => $token);
         !!!parse-error (type => 'after html:html');  
4057          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4058        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4059          !!!cp ('t80');          !!!cp ('t80');
4060          ## Turn into the main phase          !!!parse-error (type => 'after html', text => 'html', token => $token);
         !!!parse-error (type => 'after html:html');  
4061          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4062        } else {        } else {
4063          !!!cp ('t81');          !!!cp ('t81');
4064        }        }
4065    
4066  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4067  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!cp ('t82');  
         !!!parse-error (type => 'not first start tag');  
       } else {  
         !!!cp ('t83');  
       }  
4068        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4069        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4070          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3301  sub _tree_construction_main ($) { Line 4074  sub _tree_construction_main ($) {
4074               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4075          }          }
4076        }        }
4077          !!!nack ('t84.1');
4078        !!!next-token;        !!!next-token;
4079        redo B;        next B;
4080      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4081        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4082        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3316  sub _tree_construction_main ($) { Line 4090  sub _tree_construction_main ($) {
4090          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4091        }        }
4092        !!!next-token;        !!!next-token;
4093        redo B;        next B;
4094      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4095          if ($token->{type} == CHARACTER_TOKEN) {
4096            !!!cp ('t87.1');
4097            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4098            !!!next-token;
4099            next B;
4100          } elsif ($token->{type} == START_TAG_TOKEN) {
4101            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4102                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4103                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4104                ($token->{tag_name} eq 'svg' and
4105                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4106              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4107              !!!cp ('t87.2');
4108              #
4109            } elsif ({
4110                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4111                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4112                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4113                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4114                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4115                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4116                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4117                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4118                     }->{$token->{tag_name}}) {
4119              !!!cp ('t87.2');
4120              !!!parse-error (type => 'not closed',
4121                              text => $self->{open_elements}->[-1]->[0]
4122                                  ->manakai_local_name,
4123                              token => $token);
4124    
4125              pop @{$self->{open_elements}}
4126                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4127    
4128              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4129              ## Reprocess.
4130              next B;
4131            } else {
4132              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4133              my $tag_name = $token->{tag_name};
4134              if ($nsuri eq $SVG_NS) {
4135                $tag_name = {
4136                   altglyph => 'altGlyph',
4137                   altglyphdef => 'altGlyphDef',
4138                   altglyphitem => 'altGlyphItem',
4139                   animatecolor => 'animateColor',
4140                   animatemotion => 'animateMotion',
4141                   animatetransform => 'animateTransform',
4142                   clippath => 'clipPath',
4143                   feblend => 'feBlend',
4144                   fecolormatrix => 'feColorMatrix',
4145                   fecomponenttransfer => 'feComponentTransfer',
4146                   fecomposite => 'feComposite',
4147                   feconvolvematrix => 'feConvolveMatrix',
4148                   fediffuselighting => 'feDiffuseLighting',
4149                   fedisplacementmap => 'feDisplacementMap',
4150                   fedistantlight => 'feDistantLight',
4151                   feflood => 'feFlood',
4152                   fefunca => 'feFuncA',
4153                   fefuncb => 'feFuncB',
4154                   fefuncg => 'feFuncG',
4155                   fefuncr => 'feFuncR',
4156                   fegaussianblur => 'feGaussianBlur',
4157                   feimage => 'feImage',
4158                   femerge => 'feMerge',
4159                   femergenode => 'feMergeNode',
4160                   femorphology => 'feMorphology',
4161                   feoffset => 'feOffset',
4162                   fepointlight => 'fePointLight',
4163                   fespecularlighting => 'feSpecularLighting',
4164                   fespotlight => 'feSpotLight',
4165                   fetile => 'feTile',
4166                   feturbulence => 'feTurbulence',
4167                   foreignobject => 'foreignObject',
4168                   glyphref => 'glyphRef',
4169                   lineargradient => 'linearGradient',
4170                   radialgradient => 'radialGradient',
4171                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4172                   textpath => 'textPath',  
4173                }->{$tag_name} || $tag_name;
4174              }
4175    
4176              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4177    
4178              ## "adjust foreign attributes" - done in insert-element-f
4179    
4180              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4181    
4182              if ($self->{self_closing}) {
4183                pop @{$self->{open_elements}};
4184                !!!ack ('t87.3');
4185              } else {
4186                !!!cp ('t87.4');
4187              }
4188    
4189              !!!next-token;
4190              next B;
4191            }
4192          } elsif ($token->{type} == END_TAG_TOKEN) {
4193            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4194            !!!cp ('t87.5');
4195            #
4196          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4197            !!!cp ('t87.6');
4198            !!!parse-error (type => 'not closed',
4199                            text => $self->{open_elements}->[-1]->[0]
4200                                ->manakai_local_name,
4201                            token => $token);
4202    
4203            pop @{$self->{open_elements}}
4204                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4205    
4206            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4207            ## Reprocess.
4208            next B;
4209          } else {
4210            die "$0: $token->{type}: Unknown token type";        
4211          }
4212        }
4213    
4214        if ($self->{insertion_mode} & HEAD_IMS) {
4215        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4216          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4217            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4218                !!!cp ('t88.2');
4219                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4220              } else {
4221                !!!cp ('t88.1');
4222                ## Ignore the token.
4223                !!!next-token;
4224                next B;
4225              }
4226            unless (length $token->{data}) {            unless (length $token->{data}) {
4227              !!!cp ('t88');              !!!cp ('t88');
4228              !!!next-token;              !!!next-token;
4229              redo B;              next B;
4230            }            }
4231          }          }
4232    
4233          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4234            !!!cp ('t89');            !!!cp ('t89');
4235            ## As if <head>            ## As if <head>
4236            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4237            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4238            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4239                  [$self->{head_element}, $el_category->{head}];
4240    
4241            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4242            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3343  sub _tree_construction_main ($) { Line 4246  sub _tree_construction_main ($) {
4246            !!!cp ('t90');            !!!cp ('t90');
4247            ## As if </noscript>            ## As if </noscript>
4248            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4249            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4250                        
4251            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4252            ## As if </head>            ## As if </head>
# Line 3359  sub _tree_construction_main ($) { Line 4262  sub _tree_construction_main ($) {
4262            !!!cp ('t92');            !!!cp ('t92');
4263          }          }
4264    
4265              ## "after head" insertion mode          ## "after head" insertion mode
4266              ## As if <body>          ## As if <body>
4267              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4268              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4269              ## reprocess          ## reprocess
4270              redo B;          next B;
4271            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4272              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4273                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274                  !!!cp ('t93');              !!!cp ('t93');
4275                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4276                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4277                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4278                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4279                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4280                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4281                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4282                  !!!cp ('t94');              !!!next-token;
4283                  #              next B;
4284                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4285                  !!!cp ('t95');              !!!cp ('t93.2');
4286                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4287                  ## Ignore the token                              token => $token);
4288                  !!!next-token;              ## Ignore the token
4289                  redo B;              !!!nack ('t93.3');
4290                }              !!!next-token;
4291              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4292                !!!cp ('t96');            } else {
4293                ## As if <head>              !!!cp ('t95');
4294                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4295                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4296                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4297                !!!nack ('t95.1');
4298                !!!next-token;
4299                next B;
4300              }
4301            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4302              !!!cp ('t96');
4303              ## As if <head>
4304              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4305              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4306              push @{$self->{open_elements}},
4307                  [$self->{head_element}, $el_category->{head}];
4308    
4309                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4310                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4311              } else {          } else {
4312                !!!cp ('t97');            !!!cp ('t97');
4313              }          }
4314    
4315              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4316                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4317                  !!!cp ('t98');                  !!!cp ('t98');
4318                  ## As if </noscript>                  ## As if </noscript>
4319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4320                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4321                                    token => $token);
4322                                
4323                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4324                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3414  sub _tree_construction_main ($) { Line 4329  sub _tree_construction_main ($) {
4329                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4330                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                  !!!cp ('t100');                  !!!cp ('t100');
4332                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4333                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4334                    push @{$self->{open_elements}},
4335                        [$self->{head_element}, $el_category->{head}];
4336                } else {                } else {
4337                  !!!cp ('t101');                  !!!cp ('t101');
4338                }                }
4339                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4342                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4343                  !!!nack ('t101.1');
4344                !!!next-token;                !!!next-token;
4345                redo B;                next B;
4346              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4347                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4348                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!cp ('t102');                  !!!cp ('t102');
4350                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4351                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4352                    push @{$self->{open_elements}},
4353                        [$self->{head_element}, $el_category->{head}];
4354                } else {                } else {
4355                  !!!cp ('t103');                  !!!cp ('t103');
4356                }                }
4357                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4358                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4359                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4360                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                  !!!ack ('t103.1');
4362                !!!next-token;                !!!next-token;
4363                redo B;                next B;
4364              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4365                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4366                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                  !!!cp ('t104');                  !!!cp ('t104');
4368                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4369                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4370                    push @{$self->{open_elements}},
4371                        [$self->{head_element}, $el_category->{head}];
4372                } else {                } else {
4373                  !!!cp ('t105');                  !!!cp ('t105');
4374                }                }
4375                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4376                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.
4377    
4378                unless ($self->{confident}) {                unless ($self->{confident}) {
4379                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4380                    !!!cp ('t106');                    !!!cp ('t106');
4381                      ## NOTE: Whether the encoding is supported or not is handled
4382                      ## in the {change_encoding} callback.
4383                    $self->{change_encoding}                    $self->{change_encoding}
4384                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4385                             $token);
4386                                        
4387                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4388                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4389                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4390                                                 ->{has_reference});                                                 ->{has_reference});
4391                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4392                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4393                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4394                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4395                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4396                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4397                      !!!cp ('t107');                      !!!cp ('t107');
4398                        ## NOTE: Whether the encoding is supported or not is handled
4399                        ## in the {change_encoding} callback.
4400                      $self->{change_encoding}                      $self->{change_encoding}
4401                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4402                               $token);
4403                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4404                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4405                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3497  sub _tree_construction_main ($) { Line 4425  sub _tree_construction_main ($) {
4425                  }                  }
4426                }                }
4427    
4428                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4429                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                  !!!ack ('t110.1');
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4434                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435                  !!!cp ('t111');                  !!!cp ('t111');
4436                  ## As if </noscript>                  ## As if </noscript>
4437                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4438                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4439                                    token => $token);
4440                                
4441                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4442                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4443                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4444                  !!!cp ('t112');                  !!!cp ('t112');
4445                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4446                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4447                    push @{$self->{open_elements}},
4448                        [$self->{head_element}, $el_category->{head}];
4449                } else {                } else {
4450                  !!!cp ('t113');                  !!!cp ('t113');
4451                }                }
# Line 3521  sub _tree_construction_main ($) { Line 4453  sub _tree_construction_main ($) {
4453                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4454                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4455                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4456                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4457                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4458                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4459                redo B;                next B;
4460              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4461                         $token->{tag_name} eq 'noframes') {
4462                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4463                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4464                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4465                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4466                  !!!cp ('t114');                  !!!cp ('t114');
4467                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4468                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4469                    push @{$self->{open_elements}},
4470                        [$self->{head_element}, $el_category->{head}];
4471                } else {                } else {
4472                  !!!cp ('t115');                  !!!cp ('t115');
4473                }                }
4474                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4475                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4476                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4477                redo B;                next B;
4478              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4479                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4480                  !!!cp ('t116');                  !!!cp ('t116');
4481                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4482                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4483                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4484                    !!!nack ('t116.1');
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4488                  !!!cp ('t117');                  !!!cp ('t117');
4489                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4490                                    token => $token);
4491                  ## Ignore the token                  ## Ignore the token
4492                    !!!nack ('t117.1');
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                } else {                } else {
4496                  !!!cp ('t118');                  !!!cp ('t118');
4497                  #                  #
# Line 3564  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501                  !!!cp ('t119');                  !!!cp ('t119');
4502                  ## As if </noscript>                  ## As if </noscript>
4503                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4504                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4505                                    token => $token);
4506                                
4507                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4508                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4509                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4510                  !!!cp ('t120');                  !!!cp ('t120');
4511                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4512                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4513                    push @{$self->{open_elements}},
4514                        [$self->{head_element}, $el_category->{head}];
4515                } else {                } else {
4516                  !!!cp ('t121');                  !!!cp ('t121');
4517                }                }
4518    
4519                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4520                $script_start_tag->($insert_to_current);                $script_start_tag->();
4521                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4522                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4523                redo B;                next B;
4524              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4525                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4526                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4527                  !!!cp ('t122');                  !!!cp ('t122');
4528                  ## As if </noscript>                  ## As if </noscript>
4529                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4530                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4531                                    text => $token->{tag_name}, token => $token);
4532                                    
4533                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4534                  ## As if </head>                  ## As if </head>
# Line 3604  sub _tree_construction_main ($) { Line 4545  sub _tree_construction_main ($) {
4545                }                }
4546    
4547                ## "after head" insertion mode                ## "after head" insertion mode
4548                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4549                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4550                  !!!cp ('t126');                  !!!cp ('t126');
4551                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3614  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555                } else {                } else {
4556                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4557                }                }
4558                  !!!nack ('t127.1');
4559                !!!next-token;                !!!next-token;
4560                redo B;                next B;
4561              } else {              } else {
4562                !!!cp ('t128');                !!!cp ('t128');
4563                #                #
# Line 3625  sub _tree_construction_main ($) { Line 4567  sub _tree_construction_main ($) {
4567                !!!cp ('t129');                !!!cp ('t129');
4568                ## As if </noscript>                ## As if </noscript>
4569                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4570                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4571                                  text => $token->{tag_name}, token => $token);
4572                                
4573                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4574                ## As if </head>                ## As if </head>
# Line 3644  sub _tree_construction_main ($) { Line 4587  sub _tree_construction_main ($) {
4587    
4588              ## "after head" insertion mode              ## "after head" insertion mode
4589              ## As if <body>              ## As if <body>
4590              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4591              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4592              ## reprocess              ## reprocess
4593              redo B;              !!!ack-later;
4594                next B;
4595            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4596              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4597                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4598                  !!!cp ('t132');                  !!!cp ('t132');
4599                  ## As if <head>                  ## As if <head>
4600                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4601                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4602                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4603                        [$self->{head_element}, $el_category->{head}];
4604    
4605                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4607                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4608                  !!!next-token;                  !!!next-token;
4609                  redo B;                  next B;
4610                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                  !!!cp ('t133');                  !!!cp ('t133');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4615                                    text => 'head', token => $token);
4616                                    
4617                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4618                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4619                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4620                  !!!next-token;                  !!!next-token;
4621                  redo B;                  next B;
4622                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4623                  !!!cp ('t134');                  !!!cp ('t134');
4624                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4625                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4626                  !!!next-token;                  !!!next-token;
4627                  redo B;                  next B;
4628                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                    !!!cp ('t134.1');
4630                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4631                                    token => $token);
4632                    ## Ignore the token
4633                    !!!next-token;
4634                    next B;
4635                } else {                } else {
4636                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4637                }                }
4638              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4639                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3689  sub _tree_construction_main ($) { Line 4641  sub _tree_construction_main ($) {
4641                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4642                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4643                  !!!next-token;                  !!!next-token;
4644                  redo B;                  next B;
4645                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4646                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4647                  !!!cp ('t137');                  !!!cp ('t137');
4648                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4649                                    text => 'noscript', token => $token);
4650                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4651                  !!!next-token;                  !!!next-token;
4652                  redo B;                  next B;
4653                } else {                } else {
4654                  !!!cp ('t138');                  !!!cp ('t138');
4655                  #                  #
# Line 3703  sub _tree_construction_main ($) { Line 4657  sub _tree_construction_main ($) {
4657              } elsif ({              } elsif ({
4658                        body => 1, html => 1,                        body => 1, html => 1,
4659                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4660                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4662                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head');  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4663                  !!!cp ('t140');                  !!!cp ('t140');
4664                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4665                                    text => $token->{tag_name}, token => $token);
4666                  ## Ignore the token                  ## Ignore the token
4667                  !!!next-token;                  !!!next-token;
4668                  redo B;                  next B;
4669                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                    !!!cp ('t140.1');
4671                    !!!parse-error (type => 'unmatched end tag',
4672                                    text => $token->{tag_name}, token => $token);
4673                    ## Ignore the token
4674                    !!!next-token;
4675                    next B;
4676                } else {                } else {
4677                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4678                }                }
4679                              } elsif ($token->{tag_name} eq 'p') {
4680                #                !!!cp ('t142');
4681              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4682                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4683                       }->{$token->{tag_name}}) {                ## Ignore the token
4684                  !!!next-token;
4685                  next B;
4686                } elsif ($token->{tag_name} eq 'br') {
4687                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4688                  !!!cp ('t142');                  !!!cp ('t142.2');
4689                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4690                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4691                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4692                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4693      
4694                    ## Reprocess in the "after head" insertion mode...
4695                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4696                    !!!cp ('t143.2');
4697                    ## As if </head>
4698                    pop @{$self->{open_elements}};
4699                    $self->{insertion_mode} = AFTER_HEAD_IM;
4700      
4701                    ## Reprocess in the "after head" insertion mode...
4702                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4703                    !!!cp ('t143.3');
4704                    ## ISSUE: Two parse errors for <head><noscript></br>
4705                    !!!parse-error (type => 'unmatched end tag',
4706                                    text => 'br', token => $token);
4707                    ## As if </noscript>
4708                    pop @{$self->{open_elements}};
4709                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4710    
4711                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4712                } else {                  ## As if </head>
4713                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4714                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4715    
4716                #                  ## Reprocess in the "after head" insertion mode...
4717              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4718                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4719                  #                  #
4720                } else {                } else {
4721                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4722                }                }
4723    
4724                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4725                  !!!parse-error (type => 'unmatched end tag',
4726                                  text => 'br', token => $token);
4727                  ## Ignore the token
4728                  !!!next-token;
4729                  next B;
4730                } else {
4731                  !!!cp ('t145');
4732                  !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                  ## Ignore the token
4735                  !!!next-token;
4736                  next B;
4737              }              }
4738    
4739              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4740                !!!cp ('t146');                !!!cp ('t146');
4741                ## As if </noscript>                ## As if </noscript>
4742                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4743                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4744                                  text => $token->{tag_name}, token => $token);
4745                                
4746                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4747                ## As if </head>                ## As if </head>
# Line 3773  sub _tree_construction_main ($) { Line 4757  sub _tree_construction_main ($) {
4757              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4758  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4759                !!!cp ('t148');                !!!cp ('t148');
4760                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4761                                  text => $token->{tag_name}, token => $token);
4762                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4763                !!!next-token;                !!!next-token;
4764                redo B;                next B;
4765              } else {              } else {
4766                !!!cp ('t149');                !!!cp ('t149');
4767              }              }
4768    
4769              ## "after head" insertion mode              ## "after head" insertion mode
4770              ## As if <body>              ## As if <body>
4771              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4772              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4773              ## reprocess              ## reprocess
4774              redo B;              next B;
4775            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4776              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4777            }            !!!cp ('t149.1');
4778    
4779              ## NOTE: As if <head>
4780              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4781              $self->{open_elements}->[-1]->[0]->append_child
4782                  ($self->{head_element});
4783              #push @{$self->{open_elements}},
4784              #    [$self->{head_element}, $el_category->{head}];
4785              #$self->{insertion_mode} = IN_HEAD_IM;
4786              ## NOTE: Reprocess.
4787    
4788              ## NOTE: As if </head>
4789              #pop @{$self->{open_elements}};
4790              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4791              ## NOTE: Reprocess.
4792              
4793              #
4794            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4795              !!!cp ('t149.2');
4796    
4797              ## NOTE: As if </head>
4798              pop @{$self->{open_elements}};
4799              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4800              ## NOTE: Reprocess.
4801    
4802              #
4803            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804              !!!cp ('t149.3');
4805    
4806              !!!parse-error (type => 'in noscript:#eof', token => $token);
4807    
4808              ## As if </noscript>
4809              pop @{$self->{open_elements}};
4810              #$self->{insertion_mode} = IN_HEAD_IM;
4811              ## NOTE: Reprocess.
4812    
4813              ## NOTE: As if </head>
4814              pop @{$self->{open_elements}};
4815              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4816              ## NOTE: Reprocess.
4817    
4818              #
4819            } else {
4820              !!!cp ('t149.4');
4821              #
4822            }
4823    
4824            ## NOTE: As if <body>
4825            !!!insert-element ('body',, $token);
4826            $self->{insertion_mode} = IN_BODY_IM;
4827            ## NOTE: Reprocess.
4828            next B;
4829          } else {
4830            die "$0: $token->{type}: Unknown token type";
4831          }
4832    
4833            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4834      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3801  sub _tree_construction_main ($) { Line 4840  sub _tree_construction_main ($) {
4840              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4841    
4842              !!!next-token;              !!!next-token;
4843              redo B;              next B;
4844            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4845              if ({              if ({
4846                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3809  sub _tree_construction_main ($) { Line 4848  sub _tree_construction_main ($) {
4848                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4849                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4850                  ## have an element in table scope                  ## have an element in table scope
4851                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4852                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4853                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4854                      !!!cp ('t151');                      !!!cp ('t151');
4855                      $tn = $node->[1];  
4856                      last INSCOPE;                      ## Close the cell
4857                    } elsif ({                      !!!back-token; # <x>
4858                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4859                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4860                                  line => $token->{line},
4861                                  column => $token->{column}};
4862                        next B;
4863                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4864                      !!!cp ('t152');                      !!!cp ('t152');
4865                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4866                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
4867                    }                    }
4868                                    }
4869                  !!!cp ('t154');  
4870                  ## Close the cell                  !!!cp ('t153');
4871                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4872                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      text => $token->{tag_name}, token => $token);
4873                  redo B;                  ## Ignore the token
4874                    !!!nack ('t153.1');
4875                    !!!next-token;
4876                    next B;
4877                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4879                                    token => $token);
4880                                    
4881                  ## As if </caption>                  ## NOTE: As if </caption>.
4882                  ## have a table element in table scope                  ## have a table element in table scope
4883                  my $i;                  my $i;
4884                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4885                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4886                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4887                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4888                      $i = $_;                        !!!cp ('t155');
4889                      last INSCOPE;                        $i = $_;
4890                    } elsif ({                        last INSCOPE;
4891                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4892                             }->{$node->[1]}) {                        !!!cp ('t156');
4893                      !!!cp ('t156');                        last;
4894                      last INSCOPE;                      }
4895                    }                    }
4896    
4897                      !!!cp ('t157');
4898                      !!!parse-error (type => 'start tag not allowed',
4899                                      text => $token->{tag_name}, token => $token);
4900                      ## Ignore the token
4901                      !!!nack ('t157.1');
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4909                    !!!cp ('t158');                    !!!cp ('t158');
4910                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4911                  }                  }
4912    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!cp ('t159');                    !!!cp ('t159');
4915                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4916                                      text => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                  } else {                  } else {
4920                    !!!cp ('t160');                    !!!cp ('t160');
4921                  }                  }
# Line 3893  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  ## reprocess                  ## reprocess
4930                  redo B;                  !!!ack-later;
4931                    next B;
4932                } else {                } else {
4933                  !!!cp ('t161');                  !!!cp ('t161');
4934                  #                  #
# Line 3909  sub _tree_construction_main ($) { Line 4944  sub _tree_construction_main ($) {
4944                  my $i;                  my $i;
4945                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4946                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4947                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4948                      !!!cp ('t163');                      !!!cp ('t163');
4949                      $i = $_;                      $i = $_;
4950                      last INSCOPE;                      last INSCOPE;
4951                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4952                      !!!cp ('t164');                      !!!cp ('t164');
4953                      last INSCOPE;                      last INSCOPE;
4954                    }                    }
4955                  } # INSCOPE                  } # INSCOPE
4956                    unless (defined $i) {                    unless (defined $i) {
4957                      !!!cp ('t165');                      !!!cp ('t165');
4958                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
4959                                        text => $token->{tag_name},
4960                                        token => $token);
4961                      ## Ignore the token                      ## Ignore the token
4962                      !!!next-token;                      !!!next-token;
4963                      redo B;                      next B;
4964                    }                    }
4965                                    
4966                  ## generate implied end tags                  ## generate implied end tags
4967                  if ({                  while ($self->{open_elements}->[-1]->[1]
4968                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4969                    !!!cp ('t166');                    !!!cp ('t166');
4970                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4971                  }                  }
4972                    
4973                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4974                            ne $token->{tag_name}) {
4975                    !!!cp ('t167');                    !!!cp ('t167');
4976                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4977                                      text => $self->{open_elements}->[-1]->[0]
4978                                          ->manakai_local_name,
4979                                      token => $token);
4980                  } else {                  } else {
4981                    !!!cp ('t168');                    !!!cp ('t168');
4982                  }                  }
# Line 3957  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4989                                    
4990                  !!!next-token;                  !!!next-token;
4991                  redo B;                  next B;
4992                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4993                  !!!cp ('t169');                  !!!cp ('t169');
4994                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4995                                    text => $token->{tag_name}, token => $token);
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  next B;
4999                } else {                } else {
5000                  !!!cp ('t170');                  !!!cp ('t170');
5001                  #                  #
# Line 3972  sub _tree_construction_main ($) { Line 5004  sub _tree_construction_main ($) {
5004                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5005                  ## have a table element in table scope                  ## have a table element in table scope
5006                  my $i;                  my $i;
5007                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5008                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5009                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5010                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5011                      $i = $_;                        !!!cp ('t171');
5012                      last INSCOPE;                        $i = $_;
5013                    } elsif ({                        last INSCOPE;
5014                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5015                             }->{$node->[1]}) {                        !!!cp ('t172');
5016                      !!!cp ('t172');                        last;
5017                      last INSCOPE;                      }
5018                    }                    }
5019    
5020                      !!!cp ('t173');
5021                      !!!parse-error (type => 'unmatched end tag',
5022                                      text => $token->{tag_name}, token => $token);
5023                      ## Ignore the token
5024                      !!!next-token;
5025                      next B;
5026                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5027                                    
5028                  ## generate implied end tags                  ## generate implied end tags
5029                  if ({                  while ($self->{open_elements}->[-1]->[1]
5030                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5031                    !!!cp ('t174');                    !!!cp ('t174');
5032                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5033                  }                  }
5034                                    
5035                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                    !!!cp ('t175');                    !!!cp ('t175');
5037                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5038                                      text => $self->{open_elements}->[-1]->[0]
5039                                          ->manakai_local_name,
5040                                      token => $token);
5041                  } else {                  } else {
5042                    !!!cp ('t176');                    !!!cp ('t176');
5043                  }                  }
# Line 4020  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5050                                    
5051                  !!!next-token;                  !!!next-token;
5052                  redo B;                  next B;
5053                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5054                  !!!cp ('t177');                  !!!cp ('t177');
5055                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5056                                    text => $token->{tag_name}, token => $token);
5057                  ## Ignore the token                  ## Ignore the token
5058                  !!!next-token;                  !!!next-token;
5059                  redo B;                  next B;
5060                } else {                } else {
5061                  !!!cp ('t178');                  !!!cp ('t178');
5062                  #                  #
# Line 4039  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
5069                ## have an element in table scope                ## have an element in table scope
5070                my $i;                my $i;
5071                my $tn;                my $tn;
5072                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5073                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5074                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5075                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5076                    $i = $_;                      !!!cp ('t179');
5077                    last INSCOPE;                      $i = $_;
5078                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5079                    !!!cp ('t180');                      ## Close the cell
5080                    $tn = $node->[1];                      !!!back-token; # </x>
5081                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5082                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5083                  } elsif ({                                column => $token->{column}};
5084                            table => 1, html => 1,                      next B;
5085                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5086                    !!!cp ('t181');                      !!!cp ('t180');
5087                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5088                        ## NOTE: There is exactly one |td| or |th| element
5089                        ## in scope in the stack of open elements by definition.
5090                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5091                        ## ISSUE: Can this be reached?
5092                        !!!cp ('t181');
5093                        last;
5094                      }
5095                  }                  }
5096                } # INSCOPE  
               unless (defined $i) {  
5097                  !!!cp ('t182');                  !!!cp ('t182');
5098                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5099                        text => $token->{tag_name}, token => $token);
5100                  ## Ignore the token                  ## Ignore the token
5101                  !!!next-token;                  !!!next-token;
5102                  redo B;                  next B;
5103                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5104              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5105                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5106                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5107                                  token => $token);
5108    
5109                ## As if </caption>                ## As if </caption>
5110                ## have a table element in table scope                ## have a table element in table scope
5111                my $i;                my $i;
5112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5113                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5114                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5115                    !!!cp ('t184');                    !!!cp ('t184');
5116                    $i = $_;                    $i = $_;
5117                    last INSCOPE;                    last INSCOPE;
5118                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5119                    !!!cp ('t185');                    !!!cp ('t185');
5120                    last INSCOPE;                    last INSCOPE;
5121                  }                  }
5122                } # INSCOPE                } # INSCOPE
5123                unless (defined $i) {                unless (defined $i) {
5124                  !!!cp ('t186');                  !!!cp ('t186');
5125                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5126                                    text => 'caption', token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                }                }
5131                                
5132                ## generate implied end tags                ## generate implied end tags
5133                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5134                  !!!cp ('t187');                  !!!cp ('t187');
5135                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5136                }                }
5137    
5138                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5139                  !!!cp ('t188');                  !!!cp ('t188');
5140                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5141                                    text => $self->{open_elements}->[-1]->[0]
5142                                        ->manakai_local_name,
5143                                    token => $token);
5144                } else {                } else {
5145                  !!!cp ('t189');                  !!!cp ('t189');
5146                }                }
# Line 4128  sub _tree_construction_main ($) { Line 5152  sub _tree_construction_main ($) {
5152                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5153    
5154                ## reprocess                ## reprocess
5155                redo B;                next B;
5156              } elsif ({              } elsif ({
5157                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5159                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5160                  !!!cp ('t190');                  !!!cp ('t190');
5161                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5162                                    text => $token->{tag_name}, token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                } else {                } else {
5167                  !!!cp ('t191');                  !!!cp ('t191');
5168                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 5173  sub _tree_construction_main ($) {
5173                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5174                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!cp ('t192');                !!!cp ('t192');
5176                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5177                                  text => $token->{tag_name}, token => $token);
5178                ## Ignore the token                ## Ignore the token
5179                !!!next-token;                !!!next-token;
5180                redo B;                next B;
5181              } else {              } else {
5182                !!!cp ('t193');                !!!cp ('t193');
5183                #                #
5184              }              }
5185          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5186            for my $entry (@{$self->{open_elements}}) {
5187              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5188                !!!cp ('t75');
5189                !!!parse-error (type => 'in body:#eof', token => $token);
5190                last;
5191              }
5192            }
5193    
5194            ## Stop parsing.
5195            last B;
5196        } else {        } else {
5197          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5198        }        }
# Line 4164  sub _tree_construction_main ($) { Line 5201  sub _tree_construction_main ($) {
5201        #        #
5202      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5203        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5204              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5205                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5207                                
5208                unless (length $token->{data}) {            unless (length $token->{data}) {
5209                  !!!cp ('t194');              !!!cp ('t194');
5210                  !!!next-token;              !!!next-token;
5211                  redo B;              next B;
5212                } else {            } else {
5213                  !!!cp ('t195');              !!!cp ('t195');
5214                }            }
5215              }          }
5216    
5217              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5218    
5219              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5220              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4184  sub _tree_construction_main ($) { Line 5222  sub _tree_construction_main ($) {
5222              ## result in a new Text node.              ## result in a new Text node.
5223              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5224                            
5225              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]}) {  
5226                # MUST                # MUST
5227                my $foster_parent_element;                my $foster_parent_element;
5228                my $next_sibling;                my $next_sibling;
5229                my $prev_sibling;                my $prev_sibling;
5230                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5231                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5232                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5233                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5234                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4221  sub _tree_construction_main ($) { Line 5256  sub _tree_construction_main ($) {
5256                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5257                     $next_sibling);                     $next_sibling);
5258                }                }
5259              } else {            $open_tables->[-1]->[1] = 1; # tainted
5260                !!!cp ('t200');          } else {
5261                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5262              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5263            }
5264                            
5265              !!!next-token;          !!!next-token;
5266              redo B;          next B;
5267        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5268              if ({          if ({
5269                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5270                   th => 1, td => 1,               th => 1, td => 1,
5271                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5272                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5273                  ## Clear back to table context              ## Clear back to table context
5274                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5275                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5276                    !!!cp ('t201');                !!!cp ('t201');
5277                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5278                    pop @{$self->{open_elements}};              }
5279                  }              
5280                                !!!insert-element ('tbody',, $token);
5281                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5282                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5283                  ## reprocess in the "in table body" insertion mode...            }
5284                }            
5285              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5286                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5287                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5288                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5289                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5290                                    
5291                  ## Clear back to table body context              ## Clear back to table body context
5292                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5293                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5294                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5295                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5296                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5297                    pop @{$self->{open_elements}};              }
                 }  
5298                                    
5299                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5300                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5301                    !!!cp ('t204');                    !!!cp ('t204');
5302                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5303                      !!!nack ('t204');
5304                    !!!next-token;                    !!!next-token;
5305                    redo B;                    next B;
5306                  } else {                  } else {
5307                    !!!cp ('t205');                    !!!cp ('t205');
5308                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5309                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5310                  }                  }
5311                } else {                } else {
# Line 4278  sub _tree_construction_main ($) { Line 5313  sub _tree_construction_main ($) {
5313                }                }
5314    
5315                ## Clear back to table row context                ## Clear back to table row context
5316                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5317                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5318                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5320                }                }
5321                                
5322                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5323                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5324    
5325                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5326                                
5327                  !!!nack ('t207.1');
5328                !!!next-token;                !!!next-token;
5329                redo B;                next B;
5330              } elsif ({              } elsif ({
5331                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5332                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  my $i;                  my $i;
5339                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5341                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5342                      !!!cp ('t208');                      !!!cp ('t208');
5343                      $i = $_;                      $i = $_;
5344                      last INSCOPE;                      last INSCOPE;
5345                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5346                      !!!cp ('t209');                      !!!cp ('t209');
5347                      last INSCOPE;                      last INSCOPE;
5348                    }                    }
5349                  } # INSCOPE                  } # INSCOPE
5350                  unless (defined $i) {                  unless (defined $i) {
5351                   !!!cp ('t210');                    !!!cp ('t210');
5352                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  ## TODO: This type is wrong.
5353                      !!!parse-error (type => 'unmacthed end tag',
5354                                      text => $token->{tag_name}, token => $token);
5355                    ## Ignore the token                    ## Ignore the token
5356                      !!!nack ('t210.1');
5357                    !!!next-token;                    !!!next-token;
5358                    redo B;                    next B;
5359                  }                  }
5360                                    
5361                  ## Clear back to table row context                  ## Clear back to table row context
5362                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5363                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5364                    !!!cp ('t211');                    !!!cp ('t211');
5365                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5366                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5367                  }                  }
5368                                    
# Line 4337  sub _tree_construction_main ($) { Line 5371  sub _tree_construction_main ($) {
5371                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5372                    !!!cp ('t212');                    !!!cp ('t212');
5373                    ## reprocess                    ## reprocess
5374                    redo B;                    !!!ack-later;
5375                      next B;
5376                  } else {                  } else {
5377                    !!!cp ('t213');                    !!!cp ('t213');
5378                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4349  sub _tree_construction_main ($) { Line 5384  sub _tree_construction_main ($) {
5384                  my $i;                  my $i;
5385                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5386                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5387                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5388                      !!!cp ('t214');                      !!!cp ('t214');
5389                      $i = $_;                      $i = $_;
5390                      last INSCOPE;                      last INSCOPE;
5391                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5392                      !!!cp ('t215');                      !!!cp ('t215');
5393                      last INSCOPE;                      last INSCOPE;
5394                    }                    }
5395                  } # INSCOPE                  } # INSCOPE
5396                  unless (defined $i) {                  unless (defined $i) {
5397                    !!!cp ('t216');                    !!!cp ('t216');
5398  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5399                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5400                                      text => $token->{tag_name}, token => $token);
5401                    ## Ignore the token                    ## Ignore the token
5402                      !!!nack ('t216.1');
5403                    !!!next-token;                    !!!next-token;
5404                    redo B;                    next B;
5405                  }                  }
5406    
5407                  ## Clear back to table body context                  ## Clear back to table body context
5408                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5409                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5410                    !!!cp ('t217');                    !!!cp ('t217');
5411                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5412                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5413                  }                  }
5414                                    
# Line 4396  sub _tree_construction_main ($) { Line 5428  sub _tree_construction_main ($) {
5428    
5429                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5430                  ## Clear back to table context                  ## Clear back to table context
5431                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5432                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5433                    !!!cp ('t219');                    !!!cp ('t219');
5434                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5435                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5436                  }                  }
5437                                    
5438                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5439                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5440                  ## reprocess                  ## reprocess
5441                  redo B;                  !!!ack-later;
5442                    next B;
5443                } elsif ({                } elsif ({
5444                          caption => 1,                          caption => 1,
5445                          colgroup => 1,                          colgroup => 1,
5446                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5447                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5448                  ## Clear back to table context                  ## Clear back to table context
5449                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5450                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5451                    !!!cp ('t220');                    !!!cp ('t220');
5452                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5453                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5454                  }                  }
5455                                    
5456                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5457                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5458                                    
5459                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5460                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5461                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5462                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4432  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5466                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5467                  !!!next-token;                  !!!next-token;
5468                  redo B;                  !!!nack ('t220.1');
5469                    next B;
5470                } else {                } else {
5471                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5472                }                }
5473              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5474                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5475                                  text => $self->{open_elements}->[-1]->[0]
5476                                      ->manakai_local_name,
5477                                  token => $token);
5478    
5479                ## As if </table>                ## As if </table>
5480                ## have a table element in table scope                ## have a table element in table scope
5481                my $i;                my $i;
5482                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5483                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5484                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5485                    !!!cp ('t221');                    !!!cp ('t221');
5486                    $i = $_;                    $i = $_;
5487                    last INSCOPE;                    last INSCOPE;
5488                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5489                    !!!cp ('t222');                    !!!cp ('t222');
5490                    last INSCOPE;                    last INSCOPE;
5491                  }                  }
5492                } # INSCOPE                } # INSCOPE
5493                unless (defined $i) {                unless (defined $i) {
5494                  !!!cp ('t223');                  !!!cp ('t223');
5495                  !!!parse-error (type => 'unmatched end tag:table');  ## TODO: The following is wrong, maybe.
5496                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5497                                    token => $token);
5498                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5499                    !!!nack ('t223.1');
5500                  !!!next-token;                  !!!next-token;
5501                  redo B;                  next B;
5502                }                }
5503                                
5504    ## TODO: Followings are removed from the latest spec.
5505                ## generate implied end tags                ## generate implied end tags
5506                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5507                  !!!cp ('t224');                  !!!cp ('t224');
5508                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5509                }                }
5510    
5511                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5512                  !!!cp ('t225');                  !!!cp ('t225');
5513                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## NOTE: |<table><tr><table>|
5514                    !!!parse-error (type => 'not closed',
5515                                    text => $self->{open_elements}->[-1]->[0]
5516                                        ->manakai_local_name,
5517                                    token => $token);
5518                } else {                } else {
5519                  !!!cp ('t226');                  !!!cp ('t226');
5520                }                }
5521    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524    
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526    
5527                ## reprocess            ## reprocess
5528                redo B;            !!!ack-later;
5529              next B;
5530            } elsif ($token->{tag_name} eq 'style') {
5531              if (not $open_tables->[-1]->[1]) { # tainted
5532                !!!cp ('t227.8');
5533                ## NOTE: This is a "as if in head" code clone.
5534                $parse_rcdata->(CDATA_CONTENT_MODEL);
5535                next B;
5536              } else {
5537                !!!cp ('t227.7');
5538                #
5539              }
5540            } elsif ($token->{tag_name} eq 'script') {
5541              if (not $open_tables->[-1]->[1]) { # tainted
5542                !!!cp ('t227.6');
5543                ## NOTE: This is a "as if in head" code clone.
5544                $script_start_tag->();
5545                next B;
5546              } else {
5547                !!!cp ('t227.5');
5548                #
5549              }
5550            } elsif ($token->{tag_name} eq 'input') {
5551              if (not $open_tables->[-1]->[1]) { # tainted
5552                if ($token->{attributes}->{type}) { ## TODO: case
5553                  my $type = lc $token->{attributes}->{type}->{value};
5554                  if ($type eq 'hidden') {
5555                    !!!cp ('t227.3');
5556                    !!!parse-error (type => 'in table',
5557                                    text => $token->{tag_name}, token => $token);
5558    
5559                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5560    
5561                    ## TODO: form element pointer
5562    
5563                    pop @{$self->{open_elements}};
5564    
5565                    !!!next-token;
5566                    !!!ack ('t227.2.1');
5567                    next B;
5568                  } else {
5569                    !!!cp ('t227.2');
5570                    #
5571                  }
5572                } else {
5573                  !!!cp ('t227.1');
5574                  #
5575                }
5576              } else {
5577                !!!cp ('t227.4');
5578                #
5579              }
5580          } else {          } else {
5581            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5582            #            #
5583          }          }
5584    
5585            !!!parse-error (type => 'in table', text => $token->{tag_name},
5586                            token => $token);
5587    
5588            $insert = $insert_to_foster;
5589            #
5590        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5591              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5592                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4505  sub _tree_construction_main ($) { Line 5594  sub _tree_construction_main ($) {
5594                my $i;                my $i;
5595                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5596                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5597                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5598                    !!!cp ('t228');                    !!!cp ('t228');
5599                    $i = $_;                    $i = $_;
5600                    last INSCOPE;                    last INSCOPE;
5601                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5602                    !!!cp ('t229');                    !!!cp ('t229');
5603                    last INSCOPE;                    last INSCOPE;
5604                  }                  }
5605                } # INSCOPE                } # INSCOPE
5606                unless (defined $i) {                unless (defined $i) {
5607                  !!!cp ('t230');                  !!!cp ('t230');
5608                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5609                                    text => $token->{tag_name}, token => $token);
5610                  ## Ignore the token                  ## Ignore the token
5611                    !!!nack ('t230.1');
5612                  !!!next-token;                  !!!next-token;
5613                  redo B;                  next B;
5614                } else {                } else {
5615                  !!!cp ('t232');                  !!!cp ('t232');
5616                }                }
5617    
5618                ## Clear back to table row context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5621                  !!!cp ('t231');                  !!!cp ('t231');
5622                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5624                }                }
5625    
5626                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5627                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5628                !!!next-token;                !!!next-token;
5629                redo B;                !!!nack ('t231.1');
5630                  next B;
5631              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5632                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5633                  ## As if </tr>                  ## As if </tr>
# Line 4546  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635                  my $i;                  my $i;
5636                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5637                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5638                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5639                      !!!cp ('t233');                      !!!cp ('t233');
5640                      $i = $_;                      $i = $_;
5641                      last INSCOPE;                      last INSCOPE;
5642                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5643                      !!!cp ('t234');                      !!!cp ('t234');
5644                      last INSCOPE;                      last INSCOPE;
5645                    }                    }
5646                  } # INSCOPE                  } # INSCOPE
5647                  unless (defined $i) {                  unless (defined $i) {
5648                    !!!cp ('t235');                    !!!cp ('t235');
5649                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});  ## TODO: The following is wrong.
5650                      !!!parse-error (type => 'unmatched end tag',
5651                                      text => $token->{type}, token => $token);
5652                    ## Ignore the token                    ## Ignore the token
5653                      !!!nack ('t236.1');
5654                    !!!next-token;                    !!!next-token;
5655                    redo B;                    next B;
5656                  }                  }
5657                                    
5658                  ## Clear back to table row context                  ## Clear back to table row context
5659                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5660                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5661                    !!!cp ('t236');                    !!!cp ('t236');
5662                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5663                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5664                  }                  }
5665                                    
# Line 4584  sub _tree_construction_main ($) { Line 5673  sub _tree_construction_main ($) {
5673                  my $i;                  my $i;
5674                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5676                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5677                      !!!cp ('t237');                      !!!cp ('t237');
5678                      $i = $_;                      $i = $_;
5679                      last INSCOPE;                      last INSCOPE;
5680                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5681                      !!!cp ('t238');                      !!!cp ('t238');
5682                      last INSCOPE;                      last INSCOPE;
5683                    }                    }
5684                  } # INSCOPE                  } # INSCOPE
5685                  unless (defined $i) {                  unless (defined $i) {
5686                    !!!cp ('t239');                    !!!cp ('t239');
5687                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5688                                      text => $token->{tag_name}, token => $token);
5689                    ## Ignore the token                    ## Ignore the token
5690                      !!!nack ('t239.1');
5691                    !!!next-token;                    !!!next-token;
5692                    redo B;                    next B;
5693                  }                  }
5694                                    
5695                  ## Clear back to table body context                  ## Clear back to table body context
5696                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5697                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5698                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5699                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5700                  }                  }
5701                                    
# Line 4626  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5712                }                }
5713    
5714                  ## NOTE: </table> in the "in table" insertion mode.
5715                  ## When you edit the code fragment below, please ensure that
5716                  ## the code for <table> in the "in table" insertion mode
5717                  ## is synced with it.
5718    
5719                ## have a table element in table scope                ## have a table element in table scope
5720                my $i;                my $i;
5721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5723                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5724                    !!!cp ('t241');                    !!!cp ('t241');
5725                    $i = $_;                    $i = $_;
5726                    last INSCOPE;                    last INSCOPE;
5727                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5728                    !!!cp ('t242');                    !!!cp ('t242');
5729                    last INSCOPE;                    last INSCOPE;
5730                  }                  }
5731                } # INSCOPE                } # INSCOPE
5732                unless (defined $i) {                unless (defined $i) {
5733                  !!!cp ('t243');                  !!!cp ('t243');
5734                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5735                                    text => $token->{tag_name}, token => $token);
5736                  ## Ignore the token                  ## Ignore the token
5737                    !!!nack ('t243.1');
5738                  !!!next-token;                  !!!next-token;
5739                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5740                }                }
5741                                    
5742                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5743                  pop @{$open_tables};
5744                                
5745                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5746                                
5747                !!!next-token;                !!!next-token;
5748                redo B;                next B;
5749              } elsif ({              } elsif ({
5750                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5751                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4684  sub _tree_construction_main ($) { Line 5755  sub _tree_construction_main ($) {
5755                  my $i;                  my $i;
5756                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5757                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5758                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5759                      !!!cp ('t247');                      !!!cp ('t247');
5760                      $i = $_;                      $i = $_;
5761                      last INSCOPE;                      last INSCOPE;
5762                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5763                      !!!cp ('t248');                      !!!cp ('t248');
5764                      last INSCOPE;                      last INSCOPE;
5765                    }                    }
5766                  } # INSCOPE                  } # INSCOPE
5767                    unless (defined $i) {                    unless (defined $i) {
5768                      !!!cp ('t249');                      !!!cp ('t249');
5769                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5770                                        text => $token->{tag_name}, token => $token);
5771                      ## Ignore the token                      ## Ignore the token
5772                        !!!nack ('t249.1');
5773                      !!!next-token;                      !!!next-token;
5774                      redo B;                      next B;
5775                    }                    }
5776                                    
5777                  ## As if </tr>                  ## As if </tr>
# Line 4708  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779                  my $i;                  my $i;
5780                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5782                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5783                      !!!cp ('t250');                      !!!cp ('t250');
5784                      $i = $_;                      $i = $_;
5785                      last INSCOPE;                      last INSCOPE;
5786                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5787                      !!!cp ('t251');                      !!!cp ('t251');
5788                      last INSCOPE;                      last INSCOPE;
5789                    }                    }
5790                  } # INSCOPE                  } # INSCOPE
5791                    unless (defined $i) {                    unless (defined $i) {
5792                      !!!cp ('t252');                      !!!cp ('t252');
5793                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
5794                                        text => 'tr', token => $token);
5795                      ## Ignore the token                      ## Ignore the token
5796                        !!!nack ('t252.1');
5797                      !!!next-token;                      !!!next-token;
5798                      redo B;                      next B;
5799                    }                    }
5800                                    
5801                  ## Clear back to table row context                  ## Clear back to table row context
5802                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5803                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5804                    !!!cp ('t253');                    !!!cp ('t253');
5805                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5806                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5807                  }                  }
5808                                    
# Line 4745  sub _tree_construction_main ($) { Line 5815  sub _tree_construction_main ($) {
5815                my $i;                my $i;
5816                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5817                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5818                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5819                    !!!cp ('t254');                    !!!cp ('t254');
5820                    $i = $_;                    $i = $_;
5821                    last INSCOPE;                    last INSCOPE;
5822                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5823                    !!!cp ('t255');                    !!!cp ('t255');
5824                    last INSCOPE;                    last INSCOPE;
5825                  }                  }
5826                } # INSCOPE                } # INSCOPE
5827                unless (defined $i) {                unless (defined $i) {
5828                  !!!cp ('t256');                  !!!cp ('t256');
5829                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5830                                    text => $token->{tag_name}, token => $token);
5831                  ## Ignore the token                  ## Ignore the token
5832                    !!!nack ('t256.1');
5833                  !!!next-token;                  !!!next-token;
5834                  redo B;                  next B;
5835                }                }
5836    
5837                ## Clear back to table body context                ## Clear back to table body context
5838                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5839                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5840                  !!!cp ('t257');                  !!!cp ('t257');
5841                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5843                }                }
5844    
5845                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5846                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5847                  !!!nack ('t257.1');
5848                !!!next-token;                !!!next-token;
5849                redo B;                next B;
5850              } elsif ({              } elsif ({
5851                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5852                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5853                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5854                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5856                !!!cp ('t258');            !!!cp ('t258');
5857                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5858                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5859                !!!next-token;            ## Ignore the token
5860                redo B;            !!!nack ('t258.1');
5861               !!!next-token;
5862              next B;
5863          } else {          } else {
5864            !!!cp ('t259');            !!!cp ('t259');
5865            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
5866                              text => $token->{tag_name}, token => $token);
5867    
5868            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5869            #            #
5870          }          }
5871          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5872            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5873                    @{$self->{open_elements}} == 1) { # redundant, maybe
5874              !!!parse-error (type => 'in body:#eof', token => $token);
5875              !!!cp ('t259.1');
5876              #
5877            } else {
5878              !!!cp ('t259.2');
5879              #
5880            }
5881    
5882            ## Stop parsing
5883            last B;
5884        } else {        } else {
5885          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5886        }        }
# Line 4805  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891                unless (length $token->{data}) {                unless (length $token->{data}) {
5892                  !!!cp ('t260');                  !!!cp ('t260');
5893                  !!!next-token;                  !!!next-token;
5894                  redo B;                  next B;
5895                }                }
5896              }              }
5897                            
# Line 4814  sub _tree_construction_main ($) { Line 5900  sub _tree_construction_main ($) {
5900            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5901              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5902                !!!cp ('t262');                !!!cp ('t262');
5903                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5904                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5905                  !!!ack ('t262.1');
5906                !!!next-token;                !!!next-token;
5907                redo B;                next B;
5908              } else {              } else {
5909                !!!cp ('t263');                !!!cp ('t263');
5910                #                #
5911              }              }
5912            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5913              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5914                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5915                  !!!cp ('t264');                  !!!cp ('t264');
5916                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
5917                                    text => 'colgroup', token => $token);
5918                  ## Ignore the token                  ## Ignore the token
5919                  !!!next-token;                  !!!next-token;
5920                  redo B;                  next B;
5921                } else {                } else {
5922                  !!!cp ('t265');                  !!!cp ('t265');
5923                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5924                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                              next B;            
5927                }                }
5928              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5929                !!!cp ('t266');                !!!cp ('t266');
5930                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
5931                                  text => 'col', token => $token);
5932                ## Ignore the token                ## Ignore the token
5933                !!!next-token;                !!!next-token;
5934                redo B;                next B;
5935              } else {              } else {
5936                !!!cp ('t267');                !!!cp ('t267');
5937                #                #
5938              }              }
5939            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5940              !!!cp ('t268');          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5941              #              @{$self->{open_elements}} == 1) { # redundant, maybe
5942            }            !!!cp ('t270.2');
5943              ## Stop parsing.
5944              last B;
5945            } else {
5946              ## NOTE: As if </colgroup>.
5947              !!!cp ('t270.1');
5948              pop @{$self->{open_elements}}; # colgroup
5949              $self->{insertion_mode} = IN_TABLE_IM;
5950              ## Reprocess.
5951              next B;
5952            }
5953          } else {
5954            die "$0: $token->{type}: Unknown token type";
5955          }
5956    
5957            ## As if </colgroup>            ## As if </colgroup>
5958            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5959              !!!cp ('t269');              !!!cp ('t269');
5960              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5961                !!!parse-error (type => 'unmatched end tag',
5962                                text => 'colgroup', token => $token);
5963              ## Ignore the token              ## Ignore the token
5964                !!!nack ('t269.1');
5965              !!!next-token;              !!!next-token;
5966              redo B;              next B;
5967            } else {            } else {
5968              !!!cp ('t270');              !!!cp ('t270');
5969              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5970              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5971                !!!ack-later;
5972              ## reprocess              ## reprocess
5973              redo B;              next B;
5974            }            }
5975      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5976        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5977          !!!cp ('t271');          !!!cp ('t271');
5978          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5979          !!!next-token;          !!!next-token;
5980          redo B;          next B;
5981        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5982              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5983                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5984                  !!!cp ('t272');              !!!cp ('t272');
5985                  ## As if </option>              ## As if </option>
5986                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5987                } else {            } else {
5988                  !!!cp ('t273');              !!!cp ('t273');
5989                }            }
5990    
5991                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5992                !!!next-token;            !!!nack ('t273.1');
5993                redo B;            !!!next-token;
5994              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5995                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5996                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5997                  ## As if </option>              !!!cp ('t274');
5998                  pop @{$self->{open_elements}};              ## As if </option>
5999                } else {              pop @{$self->{open_elements}};
6000                  !!!cp ('t275');            } else {
6001                }              !!!cp ('t275');
6002              }
6003    
6004                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6005                  !!!cp ('t276');              !!!cp ('t276');
6006                  ## As if </optgroup>              ## As if </optgroup>
6007                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6008                } else {            } else {
6009                  !!!cp ('t277');              !!!cp ('t277');
6010                }            }
6011    
6012                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6013                !!!next-token;            !!!nack ('t277.1');
6014                redo B;            !!!next-token;
6015              } elsif ($token->{tag_name} eq 'select') {            next B;
6016                !!!parse-error (type => 'not closed:select');          } elsif ({
6017                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6018                ## have an element in table scope                   }->{$token->{tag_name}} or
6019                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6020                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6021                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6022                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6023                    !!!cp ('t278');                     tr => 1, td => 1, th => 1,
6024                    $i = $_;                    }->{$token->{tag_name}})) {
6025                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
6026                  } elsif ({            !!!parse-error (type => 'not closed', text => 'select',
6027                            table => 1, html => 1,                            token => $token);
6028                           }->{$node->[1]}) {            ## NOTE: As if the token were </select> (<select> case) or
6029                    !!!cp ('t279');            ## as if there were </select> (otherwise).
6030                    last INSCOPE;            ## have an element in table scope
6031                  }            my $i;
6032                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
6034                  !!!cp ('t280');              if ($node->[1] & SELECT_EL) {
6035                  !!!parse-error (type => 'unmatched end tag:select');                !!!cp ('t278');
6036                  ## Ignore the token                $i = $_;
6037                  !!!next-token;                last INSCOPE;
6038                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6039                }                !!!cp ('t279');
6040                  last INSCOPE;
6041                }
6042              } # INSCOPE
6043              unless (defined $i) {
6044                !!!cp ('t280');
6045                !!!parse-error (type => 'unmatched end tag',
6046                                text => 'select', token => $token);
6047                ## Ignore the token
6048                !!!nack ('t280.1');
6049                !!!next-token;
6050                next B;
6051              }
6052                                
6053                !!!cp ('t281');            !!!cp ('t281');
6054                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6055    
6056                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6057    
6058                !!!next-token;            if ($token->{tag_name} eq 'select') {
6059                redo B;              !!!nack ('t281.2');
6060                !!!next-token;
6061                next B;
6062              } else {
6063                !!!cp ('t281.1');
6064                !!!ack-later;
6065                ## Reprocess the token.
6066                next B;
6067              }
6068          } else {          } else {
6069            !!!cp ('t282');            !!!cp ('t282');
6070            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6071                              text => $token->{tag_name}, token => $token);
6072            ## Ignore the token            ## Ignore the token
6073              !!!nack ('t282.1');
6074            !!!next-token;            !!!next-token;
6075            redo B;            next B;
6076          }          }
6077        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6078              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6079                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6080                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6081                  !!!cp ('t283');              !!!cp ('t283');
6082                  ## As if </option>              ## As if </option>
6083                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6084                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6085                  !!!cp ('t284');              !!!cp ('t284');
6086                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6087                } else {            } else {
6088                  !!!cp ('t285');              !!!cp ('t285');
6089                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6090                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6091                }              ## Ignore the token
6092                !!!next-token;            }
6093                redo B;            !!!nack ('t285.1');
6094              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6095                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6096                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6097                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6098                } else {              !!!cp ('t286');
6099                  !!!cp ('t287');              pop @{$self->{open_elements}};
6100                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6101                  ## Ignore the token              !!!cp ('t287');
6102                }              !!!parse-error (type => 'unmatched end tag',
6103                !!!next-token;                              text => $token->{tag_name}, token => $token);
6104                redo B;              ## Ignore the token
6105              } elsif ($token->{tag_name} eq 'select') {            }
6106                ## have an element in table scope            !!!nack ('t287.1');
6107                my $i;            !!!next-token;
6108                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6109                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6110                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6111                    !!!cp ('t288');            my $i;
6112                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6113                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6114                  } elsif ({              if ($node->[1] & SELECT_EL) {
6115                            table => 1, html => 1,                !!!cp ('t288');
6116                           }->{$node->[1]}) {                $i = $_;
6117                    !!!cp ('t289');                last INSCOPE;
6118                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6119                  }                !!!cp ('t289');
6120                } # INSCOPE                last INSCOPE;
6121                unless (defined $i) {              }
6122                  !!!cp ('t290');            } # INSCOPE
6123                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6124                  ## Ignore the token              !!!cp ('t290');
6125                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6126                  redo B;                              text => $token->{tag_name}, token => $token);
6127                }              ## Ignore the token
6128                !!!nack ('t290.1');
6129                !!!next-token;
6130                next B;
6131              }
6132                                
6133                !!!cp ('t291');            !!!cp ('t291');
6134                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6135    
6136                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6137    
6138                !!!next-token;            !!!nack ('t291.1');
6139                redo B;            !!!next-token;
6140              } elsif ({            next B;
6141                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6142                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6143                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6144                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6145                     }->{$token->{tag_name}}) {
6146    ## TODO: The following is wrong?
6147              !!!parse-error (type => 'unmatched end tag',
6148                              text => $token->{tag_name}, token => $token);
6149                                
6150                ## have an element in table scope            ## have an element in table scope
6151                my $i;            my $i;
6152                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6153                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6154                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6155                    !!!cp ('t292');                !!!cp ('t292');
6156                    $i = $_;                $i = $_;
6157                    last INSCOPE;                last INSCOPE;
6158                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6159                            table => 1, html => 1,                !!!cp ('t293');
6160                           }->{$node->[1]}) {                last INSCOPE;
6161                    !!!cp ('t293');              }
6162                    last INSCOPE;            } # INSCOPE
6163                  }            unless (defined $i) {
6164                } # INSCOPE              !!!cp ('t294');
6165                unless (defined $i) {              ## Ignore the token
6166                  !!!cp ('t294');              !!!nack ('t294.1');
6167                  ## Ignore the token              !!!next-token;
6168                  !!!next-token;              next B;
6169                  redo B;            }
               }  
6170                                
6171                ## As if </select>            ## As if </select>
6172                ## have an element in table scope            ## have an element in table scope
6173                undef $i;            undef $i;
6174                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6176                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6177                    !!!cp ('t295');                !!!cp ('t295');
6178                    $i = $_;                $i = $_;
6179                    last INSCOPE;                last INSCOPE;
6180                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6181                            table => 1, html => 1,  ## ISSUE: Can this state be reached?
6182                           }->{$node->[1]}) {                !!!cp ('t296');
6183                    !!!cp ('t296');                last INSCOPE;
6184                    last INSCOPE;              }
6185                  }            } # INSCOPE
6186                } # INSCOPE            unless (defined $i) {
6187                unless (defined $i) {              !!!cp ('t297');
6188                  !!!cp ('t297');  ## TODO: The following error type is correct?
6189                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6190                  ## Ignore the </select> token                              text => 'select', token => $token);
6191                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6192                  redo B;              !!!nack ('t297.1');
6193                }              !!!next-token; ## TODO: ok?
6194                next B;
6195              }
6196                                
6197                !!!cp ('t298');            !!!cp ('t298');
6198                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6199    
6200                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6201    
6202                ## reprocess            !!!ack-later;
6203                redo B;            ## reprocess
6204              next B;
6205          } else {          } else {
6206            !!!cp ('t299');            !!!cp ('t299');
6207            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6208                              text => $token->{tag_name}, token => $token);
6209            ## Ignore the token            ## Ignore the token
6210              !!!nack ('t299.3');
6211            !!!next-token;            !!!next-token;
6212            redo B;            next B;
6213            }
6214          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6215            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6216                    @{$self->{open_elements}} == 1) { # redundant, maybe
6217              !!!cp ('t299.1');
6218              !!!parse-error (type => 'in body:#eof', token => $token);
6219            } else {
6220              !!!cp ('t299.2');
6221          }          }
6222    
6223            ## Stop parsing.
6224            last B;
6225        } else {        } else {
6226          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6227        }        }
# Line 5085  sub _tree_construction_main ($) { Line 6237  sub _tree_construction_main ($) {
6237            unless (length $token->{data}) {            unless (length $token->{data}) {
6238              !!!cp ('t300');              !!!cp ('t300');
6239              !!!next-token;              !!!next-token;
6240              redo B;              next B;
6241            }            }
6242          }          }
6243                    
6244          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245            !!!cp ('t301');            !!!cp ('t301');
6246            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6247    
6248            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6249          } else {          } else {
6250            !!!cp ('t302');            !!!cp ('t302');
6251          }          }
6252                    
6253          ## "after body" insertion mode          ## "after body" insertion mode
6254          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6255    
6256          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6257          ## reprocess          ## reprocess
6258          redo B;          next B;
6259        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6260          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6261            !!!cp ('t303');            !!!cp ('t303');
6262            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6263                              text => $token->{tag_name}, token => $token);
6264                        
6265            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6266          } else {          } else {
6267            !!!cp ('t304');            !!!cp ('t304');
6268          }          }
6269    
6270          ## "after body" insertion mode          ## "after body" insertion mode
6271          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6272                            text => $token->{tag_name}, token => $token);
6273    
6274          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6275            !!!ack-later;
6276          ## reprocess          ## reprocess
6277          redo B;          next B;
6278        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6279          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6280            !!!cp ('t305');            !!!cp ('t305');
6281            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6282                              text => $token->{tag_name}, token => $token);
6283                        
6284            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6285            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6286          } else {          } else {
6287            !!!cp ('t306');            !!!cp ('t306');
6288          }          }
# Line 5135  sub _tree_construction_main ($) { Line 6291  sub _tree_construction_main ($) {
6291          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6292            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6293              !!!cp ('t307');              !!!cp ('t307');
6294              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6295                                text => 'html', token => $token);
6296              ## Ignore the token              ## Ignore the token
6297              !!!next-token;              !!!next-token;
6298              redo B;              next B;
6299            } else {            } else {
6300              !!!cp ('t308');              !!!cp ('t308');
6301              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6302              !!!next-token;              !!!next-token;
6303              redo B;              next B;
6304            }            }
6305          } else {          } else {
6306            !!!cp ('t309');            !!!cp ('t309');
6307            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6308                              text => $token->{tag_name}, token => $token);
6309    
6310            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6311            ## reprocess            ## reprocess
6312            redo B;            next B;
6313          }          }
6314          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6315            !!!cp ('t309.2');
6316            ## Stop parsing
6317            last B;
6318        } else {        } else {
6319          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6320        }        }
# Line 5164  sub _tree_construction_main ($) { Line 6326  sub _tree_construction_main ($) {
6326            unless (length $token->{data}) {            unless (length $token->{data}) {
6327              !!!cp ('t310');              !!!cp ('t310');
6328              !!!next-token;              !!!next-token;
6329              redo B;              next B;
6330            }            }
6331          }          }
6332                    
6333          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6334            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6335              !!!cp ('t311');              !!!cp ('t311');
6336              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6337            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6338              !!!cp ('t312');              !!!cp ('t312');
6339              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6340            } else { # "after html frameset"            } else { # "after after frameset"
6341              !!!cp ('t313');              !!!cp ('t313');
6342              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "main" phase, "after frameset"...  
             !!!parse-error (type => 'after frameset:#character');  
6343            }            }
6344                        
6345            ## Ignore the token.            ## Ignore the token.
# Line 5192  sub _tree_construction_main ($) { Line 6350  sub _tree_construction_main ($) {
6350              !!!cp ('t315');              !!!cp ('t315');
6351              !!!next-token;              !!!next-token;
6352            }            }
6353            redo B;            next B;
6354          }          }
6355                    
6356          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6357        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         } else {  
           !!!cp ('t317');  
         }  
   
6358          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6359              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6360            !!!cp ('t318');            !!!cp ('t318');
6361            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6362              !!!nack ('t318.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6366                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6367            !!!cp ('t319');            !!!cp ('t319');
6368            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6369            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6370              !!!ack ('t319.1');
6371            !!!next-token;            !!!next-token;
6372            redo B;            next B;
6373          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6374            !!!cp ('t320');            !!!cp ('t320');
6375            ## NOTE: As if in body.            ## NOTE: As if in head.
6376            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6377            redo B;            next B;
6378    
6379              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6380              ## has no parse error.
6381          } else {          } else {
6382            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6383              !!!cp ('t321');              !!!cp ('t321');
6384              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6385            } else {                              text => $token->{tag_name}, token => $token);
6386              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6387              !!!cp ('t322');              !!!cp ('t322');
6388              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6389                                text => $token->{tag_name}, token => $token);
6390              } else { # "after after frameset"
6391                !!!cp ('t322.2');
6392                !!!parse-error (type => 'after after frameset',
6393                                text => $token->{tag_name}, token => $token);
6394            }            }
6395            ## Ignore the token            ## Ignore the token
6396              !!!nack ('t322.1');
6397            !!!next-token;            !!!next-token;
6398            redo B;            next B;
6399          }          }
6400        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         } else {  
           !!!cp ('t324');  
         }  
   
6401          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6402              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6403            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6404                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6405              !!!cp ('t325');              !!!cp ('t325');
6406              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408              ## Ignore the token              ## Ignore the token
6409              !!!next-token;              !!!next-token;
6410            } else {            } else {
# Line 5263  sub _tree_construction_main ($) { Line 6414  sub _tree_construction_main ($) {
6414            }            }
6415    
6416            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6417                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6418              !!!cp ('t327');              !!!cp ('t327');
6419              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6420            } else {            } else {
6421              !!!cp ('t328');              !!!cp ('t328');
6422            }            }
6423            redo B;            next B;
6424          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6425                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6426            !!!cp ('t329');            !!!cp ('t329');
6427            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6428            !!!next-token;            !!!next-token;
6429            redo B;            next B;
6430          } else {          } else {
6431            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6432              !!!cp ('t330');              !!!cp ('t330');
6433              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6434            } else {                              text => $token->{tag_name}, token => $token);
6435              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6436                !!!cp ('t330.1');
6437                !!!parse-error (type => 'after frameset:/',
6438                                text => $token->{tag_name}, token => $token);
6439              } else { # "after after html"
6440              !!!cp ('t331');              !!!cp ('t331');
6441              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6442                                text => $token->{tag_name}, token => $token);
6443            }            }
6444            ## Ignore the token            ## Ignore the token
6445            !!!next-token;            !!!next-token;
6446            redo B;            next B;
6447          }          }
6448          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6449            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6450                    @{$self->{open_elements}} == 1) { # redundant, maybe
6451              !!!cp ('t331.1');
6452              !!!parse-error (type => 'in body:#eof', token => $token);
6453            } else {
6454              !!!cp ('t331.2');
6455            }
6456            
6457            ## Stop parsing
6458            last B;
6459        } else {        } else {
6460          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6461        }        }
# Line 5302  sub _tree_construction_main ($) { Line 6470  sub _tree_construction_main ($) {
6470        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6471          !!!cp ('t332');          !!!cp ('t332');
6472          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6473          $script_start_tag->($insert);          $script_start_tag->();
6474          redo B;          next B;
6475        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6476          !!!cp ('t333');          !!!cp ('t333');
6477          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6478          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6479          redo B;          next B;
6480        } elsif ({        } elsif ({
6481                  base => 1, link => 1,                  base => 1, link => 1,
6482                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6483          !!!cp ('t334');          !!!cp ('t334');
6484          ## 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
6485          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6486          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6487            !!!ack ('t334.1');
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
6490        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6491          ## 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
6492          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6493          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.
6494    
6495          unless ($self->{confident}) {          unless ($self->{confident}) {
6496            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6497              !!!cp ('t335');              !!!cp ('t335');
6498                ## NOTE: Whether the encoding is supported or not is handled
6499                ## in the {change_encoding} callback.
6500              $self->{change_encoding}              $self->{change_encoding}
6501                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6502                            
6503              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6504                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6505                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6506                                           ->{has_reference});                                           ->{has_reference});
6507            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6508              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6509                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6510                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6511                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6512                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6513                !!!cp ('t336');                !!!cp ('t336');
6514                  ## NOTE: Whether the encoding is supported or not is handled
6515                  ## in the {change_encoding} callback.
6516                $self->{change_encoding}                $self->{change_encoding}
6517                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6518                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6519                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6520                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5366  sub _tree_construction_main ($) { Line 6538  sub _tree_construction_main ($) {
6538            }            }
6539          }          }
6540    
6541            !!!ack ('t338.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6545          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6546          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6547          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6548            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6549        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6550          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6551                                
6552          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6553              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6554            !!!cp ('t342');            !!!cp ('t342');
6555            ## Ignore the token            ## Ignore the token
6556          } else {          } else {
# Line 5400  sub _tree_construction_main ($) { Line 6564  sub _tree_construction_main ($) {
6564              }              }
6565            }            }
6566          }          }
6567            !!!nack ('t343.1');
6568          !!!next-token;          !!!next-token;
6569          redo B;          next B;
6570        } elsif ({        } elsif ({
6571                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6572                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6573                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6574                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6575                  pre => 1,                  pre => 1, listing => 1,
6576                    form => 1,
6577                    table => 1,
6578                    hr => 1,
6579                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6580            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6581              !!!cp ('t350');
6582              !!!parse-error (type => 'in form:form', token => $token);
6583              ## Ignore the token
6584              !!!nack ('t350.1');
6585              !!!next-token;
6586              next B;
6587            }
6588    
6589          ## has a p element in scope          ## has a p element in scope
6590          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6591            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6592              !!!cp ('t344');              !!!cp ('t344');
6593              !!!back-token;              !!!back-token; # <form>
6594              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6595              redo B;                        line => $token->{line}, column => $token->{column}};
6596            } elsif ({              next B;
6597                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6598              !!!cp ('t345');              !!!cp ('t345');
6599              last INSCOPE;              last INSCOPE;
6600            }            }
6601          } # INSCOPE          } # INSCOPE
6602                        
6603          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6604          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6605              !!!nack ('t346.1');
6606            !!!next-token;            !!!next-token;
6607            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6608              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5438  sub _tree_construction_main ($) { Line 6615  sub _tree_construction_main ($) {
6615            } else {            } else {
6616              !!!cp ('t348');              !!!cp ('t348');
6617            }            }
6618          } else {          } elsif ($token->{tag_name} eq 'form') {
6619            !!!cp ('t347');            !!!cp ('t347.1');
6620              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6621    
6622              !!!nack ('t347.2');
6623            !!!next-token;            !!!next-token;
6624          }          } elsif ($token->{tag_name} eq 'table') {
6625          redo B;            !!!cp ('t382');
6626        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6627          if (defined $self->{form_element}) {            
6628            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6629            !!!parse-error (type => 'in form:form');  
6630            ## Ignore the token            !!!nack ('t382.1');
6631              !!!next-token;
6632            } elsif ($token->{tag_name} eq 'hr') {
6633              !!!cp ('t386');
6634              pop @{$self->{open_elements}};
6635            
6636              !!!nack ('t386.1');
6637            !!!next-token;            !!!next-token;
           redo B;  
6638          } else {          } else {
6639            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6640            !!!next-token;            !!!next-token;
           redo B;  
6641          }          }
6642        } elsif ($token->{tag_name} eq 'li') {          next B;
6643          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6644          ## has a p element in scope          ## has a p element in scope
6645          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6646            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6647              !!!cp ('t353');              !!!cp ('t353');
6648              !!!back-token;              !!!back-token; # <x>
6649              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6650              redo B;                        line => $token->{line}, column => $token->{column}};
6651            } elsif ({              next B;
6652                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6653              !!!cp ('t354');              !!!cp ('t354');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
# Line 5492  sub _tree_construction_main ($) { Line 6658  sub _tree_construction_main ($) {
6658          ## Step 1          ## Step 1
6659          my $i = -1;          my $i = -1;
6660          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6661            my $li_or_dtdd = {li => {li => 1},
6662                              dt => {dt => 1, dd => 1},
6663                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6664          LI: {          LI: {
6665            ## Step 2            ## Step 2
6666            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6667              if ($i != -1) {              if ($i != -1) {
6668                !!!cp ('t355');                !!!cp ('t355');
6669                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6670                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6671                                      ->manakai_local_name,
6672                                  token => $token);
6673              } else {              } else {
6674                !!!cp ('t356');                !!!cp ('t356');
6675              }              }
# Line 5509  sub _tree_construction_main ($) { Line 6680  sub _tree_construction_main ($) {
6680            }            }
6681                        
6682            ## Step 3            ## Step 3
6683            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6684                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6685                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6686                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6687                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6688                  not ($node->[1] & DIV_EL)) {
6689              !!!cp ('t358');              !!!cp ('t358');
6690              last LI;              last LI;
6691            }            }
# Line 5525  sub _tree_construction_main ($) { Line 6697  sub _tree_construction_main ($) {
6697            redo LI;            redo LI;
6698          } # LI          } # LI
6699                        
6700          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6701            !!!nack ('t359.1');
6702          !!!next-token;          !!!next-token;
6703          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6704        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6705          ## has a p element in scope          ## has a p element in scope
6706          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6707            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6708              !!!cp ('t367');              !!!cp ('t367');
6709              !!!back-token;              !!!back-token; # <plaintext>
6710              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6711              redo B;                        line => $token->{line}, column => $token->{column}};
6712            } elsif ({              next B;
6713                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6714              !!!cp ('t368');              !!!cp ('t368');
6715              last INSCOPE;              last INSCOPE;
6716            }            }
6717          } # INSCOPE          } # INSCOPE
6718                        
6719          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6720                        
6721          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6722                        
6723            !!!nack ('t368.1');
6724          !!!next-token;          !!!next-token;
6725          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6726        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6727          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6728            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6729            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6730              !!!cp ('t371');              !!!cp ('t371');
6731              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6732                            
6733              !!!back-token;              !!!back-token; # <a>
6734              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6735              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6736                $formatting_end_tag->($token);
6737                            
6738              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6739                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5688  sub _tree_construction_main ($) { Line 6758  sub _tree_construction_main ($) {
6758                        
6759          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6760    
6761          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6762          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6763    
6764            !!!nack ('t374.1');
6765          !!!next-token;          !!!next-token;
6766          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6767        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6768          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6769    
6770          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6771          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6772            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6773            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6774              !!!cp ('t376');              !!!cp ('t376');
6775              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6776              !!!back-token;              !!!back-token; # <nobr>
6777              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6778              redo B;                        line => $token->{line}, column => $token->{column}};
6779            } elsif ({              next B;
6780                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6781              !!!cp ('t377');              !!!cp ('t377');
6782              last INSCOPE;              last INSCOPE;
6783            }            }
6784          } # INSCOPE          } # INSCOPE
6785                    
6786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6788                    
6789            !!!nack ('t377.1');
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6793          ## has a button element in scope          ## has a button element in scope
6794          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6795            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6796            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6797              !!!cp ('t378');              !!!cp ('t378');
6798              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6799              !!!back-token;              !!!back-token; # <button>
6800              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6801              redo B;                        line => $token->{line}, column => $token->{column}};
6802            } elsif ({              next B;
6803                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6804              !!!cp ('t379');              !!!cp ('t379');
6805              last INSCOPE;              last INSCOPE;
6806            }            }
# Line 5753  sub _tree_construction_main ($) { Line 6808  sub _tree_construction_main ($) {
6808                        
6809          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6810                        
6811          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6812          push @$active_formatting_elements, ['#marker', ''];  
6813            ## TODO: associate with $self->{form_element} if defined
6814    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6815          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6816            
6817          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6818          !!!next-token;          !!!next-token;
6819          redo B;          next B;
6820        } elsif ({        } elsif ({
6821                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6822                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6823                  image => 1,                  noembed => 1,
6824                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6825                    noscript => 0, ## TODO: 1 if scripting is enabled
6826                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6827          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6828            !!!cp ('t384');            !!!cp ('t381');
6829            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6830          } else {          } else {
6831            !!!cp ('t385');            !!!cp ('t399');
6832          }          }
6833            ## NOTE: There is an "as if in body" code clone.
6834          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6835          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6836        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6837          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6838                    
6839          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6840            !!!cp ('t389');            !!!cp ('t389');
6841            ## Ignore the token            ## Ignore the token
6842              !!!nack ('t389'); ## NOTE: Not acknowledged.
6843            !!!next-token;            !!!next-token;
6844            redo B;            next B;
6845          } else {          } else {
6846              !!!ack ('t391.1');
6847    
6848            my $at = $token->{attributes};            my $at = $token->{attributes};
6849            my $form_attrs;            my $form_attrs;
6850            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5867  sub _tree_construction_main ($) { Line 6854  sub _tree_construction_main ($) {
6854            delete $at->{prompt};            delete $at->{prompt};
6855            my @tokens = (            my @tokens = (
6856                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6857                           attributes => $form_attrs},                           attributes => $form_attrs,
6858                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6859                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6860                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6861                            {type => START_TAG_TOKEN, tag_name => 'p',
6862                             line => $token->{line}, column => $token->{column}},
6863                            {type => START_TAG_TOKEN, tag_name => 'label',
6864                             line => $token->{line}, column => $token->{column}},
6865                         );                         );
6866            if ($prompt_attr) {            if ($prompt_attr) {
6867              !!!cp ('t390');              !!!cp ('t390');
6868              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6869                               #line => $token->{line}, column => $token->{column},
6870                              };
6871            } else {            } else {
6872              !!!cp ('t391');              !!!cp ('t391');
6873              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6874                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6875                               #line => $token->{line}, column => $token->{column},
6876                              }; # SHOULD
6877              ## TODO: make this configurable              ## TODO: make this configurable
6878            }            }
6879            push @tokens,            push @tokens,
6880                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6881                             line => $token->{line}, column => $token->{column}},
6882                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6883                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6884                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6885                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6886                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6887            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6888                             line => $token->{line}, column => $token->{column}},
6889                            {type => END_TAG_TOKEN, tag_name => 'form',
6890                             line => $token->{line}, column => $token->{column}};
6891            !!!back-token (@tokens);            !!!back-token (@tokens);
6892            redo B;            !!!next-token;
6893              next B;
6894          }          }
6895        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6896          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6897          my $el;          my $el;
6898          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6899                    
6900          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6901          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5904  sub _tree_construction_main ($) { Line 6904  sub _tree_construction_main ($) {
6904          $insert->($el);          $insert->($el);
6905                    
6906          my $text = '';          my $text = '';
6907            !!!nack ('t392.1');
6908          !!!next-token;          !!!next-token;
6909          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6910            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5934  sub _tree_construction_main ($) { Line 6935  sub _tree_construction_main ($) {
6935            ## Ignore the token            ## Ignore the token
6936          } else {          } else {
6937            !!!cp ('t398');            !!!cp ('t398');
6938            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6939          }          }
6940          !!!next-token;          !!!next-token;
6941            next B;
6942          } elsif ($token->{tag_name} eq 'rt' or
6943                   $token->{tag_name} eq 'rp') {
6944            ## has a |ruby| element in scope
6945            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946              my $node = $self->{open_elements}->[$_];
6947              if ($node->[1] & RUBY_EL) {
6948                !!!cp ('t398.1');
6949                ## generate implied end tags
6950                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6951                  !!!cp ('t398.2');
6952                  pop @{$self->{open_elements}};
6953                }
6954                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6955                  !!!cp ('t398.3');
6956                  !!!parse-error (type => 'not closed',
6957                                  text => $self->{open_elements}->[-1]->[0]
6958                                      ->manakai_local_name,
6959                                  token => $token);
6960                  pop @{$self->{open_elements}}
6961                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6962                }
6963                last INSCOPE;
6964              } elsif ($node->[1] & SCOPING_EL) {
6965                !!!cp ('t398.4');
6966                last INSCOPE;
6967              }
6968            } # INSCOPE
6969    
6970            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6971    
6972            !!!nack ('t398.5');
6973            !!!next-token;
6974          redo B;          redo B;
6975        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6976                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6977          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6980    
6981            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6982    
6983            ## "adjust foreign attributes" - done in insert-element-f
6984                    
6985          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6986                    
6987          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6988              pop @{$self->{open_elements}};
6989              !!!ack ('t398.1');
6990            } else {
6991              !!!cp ('t398.2');
6992              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6993              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6994              ## mode, "in body" (not "in foreign content") secondary insertion
6995              ## mode, maybe.
6996            }
6997    
6998          !!!next-token;          !!!next-token;
6999          redo B;          next B;
7000        } elsif ({        } elsif ({
7001                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7002                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5964  sub _tree_construction_main ($) { Line 7004  sub _tree_construction_main ($) {
7004                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7005                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7006          !!!cp ('t401');          !!!cp ('t401');
7007          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7008                            text => $token->{tag_name}, token => $token);
7009          ## Ignore the token          ## Ignore the token
7010            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013                    
7014          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7015        } else {        } else {
7016          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7017              !!!cp ('t384');
7018              !!!parse-error (type => 'image', token => $token);
7019              $token->{tag_name} = 'img';
7020            } else {
7021              !!!cp ('t385');
7022            }
7023    
7024            ## NOTE: There is an "as if <br>" code clone.
7025          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7026                    
7027          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7028    
7029            if ({
7030                 applet => 1, marquee => 1, object => 1,
7031                }->{$token->{tag_name}}) {
7032              !!!cp ('t380');
7033              push @$active_formatting_elements, ['#marker', ''];
7034              !!!nack ('t380.1');
7035            } elsif ({
7036                      b => 1, big => 1, em => 1, font => 1, i => 1,
7037                      s => 1, small => 1, strile => 1,
7038                      strong => 1, tt => 1, u => 1,
7039                     }->{$token->{tag_name}}) {
7040              !!!cp ('t375');
7041              push @$active_formatting_elements, $self->{open_elements}->[-1];
7042              !!!nack ('t375.1');
7043            } elsif ($token->{tag_name} eq 'input') {
7044              !!!cp ('t388');
7045              ## TODO: associate with $self->{form_element} if defined
7046              pop @{$self->{open_elements}};
7047              !!!ack ('t388.2');
7048            } elsif ({
7049                      area => 1, basefont => 1, bgsound => 1, br => 1,
7050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7051                      #image => 1,
7052                     }->{$token->{tag_name}}) {
7053              !!!cp ('t388.1');
7054              pop @{$self->{open_elements}};
7055              !!!ack ('t388.3');
7056            } elsif ($token->{tag_name} eq 'select') {
7057              ## TODO: associate with $self->{form_element} if defined
7058            
7059              if ($self->{insertion_mode} & TABLE_IMS or
7060                  $self->{insertion_mode} & BODY_TABLE_IMS or
7061                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7062                !!!cp ('t400.1');
7063                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7064              } else {
7065                !!!cp ('t400.2');
7066                $self->{insertion_mode} = IN_SELECT_IM;
7067              }
7068              !!!nack ('t400.3');
7069            } else {
7070              !!!nack ('t402');
7071            }
7072                    
7073          !!!next-token;          !!!next-token;
7074          redo B;          next B;
7075        }        }
7076      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7077        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7078          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7079              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7080            for (@{$self->{open_elements}}) {          INSCOPE: {
7081              unless ({            for (reverse @{$self->{open_elements}}) {
7082                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7083                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7084                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7085                      }->{$_->[1]}) {                last INSCOPE;
7086                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7087                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7088              } else {                last;
               !!!cp ('t404');  
7089              }              }
7090            }            }
7091    
7092            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7093                              text => $token->{tag_name}, token => $token);
7094              ## NOTE: Ignore the token.
7095            !!!next-token;            !!!next-token;
7096            redo B;            next B;
7097          } else {          } # INSCOPE
7098            !!!cp ('t405');  
7099            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7100            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7101            !!!next-token;              !!!cp ('t403');
7102            redo B;              !!!parse-error (type => 'not closed',
7103                                text => $_->[0]->manakai_local_name,
7104                                token => $token);
7105                last;
7106              } else {
7107                !!!cp ('t404');
7108              }
7109          }          }
7110    
7111            $self->{insertion_mode} = AFTER_BODY_IM;
7112            !!!next-token;
7113            next B;
7114        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7115          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
7116            ## up-to-date, though it has same effect as speced.
7117            if (@{$self->{open_elements}} > 1 and
7118                $self->{open_elements}->[1]->[1] & BODY_EL) {
7119            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7120            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7121              !!!cp ('t406');              !!!cp ('t406');
7122              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7123                                text => $self->{open_elements}->[1]->[0]
7124                                    ->manakai_local_name,
7125                                token => $token);
7126            } else {            } else {
7127              !!!cp ('t407');              !!!cp ('t407');
7128            }            }
7129            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7130            ## reprocess            ## reprocess
7131            redo B;            next B;
7132          } else {          } else {
7133            !!!cp ('t408');            !!!cp ('t408');
7134            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7135                              text => $token->{tag_name}, token => $token);
7136            ## Ignore the token            ## Ignore the token
7137            !!!next-token;            !!!next-token;
7138            redo B;            next B;
7139          }          }
7140        } elsif ({        } elsif ({
7141                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7142                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7143                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7144                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7145                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7146                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7147          ## has an element in scope          ## has an element in scope
7148          my $i;          my $i;
7149          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7150            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7151            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7152              !!!cp ('t410');              !!!cp ('t410');
7153              $i = $_;              $i = $_;
7154              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7155            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7156              !!!cp ('t411');              !!!cp ('t411');
7157              last INSCOPE;              last INSCOPE;
7158            }            }
7159          } # INSCOPE          } # INSCOPE
7160            
7161          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7162            if (defined $i) {            !!!cp ('t413');
7163              !!!parse-error (type => 'unmatched end tag',
7164                              text => $token->{tag_name}, token => $token);
7165              ## NOTE: Ignore the token.
7166            } else {
7167              ## Step 1. generate implied end tags
7168              while ({
7169                      ## END_TAG_OPTIONAL_EL
7170                      dd => ($token->{tag_name} ne 'dd'),
7171                      dt => ($token->{tag_name} ne 'dt'),
7172                      li => ($token->{tag_name} ne 'li'),
7173                      p => 1,
7174                      rt => 1,
7175                      rp => 1,
7176                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7177                !!!cp ('t409');
7178                pop @{$self->{open_elements}};
7179              }
7180    
7181              ## Step 2.
7182              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7183                      ne $token->{tag_name}) {
7184              !!!cp ('t412');              !!!cp ('t412');
7185              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7186                                text => $self->{open_elements}->[-1]->[0]
7187                                    ->manakai_local_name,
7188                                token => $token);
7189            } else {            } else {
7190              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7191            }            }
7192          }  
7193                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7194            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7195          } elsif ($token->{tag_name} eq 'p') {  
7196            !!!cp ('t415');            ## Step 4.
7197            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7198            my $el;                if {
7199            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7200            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7201          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7202          !!!next-token;          !!!next-token;
7203          redo B;          next B;
7204        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7205            undef $self->{form_element};
7206    
7207          ## has an element in scope          ## has an element in scope
7208            my $i;
7209          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7210            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7211            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7212              !!!cp ('t418');              !!!cp ('t418');
7213                $i = $_;
7214              last INSCOPE;              last INSCOPE;
7215            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7216              !!!cp ('t419');              !!!cp ('t419');
7217              last INSCOPE;              last INSCOPE;
7218            }            }
7219          } # INSCOPE          } # INSCOPE
7220            
7221          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
7222            !!!cp ('t421');            !!!cp ('t421');
7223            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7224                              text => $token->{tag_name}, token => $token);
7225              ## NOTE: Ignore the token.
7226            } else {
7227              ## Step 1. generate implied end tags
7228              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7229                !!!cp ('t417');
7230                pop @{$self->{open_elements}};
7231              }
7232              
7233              ## Step 2.
7234              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7235                      ne $token->{tag_name}) {
7236                !!!cp ('t417.1');
7237                !!!parse-error (type => 'not closed',
7238                                text => $self->{open_elements}->[-1]->[0]
7239                                    ->manakai_local_name,
7240                                token => $token);
7241              } else {
7242                !!!cp ('t420');
7243              }  
7244              
7245              ## Step 3.
7246              splice @{$self->{open_elements}}, $i;
7247          }          }
7248    
         undef $self->{form_element};  
7249          !!!next-token;          !!!next-token;
7250          redo B;          next B;
7251        } elsif ({        } elsif ({
7252                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7253                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6141  sub _tree_construction_main ($) { Line 7255  sub _tree_construction_main ($) {
7255          my $i;          my $i;
7256          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7257            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7258            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
7259              !!!cp ('t423');              !!!cp ('t423');
7260              $i = $_;              $i = $_;
7261              last INSCOPE;              last INSCOPE;
7262            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7263              !!!cp ('t424');              !!!cp ('t424');
7264              last INSCOPE;              last INSCOPE;
7265            }            }
7266          } # INSCOPE          } # INSCOPE
7267    
7268            unless (defined $i) { # has an element in scope
7269              !!!cp ('t425.1');
7270              !!!parse-error (type => 'unmatched end tag',
7271                              text => $token->{tag_name}, token => $token);
7272              ## NOTE: Ignore the token.
7273            } else {
7274              ## Step 1. generate implied end tags
7275              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7276                !!!cp ('t422');
7277                pop @{$self->{open_elements}};
7278              }
7279              
7280              ## Step 2.
7281              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7282                      ne $token->{tag_name}) {
7283                !!!cp ('t425');
7284                !!!parse-error (type => 'unmatched end tag',
7285                                text => $token->{tag_name}, token => $token);
7286              } else {
7287                !!!cp ('t426');
7288              }
7289    
7290              ## Step 3.
7291              splice @{$self->{open_elements}}, $i;
7292            }
7293                    
7294          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7295            !!!cp ('t425');          next B;
7296            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7297            ## has an element in scope
7298            my $i;
7299            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7300              my $node = $self->{open_elements}->[$_];
7301              if ($node->[1] & P_EL) {
7302                !!!cp ('t410.1');
7303                $i = $_;
7304                last INSCOPE;
7305              } elsif ($node->[1] & SCOPING_EL) {
7306                !!!cp ('t411.1');
7307                last INSCOPE;
7308              }
7309            } # INSCOPE
7310    
7311            if (defined $i) {
7312              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7313                      ne $token->{tag_name}) {
7314                !!!cp ('t412.1');
7315                !!!parse-error (type => 'not closed',
7316                                text => $self->{open_elements}->[-1]->[0]
7317                                    ->manakai_local_name,
7318                                token => $token);
7319              } else {
7320                !!!cp ('t414.1');
7321              }
7322    
7323              splice @{$self->{open_elements}}, $i;
7324          } else {          } else {
7325            !!!cp ('t426');            !!!cp ('t413.1');
7326              !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328    
7329              !!!cp ('t415.1');
7330              ## As if <p>, then reprocess the current token
7331              my $el;
7332              !!!create-element ($el, $HTML_NS, 'p',, $token);
7333              $insert->($el);
7334              ## NOTE: Not inserted into |$self->{open_elements}|.
7335          }          }
7336            
         splice @{$self->{open_elements}}, $i if defined $i;  
7337          !!!next-token;          !!!next-token;
7338          redo B;          next B;
7339        } elsif ({        } elsif ({
7340                  a => 1,                  a => 1,
7341                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6186  sub _tree_construction_main ($) { Line 7343  sub _tree_construction_main ($) {
7343                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7345          !!!cp ('t427');          !!!cp ('t427');
7346          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7347          redo B;          next B;
7348        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7349          !!!cp ('t428');          !!!cp ('t428');
7350          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7351                            text => 'br', token => $token);
7352    
7353          ## As if <br>          ## As if <br>
7354          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7355                    
7356          my $el;          my $el;
7357          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7358          $insert->($el);          $insert->($el);
7359                    
7360          ## Ignore the token.          ## Ignore the token.
7361          !!!next-token;          !!!next-token;
7362          redo B;          next B;
7363        } elsif ({        } elsif ({
7364                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7365                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6215  sub _tree_construction_main ($) { Line 7373  sub _tree_construction_main ($) {
7373                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7374                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7375          !!!cp ('t429');          !!!cp ('t429');
7376          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7377                            text => $token->{tag_name}, token => $token);
7378          ## Ignore the token          ## Ignore the token
7379          !!!next-token;          !!!next-token;
7380          redo B;          next B;
7381                    
7382          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7383                    
# Line 6229  sub _tree_construction_main ($) { Line 7388  sub _tree_construction_main ($) {
7388    
7389          ## Step 2          ## Step 2
7390          S2: {          S2: {
7391            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7392              ## Step 1              ## Step 1
7393              ## generate implied end tags              ## generate implied end tags
7394              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7395                !!!cp ('t430');                !!!cp ('t430');
7396                !!!back-token;                ## NOTE: |<ruby><rt></ruby>|.
7397                $token = {type => END_TAG_TOKEN,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7398                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                ## which seems wrong.
7399                redo B;                pop @{$self->{open_elements}};
7400                  $node_i++;
7401              }              }
7402                    
7403              ## Step 2              ## Step 2
7404              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7405                        ne $token->{tag_name}) {
7406                !!!cp ('t431');                !!!cp ('t431');
7407                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7408                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7409                                  text => $self->{open_elements}->[-1]->[0]
7410                                      ->manakai_local_name,
7411                                  token => $token);
7412              } else {              } else {
7413                !!!cp ('t432');                !!!cp ('t432');
7414              }              }
7415                            
7416              ## Step 3              ## Step 3
7417              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7418    
7419              !!!next-token;              !!!next-token;
7420              last S2;              last S2;
7421            } else {            } else {
7422              ## Step 3              ## Step 3
7423              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7424                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7425                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7426                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7427                !!!cp ('t433');                !!!cp ('t433');
7428                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7429                                  text => $token->{tag_name}, token => $token);
7430                ## Ignore the token                ## Ignore the token
7431                !!!next-token;                !!!next-token;
7432                last S2;                last S2;
# Line 6281  sub _tree_construction_main ($) { Line 7442  sub _tree_construction_main ($) {
7442            ## Step 5;            ## Step 5;
7443            redo S2;            redo S2;
7444          } # S2          } # S2
7445          redo B;          next B;
7446        }        }
7447      }      }
7448      redo B;      next B;
7449      } continue { # B
7450        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7451          ## NOTE: The code below is executed in cases where it does not have
7452          ## to be, but it it is harmless even in those cases.
7453          ## has an element in scope
7454          INSCOPE: {
7455            for (reverse 0..$#{$self->{open_elements}}) {
7456              my $node = $self->{open_elements}->[$_];
7457              if ($node->[1] & FOREIGN_EL) {
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                last;
7461              }
7462            }
7463            
7464            ## NOTE: No foreign element in scope.
7465            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7466          } # INSCOPE
7467        }
7468    } # B    } # B
7469    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7470    ## Stop parsing # MUST    ## Stop parsing # MUST
7471        
7472    ## TODO: script stuffs    ## TODO: script stuffs
7473  } # _tree_construct_main  } # _tree_construct_main
7474    
7475  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7476    my $class = shift;    my $class = shift;
7477    my $node = shift;    my $node = shift;
7478    my $s = \$_[0];    my $s = \$_[0];
7479    my $onerror = $_[1];    my $onerror = $_[1];
7480      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7481    
7482    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7483    
# Line 6320  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496      }      }
7497    
7498      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7499      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7500    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7501      ## TODO: If non-html element      ## TODO: If non-html element
7502    
7503      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7504    
7505    ## TODO: Support for $get_wrapper
7506    
7507      ## Step 1 # MUST      ## Step 1 # MUST
7508      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7509      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6333  sub set_inner_html ($$$) { Line 7511  sub set_inner_html ($$$) {
7511      my $p = $class->new;      my $p = $class->new;
7512      $p->{document} = $doc;      $p->{document} = $doc;
7513    
7514      ## Step 9 # MUST      ## Step 8 # MUST
7515      my $i = 0;      my $i = 0;
7516      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7517      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7518      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7519        my $self = shift;        my $self = shift;
7520    
# Line 6345  sub set_inner_html ($$$) { Line 7523  sub set_inner_html ($$$) {
7523    
7524        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7525        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7526        $column++;  
7527          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7528          $p->{column}++;
7529    
7530        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7531          $line++;          $p->{line}++;
7532          $column = 0;          $p->{column} = 0;
7533          !!!cp ('i1');          !!!cp ('i1');
7534        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7535          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7536          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7537          $line++;          $p->{line}++;
7538          $column = 0;          $p->{column} = 0;
7539          !!!cp ('i2');          !!!cp ('i2');
7540        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7541          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6364  sub set_inner_html ($$$) { Line 7544  sub set_inner_html ($$$) {
7544          !!!cp ('i4');          !!!cp ('i4');
7545          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7546          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7547          } elsif ($self->{next_char} <= 0x0008 or
7548                   (0x000E <= $self->{next_char} and
7549                    $self->{next_char} <= 0x001F) or
7550                   (0x007F <= $self->{next_char} and
7551                    $self->{next_char} <= 0x009F) or
7552                   (0xD800 <= $self->{next_char} and
7553                    $self->{next_char} <= 0xDFFF) or
7554                   (0xFDD0 <= $self->{next_char} and
7555                    $self->{next_char} <= 0xFDDF) or
7556                   {
7557                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7558                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7559                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7560                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7561                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7562                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7563                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7564                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7565                    0x10FFFE => 1, 0x10FFFF => 1,
7566                   }->{$self->{next_char}}) {
7567            !!!cp ('i4.1');
7568            if ($self->{next_char} < 0x10000) {
7569              !!!parse-error (type => 'control char',
7570                              text => (sprintf 'U+%04X', $self->{next_char}));
7571            } else {
7572              !!!parse-error (type => 'control char',
7573                              text => (sprintf 'U-%08X', $self->{next_char}));
7574            }
7575        }        }
7576      };      };
7577      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6371  sub set_inner_html ($$$) { Line 7579  sub set_inner_html ($$$) {
7579            
7580      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7581        my (%opt) = @_;        my (%opt) = @_;
7582        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7583          my $column = $opt{column};
7584          if (defined $opt{token} and defined $opt{token}->{line}) {
7585            $line = $opt{token}->{line};
7586            $column = $opt{token}->{column};
7587          }
7588          warn "Parse error ($opt{type}) at line $line column $column\n";
7589      };      };
7590      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7591        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7592      };      };
7593            
7594      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6398  sub set_inner_html ($$$) { Line 7612  sub set_inner_html ($$$) {
7612          unless defined $p->{content_model};          unless defined $p->{content_model};
7613          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7614    
7615      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7616          ## TODO: Foreign element OK?
7617    
7618      ## Step 4      ## Step 3
7619      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7620        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7621    
7622      ## Step 5 # MUST      ## Step 4 # MUST
7623      $doc->append_child ($root);      $doc->append_child ($root);
7624    
7625      ## Step 6 # MUST      ## Step 5 # MUST
7626      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7627    
7628      undef $p->{head_element};      undef $p->{head_element};
7629    
7630      ## Step 7 # MUST      ## Step 6 # MUST
7631      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7632    
7633      ## Step 8 # MUST      ## Step 7 # MUST
7634      my $anode = $node;      my $anode = $node;
7635      AN: while (defined $anode) {      AN: while (defined $anode) {
7636        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
# Line 6431  sub set_inner_html ($$$) { Line 7646  sub set_inner_html ($$$) {
7646        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7647      } # AN      } # AN
7648            
7649      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7650      {      {
7651        my $self = $p;        my $self = $p;
7652        !!!next-token;        !!!next-token;
7653      }      }
7654      $p->_tree_construction_main;      $p->_tree_construction_main;
7655    
7656      ## Step 11 # MUST      ## Step 10 # MUST
7657      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7658      for (@cn) {      for (@cn) {
7659        $node->remove_child ($_);        $node->remove_child ($_);
7660      }      }
7661      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7662    
7663      ## Step 12 # MUST      ## Step 11 # MUST
7664      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7665      for (@cn) {      for (@cn) {
7666        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 6455  sub set_inner_html ($$$) { Line 7669  sub set_inner_html ($$$) {
7669      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7670    
7671      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7672    
7673        delete $p->{parse_error}; # delete loop
7674    } else {    } else {
7675      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
7676    }    }

Legend:
Removed from v.1.82  
changed lines
  Added in v.1.162

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24