/[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.86 by wakaba, Thu Mar 6 15:56:52 2008 UTC revision 1.167 by wakaba, Sat Sep 13 09:02:28 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_SECTION_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
814    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
815    sub ENTITY_STATE () { 44 } # "consume a character reference" in the spec
816    
817  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
818  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 829  sub TABLE_IMS ()      { 0b1000000 }
829  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
830  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
831  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
832    sub SELECT_IMS ()     { 0b10000000000 }
833    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
834        ## NOTE: "in foreign content" insertion mode is special; it is combined
835        ## with the secondary insertion mode.  In this parser, they are stored
836        ## together in the bit-or'ed form.
837    
838  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
839    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 856  sub IN_TABLE_IM () { TABLE_IMS }
856  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
857  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
858  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
859  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
860    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
861  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
862    
863  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 333  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 865  sub IN_COLUMN_GROUP_IM () { 0b10 }
865  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
866    my $self = shift;    my $self = shift;
867    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
868      #$self->{state_keyword}; # initialized when used
869    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
870    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
871    undef $self->{current_attribute};    undef $self->{current_attribute};
872    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
873    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
874      delete $self->{self_closing};
875    $self->{char} = [];    $self->{char} = [];
876    # $self->{next_char}    # $self->{next_char}
877    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 892  sub _initialize_tokenizer ($) {
892  ##        ->{value}  ##        ->{value}
893  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
894  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
895    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
896    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
897    ##     while the token is pushed back to the stack.
898    
899  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
900    
# Line 384  sub _initialize_tokenizer ($) { Line 921  sub _initialize_tokenizer ($) {
921    
922  sub _get_next_token ($) {  sub _get_next_token ($) {
923    my $self = shift;    my $self = shift;
924    
925      if ($self->{self_closing}) {
926        !!!parse-error (type => 'nestc', token => $self->{current_token});
927        ## NOTE: The |self_closing| flag is only set by start tag token.
928        ## In addition, when a start tag token is emitted, it is always set to
929        ## |current_token|.
930        delete $self->{self_closing};
931      }
932    
933    if (@{$self->{token}}) {    if (@{$self->{token}}) {
934        $self->{self_closing} = $self->{token}->[0]->{self_closing};
935      return shift @{$self->{token}};      return shift @{$self->{token}};
936    }    }
937    
# Line 394  sub _get_next_token ($) { Line 941  sub _get_next_token ($) {
941          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
942              not $self->{escape}) {              not $self->{escape}) {
943            !!!cp (1);            !!!cp (1);
944            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
945              ## "entity data state".  In this implementation, the tokenizer
946              ## is switched to the |ENTITY_STATE|, which is an implementation
947              ## of the "consume a character reference" algorithm.
948              #$self->{state} = ENTITY_DATA_STATE;
949              $self->{entity_in_attr} = 0;
950              $self->{entity_additional} = -1;
951              $self->{state} = ENTITY_STATE;
952            !!!next-input-character;            !!!next-input-character;
953            redo A;            redo A;
954          } else {          } else {
# Line 447  sub _get_next_token ($) { Line 1001  sub _get_next_token ($) {
1001          #          #
1002        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1003          !!!cp (11);          !!!cp (11);
1004          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1005                      line => $self->{line}, column => $self->{column}});
1006          last A; ## TODO: ok?          last A; ## TODO: ok?
1007        } else {        } else {
1008          !!!cp (12);          !!!cp (12);
1009        }        }
1010        # Anything else        # Anything else
1011        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1012                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1013                       line => $self->{line}, column => $self->{column},
1014                      };
1015        ## Stay in the data state        ## Stay in the data state
1016        !!!next-input-character;        !!!next-input-character;
1017    
# Line 462  sub _get_next_token ($) { Line 1019  sub _get_next_token ($) {
1019    
1020        redo A;        redo A;
1021      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1022        ## (cannot happen in CDATA state)        my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1023          
1024        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->{entity_return};
1025    
1026        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
1027        # next-input-character is already done        # next-input-character is already done
1028    
1029        unless (defined $token) {        unless (defined $token) {
1030          !!!cp (13);          !!!cp (13);
1031          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
1032                      line => $l, column => $c,
1033                     });
1034        } else {        } else {
1035          !!!cp (14);          !!!cp (14);
1036          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049            ## reconsume            ## reconsume
1050            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1051    
1052            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1053                        line => $self->{line_prev},
1054                        column => $self->{column_prev},
1055                       });
1056    
1057            redo A;            redo A;
1058          }          }
# Line 510  sub _get_next_token ($) { Line 1072  sub _get_next_token ($) {
1072            !!!cp (19);            !!!cp (19);
1073            $self->{current_token}            $self->{current_token}
1074              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1075                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1076                   line => $self->{line_prev},
1077                   column => $self->{column_prev}};
1078            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1079            !!!next-input-character;            !!!next-input-character;
1080            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1082  sub _get_next_token ($) {
1082                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1083            !!!cp (20);            !!!cp (20);
1084            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1085                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1086                                        line => $self->{line_prev},
1087                                        column => $self->{column_prev}};
1088            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1089            !!!next-input-character;            !!!next-input-character;
1090            redo A;            redo A;
1091          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1092            !!!cp (21);            !!!cp (21);
1093            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1094                              line => $self->{line_prev},
1095                              column => $self->{column_prev});
1096            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1097            !!!next-input-character;            !!!next-input-character;
1098    
1099            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1100                        line => $self->{line_prev},
1101                        column => $self->{column_prev},
1102                       });
1103    
1104            redo A;            redo A;
1105          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1106            !!!cp (22);            !!!cp (22);
1107            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1108                              line => $self->{line_prev},
1109                              column => $self->{column_prev});
1110            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1111              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1112                                        line => $self->{line_prev},
1113                                        column => $self->{column_prev},
1114                                       };
1115            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1116            redo A;            redo A;
1117          } else {          } else {
1118            !!!cp (23);            !!!cp (23);
1119            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1120                              line => $self->{line_prev},
1121                              column => $self->{column_prev});
1122            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1123            ## reconsume            ## reconsume
1124    
1125            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1126                        line => $self->{line_prev},
1127                        column => $self->{column_prev},
1128                       });
1129    
1130            redo A;            redo A;
1131          }          }
# Line 551  sub _get_next_token ($) { Line 1133  sub _get_next_token ($) {
1133          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1134        }        }
1135      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1136          ## NOTE: The "close tag open state" in the spec is implemented as
1137          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1138    
1139          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1140        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1141          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1142            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1143            my @next_char;            $self->{state_keyword} = '';
1144            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1145              push @next_char, $self->{next_char};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1146          } else {          } else {
1147            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1148              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1149            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1150            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1151            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1152              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1153                        line => $l, column => $c,
1154                       });
1155            redo A;            redo A;
1156          }          }
1157        }        }
1158          
1159        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1160            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1161          !!!cp (29);          !!!cp (29);
1162          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1163                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1164                   tag_name => chr ($self->{next_char} + 0x0020),
1165                   line => $l, column => $c};
1166          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1170  sub _get_next_token ($) {
1170                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1171          !!!cp (30);          !!!cp (30);
1172          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1173                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1174                                      line => $l, column => $c};
1175          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1176          !!!next-input-character;          !!!next-input-character;
1177          redo A;          redo A;
1178        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1179          !!!cp (31);          !!!cp (31);
1180          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1181                            line => $self->{line_prev}, ## "<" in "</>"
1182                            column => $self->{column_prev} - 1);
1183          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1184          !!!next-input-character;          !!!next-input-character;
1185          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1189  sub _get_next_token ($) {
1189          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1190          # reconsume          # reconsume
1191    
1192          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1193                      line => $l, column => $c,
1194                     });
1195    
1196          redo A;          redo A;
1197        } else {        } else {
1198          !!!cp (33);          !!!cp (33);
1199          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1200          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1201          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1202          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1203                                      column => $self->{column_prev} - 1,
1204                                     };
1205            ## NOTE: $self->{next_char} is intentionally left as is.
1206            ## Although the "anything else" case of the spec not explicitly
1207            ## states that the next input character is to be reconsumed,
1208            ## it will be included to the |data| of the comment token
1209            ## generated from the bogus end tag, as defined in the
1210            ## "bogus comment state" entry.
1211            redo A;
1212          }
1213        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1214          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1215          if (length $ch) {
1216            my $CH = $ch;
1217            $ch =~ tr/a-z/A-Z/;
1218            my $nch = chr $self->{next_char};
1219            if ($nch eq $ch or $nch eq $CH) {
1220              !!!cp (24);
1221              ## Stay in the state.
1222              $self->{state_keyword} .= $nch;
1223              !!!next-input-character;
1224              redo A;
1225            } else {
1226              !!!cp (25);
1227              $self->{state} = DATA_STATE;
1228              ## Reconsume.
1229              !!!emit ({type => CHARACTER_TOKEN,
1230                        data => '</' . $self->{state_keyword},
1231                        line => $self->{line_prev},
1232                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1233                       });
1234              redo A;
1235            }
1236          } else { # after "<{tag-name}"
1237            unless ({
1238                     0x0009 => 1, # HT
1239                     0x000A => 1, # LF
1240                     0x000B => 1, # VT
1241                     0x000C => 1, # FF
1242                     0x0020 => 1, # SP
1243                     0x003E => 1, # >
1244                     0x002F => 1, # /
1245                     -1 => 1, # EOF
1246                    }->{$self->{next_char}}) {
1247              !!!cp (26);
1248              ## Reconsume.
1249              $self->{state} = DATA_STATE;
1250              !!!emit ({type => CHARACTER_TOKEN,
1251                        data => '</' . $self->{state_keyword},
1252                        line => $self->{line_prev},
1253                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1254                       });
1255              redo A;
1256            } else {
1257              !!!cp (27);
1258              $self->{current_token}
1259                  = {type => END_TAG_TOKEN,
1260                     tag_name => $self->{last_emitted_start_tag_name},
1261                     line => $self->{line_prev},
1262                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1263              $self->{state} = TAG_NAME_STATE;
1264              ## Reconsume.
1265              redo A;
1266            }
1267        }        }
1268      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1269        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 657  sub _get_next_token ($) { Line 1278  sub _get_next_token ($) {
1278        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1279          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1280            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1281            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1282          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1283            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1310          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1311            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1312            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1313          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1314            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329    
1330          redo A;          redo A;
1331        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1332            !!!cp (42);
1333            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1334          !!!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  
1335          redo A;          redo A;
1336        } else {        } else {
1337          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1354  sub _get_next_token ($) {
1354        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1355          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1356            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1357            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1358          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1359            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1375  sub _get_next_token ($) {
1375        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1376                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1377          !!!cp (49);          !!!cp (49);
1378          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1379                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1380                   value => '',
1381                   line => $self->{line}, column => $self->{column}};
1382          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1386            !!!cp (50);
1387            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1388          !!!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  
1389          redo A;          redo A;
1390        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1391          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1392          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1393            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1394            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1395          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1396            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1420  sub _get_next_token ($) {
1420          } else {          } else {
1421            !!!cp (56);            !!!cp (56);
1422          }          }
1423          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1424                                value => ''};              = {name => chr ($self->{next_char}),
1425                   value => '',
1426                   line => $self->{line}, column => $self->{column}};
1427          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1433  sub _get_next_token ($) {
1433          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1434              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1435            !!!cp (57);            !!!cp (57);
1436            !!!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});
1437            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1438          } else {          } else {
1439            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1462  sub _get_next_token ($) {
1462          $before_leave->();          $before_leave->();
1463          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1464            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1465            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1466          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1467            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1486  sub _get_next_token ($) {
1486          !!!next-input-character;          !!!next-input-character;
1487          redo A;          redo A;
1488        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1489            !!!cp (64);
1490          $before_leave->();          $before_leave->();
1491            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1492          !!!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  
1493          redo A;          redo A;
1494        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1495          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1496          $before_leave->();          $before_leave->();
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1547          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1548            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1549            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1550          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1551            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1568  sub _get_next_token ($) {
1568        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1569                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1570          !!!cp (76);          !!!cp (76);
1571          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1572                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1573                   value => '',
1574                   line => $self->{line}, column => $self->{column}};
1575          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1576          !!!next-input-character;          !!!next-input-character;
1577          redo A;          redo A;
1578        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1579            !!!cp (77);
1580            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1581          !!!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  
1582          redo A;          redo A;
1583        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1584          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1585          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1586            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1587            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1588          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1589            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1034  sub _get_next_token ($) { Line 1604  sub _get_next_token ($) {
1604    
1605          redo A;          redo A;
1606        } else {        } else {
1607          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1608          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1609                                value => ''};            !!!cp (78);
1610              !!!parse-error (type => 'bad attribute name');
1611            } else {
1612              !!!cp (82);
1613            }
1614            $self->{current_attribute}
1615                = {name => chr ($self->{next_char}),
1616                   value => '',
1617                   line => $self->{line}, column => $self->{column}};
1618          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1619          !!!next-input-character;          !!!next-input-character;
1620          redo A;                  redo A;        
# Line 1067  sub _get_next_token ($) { Line 1645  sub _get_next_token ($) {
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1648            !!!parse-error (type => 'empty unquoted attribute value');
1649          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1650            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1651            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1652          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1653            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1671  sub _get_next_token ($) {
1671          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1672          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1673            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1674            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1675          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1676            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1136  sub _get_next_token ($) { Line 1711  sub _get_next_token ($) {
1711        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1712          !!!cp (96);          !!!cp (96);
1713          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1714          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1715            ## "entity in attribute value state".  In this implementation, the
1716            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1717            ## implementation of the "consume a character reference" algorithm.
1718            #$self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1719            $self->{entity_in_attr} = 1;
1720            $self->{entity_additional} = 0x0022; # "
1721            $self->{state} = ENTITY_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
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 (97);            !!!cp (97);
           $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 1180  sub _get_next_token ($) { Line 1760  sub _get_next_token ($) {
1760        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1761          !!!cp (102);          !!!cp (102);
1762          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1763          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1764            ## "entity in attribute value state".  In this implementation, the
1765            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1766            ## implementation of the "consume a character reference" algorithm.
1767            #$self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1768            $self->{entity_in_attr} = 1;
1769            $self->{entity_additional} = 0x0027; # '
1770            $self->{state} = ENTITY_STATE;
1771          !!!next-input-character;          !!!next-input-character;
1772          redo A;          redo A;
1773        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1774          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1775          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1776            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1777            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1778          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1779            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1228  sub _get_next_token ($) { Line 1813  sub _get_next_token ($) {
1813        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1814          !!!cp (108);          !!!cp (108);
1815          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1816          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1817            ## "entity in attribute value state".  In this implementation, the
1818            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1819            ## implementation of the "consume a character reference" algorithm.
1820            #$self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1821            $self->{entity_in_attr} = 1;
1822            $self->{entity_additional} = -1;
1823            $self->{state} = ENTITY_STATE;
1824          !!!next-input-character;          !!!next-input-character;
1825          redo A;          redo A;
1826        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1827          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1828            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1829            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1830          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1831            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1849  sub _get_next_token ($) {
1849          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1850          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1851            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1852            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1853          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1854            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1297  sub _get_next_token ($) { Line 1885  sub _get_next_token ($) {
1885          redo A;          redo A;
1886        }        }
1887      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1888        my $token = $self->_tokenize_attempt_to_consume_an_entity        my $token = $self->{entity_return};
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
1889    
1890        unless (defined $token) {        unless (defined $token) {
1891          !!!cp (117);          !!!cp (117);
# Line 1331  sub _get_next_token ($) { Line 1913  sub _get_next_token ($) {
1913        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1914          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1915            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1917          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1918            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1933  sub _get_next_token ($) {
1933    
1934          redo A;          redo A;
1935        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1936            !!!cp (122);
1937            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1938          !!!next-input-character;          !!!next-input-character;
1939          if ($self->{next_char} == 0x003E and # >          redo A;
1940              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1941              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1942            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1943            !!!cp (122);            !!!cp (122.3);
1944            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1945            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1946              if ($self->{current_token}->{attributes}) {
1947                !!!cp (122.1);
1948                !!!parse-error (type => 'end tag attribute');
1949              } else {
1950                ## NOTE: This state should never be reached.
1951                !!!cp (122.2);
1952              }
1953          } else {          } else {
1954            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1955          }          }
1956          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1957          # next-input-character is already done          ## Reconsume.
1958            !!!emit ($self->{current_token}); # start tag or end tag
1959          redo A;          redo A;
1960        } else {        } else {
1961          !!!cp (124);          !!!cp ('124.1');
1962          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1963          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1964          ## reconsume          ## reconsume
1965          redo A;          redo A;
1966        }        }
1967      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1968        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1969                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1970        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1971              !!!parse-error (type => 'nestc', token => $self->{current_token});
1972        BC: {            ## TODO: Different type than slash in start tag
1973          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1974            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1975            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1976            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1977              } else {
1978            !!!emit ($token);              !!!cp ('124.5');
1979              }
1980              ## TODO: Test |<title></title/>|
1981            } else {
1982              !!!cp ('124.3');
1983              $self->{self_closing} = 1;
1984            }
1985    
1986            redo A;          $self->{state} = DATA_STATE;
1987          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1988    
1989            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1990    
1991            redo A;          redo A;
1992          } elsif ($self->{next_char} == -1) {
1993            !!!parse-error (type => 'unclosed tag');
1994            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1995              !!!cp (124.7);
1996              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1997            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1998              if ($self->{current_token}->{attributes}) {
1999                !!!cp (124.5);
2000                !!!parse-error (type => 'end tag attribute');
2001              } else {
2002                ## NOTE: This state should never be reached.
2003                !!!cp (124.6);
2004              }
2005          } else {          } else {
2006            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2007          }          }
2008        } # BC          $self->{state} = DATA_STATE;
2009            ## Reconsume.
2010            !!!emit ($self->{current_token}); # start tag or end tag
2011            redo A;
2012          } else {
2013            !!!cp ('124.4');
2014            !!!parse-error (type => 'nestc');
2015            ## TODO: This error type is wrong.
2016            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2017            ## Reconsume.
2018            redo A;
2019          }
2020        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2021          ## (only happen if PCDATA state)
2022    
2023          ## NOTE: Unlike spec's "bogus comment state", this implementation
2024          ## consumes characters one-by-one basis.
2025          
2026          if ($self->{next_char} == 0x003E) { # >
2027            !!!cp (124);
2028            $self->{state} = DATA_STATE;
2029            !!!next-input-character;
2030    
2031            !!!emit ($self->{current_token}); # comment
2032            redo A;
2033          } elsif ($self->{next_char} == -1) {
2034            !!!cp (125);
2035            $self->{state} = DATA_STATE;
2036            ## reconsume
2037    
2038        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # comment
2039            redo A;
2040          } else {
2041            !!!cp (126);
2042            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2043            ## Stay in the state.
2044            !!!next-input-character;
2045            redo A;
2046          }
2047      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2048        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2049                
2050        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2051            !!!cp (133);
2052            $self->{state} = MD_HYPHEN_STATE;
2053          !!!next-input-character;          !!!next-input-character;
2054          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2055        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2056                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2057            ## ASCII case-insensitive.
2058            !!!cp (130);
2059            $self->{state} = MD_DOCTYPE_STATE;
2060            $self->{state_keyword} = chr $self->{next_char};
2061          !!!next-input-character;          !!!next-input-character;
2062          push @next_char, $self->{next_char};          redo A;
2063          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2064              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2065            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2066            push @next_char, $self->{next_char};          !!!cp (135.4);                
2067            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2068                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2069              !!!next-input-character;          !!!next-input-character;
2070              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2071        } else {        } else {
2072          !!!cp (136);          !!!cp (136);
2073        }        }
2074    
2075        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2076        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2077        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2078          ## Reconsume.
2079        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2080          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2081                                    line => $self->{line_prev},
2082                                    column => $self->{column_prev} - 1,
2083                                   };
2084        redo A;        redo A;
2085              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2086        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2087        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2088            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2089                                      line => $self->{line_prev},
2090                                      column => $self->{column_prev} - 2,
2091                                     };
2092            $self->{state} = COMMENT_START_STATE;
2093            !!!next-input-character;
2094            redo A;
2095          } else {
2096            !!!cp (128);
2097            !!!parse-error (type => 'bogus comment',
2098                            line => $self->{line_prev},
2099                            column => $self->{column_prev} - 2);
2100            $self->{state} = BOGUS_COMMENT_STATE;
2101            ## Reconsume.
2102            $self->{current_token} = {type => COMMENT_TOKEN,
2103                                      data => '-',
2104                                      line => $self->{line_prev},
2105                                      column => $self->{column_prev} - 2,
2106                                     };
2107            redo A;
2108          }
2109        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2110          ## ASCII case-insensitive.
2111          if ($self->{next_char} == [
2112                undef,
2113                0x004F, # O
2114                0x0043, # C
2115                0x0054, # T
2116                0x0059, # Y
2117                0x0050, # P
2118              ]->[length $self->{state_keyword}] or
2119              $self->{next_char} == [
2120                undef,
2121                0x006F, # o
2122                0x0063, # c
2123                0x0074, # t
2124                0x0079, # y
2125                0x0070, # p
2126              ]->[length $self->{state_keyword}]) {
2127            !!!cp (131);
2128            ## Stay in the state.
2129            $self->{state_keyword} .= chr $self->{next_char};
2130            !!!next-input-character;
2131            redo A;
2132          } elsif ((length $self->{state_keyword}) == 6 and
2133                   ($self->{next_char} == 0x0045 or # E
2134                    $self->{next_char} == 0x0065)) { # e
2135            !!!cp (129);
2136            $self->{state} = DOCTYPE_STATE;
2137            $self->{current_token} = {type => DOCTYPE_TOKEN,
2138                                      quirks => 1,
2139                                      line => $self->{line_prev},
2140                                      column => $self->{column_prev} - 7,
2141                                     };
2142            !!!next-input-character;
2143            redo A;
2144          } else {
2145            !!!cp (132);        
2146            !!!parse-error (type => 'bogus comment',
2147                            line => $self->{line_prev},
2148                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2149            $self->{state} = BOGUS_COMMENT_STATE;
2150            ## Reconsume.
2151            $self->{current_token} = {type => COMMENT_TOKEN,
2152                                      data => $self->{state_keyword},
2153                                      line => $self->{line_prev},
2154                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2155                                     };
2156            redo A;
2157          }
2158        } elsif ($self->{state} == MD_CDATA_STATE) {
2159          if ($self->{next_char} == {
2160                '[' => 0x0043, # C
2161                '[C' => 0x0044, # D
2162                '[CD' => 0x0041, # A
2163                '[CDA' => 0x0054, # T
2164                '[CDAT' => 0x0041, # A
2165              }->{$self->{state_keyword}}) {
2166            !!!cp (135.1);
2167            ## Stay in the state.
2168            $self->{state_keyword} .= chr $self->{next_char};
2169            !!!next-input-character;
2170            redo A;
2171          } elsif ($self->{state_keyword} eq '[CDATA' and
2172                   $self->{next_char} == 0x005B) { # [
2173            !!!cp (135.2);
2174            $self->{current_token} = {type => CHARACTER_TOKEN,
2175                                      data => '',
2176                                      line => $self->{line_prev},
2177                                      column => $self->{column_prev} - 7};
2178            $self->{state} = CDATA_SECTION_STATE;
2179            !!!next-input-character;
2180            redo A;
2181          } else {
2182            !!!cp (135.3);
2183            !!!parse-error (type => 'bogus comment',
2184                            line => $self->{line_prev},
2185                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2186            $self->{state} = BOGUS_COMMENT_STATE;
2187            ## Reconsume.
2188            $self->{current_token} = {type => COMMENT_TOKEN,
2189                                      data => $self->{state_keyword},
2190                                      line => $self->{line_prev},
2191                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2192                                     };
2193            redo A;
2194          }
2195      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2196        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2197          !!!cp (137);          !!!cp (137);
# Line 1603  sub _get_next_token ($) { Line 2311  sub _get_next_token ($) {
2311          redo A;          redo A;
2312        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2313          !!!cp (152);          !!!cp (152);
2314          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2315                            line => $self->{line_prev},
2316                            column => $self->{column_prev});
2317          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2318          ## Stay in the state          ## Stay in the state
2319          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2329  sub _get_next_token ($) {
2329          redo A;          redo A;
2330        } else {        } else {
2331          !!!cp (154);          !!!cp (154);
2332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2333                            line => $self->{line_prev},
2334                            column => $self->{column_prev});
2335          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2336          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2337          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2370  sub _get_next_token ($) {
2370          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2371          !!!next-input-character;          !!!next-input-character;
2372    
2373          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2374    
2375          redo A;          redo A;
2376        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2379  sub _get_next_token ($) {
2379          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2380          ## reconsume          ## reconsume
2381    
2382          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2383    
2384          redo A;          redo A;
2385        } else {        } else {
2386          !!!cp (160);          !!!cp (160);
2387          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2388              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2389  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2390          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2391          !!!next-input-character;          !!!next-input-character;
# Line 1749  sub _get_next_token ($) { Line 2458  sub _get_next_token ($) {
2458          redo A;          redo A;
2459        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2460                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2461            $self->{state} = PUBLIC_STATE;
2462            $self->{state_keyword} = chr $self->{next_char};
2463          !!!next-input-character;          !!!next-input-character;
2464          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
2465        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2466                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2467            $self->{state} = SYSTEM_STATE;
2468            $self->{state_keyword} = chr $self->{next_char};
2469          !!!next-input-character;          !!!next-input-character;
2470          if ($self->{next_char} == 0x0059 or # Y          redo A;
             $self->{next_char} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0053 or # S  
               $self->{next_char} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0045 or # E  
                   $self->{next_char} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x004D or # M  
                     $self->{next_char} == 0x006D) { # m  
                   !!!cp (174);  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (175);  
                 }  
               } else {  
                 !!!cp (176);  
               }  
             } else {  
               !!!cp (177);  
             }  
           } else {  
             !!!cp (178);  
           }  
         } else {  
           !!!cp (179);  
         }  
   
         #  
2471        } else {        } else {
2472          !!!cp (180);          !!!cp (180);
2473            !!!parse-error (type => 'string after DOCTYPE name');
2474            $self->{current_token}->{quirks} = 1;
2475    
2476            $self->{state} = BOGUS_DOCTYPE_STATE;
2477          !!!next-input-character;          !!!next-input-character;
2478          #          redo A;
2479        }        }
2480        } elsif ($self->{state} == PUBLIC_STATE) {
2481          ## ASCII case-insensitive
2482          if ($self->{next_char} == [
2483                undef,
2484                0x0055, # U
2485                0x0042, # B
2486                0x004C, # L
2487                0x0049, # I
2488              ]->[length $self->{state_keyword}] or
2489              $self->{next_char} == [
2490                undef,
2491                0x0075, # u
2492                0x0062, # b
2493                0x006C, # l
2494                0x0069, # i
2495              ]->[length $self->{state_keyword}]) {
2496            !!!cp (175);
2497            ## Stay in the state.
2498            $self->{state_keyword} .= chr $self->{next_char};
2499            !!!next-input-character;
2500            redo A;
2501          } elsif ((length $self->{state_keyword}) == 5 and
2502                   ($self->{next_char} == 0x0043 or # C
2503                    $self->{next_char} == 0x0063)) { # c
2504            !!!cp (168);
2505            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2506            !!!next-input-character;
2507            redo A;
2508          } else {
2509            !!!cp (169);
2510            !!!parse-error (type => 'string after DOCTYPE name',
2511                            line => $self->{line_prev},
2512                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2513            $self->{current_token}->{quirks} = 1;
2514    
2515        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2516        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2517            redo A;
2518          }
2519        } elsif ($self->{state} == SYSTEM_STATE) {
2520          ## ASCII case-insensitive
2521          if ($self->{next_char} == [
2522                undef,
2523                0x0059, # Y
2524                0x0053, # S
2525                0x0054, # T
2526                0x0045, # E
2527              ]->[length $self->{state_keyword}] or
2528              $self->{next_char} == [
2529                undef,
2530                0x0079, # y
2531                0x0073, # s
2532                0x0074, # t
2533                0x0065, # e
2534              ]->[length $self->{state_keyword}]) {
2535            !!!cp (170);
2536            ## Stay in the state.
2537            $self->{state_keyword} .= chr $self->{next_char};
2538            !!!next-input-character;
2539            redo A;
2540          } elsif ((length $self->{state_keyword}) == 5 and
2541                   ($self->{next_char} == 0x004D or # M
2542                    $self->{next_char} == 0x006D)) { # m
2543            !!!cp (171);
2544            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2545            !!!next-input-character;
2546            redo A;
2547          } else {
2548            !!!cp (172);
2549            !!!parse-error (type => 'string after DOCTYPE name',
2550                            line => $self->{line_prev},
2551                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2552            $self->{current_token}->{quirks} = 1;
2553    
2554        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2555        # next-input-character is already done          ## Reconsume.
2556        redo A;          redo A;
2557          }
2558      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2559        if ({        if ({
2560              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2067  sub _get_next_token ($) { Line 2787  sub _get_next_token ($) {
2787          redo A;          redo A;
2788        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2789          !!!cp (208);          !!!cp (208);
2790          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2791    
2792          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2793          !!!next-input-character;          !!!next-input-character;
# Line 2103  sub _get_next_token ($) { Line 2823  sub _get_next_token ($) {
2823          redo A;          redo A;
2824        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2825          !!!cp (212);          !!!cp (212);
2826          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2827    
2828          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2829          !!!next-input-character;          !!!next-input-character;
# Line 2151  sub _get_next_token ($) { Line 2871  sub _get_next_token ($) {
2871        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2872          !!!cp (217);          !!!cp (217);
2873          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2874          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2875          ## reconsume          ## reconsume
2876    
# Line 2192  sub _get_next_token ($) { Line 2911  sub _get_next_token ($) {
2911          !!!next-input-character;          !!!next-input-character;
2912          redo A;          redo A;
2913        }        }
2914      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2915        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2916      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2917    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2918          
2919          if ($self->{next_char} == 0x005D) { # ]
2920            !!!cp (221.1);
2921            $self->{state} = CDATA_SECTION_MSE1_STATE;
2922            !!!next-input-character;
2923            redo A;
2924          } elsif ($self->{next_char} == -1) {
2925            $self->{state} = DATA_STATE;
2926            !!!next-input-character;
2927            if (length $self->{current_token}->{data}) { # character
2928              !!!cp (221.2);
2929              !!!emit ($self->{current_token}); # character
2930            } else {
2931              !!!cp (221.3);
2932              ## No token to emit. $self->{current_token} is discarded.
2933            }        
2934            redo A;
2935          } else {
2936            !!!cp (221.4);
2937            $self->{current_token}->{data} .= chr $self->{next_char};
2938            ## Stay in the state.
2939            !!!next-input-character;
2940            redo A;
2941          }
2942    
2943    die "$0: _get_next_token: unexpected case";        ## ISSUE: "text tokens" in spec.
2944  } # _get_next_token      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2945          if ($self->{next_char} == 0x005D) { # ]
2946            !!!cp (221.5);
2947            $self->{state} = CDATA_SECTION_MSE2_STATE;
2948            !!!next-input-character;
2949            redo A;
2950          } else {
2951            !!!cp (221.6);
2952            $self->{current_token}->{data} .= ']';
2953            $self->{state} = CDATA_SECTION_STATE;
2954            ## Reconsume.
2955            redo A;
2956          }
2957        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2958          if ($self->{next_char} == 0x003E) { # >
2959            $self->{state} = DATA_STATE;
2960            !!!next-input-character;
2961            if (length $self->{current_token}->{data}) { # character
2962              !!!cp (221.7);
2963              !!!emit ($self->{current_token}); # character
2964            } else {
2965              !!!cp (221.8);
2966              ## No token to emit. $self->{current_token} is discarded.
2967            }
2968            redo A;
2969          } elsif ($self->{next_char} == 0x005D) { # ]
2970            !!!cp (221.9); # character
2971            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2972            ## Stay in the state.
2973            !!!next-input-character;
2974            redo A;
2975          } else {
2976            !!!cp (221.11);
2977            $self->{current_token}->{data} .= ']]'; # character
2978            $self->{state} = CDATA_SECTION_STATE;
2979            ## Reconsume.
2980            redo A;
2981          }
2982    
2983        } elsif ($self->{state} == ENTITY_STATE) {
2984          my $in_attr = $self->{entity_in_attr};
2985          my $additional = $self->{entity_additional};
2986    
2987  sub _tokenize_attempt_to_consume_an_entity ($$$) {    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
   my ($self, $in_attr, $additional) = @_;  
2988    
2989    if ({    if ({
2990         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 2211  sub _tokenize_attempt_to_consume_an_enti Line 2994  sub _tokenize_attempt_to_consume_an_enti
2994      !!!cp (1001);      !!!cp (1001);
2995      ## Don't consume      ## Don't consume
2996      ## No error      ## No error
2997      return undef;      $self->{entity_return} = undef;
2998        $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
2999        redo A;
3000    } elsif ($self->{next_char} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
3001      !!!next-input-character;      !!!next-input-character;
3002      if ($self->{next_char} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 3028  sub _tokenize_attempt_to_consume_an_enti
3028            redo X;            redo X;
3029          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
3030            !!!cp (1005);            !!!cp (1005);
3031            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
3032            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
3033            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
3034            return undef;            $self->{entity_return} = undef;
3035              $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3036              redo A;
3037          } elsif ($self->{next_char} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
3038            !!!cp (1006);            !!!cp (1006);
3039            !!!next-input-character;            !!!next-input-character;
3040          } else {          } else {
3041            !!!cp (1007);            !!!cp (1007);
3042            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
3043          }          }
3044    
3045          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3046            !!!cp (1008);            !!!cp (1008);
3047            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => 'invalid character reference',
3048                              text => (sprintf 'U+%04X', $code),
3049                              line => $l, column => $c);
3050            $code = 0xFFFD;            $code = 0xFFFD;
3051          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
3052            !!!cp (1009);            !!!cp (1009);
3053            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => 'invalid character reference',
3054                              text => (sprintf 'U-%08X', $code),
3055                              line => $l, column => $c);
3056            $code = 0xFFFD;            $code = 0xFFFD;
3057          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
3058            !!!cp (1010);            !!!cp (1010);
3059            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3060            $code = 0x000A;            $code = 0x000A;
3061          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
3062            !!!cp (1011);            !!!cp (1011);
3063            !!!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);
3064            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
3065          }          }
3066    
3067          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{entity_return} = {type => CHARACTER_TOKEN, data => chr $code,
3068                  has_reference => 1};                  has_reference => 1,
3069                    line => $l, column => $c,
3070                   };
3071            $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3072            redo A;
3073        } # X        } # X
3074      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
3075               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 3090  sub _tokenize_attempt_to_consume_an_enti
3090          !!!next-input-character;          !!!next-input-character;
3091        } else {        } else {
3092          !!!cp (1014);          !!!cp (1014);
3093          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
3094        }        }
3095    
3096        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3097          !!!cp (1015);          !!!cp (1015);
3098          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3099                            text => (sprintf 'U+%04X', $code),
3100                            line => $l, column => $c);
3101          $code = 0xFFFD;          $code = 0xFFFD;
3102        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3103          !!!cp (1016);          !!!cp (1016);
3104          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3105                            text => (sprintf 'U-%08X', $code),
3106                            line => $l, column => $c);
3107          $code = 0xFFFD;          $code = 0xFFFD;
3108        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3109          !!!cp (1017);          !!!cp (1017);
3110          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3111                            line => $l, column => $c);
3112          $code = 0x000A;          $code = 0x000A;
3113        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3114          !!!cp (1018);          !!!cp (1018);
3115          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3116                            text => (sprintf 'U+%04X', $code),
3117                            line => $l, column => $c);
3118          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3119        }        }
3120                
3121        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        $self->{entity_return} = {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3122                  line => $l, column => $c,
3123                 };
3124          $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3125          redo A;
3126      } else {      } else {
3127        !!!cp (1019);        !!!cp (1019);
3128        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3129        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
3130        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
3131        return undef;        $self->{entity_return} = undef;
3132          $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3133          redo A;
3134      }      }
3135    } elsif ((0x0041 <= $self->{next_char} and    } elsif ((0x0041 <= $self->{next_char} and
3136              $self->{next_char} <= 0x005A) or              $self->{next_char} <= 0x005A) or
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 3144  sub _tokenize_attempt_to_consume_an_enti
3144      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3145      our $EntityChar;      our $EntityChar;
3146    
3147      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3148             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3149             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
3150               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 3177  sub _tokenize_attempt_to_consume_an_enti
3177            
3178      if ($match > 0) {      if ($match > 0) {
3179        !!!cp (1023);        !!!cp (1023);
3180        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        $self->{entity_return} = {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3181                  line => $l, column => $c,
3182                 };
3183          $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3184          redo A;
3185      } elsif ($match < 0) {      } elsif ($match < 0) {
3186        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3187        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3188          !!!cp (1024);          !!!cp (1024);
3189          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $self->{entity_return} = {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3190                    line => $l, column => $c,
3191                   };
3192            $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3193            redo A;
3194        } else {        } else {
3195          !!!cp (1025);          !!!cp (1025);
3196          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          $self->{entity_return} = {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3197                    line => $l, column => $c,
3198                   };
3199            $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3200            redo A;
3201        }        }
3202      } else {      } else {
3203        !!!cp (1026);        !!!cp (1026);
3204        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3205        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3206        return {type => CHARACTER_TOKEN, data => '&'.$value};        $self->{entity_return} = {type => CHARACTER_TOKEN, data => '&'.$value,
3207                  line => $l, column => $c,
3208                 };
3209          $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3210          redo A;
3211      }      }
3212    } else {    } else {
3213      !!!cp (1027);      !!!cp (1027);
3214      ## no characters are consumed      ## no characters are consumed
3215      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3216      return undef;      $self->{entity_return} = undef;
3217        $self->{state} = $self->{entity_in_attr} ? ENTITY_IN_ATTRIBUTE_VALUE_STATE : ENTITY_DATA_STATE;
3218        redo A;
3219    }    }
3220  } # _tokenize_attempt_to_consume_an_entity  
3221        } else {
3222          die "$0: $self->{state}: Unknown state";
3223        }
3224      } # A  
3225    
3226      die "$0: _get_next_token: unexpected case";
3227    } # _get_next_token
3228    
3229  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3230    my $self = shift;    my $self = shift;
# Line 2400  sub _initialize_tree_constructor ($) { Line 3233  sub _initialize_tree_constructor ($) {
3233    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3234    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3235    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3236      $self->{document}->set_user_data (manakai_source_line => 1);
3237      $self->{document}->set_user_data (manakai_source_column => 1);
3238  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3239    
3240  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2454  sub _tree_construction_initial ($) { Line 3289  sub _tree_construction_initial ($) {
3289        ## language.        ## language.
3290        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3291        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3292        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3293        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3294            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3295          !!!cp ('t1');          !!!cp ('t1');
3296          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3297        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3298          !!!cp ('t2');          !!!cp ('t2');
3299          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3300          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3301            if ($token->{public_identifier} eq 'XSLT-compat') {
3302              !!!cp ('t1.2');
3303              !!!parse-error (type => 'XSLT-compat', token => $token,
3304                              level => $self->{level}->{should});
3305            } else {
3306              !!!parse-error (type => 'not HTML5', token => $token);
3307            }
3308        } else {        } else {
3309          !!!cp ('t3');          !!!cp ('t3');
3310            #
3311        }        }
3312                
3313        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3314          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3315          ## NOTE: Default value for both |public_id| and |system_id| attributes
3316          ## are empty strings, so that we don't set any value in missing cases.
3317        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3318            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3319        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3328  sub _tree_construction_initial ($) {
3328        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3329          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3330          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3331          if ({          my $prefix = [
3332            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3333            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3334            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3335            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3336            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3337            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3338            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3339            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3340            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3341            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3342            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3343            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3344            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3345            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3346            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3347            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3348            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3349            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3350            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3351            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3352            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3353            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3354            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3355            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3356            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3357            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3358            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3359            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3360            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3361            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3362            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3363            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3364            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3365            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3366            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3367            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3368            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3369            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3370            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3371            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3372            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3373            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3374            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3375            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3376            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3377            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3378            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3379            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3380            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3381            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3382            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3383            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3384            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3385            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3386            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3387            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3388            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3389            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3390            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3391            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3392            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3393            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3394            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3395            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3396            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3397            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3398            "-//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}) {  
3399            !!!cp ('t5');            !!!cp ('t5');
3400            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3401          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3402                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3403            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3404              !!!cp ('t6');              !!!cp ('t6');
3405              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3407  sub _tree_construction_initial ($) {
3407              !!!cp ('t7');              !!!cp ('t7');
3408              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3409            }            }
3410          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3411                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3412            !!!cp ('t8');            !!!cp ('t8');
3413            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3414          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3421  sub _tree_construction_initial ($) {
3421          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3422          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3423          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") {
3424            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3425              ## marked as quirks.
3426            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3427            !!!cp ('t11');            !!!cp ('t11');
3428          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3441  sub _tree_construction_initial ($) {
3441                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3442               }->{$token->{type}}) {               }->{$token->{type}}) {
3443        !!!cp ('t14');        !!!cp ('t14');
3444        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3445        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3446        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3447        ## reprocess        ## reprocess
3448          !!!ack-later;
3449        return;        return;
3450      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3451        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3463  sub _tree_construction_initial ($) {
3463          !!!cp ('t17');          !!!cp ('t17');
3464        }        }
3465    
3466        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3467        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3468        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3469        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3492  sub _tree_construction_root_element ($)
3492    B: {    B: {
3493        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3494          !!!cp ('t19');          !!!cp ('t19');
3495          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3496          ## Ignore the token          ## Ignore the token
3497          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3498          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3526  sub _tree_construction_root_element ($)
3526        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3527          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3528            my $root_element;            my $root_element;
3529            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3530            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3531            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3532                  [$root_element, $el_category->{html}];
3533    
3534            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3535              !!!cp ('t24');              !!!cp ('t24');
3536              $self->{application_cache_selection}              $self->{application_cache_selection}
3537                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3538              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3539                ## According to Hixie (#whatwg 2008-03-19), it should be
3540                ## resolved against the base URI of the document in HTML
3541                ## or xml:base of the element in XHTML.
3542            } else {            } else {
3543              !!!cp ('t25');              !!!cp ('t25');
3544              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3545            }            }
3546    
3547              !!!nack ('t25c');
3548    
3549            !!!next-token;            !!!next-token;
3550            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3551          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3562  sub _tree_construction_root_element ($)
3562          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3563        }        }
3564    
3565      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3566        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3567      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3568      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3569    
3570      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3571    
3572      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3573        !!!ack-later;
3574      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3575    
3576      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3591  sub _reset_insertion_mode ($) {
3591            
3592      ## Step 3      ## Step 3
3593      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"!?  
3594        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3595          $last = 1;          $last = 1;
3596          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3597            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3598                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3599              !!!cp ('t27');          } else {
3600              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3601          }          }
3602        }        }
3603              
3604        ## Step 4..13        ## Step 4..14
3605        my $new_mode = {        my $new_mode;
3606          if ($node->[1] & FOREIGN_EL) {
3607            !!!cp ('t28.1');
3608            ## NOTE: Strictly spaking, the line below only applies to MathML and
3609            ## SVG elements.  Currently the HTML syntax supports only MathML and
3610            ## SVG elements as foreigners.
3611            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3612          } elsif ($node->[1] & TABLE_CELL_EL) {
3613            if ($last) {
3614              !!!cp ('t28.2');
3615              #
3616            } else {
3617              !!!cp ('t28.3');
3618              $new_mode = IN_CELL_IM;
3619            }
3620          } else {
3621            !!!cp ('t28.4');
3622            $new_mode = {
3623                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3624                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3625                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3626                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3627                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3628                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3633  sub _reset_insertion_mode ($) {
3633                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3634                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3635                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3636                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3637          }
3638        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3639                
3640        ## Step 14        ## Step 15
3641        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3642          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3643            !!!cp ('t29');            !!!cp ('t29');
3644            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3652  sub _reset_insertion_mode ($) {
3652          !!!cp ('t31');          !!!cp ('t31');
3653        }        }
3654                
3655        ## Step 15        ## Step 16
3656        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3657                
3658        ## Step 16        ## Step 17
3659        $i--;        $i--;
3660        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3661                
3662        ## Step 17        ## Step 18
3663        redo S3;        redo S3;
3664      } # S3      } # S3
3665    
# Line 2908  sub _tree_construction_main ($) { Line 3763  sub _tree_construction_main ($) {
3763      !!!cp ('t39');      !!!cp ('t39');
3764    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3765    
3766    my $parse_rcdata = sub ($$) {    my $insert;
3767      my ($content_model_flag, $insert) = @_;  
3768      my $parse_rcdata = sub ($) {
3769        my ($content_model_flag) = @_;
3770    
3771      ## Step 1      ## Step 1
3772      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3773      my $el;      my $el;
3774      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3775    
3776      ## Step 2      ## Step 2
3777      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3778    
3779      ## Step 3      ## Step 3
3780      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3782  sub _tree_construction_main ($) {
3782    
3783      ## Step 4      ## Step 4
3784      my $text = '';      my $text = '';
3785        !!!nack ('t40.1');
3786      !!!next-token;      !!!next-token;
3787      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3788        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3805  sub _tree_construction_main ($) {
3805          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3806        !!!cp ('t42');        !!!cp ('t42');
3807        ## 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});  
3808      } else {      } else {
3809        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3810          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3811            !!!cp ('t43');
3812            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3813          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3814            !!!cp ('t44');
3815            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3816          } else {
3817            die "$0: $content_model_flag in parse_rcdata";
3818          }
3819      }      }
3820      !!!next-token;      !!!next-token;
3821    }; # $parse_rcdata    }; # $parse_rcdata
3822    
3823    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3824      my $script_el;      my $script_el;
3825      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3826      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3827    
3828      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3829      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3830            
3831      my $text = '';      my $text = '';
3832        !!!nack ('t45.1');
3833      !!!next-token;      !!!next-token;
3834      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3835        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3849  sub _tree_construction_main ($) {
3849        ## Ignore the token        ## Ignore the token
3850      } else {      } else {
3851        !!!cp ('t48');        !!!cp ('t48');
3852        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3853        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3854        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3855      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3872  sub _tree_construction_main ($) {
3872      !!!next-token;      !!!next-token;
3873    }; # $script_start_tag    }; # $script_start_tag
3874    
3875      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3876      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3877      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3878    
3879    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3880      my $tag_name = shift;      my $end_tag_token = shift;
3881        my $tag_name = $end_tag_token->{tag_name};
3882    
3883        ## NOTE: The adoption agency algorithm (AAA).
3884    
3885      FET: {      FET: {
3886        ## Step 1        ## Step 1
3887        my $formatting_element;        my $formatting_element;
3888        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3889        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3890          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3891              !!!cp ('t52');
3892              last AFE;
3893            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3894                         eq $tag_name) {
3895            !!!cp ('t51');            !!!cp ('t51');
3896            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3897            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3898            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3899          }          }
3900        } # AFE        } # AFE
3901        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3902          !!!cp ('t53');          !!!cp ('t53');
3903          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3904          ## Ignore the token          ## Ignore the token
3905          !!!next-token;          !!!next-token;
3906          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3917  sub _tree_construction_main ($) {
3917              last INSCOPE;              last INSCOPE;
3918            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3919              !!!cp ('t55');              !!!cp ('t55');
3920              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
3921                                text => $token->{tag_name},
3922                                token => $end_tag_token);
3923              ## Ignore the token              ## Ignore the token
3924              !!!next-token;              !!!next-token;
3925              return;              return;
3926            }            }
3927          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3928            !!!cp ('t56');            !!!cp ('t56');
3929            $in_scope = 0;            $in_scope = 0;
3930          }          }
3931        } # INSCOPE        } # INSCOPE
3932        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3933          !!!cp ('t57');          !!!cp ('t57');
3934          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
3935                            text => $token->{tag_name},
3936                            token => $end_tag_token);
3937          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3938          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3939          return;          return;
3940        }        }
3941        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3942          !!!cp ('t58');          !!!cp ('t58');
3943          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3944                            text => $self->{open_elements}->[-1]->[0]
3945                                ->manakai_local_name,
3946                            token => $end_tag_token);
3947        }        }
3948                
3949        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3951  sub _tree_construction_main ($) {
3951        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3952        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3953          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3954          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3955              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3956              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3957               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3958            !!!cp ('t59');            !!!cp ('t59');
3959            $furthest_block = $node;            $furthest_block = $node;
3960            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 4040  sub _tree_construction_main ($) {
4040        } # S7          } # S7  
4041                
4042        ## Step 8        ## Step 8
4043        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4044            my $foster_parent_element;
4045            my $next_sibling;
4046            OE: for (reverse 0..$#{$self->{open_elements}}) {
4047              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4048                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4049                                 if (defined $parent and $parent->node_type == 1) {
4050                                   !!!cp ('t65.1');
4051                                   $foster_parent_element = $parent;
4052                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4053                                 } else {
4054                                   !!!cp ('t65.2');
4055                                   $foster_parent_element
4056                                     = $self->{open_elements}->[$_ - 1]->[0];
4057                                 }
4058                                 last OE;
4059                               }
4060                             } # OE
4061                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4062                               unless defined $foster_parent_element;
4063            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4064            $open_tables->[-1]->[1] = 1; # tainted
4065          } else {
4066            !!!cp ('t65.3');
4067            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4068          }
4069                
4070        ## Step 9        ## Step 9
4071        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 4111  sub _tree_construction_main ($) {
4111      } # FET      } # FET
4112    }; # $formatting_end_tag    }; # $formatting_end_tag
4113    
4114    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4115      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4116    }; # $insert_to_current    }; # $insert_to_current
4117    
4118    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4119                         my $child = shift;      my $child = shift;
4120                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4121                              table => 1, tbody => 1, tfoot => 1,        # MUST
4122                              thead => 1, tr => 1,        my $foster_parent_element;
4123                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4124                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4125                           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') {  
4126                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4127                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4128                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 4140  sub _tree_construction_main ($) {
4140                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4141                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4142                             ($child, $next_sibling);                             ($child, $next_sibling);
4143                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4144                           !!!cp ('t72');      } else {
4145                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4146                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4147        }
4148    }; # $insert_to_foster    }; # $insert_to_foster
4149    
4150    my $insert;    B: while (1) {
   
   B: {  
4151      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4152        !!!cp ('t73');        !!!cp ('t73');
4153        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4154        ## Ignore the token        ## Ignore the token
4155        ## Stay in the phase        ## Stay in the phase
4156        !!!next-token;        !!!next-token;
4157        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  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         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;  
4158      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4159               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4160        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4161          !!!cp ('t79');          !!!cp ('t79');
4162          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4163          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4164        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4165          !!!cp ('t80');          !!!cp ('t80');
4166          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4167          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4168        } else {        } else {
4169          !!!cp ('t81');          !!!cp ('t81');
4170        }        }
4171    
4172        !!!cp ('t82');        !!!cp ('t82');
4173        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4174        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4175        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4176          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 4180  sub _tree_construction_main ($) {
4180               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4181          }          }
4182        }        }
4183          !!!nack ('t84.1');
4184        !!!next-token;        !!!next-token;
4185        redo B;        next B;
4186      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4187        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4188        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 4196  sub _tree_construction_main ($) {
4196          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4197        }        }
4198        !!!next-token;        !!!next-token;
4199        redo B;        next B;
4200      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4201          if ($token->{type} == CHARACTER_TOKEN) {
4202            !!!cp ('t87.1');
4203            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4204            !!!next-token;
4205            next B;
4206          } elsif ($token->{type} == START_TAG_TOKEN) {
4207            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4208                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4209                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4210                ($token->{tag_name} eq 'svg' and
4211                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4212              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4213              !!!cp ('t87.2');
4214              #
4215            } elsif ({
4216                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4217                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4218                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4219                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4220                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4221                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4222                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4223                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4224                     }->{$token->{tag_name}}) {
4225              !!!cp ('t87.2');
4226              !!!parse-error (type => 'not closed',
4227                              text => $self->{open_elements}->[-1]->[0]
4228                                  ->manakai_local_name,
4229                              token => $token);
4230    
4231              pop @{$self->{open_elements}}
4232                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4233    
4234              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4235              ## Reprocess.
4236              next B;
4237            } else {
4238              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4239              my $tag_name = $token->{tag_name};
4240              if ($nsuri eq $SVG_NS) {
4241                $tag_name = {
4242                   altglyph => 'altGlyph',
4243                   altglyphdef => 'altGlyphDef',
4244                   altglyphitem => 'altGlyphItem',
4245                   animatecolor => 'animateColor',
4246                   animatemotion => 'animateMotion',
4247                   animatetransform => 'animateTransform',
4248                   clippath => 'clipPath',
4249                   feblend => 'feBlend',
4250                   fecolormatrix => 'feColorMatrix',
4251                   fecomponenttransfer => 'feComponentTransfer',
4252                   fecomposite => 'feComposite',
4253                   feconvolvematrix => 'feConvolveMatrix',
4254                   fediffuselighting => 'feDiffuseLighting',
4255                   fedisplacementmap => 'feDisplacementMap',
4256                   fedistantlight => 'feDistantLight',
4257                   feflood => 'feFlood',
4258                   fefunca => 'feFuncA',
4259                   fefuncb => 'feFuncB',
4260                   fefuncg => 'feFuncG',
4261                   fefuncr => 'feFuncR',
4262                   fegaussianblur => 'feGaussianBlur',
4263                   feimage => 'feImage',
4264                   femerge => 'feMerge',
4265                   femergenode => 'feMergeNode',
4266                   femorphology => 'feMorphology',
4267                   feoffset => 'feOffset',
4268                   fepointlight => 'fePointLight',
4269                   fespecularlighting => 'feSpecularLighting',
4270                   fespotlight => 'feSpotLight',
4271                   fetile => 'feTile',
4272                   feturbulence => 'feTurbulence',
4273                   foreignobject => 'foreignObject',
4274                   glyphref => 'glyphRef',
4275                   lineargradient => 'linearGradient',
4276                   radialgradient => 'radialGradient',
4277                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4278                   textpath => 'textPath',  
4279                }->{$tag_name} || $tag_name;
4280              }
4281    
4282              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4283    
4284              ## "adjust foreign attributes" - done in insert-element-f
4285    
4286              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4287    
4288              if ($self->{self_closing}) {
4289                pop @{$self->{open_elements}};
4290                !!!ack ('t87.3');
4291              } else {
4292                !!!cp ('t87.4');
4293              }
4294    
4295              !!!next-token;
4296              next B;
4297            }
4298          } elsif ($token->{type} == END_TAG_TOKEN) {
4299            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4300            !!!cp ('t87.5');
4301            #
4302          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4303            !!!cp ('t87.6');
4304            !!!parse-error (type => 'not closed',
4305                            text => $self->{open_elements}->[-1]->[0]
4306                                ->manakai_local_name,
4307                            token => $token);
4308    
4309            pop @{$self->{open_elements}}
4310                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4311    
4312            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4313            ## Reprocess.
4314            next B;
4315          } else {
4316            die "$0: $token->{type}: Unknown token type";        
4317          }
4318        }
4319    
4320        if ($self->{insertion_mode} & HEAD_IMS) {
4321        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4322          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4323            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4324                !!!cp ('t88.2');
4325                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4326              } else {
4327                !!!cp ('t88.1');
4328                ## Ignore the token.
4329                !!!next-token;
4330                next B;
4331              }
4332            unless (length $token->{data}) {            unless (length $token->{data}) {
4333              !!!cp ('t88');              !!!cp ('t88');
4334              !!!next-token;              !!!next-token;
4335              redo B;              next B;
4336            }            }
4337          }          }
4338    
4339          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4340            !!!cp ('t89');            !!!cp ('t89');
4341            ## As if <head>            ## As if <head>
4342            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4343            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4344            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4345                  [$self->{head_element}, $el_category->{head}];
4346    
4347            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4348            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352            !!!cp ('t90');            !!!cp ('t90');
4353            ## As if </noscript>            ## As if </noscript>
4354            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4355            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4356                        
4357            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4358            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 4368  sub _tree_construction_main ($) {
4368            !!!cp ('t92');            !!!cp ('t92');
4369          }          }
4370    
4371              ## "after head" insertion mode          ## "after head" insertion mode
4372              ## As if <body>          ## As if <body>
4373              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4374              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4375              ## reprocess          ## reprocess
4376              redo B;          next B;
4377            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4378              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4379                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4380                  !!!cp ('t93');              !!!cp ('t93');
4381                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4382                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4383                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4384                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4385                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4386                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4387                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4388                  !!!cp ('t94');              !!!next-token;
4389                  #              next B;
4390                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4391                  !!!cp ('t95');              !!!cp ('t93.2');
4392                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4393                  ## Ignore the token                              token => $token);
4394                  !!!next-token;              ## Ignore the token
4395                  redo B;              !!!nack ('t93.3');
4396                }              !!!next-token;
4397              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4398                !!!cp ('t96');            } else {
4399                ## As if <head>              !!!cp ('t95');
4400                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4401                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4402                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4403                !!!nack ('t95.1');
4404                !!!next-token;
4405                next B;
4406              }
4407            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4408              !!!cp ('t96');
4409              ## As if <head>
4410              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4411              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4412              push @{$self->{open_elements}},
4413                  [$self->{head_element}, $el_category->{head}];
4414    
4415                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4416                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4417              } else {          } else {
4418                !!!cp ('t97');            !!!cp ('t97');
4419              }          }
4420    
4421              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4422                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4423                  !!!cp ('t98');                  !!!cp ('t98');
4424                  ## As if </noscript>                  ## As if </noscript>
4425                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4426                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4427                                    token => $token);
4428                                
4429                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4430                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 4435  sub _tree_construction_main ($) {
4435                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4436                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4437                  !!!cp ('t100');                  !!!cp ('t100');
4438                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4439                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4440                    push @{$self->{open_elements}},
4441                        [$self->{head_element}, $el_category->{head}];
4442                } else {                } else {
4443                  !!!cp ('t101');                  !!!cp ('t101');
4444                }                }
4445                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4446                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4447                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4448                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4449                  !!!nack ('t101.1');
4450                !!!next-token;                !!!next-token;
4451                redo B;                next B;
4452              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4453                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4454                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4455                  !!!cp ('t102');                  !!!cp ('t102');
4456                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4457                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4458                    push @{$self->{open_elements}},
4459                        [$self->{head_element}, $el_category->{head}];
4460                } else {                } else {
4461                  !!!cp ('t103');                  !!!cp ('t103');
4462                }                }
4463                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4464                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4465                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4466                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4467                  !!!ack ('t103.1');
4468                !!!next-token;                !!!next-token;
4469                redo B;                next B;
4470              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4471                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4472                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4473                  !!!cp ('t104');                  !!!cp ('t104');
4474                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4475                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4476                    push @{$self->{open_elements}},
4477                        [$self->{head_element}, $el_category->{head}];
4478                } else {                } else {
4479                  !!!cp ('t105');                  !!!cp ('t105');
4480                }                }
4481                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4482                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.
4483    
4484                unless ($self->{confident}) {                unless ($self->{confident}) {
4485                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4486                    !!!cp ('t106');                    !!!cp ('t106');
4487                      ## NOTE: Whether the encoding is supported or not is handled
4488                      ## in the {change_encoding} callback.
4489                    $self->{change_encoding}                    $self->{change_encoding}
4490                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4491                             $token);
4492                                        
4493                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4494                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4495                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4496                                                 ->{has_reference});                                                 ->{has_reference});
4497                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4498                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4499                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4500                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4501                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4502                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4503                      !!!cp ('t107');                      !!!cp ('t107');
4504                        ## NOTE: Whether the encoding is supported or not is handled
4505                        ## in the {change_encoding} callback.
4506                      $self->{change_encoding}                      $self->{change_encoding}
4507                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4508                               $token);
4509                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4510                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4511                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4531  sub _tree_construction_main ($) {
4531                  }                  }
4532                }                }
4533    
4534                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4535                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4536                  !!!ack ('t110.1');
4537                !!!next-token;                !!!next-token;
4538                redo B;                next B;
4539              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4540                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4541                  !!!cp ('t111');                  !!!cp ('t111');
4542                  ## As if </noscript>                  ## As if </noscript>
4543                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4544                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4545                                    token => $token);
4546                                
4547                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4548                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4549                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4550                  !!!cp ('t112');                  !!!cp ('t112');
4551                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4552                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4553                    push @{$self->{open_elements}},
4554                        [$self->{head_element}, $el_category->{head}];
4555                } else {                } else {
4556                  !!!cp ('t113');                  !!!cp ('t113');
4557                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4559  sub _tree_construction_main ($) {
4559                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4560                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4561                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4562                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4563                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4564                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4565                redo B;                next B;
4566              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4567                         $token->{tag_name} eq 'noframes') {
4568                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4569                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4570                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4571                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4572                  !!!cp ('t114');                  !!!cp ('t114');
4573                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4574                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4575                    push @{$self->{open_elements}},
4576                        [$self->{head_element}, $el_category->{head}];
4577                } else {                } else {
4578                  !!!cp ('t115');                  !!!cp ('t115');
4579                }                }
4580                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4581                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4582                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4583                redo B;                next B;
4584              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4585                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4586                  !!!cp ('t116');                  !!!cp ('t116');
4587                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4588                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4589                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4590                    !!!nack ('t116.1');
4591                  !!!next-token;                  !!!next-token;
4592                  redo B;                  next B;
4593                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4594                  !!!cp ('t117');                  !!!cp ('t117');
4595                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4596                                    token => $token);
4597                  ## Ignore the token                  ## Ignore the token
4598                    !!!nack ('t117.1');
4599                  !!!next-token;                  !!!next-token;
4600                  redo B;                  next B;
4601                } else {                } else {
4602                  !!!cp ('t118');                  !!!cp ('t118');
4603                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4607  sub _tree_construction_main ($) {
4607                  !!!cp ('t119');                  !!!cp ('t119');
4608                  ## As if </noscript>                  ## As if </noscript>
4609                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4610                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4611                                    token => $token);
4612                                
4613                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4614                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4615                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4616                  !!!cp ('t120');                  !!!cp ('t120');
4617                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4618                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4619                    push @{$self->{open_elements}},
4620                        [$self->{head_element}, $el_category->{head}];
4621                } else {                } else {
4622                  !!!cp ('t121');                  !!!cp ('t121');
4623                }                }
4624    
4625                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4626                $script_start_tag->($insert_to_current);                $script_start_tag->();
4627                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4628                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4629                redo B;                next B;
4630              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4631                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4632                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4633                  !!!cp ('t122');                  !!!cp ('t122');
4634                  ## As if </noscript>                  ## As if </noscript>
4635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4636                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4637                                    text => $token->{tag_name}, token => $token);
4638                                    
4639                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4640                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4651  sub _tree_construction_main ($) {
4651                }                }
4652    
4653                ## "after head" insertion mode                ## "after head" insertion mode
4654                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4655                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4656                  !!!cp ('t126');                  !!!cp ('t126');
4657                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4661  sub _tree_construction_main ($) {
4661                } else {                } else {
4662                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4663                }                }
4664                  !!!nack ('t127.1');
4665                !!!next-token;                !!!next-token;
4666                redo B;                next B;
4667              } else {              } else {
4668                !!!cp ('t128');                !!!cp ('t128');
4669                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4673  sub _tree_construction_main ($) {
4673                !!!cp ('t129');                !!!cp ('t129');
4674                ## As if </noscript>                ## As if </noscript>
4675                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4676                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4677                                  text => $token->{tag_name}, token => $token);
4678                                
4679                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4680                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4693  sub _tree_construction_main ($) {
4693    
4694              ## "after head" insertion mode              ## "after head" insertion mode
4695              ## As if <body>              ## As if <body>
4696              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4697              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4698              ## reprocess              ## reprocess
4699              redo B;              !!!ack-later;
4700                next B;
4701            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4702              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4703                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4704                  !!!cp ('t132');                  !!!cp ('t132');
4705                  ## As if <head>                  ## As if <head>
4706                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4707                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4708                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4709                        [$self->{head_element}, $el_category->{head}];
4710    
4711                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4712                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4713                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4714                  !!!next-token;                  !!!next-token;
4715                  redo B;                  next B;
4716                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4717                  !!!cp ('t133');                  !!!cp ('t133');
4718                  ## As if </noscript>                  ## As if </noscript>
4719                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4720                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4721                                    text => 'head', token => $token);
4722                                    
4723                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4724                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4725                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4726                  !!!next-token;                  !!!next-token;
4727                  redo B;                  next B;
4728                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4729                  !!!cp ('t134');                  !!!cp ('t134');
4730                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4731                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4732                  !!!next-token;                  !!!next-token;
4733                  redo B;                  next B;
4734                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4735                    !!!cp ('t134.1');
4736                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4737                                    token => $token);
4738                    ## Ignore the token
4739                    !!!next-token;
4740                    next B;
4741                } else {                } else {
4742                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4743                }                }
4744              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4745                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3706  sub _tree_construction_main ($) { Line 4747  sub _tree_construction_main ($) {
4747                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4748                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4749                  !!!next-token;                  !!!next-token;
4750                  redo B;                  next B;
4751                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4752                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4753                  !!!cp ('t137');                  !!!cp ('t137');
4754                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4755                                    text => 'noscript', token => $token);
4756                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4757                  !!!next-token;                  !!!next-token;
4758                  redo B;                  next B;
4759                } else {                } else {
4760                  !!!cp ('t138');                  !!!cp ('t138');
4761                  #                  #
# Line 3720  sub _tree_construction_main ($) { Line 4763  sub _tree_construction_main ($) {
4763              } elsif ({              } elsif ({
4764                        body => 1, html => 1,                        body => 1, html => 1,
4765                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4766                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4767                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4768                  ## 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) {  
4769                  !!!cp ('t140');                  !!!cp ('t140');
4770                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4771                                    text => $token->{tag_name}, token => $token);
4772                  ## Ignore the token                  ## Ignore the token
4773                  !!!next-token;                  !!!next-token;
4774                  redo B;                  next B;
4775                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4776                    !!!cp ('t140.1');
4777                    !!!parse-error (type => 'unmatched end tag',
4778                                    text => $token->{tag_name}, token => $token);
4779                    ## Ignore the token
4780                    !!!next-token;
4781                    next B;
4782                } else {                } else {
4783                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4784                }                }
4785                              } elsif ($token->{tag_name} eq 'p') {
4786                #                !!!cp ('t142');
4787              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4788                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4789                       }->{$token->{tag_name}}) {                ## Ignore the token
4790                  !!!next-token;
4791                  next B;
4792                } elsif ($token->{tag_name} eq 'br') {
4793                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4794                  !!!cp ('t142');                  !!!cp ('t142.2');
4795                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4796                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4797                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4798                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4799      
4800                    ## Reprocess in the "after head" insertion mode...
4801                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4802                    !!!cp ('t143.2');
4803                    ## As if </head>
4804                    pop @{$self->{open_elements}};
4805                    $self->{insertion_mode} = AFTER_HEAD_IM;
4806      
4807                    ## Reprocess in the "after head" insertion mode...
4808                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809                    !!!cp ('t143.3');
4810                    ## ISSUE: Two parse errors for <head><noscript></br>
4811                    !!!parse-error (type => 'unmatched end tag',
4812                                    text => 'br', token => $token);
4813                    ## As if </noscript>
4814                    pop @{$self->{open_elements}};
4815                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4816    
4817                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4818                } else {                  ## As if </head>
4819                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4820                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4821    
4822                #                  ## Reprocess in the "after head" insertion mode...
4823              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4824                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4825                  #                  #
4826                } else {                } else {
4827                  !!!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;  
4828                }                }
4829    
4830                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4831                  !!!parse-error (type => 'unmatched end tag',
4832                                  text => 'br', token => $token);
4833                  ## Ignore the token
4834                  !!!next-token;
4835                  next B;
4836                } else {
4837                  !!!cp ('t145');
4838                  !!!parse-error (type => 'unmatched end tag',
4839                                  text => $token->{tag_name}, token => $token);
4840                  ## Ignore the token
4841                  !!!next-token;
4842                  next B;
4843              }              }
4844    
4845              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4846                !!!cp ('t146');                !!!cp ('t146');
4847                ## As if </noscript>                ## As if </noscript>
4848                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4849                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4850                                  text => $token->{tag_name}, token => $token);
4851                                
4852                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4853                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4863  sub _tree_construction_main ($) {
4863              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4864  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4865                !!!cp ('t148');                !!!cp ('t148');
4866                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4867                                  text => $token->{tag_name}, token => $token);
4868                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4869                !!!next-token;                !!!next-token;
4870                redo B;                next B;
4871              } else {              } else {
4872                !!!cp ('t149');                !!!cp ('t149');
4873              }              }
4874    
4875              ## "after head" insertion mode              ## "after head" insertion mode
4876              ## As if <body>              ## As if <body>
4877              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4878              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4879              ## reprocess              ## reprocess
4880              redo B;              next B;
4881            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4882              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4883            }            !!!cp ('t149.1');
4884    
4885              ## NOTE: As if <head>
4886              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4887              $self->{open_elements}->[-1]->[0]->append_child
4888                  ($self->{head_element});
4889              #push @{$self->{open_elements}},
4890              #    [$self->{head_element}, $el_category->{head}];
4891              #$self->{insertion_mode} = IN_HEAD_IM;
4892              ## NOTE: Reprocess.
4893    
4894              ## NOTE: As if </head>
4895              #pop @{$self->{open_elements}};
4896              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4897              ## NOTE: Reprocess.
4898              
4899              #
4900            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4901              !!!cp ('t149.2');
4902    
4903              ## NOTE: As if </head>
4904              pop @{$self->{open_elements}};
4905              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4906              ## NOTE: Reprocess.
4907    
4908              #
4909            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4910              !!!cp ('t149.3');
4911    
4912              !!!parse-error (type => 'in noscript:#eof', token => $token);
4913    
4914              ## As if </noscript>
4915              pop @{$self->{open_elements}};
4916              #$self->{insertion_mode} = IN_HEAD_IM;
4917              ## NOTE: Reprocess.
4918    
4919              ## NOTE: As if </head>
4920              pop @{$self->{open_elements}};
4921              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4922              ## NOTE: Reprocess.
4923    
4924              #
4925            } else {
4926              !!!cp ('t149.4');
4927              #
4928            }
4929    
4930            ## NOTE: As if <body>
4931            !!!insert-element ('body',, $token);
4932            $self->{insertion_mode} = IN_BODY_IM;
4933            ## NOTE: Reprocess.
4934            next B;
4935          } else {
4936            die "$0: $token->{type}: Unknown token type";
4937          }
4938    
4939            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4940      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4947    
4948              !!!next-token;              !!!next-token;
4949              redo B;              next B;
4950            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4951              if ({              if ({
4952                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4954  sub _tree_construction_main ($) {
4954                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4955                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4956                  ## have an element in table scope                  ## have an element in table scope
4957                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4958                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4959                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4960                      !!!cp ('t151');                      !!!cp ('t151');
4961                      $tn = $node->[1];  
4962                      last INSCOPE;                      ## Close the cell
4963                    } elsif ({                      !!!back-token; # <x>
4964                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4965                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4966                                  line => $token->{line},
4967                                  column => $token->{column}};
4968                        next B;
4969                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4970                      !!!cp ('t152');                      !!!cp ('t152');
4971                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4972                        last;
4973                    }                    }
4974                  } # INSCOPE                  }
4975                    unless (defined $tn) {  
4976                      !!!cp ('t153');                  !!!cp ('t153');
4977  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4978                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      text => $token->{tag_name}, token => $token);
4979                      ## Ignore the token                  ## Ignore the token
4980                      !!!next-token;                  !!!nack ('t153.1');
4981                      redo B;                  !!!next-token;
4982                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4983                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4984                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4985                                    token => $token);
4986                                    
4987                  ## As if </caption>                  ## NOTE: As if </caption>.
4988                  ## have a table element in table scope                  ## have a table element in table scope
4989                  my $i;                  my $i;
4990                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4991                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4992                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4993                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4994                      $i = $_;                        !!!cp ('t155');
4995                      last INSCOPE;                        $i = $_;
4996                    } elsif ({                        last INSCOPE;
4997                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4998                             }->{$node->[1]}) {                        !!!cp ('t156');
4999                      !!!cp ('t156');                        last;
5000                      last INSCOPE;                      }
5001                    }                    }
5002    
5003                      !!!cp ('t157');
5004                      !!!parse-error (type => 'start tag not allowed',
5005                                      text => $token->{tag_name}, token => $token);
5006                      ## Ignore the token
5007                      !!!nack ('t157.1');
5008                      !!!next-token;
5009                      next B;
5010                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5011                                    
5012                  ## generate implied end tags                  ## generate implied end tags
5013                  while ({                  while ($self->{open_elements}->[-1]->[1]
5014                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5015                    !!!cp ('t158');                    !!!cp ('t158');
5016                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5017                  }                  }
5018    
5019                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5020                    !!!cp ('t159');                    !!!cp ('t159');
5021                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5022                                      text => $self->{open_elements}->[-1]->[0]
5023                                          ->manakai_local_name,
5024                                      token => $token);
5025                  } else {                  } else {
5026                    !!!cp ('t160');                    !!!cp ('t160');
5027                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 5033  sub _tree_construction_main ($) {
5033                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5034                                    
5035                  ## reprocess                  ## reprocess
5036                  redo B;                  !!!ack-later;
5037                    next B;
5038                } else {                } else {
5039                  !!!cp ('t161');                  !!!cp ('t161');
5040                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050                  my $i;                  my $i;
5051                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5052                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5053                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5054                      !!!cp ('t163');                      !!!cp ('t163');
5055                      $i = $_;                      $i = $_;
5056                      last INSCOPE;                      last INSCOPE;
5057                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5058                      !!!cp ('t164');                      !!!cp ('t164');
5059                      last INSCOPE;                      last INSCOPE;
5060                    }                    }
5061                  } # INSCOPE                  } # INSCOPE
5062                    unless (defined $i) {                    unless (defined $i) {
5063                      !!!cp ('t165');                      !!!cp ('t165');
5064                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5065                                        text => $token->{tag_name},
5066                                        token => $token);
5067                      ## Ignore the token                      ## Ignore the token
5068                      !!!next-token;                      !!!next-token;
5069                      redo B;                      next B;
5070                    }                    }
5071                                    
5072                  ## generate implied end tags                  ## generate implied end tags
5073                  while ({                  while ($self->{open_elements}->[-1]->[1]
5074                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5075                    !!!cp ('t166');                    !!!cp ('t166');
5076                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5077                  }                  }
5078    
5079                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5080                            ne $token->{tag_name}) {
5081                    !!!cp ('t167');                    !!!cp ('t167');
5082                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5083                                      text => $self->{open_elements}->[-1]->[0]
5084                                          ->manakai_local_name,
5085                                      token => $token);
5086                  } else {                  } else {
5087                    !!!cp ('t168');                    !!!cp ('t168');
5088                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 5094  sub _tree_construction_main ($) {
5094                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5095                                    
5096                  !!!next-token;                  !!!next-token;
5097                  redo B;                  next B;
5098                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5099                  !!!cp ('t169');                  !!!cp ('t169');
5100                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5101                                    text => $token->{tag_name}, token => $token);
5102                  ## Ignore the token                  ## Ignore the token
5103                  !!!next-token;                  !!!next-token;
5104                  redo B;                  next B;
5105                } else {                } else {
5106                  !!!cp ('t170');                  !!!cp ('t170');
5107                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 5110  sub _tree_construction_main ($) {
5110                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5111                  ## have a table element in table scope                  ## have a table element in table scope
5112                  my $i;                  my $i;
5113                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5114                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5115                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5116                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5117                      $i = $_;                        !!!cp ('t171');
5118                      last INSCOPE;                        $i = $_;
5119                    } elsif ({                        last INSCOPE;
5120                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5121                             }->{$node->[1]}) {                        !!!cp ('t172');
5122                      !!!cp ('t172');                        last;
5123                      last INSCOPE;                      }
5124                    }                    }
5125    
5126                      !!!cp ('t173');
5127                      !!!parse-error (type => 'unmatched end tag',
5128                                      text => $token->{tag_name}, token => $token);
5129                      ## Ignore the token
5130                      !!!next-token;
5131                      next B;
5132                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5133                                    
5134                  ## generate implied end tags                  ## generate implied end tags
5135                  while ({                  while ($self->{open_elements}->[-1]->[1]
5136                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5137                    !!!cp ('t174');                    !!!cp ('t174');
5138                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5139                  }                  }
5140                                    
5141                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5142                    !!!cp ('t175');                    !!!cp ('t175');
5143                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5144                                      text => $self->{open_elements}->[-1]->[0]
5145                                          ->manakai_local_name,
5146                                      token => $token);
5147                  } else {                  } else {
5148                    !!!cp ('t176');                    !!!cp ('t176');
5149                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 5155  sub _tree_construction_main ($) {
5155                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5156                                    
5157                  !!!next-token;                  !!!next-token;
5158                  redo B;                  next B;
5159                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5160                  !!!cp ('t177');                  !!!cp ('t177');
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 ('t178');                  !!!cp ('t178');
5168                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 5175  sub _tree_construction_main ($) {
5175                ## have an element in table scope                ## have an element in table scope
5176                my $i;                my $i;
5177                my $tn;                my $tn;
5178                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5179                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5180                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5181                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5182                    $i = $_;                      !!!cp ('t179');
5183                    last INSCOPE;                      $i = $_;
5184                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5185                    !!!cp ('t180');                      ## Close the cell
5186                    $tn = $node->[1];                      !!!back-token; # </x>
5187                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5188                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5189                  } elsif ({                                column => $token->{column}};
5190                            table => 1, html => 1,                      next B;
5191                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5192                    !!!cp ('t181');                      !!!cp ('t180');
5193                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5194                        ## NOTE: There is exactly one |td| or |th| element
5195                        ## in scope in the stack of open elements by definition.
5196                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5197                        ## ISSUE: Can this be reached?
5198                        !!!cp ('t181');
5199                        last;
5200                      }
5201                  }                  }
5202                } # INSCOPE  
               unless (defined $i) {  
5203                  !!!cp ('t182');                  !!!cp ('t182');
5204                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5205                        text => $token->{tag_name}, token => $token);
5206                  ## Ignore the token                  ## Ignore the token
5207                  !!!next-token;                  !!!next-token;
5208                  redo B;                  next B;
5209                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5210              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5211                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5212                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5213                                  token => $token);
5214    
5215                ## As if </caption>                ## As if </caption>
5216                ## have a table element in table scope                ## have a table element in table scope
5217                my $i;                my $i;
5218                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5219                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5220                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5221                    !!!cp ('t184');                    !!!cp ('t184');
5222                    $i = $_;                    $i = $_;
5223                    last INSCOPE;                    last INSCOPE;
5224                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5225                    !!!cp ('t185');                    !!!cp ('t185');
5226                    last INSCOPE;                    last INSCOPE;
5227                  }                  }
5228                } # INSCOPE                } # INSCOPE
5229                unless (defined $i) {                unless (defined $i) {
5230                  !!!cp ('t186');                  !!!cp ('t186');
5231                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5232                                    text => 'caption', token => $token);
5233                  ## Ignore the token                  ## Ignore the token
5234                  !!!next-token;                  !!!next-token;
5235                  redo B;                  next B;
5236                }                }
5237                                
5238                ## generate implied end tags                ## generate implied end tags
5239                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5240                  !!!cp ('t187');                  !!!cp ('t187');
5241                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5242                }                }
5243    
5244                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5245                  !!!cp ('t188');                  !!!cp ('t188');
5246                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5247                                    text => $self->{open_elements}->[-1]->[0]
5248                                        ->manakai_local_name,
5249                                    token => $token);
5250                } else {                } else {
5251                  !!!cp ('t189');                  !!!cp ('t189');
5252                }                }
# Line 4120  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5259    
5260                ## reprocess                ## reprocess
5261                redo B;                next B;
5262              } elsif ({              } elsif ({
5263                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5264                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5265                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5266                  !!!cp ('t190');                  !!!cp ('t190');
5267                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5268                                    text => $token->{tag_name}, token => $token);
5269                  ## Ignore the token                  ## Ignore the token
5270                  !!!next-token;                  !!!next-token;
5271                  redo B;                  next B;
5272                } else {                } else {
5273                  !!!cp ('t191');                  !!!cp ('t191');
5274                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5280                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5281                !!!cp ('t192');                !!!cp ('t192');
5282                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5283                                  text => $token->{tag_name}, token => $token);
5284                ## Ignore the token                ## Ignore the token
5285                !!!next-token;                !!!next-token;
5286                redo B;                next B;
5287              } else {              } else {
5288                !!!cp ('t193');                !!!cp ('t193');
5289                #                #
5290              }              }
5291          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5292            for my $entry (@{$self->{open_elements}}) {
5293              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5294                !!!cp ('t75');
5295                !!!parse-error (type => 'in body:#eof', token => $token);
5296                last;
5297              }
5298            }
5299    
5300            ## Stop parsing.
5301            last B;
5302        } else {        } else {
5303          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5304        }        }
# Line 4156  sub _tree_construction_main ($) { Line 5307  sub _tree_construction_main ($) {
5307        #        #
5308      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5309        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5310              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5311                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5312              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5313                                
5314                unless (length $token->{data}) {            unless (length $token->{data}) {
5315                  !!!cp ('t194');              !!!cp ('t194');
5316                  !!!next-token;              !!!next-token;
5317                  redo B;              next B;
5318                } else {            } else {
5319                  !!!cp ('t195');              !!!cp ('t195');
5320                }            }
5321              }          }
5322    
5323              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5324    
5325              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5326              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 5328  sub _tree_construction_main ($) {
5328              ## result in a new Text node.              ## result in a new Text node.
5329              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5330                            
5331              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]}) {  
5332                # MUST                # MUST
5333                my $foster_parent_element;                my $foster_parent_element;
5334                my $next_sibling;                my $next_sibling;
5335                my $prev_sibling;                my $prev_sibling;
5336                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5337                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5338                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5339                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5340                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 5362  sub _tree_construction_main ($) {
5362                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5363                     $next_sibling);                     $next_sibling);
5364                }                }
5365              } else {            $open_tables->[-1]->[1] = 1; # tainted
5366                !!!cp ('t200');          } else {
5367                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5368              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5369            }
5370                            
5371              !!!next-token;          !!!next-token;
5372              redo B;          next B;
5373        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5374              if ({          if ({
5375                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5376                   th => 1, td => 1,               th => 1, td => 1,
5377                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5378                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5379                  ## Clear back to table context              ## Clear back to table context
5380                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5381                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5382                    !!!cp ('t201');                !!!cp ('t201');
5383                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5384                    pop @{$self->{open_elements}};              }
5385                  }              
5386                                !!!insert-element ('tbody',, $token);
5387                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5388                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5389                  ## reprocess in the "in table body" insertion mode...            }
5390                }            
5391              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5392                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5393                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5394                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5395                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5396                                    
5397                  ## Clear back to table body context              ## Clear back to table body context
5398                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5399                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5400                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5401                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5402                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5403                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
5404                                    
5405                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5406                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5407                    !!!cp ('t204');                    !!!cp ('t204');
5408                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5409                      !!!nack ('t204');
5410                    !!!next-token;                    !!!next-token;
5411                    redo B;                    next B;
5412                  } else {                  } else {
5413                    !!!cp ('t205');                    !!!cp ('t205');
5414                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5415                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5416                  }                  }
5417                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 5419  sub _tree_construction_main ($) {
5419                }                }
5420    
5421                ## Clear back to table row context                ## Clear back to table row context
5422                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5423                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5424                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5425                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5426                }                }
5427                                
5428                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5429                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5430    
5431                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5432                                
5433                  !!!nack ('t207.1');
5434                !!!next-token;                !!!next-token;
5435                redo B;                next B;
5436              } elsif ({              } elsif ({
5437                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5438                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 5444  sub _tree_construction_main ($) {
5444                  my $i;                  my $i;
5445                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5446                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5447                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5448                      !!!cp ('t208');                      !!!cp ('t208');
5449                      $i = $_;                      $i = $_;
5450                      last INSCOPE;                      last INSCOPE;
5451                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5452                      !!!cp ('t209');                      !!!cp ('t209');
5453                      last INSCOPE;                      last INSCOPE;
5454                    }                    }
5455                  } # INSCOPE                  } # INSCOPE
5456                  unless (defined $i) {                  unless (defined $i) {
5457                   !!!cp ('t210');                    !!!cp ('t210');
5458  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5459                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5460                                      text => $token->{tag_name}, token => $token);
5461                    ## Ignore the token                    ## Ignore the token
5462                      !!!nack ('t210.1');
5463                    !!!next-token;                    !!!next-token;
5464                    redo B;                    next B;
5465                  }                  }
5466                                    
5467                  ## Clear back to table row context                  ## Clear back to table row context
5468                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5469                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5470                    !!!cp ('t211');                    !!!cp ('t211');
5471                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5472                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5473                  }                  }
5474                                    
# Line 4335  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5478                    !!!cp ('t212');                    !!!cp ('t212');
5479                    ## reprocess                    ## reprocess
5480                    redo B;                    !!!ack-later;
5481                      next B;
5482                  } else {                  } else {
5483                    !!!cp ('t213');                    !!!cp ('t213');
5484                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 5490  sub _tree_construction_main ($) {
5490                  my $i;                  my $i;
5491                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5492                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5493                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5494                      !!!cp ('t214');                      !!!cp ('t214');
5495                      $i = $_;                      $i = $_;
5496                      last INSCOPE;                      last INSCOPE;
5497                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5498                      !!!cp ('t215');                      !!!cp ('t215');
5499                      last INSCOPE;                      last INSCOPE;
5500                    }                    }
5501                  } # INSCOPE                  } # INSCOPE
5502                  unless (defined $i) {                  unless (defined $i) {
5503                    !!!cp ('t216');                    !!!cp ('t216');
5504  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5505                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5506                                      text => $token->{tag_name}, token => $token);
5507                    ## Ignore the token                    ## Ignore the token
5508                      !!!nack ('t216.1');
5509                    !!!next-token;                    !!!next-token;
5510                    redo B;                    next B;
5511                  }                  }
5512    
5513                  ## Clear back to table body context                  ## Clear back to table body context
5514                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5515                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5516                    !!!cp ('t217');                    !!!cp ('t217');
5517                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5518                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5519                  }                  }
5520                                    
# Line 4395  sub _tree_construction_main ($) { Line 5534  sub _tree_construction_main ($) {
5534    
5535                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5536                  ## Clear back to table context                  ## Clear back to table context
5537                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5538                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5539                    !!!cp ('t219');                    !!!cp ('t219');
5540                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5541                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5542                  }                  }
5543                                    
5544                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5545                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5546                  ## reprocess                  ## reprocess
5547                  redo B;                  !!!ack-later;
5548                    next B;
5549                } elsif ({                } elsif ({
5550                          caption => 1,                          caption => 1,
5551                          colgroup => 1,                          colgroup => 1,
5552                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5553                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5554                  ## Clear back to table context                  ## Clear back to table context
5555                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5556                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5557                    !!!cp ('t220');                    !!!cp ('t220');
5558                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5559                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5560                  }                  }
5561                                    
5562                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5563                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5564                                    
5565                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5566                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5567                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5568                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5571  sub _tree_construction_main ($) {
5571                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5572                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5573                  !!!next-token;                  !!!next-token;
5574                  redo B;                  !!!nack ('t220.1');
5575                    next B;
5576                } else {                } else {
5577                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5578                }                }
5579              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5580                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5581                                  text => $self->{open_elements}->[-1]->[0]
5582                                      ->manakai_local_name,
5583                                  token => $token);
5584    
5585                ## As if </table>                ## As if </table>
5586                ## have a table element in table scope                ## have a table element in table scope
5587                my $i;                my $i;
5588                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5589                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5590                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5591                    !!!cp ('t221');                    !!!cp ('t221');
5592                    $i = $_;                    $i = $_;
5593                    last INSCOPE;                    last INSCOPE;
5594                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5595                    !!!cp ('t222');                    !!!cp ('t222');
5596                    last INSCOPE;                    last INSCOPE;
5597                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5599  sub _tree_construction_main ($) {
5599                unless (defined $i) {                unless (defined $i) {
5600                  !!!cp ('t223');                  !!!cp ('t223');
5601  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5602                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5603                                    token => $token);
5604                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5605                    !!!nack ('t223.1');
5606                  !!!next-token;                  !!!next-token;
5607                  redo B;                  next B;
5608                }                }
5609                                
5610    ## TODO: Followings are removed from the latest spec.
5611                ## generate implied end tags                ## generate implied end tags
5612                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5613                  !!!cp ('t224');                  !!!cp ('t224');
5614                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5615                }                }
5616    
5617                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5618                  !!!cp ('t225');                  !!!cp ('t225');
5619  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5620                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5621                                    text => $self->{open_elements}->[-1]->[0]
5622                                        ->manakai_local_name,
5623                                    token => $token);
5624                } else {                } else {
5625                  !!!cp ('t226');                  !!!cp ('t226');
5626                }                }
5627    
5628                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5629                  pop @{$open_tables};
5630    
5631                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5632    
5633                ## reprocess            ## reprocess
5634                redo B;            !!!ack-later;
5635              next B;
5636            } elsif ($token->{tag_name} eq 'style') {
5637              if (not $open_tables->[-1]->[1]) { # tainted
5638                !!!cp ('t227.8');
5639                ## NOTE: This is a "as if in head" code clone.
5640                $parse_rcdata->(CDATA_CONTENT_MODEL);
5641                next B;
5642              } else {
5643                !!!cp ('t227.7');
5644                #
5645              }
5646            } elsif ($token->{tag_name} eq 'script') {
5647              if (not $open_tables->[-1]->[1]) { # tainted
5648                !!!cp ('t227.6');
5649                ## NOTE: This is a "as if in head" code clone.
5650                $script_start_tag->();
5651                next B;
5652              } else {
5653                !!!cp ('t227.5');
5654                #
5655              }
5656            } elsif ($token->{tag_name} eq 'input') {
5657              if (not $open_tables->[-1]->[1]) { # tainted
5658                if ($token->{attributes}->{type}) { ## TODO: case
5659                  my $type = lc $token->{attributes}->{type}->{value};
5660                  if ($type eq 'hidden') {
5661                    !!!cp ('t227.3');
5662                    !!!parse-error (type => 'in table',
5663                                    text => $token->{tag_name}, token => $token);
5664    
5665                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5666    
5667                    ## TODO: form element pointer
5668    
5669                    pop @{$self->{open_elements}};
5670    
5671                    !!!next-token;
5672                    !!!ack ('t227.2.1');
5673                    next B;
5674                  } else {
5675                    !!!cp ('t227.2');
5676                    #
5677                  }
5678                } else {
5679                  !!!cp ('t227.1');
5680                  #
5681                }
5682              } else {
5683                !!!cp ('t227.4');
5684                #
5685              }
5686          } else {          } else {
5687            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5688            #            #
5689          }          }
5690    
5691            !!!parse-error (type => 'in table', text => $token->{tag_name},
5692                            token => $token);
5693    
5694            $insert = $insert_to_foster;
5695            #
5696        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5697              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5698                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5700  sub _tree_construction_main ($) {
5700                my $i;                my $i;
5701                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5702                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5703                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5704                    !!!cp ('t228');                    !!!cp ('t228');
5705                    $i = $_;                    $i = $_;
5706                    last INSCOPE;                    last INSCOPE;
5707                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5708                    !!!cp ('t229');                    !!!cp ('t229');
5709                    last INSCOPE;                    last INSCOPE;
5710                  }                  }
5711                } # INSCOPE                } # INSCOPE
5712                unless (defined $i) {                unless (defined $i) {
5713                  !!!cp ('t230');                  !!!cp ('t230');
5714                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5715                                    text => $token->{tag_name}, token => $token);
5716                  ## Ignore the token                  ## Ignore the token
5717                    !!!nack ('t230.1');
5718                  !!!next-token;                  !!!next-token;
5719                  redo B;                  next B;
5720                } else {                } else {
5721                  !!!cp ('t232');                  !!!cp ('t232');
5722                }                }
5723    
5724                ## Clear back to table row context                ## Clear back to table row context
5725                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5726                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5727                  !!!cp ('t231');                  !!!cp ('t231');
5728  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5729                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5730                }                }
5731    
5732                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5733                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5734                !!!next-token;                !!!next-token;
5735                redo B;                !!!nack ('t231.1');
5736                  next B;
5737              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5738                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5739                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5741  sub _tree_construction_main ($) {
5741                  my $i;                  my $i;
5742                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5743                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5744                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5745                      !!!cp ('t233');                      !!!cp ('t233');
5746                      $i = $_;                      $i = $_;
5747                      last INSCOPE;                      last INSCOPE;
5748                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5749                      !!!cp ('t234');                      !!!cp ('t234');
5750                      last INSCOPE;                      last INSCOPE;
5751                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5753  sub _tree_construction_main ($) {
5753                  unless (defined $i) {                  unless (defined $i) {
5754                    !!!cp ('t235');                    !!!cp ('t235');
5755  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5756                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5757                                      text => $token->{type}, token => $token);
5758                    ## Ignore the token                    ## Ignore the token
5759                      !!!nack ('t236.1');
5760                    !!!next-token;                    !!!next-token;
5761                    redo B;                    next B;
5762                  }                  }
5763                                    
5764                  ## Clear back to table row context                  ## Clear back to table row context
5765                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5766                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5767                    !!!cp ('t236');                    !!!cp ('t236');
5768  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5769                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5770                  }                  }
5771                                    
# Line 4584  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 ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5783                      !!!cp ('t237');                      !!!cp ('t237');
5784                      $i = $_;                      $i = $_;
5785                      last INSCOPE;                      last INSCOPE;
5786                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5787                      !!!cp ('t238');                      !!!cp ('t238');
5788                      last INSCOPE;                      last INSCOPE;
5789                    }                    }
5790                  } # INSCOPE                  } # INSCOPE
5791                  unless (defined $i) {                  unless (defined $i) {
5792                    !!!cp ('t239');                    !!!cp ('t239');
5793                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5794                                      text => $token->{tag_name}, token => $token);
5795                    ## Ignore the token                    ## Ignore the token
5796                      !!!nack ('t239.1');
5797                    !!!next-token;                    !!!next-token;
5798                    redo B;                    next B;
5799                  }                  }
5800                                    
5801                  ## Clear back to table body context                  ## Clear back to table body context
5802                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5803                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5804                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5805                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5806                  }                  }
5807                                    
# Line 4626  sub _tree_construction_main ($) { Line 5817  sub _tree_construction_main ($) {
5817                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5818                }                }
5819    
5820                  ## NOTE: </table> in the "in table" insertion mode.
5821                  ## When you edit the code fragment below, please ensure that
5822                  ## the code for <table> in the "in table" insertion mode
5823                  ## is synced with it.
5824    
5825                ## have a table element in table scope                ## have a table element in table scope
5826                my $i;                my $i;
5827                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5828                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5829                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5830                    !!!cp ('t241');                    !!!cp ('t241');
5831                    $i = $_;                    $i = $_;
5832                    last INSCOPE;                    last INSCOPE;
5833                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5834                    !!!cp ('t242');                    !!!cp ('t242');
5835                    last INSCOPE;                    last INSCOPE;
5836                  }                  }
5837                } # INSCOPE                } # INSCOPE
5838                unless (defined $i) {                unless (defined $i) {
5839                  !!!cp ('t243');                  !!!cp ('t243');
5840                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5841                                    text => $token->{tag_name}, token => $token);
5842                  ## Ignore the token                  ## Ignore the token
5843                    !!!nack ('t243.1');
5844                  !!!next-token;                  !!!next-token;
5845                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5846                }                }
5847                                    
5848                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5849                  pop @{$open_tables};
5850                                
5851                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5852                                
5853                !!!next-token;                !!!next-token;
5854                redo B;                next B;
5855              } elsif ({              } elsif ({
5856                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5857                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 5861  sub _tree_construction_main ($) {
5861                  my $i;                  my $i;
5862                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5863                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5864                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5865                      !!!cp ('t247');                      !!!cp ('t247');
5866                      $i = $_;                      $i = $_;
5867                      last INSCOPE;                      last INSCOPE;
5868                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5869                      !!!cp ('t248');                      !!!cp ('t248');
5870                      last INSCOPE;                      last INSCOPE;
5871                    }                    }
5872                  } # INSCOPE                  } # INSCOPE
5873                    unless (defined $i) {                    unless (defined $i) {
5874                      !!!cp ('t249');                      !!!cp ('t249');
5875                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5876                                        text => $token->{tag_name}, token => $token);
5877                      ## Ignore the token                      ## Ignore the token
5878                        !!!nack ('t249.1');
5879                      !!!next-token;                      !!!next-token;
5880                      redo B;                      next B;
5881                    }                    }
5882                                    
5883                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 5885  sub _tree_construction_main ($) {
5885                  my $i;                  my $i;
5886                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5887                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5888                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5889                      !!!cp ('t250');                      !!!cp ('t250');
5890                      $i = $_;                      $i = $_;
5891                      last INSCOPE;                      last INSCOPE;
5892                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5893                      !!!cp ('t251');                      !!!cp ('t251');
5894                      last INSCOPE;                      last INSCOPE;
5895                    }                    }
5896                  } # INSCOPE                  } # INSCOPE
5897                    unless (defined $i) {                    unless (defined $i) {
5898                      !!!cp ('t252');                      !!!cp ('t252');
5899                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
5900                                        text => 'tr', token => $token);
5901                      ## Ignore the token                      ## Ignore the token
5902                        !!!nack ('t252.1');
5903                      !!!next-token;                      !!!next-token;
5904                      redo B;                      next B;
5905                    }                    }
5906                                    
5907                  ## Clear back to table row context                  ## Clear back to table row context
5908                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5909                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5910                    !!!cp ('t253');                    !!!cp ('t253');
5911  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5912                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5913                  }                  }
5914                                    
# Line 4742  sub _tree_construction_main ($) { Line 5921  sub _tree_construction_main ($) {
5921                my $i;                my $i;
5922                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5923                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5924                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5925                    !!!cp ('t254');                    !!!cp ('t254');
5926                    $i = $_;                    $i = $_;
5927                    last INSCOPE;                    last INSCOPE;
5928                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5929                    !!!cp ('t255');                    !!!cp ('t255');
5930                    last INSCOPE;                    last INSCOPE;
5931                  }                  }
5932                } # INSCOPE                } # INSCOPE
5933                unless (defined $i) {                unless (defined $i) {
5934                  !!!cp ('t256');                  !!!cp ('t256');
5935                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5936                                    text => $token->{tag_name}, token => $token);
5937                  ## Ignore the token                  ## Ignore the token
5938                    !!!nack ('t256.1');
5939                  !!!next-token;                  !!!next-token;
5940                  redo B;                  next B;
5941                }                }
5942    
5943                ## Clear back to table body context                ## Clear back to table body context
5944                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5945                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5946                  !!!cp ('t257');                  !!!cp ('t257');
5947  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5948                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5949                }                }
5950    
5951                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5952                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5953                  !!!nack ('t257.1');
5954                !!!next-token;                !!!next-token;
5955                redo B;                next B;
5956              } elsif ({              } elsif ({
5957                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5958                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5959                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5960                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5961                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5962                !!!cp ('t258');            !!!cp ('t258');
5963                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
5964                ## Ignore the token                            text => $token->{tag_name}, token => $token);
5965                !!!next-token;            ## Ignore the token
5966                redo B;            !!!nack ('t258.1');
5967               !!!next-token;
5968              next B;
5969          } else {          } else {
5970            !!!cp ('t259');            !!!cp ('t259');
5971            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
5972                              text => $token->{tag_name}, token => $token);
5973    
5974            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5975            #            #
5976          }          }
5977          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5978            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5979                    @{$self->{open_elements}} == 1) { # redundant, maybe
5980              !!!parse-error (type => 'in body:#eof', token => $token);
5981              !!!cp ('t259.1');
5982              #
5983            } else {
5984              !!!cp ('t259.2');
5985              #
5986            }
5987    
5988            ## Stop parsing
5989            last B;
5990        } else {        } else {
5991          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5992        }        }
# Line 4803  sub _tree_construction_main ($) { Line 5997  sub _tree_construction_main ($) {
5997                unless (length $token->{data}) {                unless (length $token->{data}) {
5998                  !!!cp ('t260');                  !!!cp ('t260');
5999                  !!!next-token;                  !!!next-token;
6000                  redo B;                  next B;
6001                }                }
6002              }              }
6003                            
# Line 4812  sub _tree_construction_main ($) { Line 6006  sub _tree_construction_main ($) {
6006            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6007              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6008                !!!cp ('t262');                !!!cp ('t262');
6009                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6010                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6011                  !!!ack ('t262.1');
6012                !!!next-token;                !!!next-token;
6013                redo B;                next B;
6014              } else {              } else {
6015                !!!cp ('t263');                !!!cp ('t263');
6016                #                #
6017              }              }
6018            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6019              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6020                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6021                  !!!cp ('t264');                  !!!cp ('t264');
6022                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6023                                    text => 'colgroup', token => $token);
6024                  ## Ignore the token                  ## Ignore the token
6025                  !!!next-token;                  !!!next-token;
6026                  redo B;                  next B;
6027                } else {                } else {
6028                  !!!cp ('t265');                  !!!cp ('t265');
6029                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6030                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6031                  !!!next-token;                  !!!next-token;
6032                  redo B;                              next B;            
6033                }                }
6034              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6035                !!!cp ('t266');                !!!cp ('t266');
6036                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6037                                  text => 'col', token => $token);
6038                ## Ignore the token                ## Ignore the token
6039                !!!next-token;                !!!next-token;
6040                redo B;                next B;
6041              } else {              } else {
6042                !!!cp ('t267');                !!!cp ('t267');
6043                #                #
6044              }              }
6045            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6046              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6047            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6048              !!!cp ('t270.2');
6049              ## Stop parsing.
6050              last B;
6051            } else {
6052              ## NOTE: As if </colgroup>.
6053              !!!cp ('t270.1');
6054              pop @{$self->{open_elements}}; # colgroup
6055              $self->{insertion_mode} = IN_TABLE_IM;
6056              ## Reprocess.
6057              next B;
6058            }
6059          } else {
6060            die "$0: $token->{type}: Unknown token type";
6061          }
6062    
6063            ## As if </colgroup>            ## As if </colgroup>
6064            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6065              !!!cp ('t269');              !!!cp ('t269');
6066              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6067                !!!parse-error (type => 'unmatched end tag',
6068                                text => 'colgroup', token => $token);
6069              ## Ignore the token              ## Ignore the token
6070                !!!nack ('t269.1');
6071              !!!next-token;              !!!next-token;
6072              redo B;              next B;
6073            } else {            } else {
6074              !!!cp ('t270');              !!!cp ('t270');
6075              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6076              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6077                !!!ack-later;
6078              ## reprocess              ## reprocess
6079              redo B;              next B;
6080            }            }
6081      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6082        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6083          !!!cp ('t271');          !!!cp ('t271');
6084          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6085          !!!next-token;          !!!next-token;
6086          redo B;          next B;
6087        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6088              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6089                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6090                  !!!cp ('t272');              !!!cp ('t272');
6091                  ## As if </option>              ## As if </option>
6092                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6093                } else {            } else {
6094                  !!!cp ('t273');              !!!cp ('t273');
6095                }            }
6096    
6097                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6098                !!!next-token;            !!!nack ('t273.1');
6099                redo B;            !!!next-token;
6100              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6101                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6102                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6103                  ## As if </option>              !!!cp ('t274');
6104                  pop @{$self->{open_elements}};              ## As if </option>
6105                } else {              pop @{$self->{open_elements}};
6106                  !!!cp ('t275');            } else {
6107                }              !!!cp ('t275');
6108              }
6109    
6110                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6111                  !!!cp ('t276');              !!!cp ('t276');
6112                  ## As if </optgroup>              ## As if </optgroup>
6113                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6114                } else {            } else {
6115                  !!!cp ('t277');              !!!cp ('t277');
6116                }            }
6117    
6118                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6119                !!!next-token;            !!!nack ('t277.1');
6120                redo B;            !!!next-token;
6121              } elsif ($token->{tag_name} eq 'select') {            next B;
6122  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
6123                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
6124                ## As if </select> instead                   }->{$token->{tag_name}} or
6125                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6126                my $i;                    {
6127                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
6128                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
6129                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
6130                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
6131                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
6132                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
6133                  } elsif ({                            token => $token);
6134                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
6135                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
6136                    !!!cp ('t279');            ## have an element in table scope
6137                    last INSCOPE;            my $i;
6138                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6139                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6140                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6141                  !!!cp ('t280');                !!!cp ('t278');
6142                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6143                  ## Ignore the token                last INSCOPE;
6144                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6145                  redo B;                !!!cp ('t279');
6146                }                last INSCOPE;
6147                }
6148              } # INSCOPE
6149              unless (defined $i) {
6150                !!!cp ('t280');
6151                !!!parse-error (type => 'unmatched end tag',
6152                                text => 'select', token => $token);
6153                ## Ignore the token
6154                !!!nack ('t280.1');
6155                !!!next-token;
6156                next B;
6157              }
6158                                
6159                !!!cp ('t281');            !!!cp ('t281');
6160                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6161    
6162                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6163    
6164                !!!next-token;            if ($token->{tag_name} eq 'select') {
6165                redo B;              !!!nack ('t281.2');
6166                !!!next-token;
6167                next B;
6168              } else {
6169                !!!cp ('t281.1');
6170                !!!ack-later;
6171                ## Reprocess the token.
6172                next B;
6173              }
6174          } else {          } else {
6175            !!!cp ('t282');            !!!cp ('t282');
6176            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6177                              text => $token->{tag_name}, token => $token);
6178            ## Ignore the token            ## Ignore the token
6179              !!!nack ('t282.1');
6180            !!!next-token;            !!!next-token;
6181            redo B;            next B;
6182          }          }
6183        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6184              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6185                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6186                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6187                  !!!cp ('t283');              !!!cp ('t283');
6188                  ## As if </option>              ## As if </option>
6189                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6190                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6191                  !!!cp ('t284');              !!!cp ('t284');
6192                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6193                } else {            } else {
6194                  !!!cp ('t285');              !!!cp ('t285');
6195                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6196                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6197                }              ## Ignore the token
6198                !!!next-token;            }
6199                redo B;            !!!nack ('t285.1');
6200              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6201                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6202                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6203                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6204                } else {              !!!cp ('t286');
6205                  !!!cp ('t287');              pop @{$self->{open_elements}};
6206                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6207                  ## Ignore the token              !!!cp ('t287');
6208                }              !!!parse-error (type => 'unmatched end tag',
6209                !!!next-token;                              text => $token->{tag_name}, token => $token);
6210                redo B;              ## Ignore the token
6211              } elsif ($token->{tag_name} eq 'select') {            }
6212                ## have an element in table scope            !!!nack ('t287.1');
6213                my $i;            !!!next-token;
6214                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6215                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6216                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6217                    !!!cp ('t288');            my $i;
6218                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6219                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6220                  } elsif ({              if ($node->[1] & SELECT_EL) {
6221                            table => 1, html => 1,                !!!cp ('t288');
6222                           }->{$node->[1]}) {                $i = $_;
6223                    !!!cp ('t289');                last INSCOPE;
6224                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6225                  }                !!!cp ('t289');
6226                } # INSCOPE                last INSCOPE;
6227                unless (defined $i) {              }
6228                  !!!cp ('t290');            } # INSCOPE
6229                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6230                  ## Ignore the token              !!!cp ('t290');
6231                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6232                  redo B;                              text => $token->{tag_name}, token => $token);
6233                }              ## Ignore the token
6234                !!!nack ('t290.1');
6235                !!!next-token;
6236                next B;
6237              }
6238                                
6239                !!!cp ('t291');            !!!cp ('t291');
6240                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6241    
6242                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6243    
6244                !!!next-token;            !!!nack ('t291.1');
6245                redo B;            !!!next-token;
6246              } elsif ({            next B;
6247                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6248                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6249                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6250                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6251                     }->{$token->{tag_name}}) {
6252  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6253                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6254                              text => $token->{tag_name}, token => $token);
6255                                
6256                ## have an element in table scope            ## have an element in table scope
6257                my $i;            my $i;
6258                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6259                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6260                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6261                    !!!cp ('t292');                !!!cp ('t292');
6262                    $i = $_;                $i = $_;
6263                    last INSCOPE;                last INSCOPE;
6264                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6265                            table => 1, html => 1,                !!!cp ('t293');
6266                           }->{$node->[1]}) {                last INSCOPE;
6267                    !!!cp ('t293');              }
6268                    last INSCOPE;            } # INSCOPE
6269                  }            unless (defined $i) {
6270                } # INSCOPE              !!!cp ('t294');
6271                unless (defined $i) {              ## Ignore the token
6272                  !!!cp ('t294');              !!!nack ('t294.1');
6273                  ## Ignore the token              !!!next-token;
6274                  !!!next-token;              next B;
6275                  redo B;            }
               }  
6276                                
6277                ## As if </select>            ## As if </select>
6278                ## have an element in table scope            ## have an element in table scope
6279                undef $i;            undef $i;
6280                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6281                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6282                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6283                    !!!cp ('t295');                !!!cp ('t295');
6284                    $i = $_;                $i = $_;
6285                    last INSCOPE;                last INSCOPE;
6286                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6287  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6288                    !!!cp ('t296');                !!!cp ('t296');
6289                    last INSCOPE;                last INSCOPE;
6290                  }              }
6291                } # INSCOPE            } # INSCOPE
6292                unless (defined $i) {            unless (defined $i) {
6293                  !!!cp ('t297');              !!!cp ('t297');
6294  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6295                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6296                  ## Ignore the </select> token                              text => 'select', token => $token);
6297                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6298                  redo B;              !!!nack ('t297.1');
6299                }              !!!next-token; ## TODO: ok?
6300                next B;
6301              }
6302                                
6303                !!!cp ('t298');            !!!cp ('t298');
6304                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6305    
6306                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6307    
6308                ## reprocess            !!!ack-later;
6309                redo B;            ## reprocess
6310              next B;
6311          } else {          } else {
6312            !!!cp ('t299');            !!!cp ('t299');
6313            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6314                              text => $token->{tag_name}, token => $token);
6315            ## Ignore the token            ## Ignore the token
6316              !!!nack ('t299.3');
6317            !!!next-token;            !!!next-token;
6318            redo B;            next B;
6319          }          }
6320          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6321            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6322                    @{$self->{open_elements}} == 1) { # redundant, maybe
6323              !!!cp ('t299.1');
6324              !!!parse-error (type => 'in body:#eof', token => $token);
6325            } else {
6326              !!!cp ('t299.2');
6327            }
6328    
6329            ## Stop parsing.
6330            last B;
6331        } else {        } else {
6332          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6333        }        }
# Line 5086  sub _tree_construction_main ($) { Line 6343  sub _tree_construction_main ($) {
6343            unless (length $token->{data}) {            unless (length $token->{data}) {
6344              !!!cp ('t300');              !!!cp ('t300');
6345              !!!next-token;              !!!next-token;
6346              redo B;              next B;
6347            }            }
6348          }          }
6349                    
6350          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6351            !!!cp ('t301');            !!!cp ('t301');
6352            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6353    
6354            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6355          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 6357  sub _tree_construction_main ($) {
6357          }          }
6358                    
6359          ## "after body" insertion mode          ## "after body" insertion mode
6360          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6361    
6362          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6363          ## reprocess          ## reprocess
6364          redo B;          next B;
6365        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6366          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6367            !!!cp ('t303');            !!!cp ('t303');
6368            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6369                              text => $token->{tag_name}, token => $token);
6370                        
6371            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6372          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 6374  sub _tree_construction_main ($) {
6374          }          }
6375    
6376          ## "after body" insertion mode          ## "after body" insertion mode
6377          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6378                            text => $token->{tag_name}, token => $token);
6379    
6380          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6381            !!!ack-later;
6382          ## reprocess          ## reprocess
6383          redo B;          next B;
6384        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6385          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6386            !!!cp ('t305');            !!!cp ('t305');
6387            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6388                              text => $token->{tag_name}, token => $token);
6389                        
6390            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6391            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 6397  sub _tree_construction_main ($) {
6397          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6398            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6399              !!!cp ('t307');              !!!cp ('t307');
6400              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6401                                text => 'html', token => $token);
6402              ## Ignore the token              ## Ignore the token
6403              !!!next-token;              !!!next-token;
6404              redo B;              next B;
6405            } else {            } else {
6406              !!!cp ('t308');              !!!cp ('t308');
6407              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6408              !!!next-token;              !!!next-token;
6409              redo B;              next B;
6410            }            }
6411          } else {          } else {
6412            !!!cp ('t309');            !!!cp ('t309');
6413            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6414                              text => $token->{tag_name}, token => $token);
6415    
6416            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6417            ## reprocess            ## reprocess
6418            redo B;            next B;
6419          }          }
6420          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6421            !!!cp ('t309.2');
6422            ## Stop parsing
6423            last B;
6424        } else {        } else {
6425          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6426        }        }
# Line 5165  sub _tree_construction_main ($) { Line 6432  sub _tree_construction_main ($) {
6432            unless (length $token->{data}) {            unless (length $token->{data}) {
6433              !!!cp ('t310');              !!!cp ('t310');
6434              !!!next-token;              !!!next-token;
6435              redo B;              next B;
6436            }            }
6437          }          }
6438                    
6439          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6440            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6441              !!!cp ('t311');              !!!cp ('t311');
6442              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6443            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6444              !!!cp ('t312');              !!!cp ('t312');
6445              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6446            } else { # "after html frameset"            } else { # "after after frameset"
6447              !!!cp ('t313');              !!!cp ('t313');
6448              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character');  
6449            }            }
6450                        
6451            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 6456  sub _tree_construction_main ($) {
6456              !!!cp ('t315');              !!!cp ('t315');
6457              !!!next-token;              !!!next-token;
6458            }            }
6459            redo B;            next B;
6460          }          }
6461                    
6462          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6463        } 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 "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6464          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6465              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6466            !!!cp ('t318');            !!!cp ('t318');
6467            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6468              !!!nack ('t318.1');
6469            !!!next-token;            !!!next-token;
6470            redo B;            next B;
6471          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6472                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6473            !!!cp ('t319');            !!!cp ('t319');
6474            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6475            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6476              !!!ack ('t319.1');
6477            !!!next-token;            !!!next-token;
6478            redo B;            next B;
6479          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6480            !!!cp ('t320');            !!!cp ('t320');
6481            ## NOTE: As if in body.            ## NOTE: As if in head.
6482            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6483            redo B;            next B;
6484    
6485              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6486              ## has no parse error.
6487          } else {          } else {
6488            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6489              !!!cp ('t321');              !!!cp ('t321');
6490              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6491            } else {                              text => $token->{tag_name}, token => $token);
6492              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6493              !!!cp ('t322');              !!!cp ('t322');
6494              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6495                                text => $token->{tag_name}, token => $token);
6496              } else { # "after after frameset"
6497                !!!cp ('t322.2');
6498                !!!parse-error (type => 'after after frameset',
6499                                text => $token->{tag_name}, token => $token);
6500            }            }
6501            ## Ignore the token            ## Ignore the token
6502              !!!nack ('t322.1');
6503            !!!next-token;            !!!next-token;
6504            redo B;            next B;
6505          }          }
6506        } 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 "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6507          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6508              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6509            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6510                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6511              !!!cp ('t325');              !!!cp ('t325');
6512              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6513                                text => $token->{tag_name}, token => $token);
6514              ## Ignore the token              ## Ignore the token
6515              !!!next-token;              !!!next-token;
6516            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 6520  sub _tree_construction_main ($) {
6520            }            }
6521    
6522            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6523                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6524              !!!cp ('t327');              !!!cp ('t327');
6525              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6526            } else {            } else {
6527              !!!cp ('t328');              !!!cp ('t328');
6528            }            }
6529            redo B;            next B;
6530          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6531                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6532            !!!cp ('t329');            !!!cp ('t329');
6533            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6534            !!!next-token;            !!!next-token;
6535            redo B;            next B;
6536          } else {          } else {
6537            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6538              !!!cp ('t330');              !!!cp ('t330');
6539              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6540            } else {                              text => $token->{tag_name}, token => $token);
6541              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6542                !!!cp ('t330.1');
6543                !!!parse-error (type => 'after frameset:/',
6544                                text => $token->{tag_name}, token => $token);
6545              } else { # "after after html"
6546              !!!cp ('t331');              !!!cp ('t331');
6547              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6548                                text => $token->{tag_name}, token => $token);
6549            }            }
6550            ## Ignore the token            ## Ignore the token
6551            !!!next-token;            !!!next-token;
6552            redo B;            next B;
6553            }
6554          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6555            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6556                    @{$self->{open_elements}} == 1) { # redundant, maybe
6557              !!!cp ('t331.1');
6558              !!!parse-error (type => 'in body:#eof', token => $token);
6559            } else {
6560              !!!cp ('t331.2');
6561          }          }
6562            
6563            ## Stop parsing
6564            last B;
6565        } else {        } else {
6566          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6567        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6576  sub _tree_construction_main ($) {
6576        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6577          !!!cp ('t332');          !!!cp ('t332');
6578          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6579          $script_start_tag->($insert);          $script_start_tag->();
6580          redo B;          next B;
6581        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6582          !!!cp ('t333');          !!!cp ('t333');
6583          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6584          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6585          redo B;          next B;
6586        } elsif ({        } elsif ({
6587                  base => 1, link => 1,                  base => 1, link => 1,
6588                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6589          !!!cp ('t334');          !!!cp ('t334');
6590          ## 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
6591          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6592          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6593            !!!ack ('t334.1');
6594          !!!next-token;          !!!next-token;
6595          redo B;          next B;
6596        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6597          ## 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
6598          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6599          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.
6600    
6601          unless ($self->{confident}) {          unless ($self->{confident}) {
6602            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6603              !!!cp ('t335');              !!!cp ('t335');
6604                ## NOTE: Whether the encoding is supported or not is handled
6605                ## in the {change_encoding} callback.
6606              $self->{change_encoding}              $self->{change_encoding}
6607                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6608                            
6609              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6610                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6611                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6612                                           ->{has_reference});                                           ->{has_reference});
6613            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6614              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6615                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6616                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6617                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6618                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6619                !!!cp ('t336');                !!!cp ('t336');
6620                  ## NOTE: Whether the encoding is supported or not is handled
6621                  ## in the {change_encoding} callback.
6622                $self->{change_encoding}                $self->{change_encoding}
6623                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6624                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6625                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6626                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6644  sub _tree_construction_main ($) {
6644            }            }
6645          }          }
6646    
6647            !!!ack ('t338.1');
6648          !!!next-token;          !!!next-token;
6649          redo B;          next B;
6650        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6651          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6652          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6653          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6654            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6655        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6656          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6657                                
6658          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6659              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6660            !!!cp ('t342');            !!!cp ('t342');
6661            ## Ignore the token            ## Ignore the token
6662          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670              }              }
6671            }            }
6672          }          }
6673            !!!nack ('t343.1');
6674          !!!next-token;          !!!next-token;
6675          redo B;          next B;
6676        } elsif ({        } elsif ({
6677                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6678                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6679                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6680                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6681                  pre => 1,                  pre => 1, listing => 1,
6682                    form => 1,
6683                    table => 1,
6684                    hr => 1,
6685                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6686            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6687              !!!cp ('t350');
6688              !!!parse-error (type => 'in form:form', token => $token);
6689              ## Ignore the token
6690              !!!nack ('t350.1');
6691              !!!next-token;
6692              next B;
6693            }
6694    
6695          ## has a p element in scope          ## has a p element in scope
6696          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6697            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6698              !!!cp ('t344');              !!!cp ('t344');
6699              !!!back-token;              !!!back-token; # <form>
6700              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6701              redo B;                        line => $token->{line}, column => $token->{column}};
6702            } elsif ({              next B;
6703                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6704              !!!cp ('t345');              !!!cp ('t345');
6705              last INSCOPE;              last INSCOPE;
6706            }            }
6707          } # INSCOPE          } # INSCOPE
6708                        
6709          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6710          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6711              !!!nack ('t346.1');
6712            !!!next-token;            !!!next-token;
6713            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6714              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6721  sub _tree_construction_main ($) {
6721            } else {            } else {
6722              !!!cp ('t348');              !!!cp ('t348');
6723            }            }
6724          } else {          } elsif ($token->{tag_name} eq 'form') {
6725            !!!cp ('t347');            !!!cp ('t347.1');
6726              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6727    
6728              !!!nack ('t347.2');
6729            !!!next-token;            !!!next-token;
6730          }          } elsif ($token->{tag_name} eq 'table') {
6731          redo B;            !!!cp ('t382');
6732        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6733          if (defined $self->{form_element}) {            
6734            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6735            !!!parse-error (type => 'in form:form');  
6736            ## Ignore the token            !!!nack ('t382.1');
6737              !!!next-token;
6738            } elsif ($token->{tag_name} eq 'hr') {
6739              !!!cp ('t386');
6740              pop @{$self->{open_elements}};
6741            
6742              !!!nack ('t386.1');
6743            !!!next-token;            !!!next-token;
           redo B;  
6744          } else {          } else {
6745            ## 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];  
6746            !!!next-token;            !!!next-token;
           redo B;  
6747          }          }
6748        } elsif ($token->{tag_name} eq 'li') {          next B;
6749          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6750          ## has a p element in scope          ## has a p element in scope
6751          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6752            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6753              !!!cp ('t353');              !!!cp ('t353');
6754              !!!back-token;              !!!back-token; # <x>
6755              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6756              redo B;                        line => $token->{line}, column => $token->{column}};
6757            } elsif ({              next B;
6758                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6759              !!!cp ('t354');              !!!cp ('t354');
6760              last INSCOPE;              last INSCOPE;
6761            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6764  sub _tree_construction_main ($) {
6764          ## Step 1          ## Step 1
6765          my $i = -1;          my $i = -1;
6766          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6767            my $li_or_dtdd = {li => {li => 1},
6768                              dt => {dt => 1, dd => 1},
6769                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6770          LI: {          LI: {
6771            ## Step 2            ## Step 2
6772            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6773              if ($i != -1) {              if ($i != -1) {
6774                !!!cp ('t355');                !!!cp ('t355');
6775                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6776                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6777                                      ->manakai_local_name,
6778                                  token => $token);
6779              } else {              } else {
6780                !!!cp ('t356');                !!!cp ('t356');
6781              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6786  sub _tree_construction_main ($) {
6786            }            }
6787                        
6788            ## Step 3            ## Step 3
6789            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6790                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6791                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6792                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6793                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6794                  not ($node->[1] & DIV_EL)) {
6795              !!!cp ('t358');              !!!cp ('t358');
6796              last LI;              last LI;
6797            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6803  sub _tree_construction_main ($) {
6803            redo LI;            redo LI;
6804          } # LI          } # LI
6805                        
6806          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6807          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6808          !!!next-token;          !!!next-token;
6809          redo B;          next B;
6810        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6811          ## has a p element in scope          ## has a p element in scope
6812          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6813            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6814              !!!cp ('t367');              !!!cp ('t367');
6815              !!!back-token;              !!!back-token; # <plaintext>
6816              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6817              redo B;                        line => $token->{line}, column => $token->{column}};
6818            } elsif ({              next B;
6819                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6820              !!!cp ('t368');              !!!cp ('t368');
6821              last INSCOPE;              last INSCOPE;
6822            }            }
6823          } # INSCOPE          } # INSCOPE
6824                        
6825          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6826                        
6827          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6828                        
6829            !!!nack ('t368.1');
6830          !!!next-token;          !!!next-token;
6831          redo B;          next B;
6832        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6833          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6834            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6835            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6836              !!!cp ('t371');              !!!cp ('t371');
6837              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6838                            
6839              !!!back-token;              !!!back-token; # <a>
6840              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6841              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6842                $formatting_end_tag->($token);
6843                            
6844              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6845                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 6864  sub _tree_construction_main ($) {
6864                        
6865          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6866    
6867          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6868          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6869    
6870            !!!nack ('t374.1');
6871          !!!next-token;          !!!next-token;
6872          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;  
6873        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6874          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6875    
6876          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6877          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6878            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6879            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6880              !!!cp ('t376');              !!!cp ('t376');
6881              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6882              !!!back-token;              !!!back-token; # <nobr>
6883              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6884              redo B;                        line => $token->{line}, column => $token->{column}};
6885            } elsif ({              next B;
6886                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6887              !!!cp ('t377');              !!!cp ('t377');
6888              last INSCOPE;              last INSCOPE;
6889            }            }
6890          } # INSCOPE          } # INSCOPE
6891                    
6892          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6893          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6894                    
6895            !!!nack ('t377.1');
6896          !!!next-token;          !!!next-token;
6897          redo B;          next B;
6898        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6899          ## has a button element in scope          ## has a button element in scope
6900          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6901            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6902            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6903              !!!cp ('t378');              !!!cp ('t378');
6904              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6905              !!!back-token;              !!!back-token; # <button>
6906              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6907              redo B;                        line => $token->{line}, column => $token->{column}};
6908            } elsif ({              next B;
6909                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6910              !!!cp ('t379');              !!!cp ('t379');
6911              last INSCOPE;              last INSCOPE;
6912            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6914  sub _tree_construction_main ($) {
6914                        
6915          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6916                        
6917          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6918    
6919          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6920    
6921          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6922    
6923            !!!nack ('t379.1');
6924          !!!next-token;          !!!next-token;
6925          redo B;          next 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});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         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;  
             
         !!!next-token;  
         redo B;  
6926        } elsif ({        } elsif ({
6927                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6928                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6929                  image => 1,                  noembed => 1,
6930                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6931                    noscript => 0, ## TODO: 1 if scripting is enabled
6932                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6933          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6934            !!!cp ('t384');            !!!cp ('t381');
6935            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6936          } else {          } else {
6937            !!!cp ('t385');            !!!cp ('t399');
6938          }          }
6939            ## NOTE: There is an "as if in body" code clone.
6940          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6941          $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;  
6942        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6943          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6944                    
6945          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6946            !!!cp ('t389');            !!!cp ('t389');
6947            ## Ignore the token            ## Ignore the token
6948              !!!nack ('t389'); ## NOTE: Not acknowledged.
6949            !!!next-token;            !!!next-token;
6950            redo B;            next B;
6951          } else {          } else {
6952              !!!ack ('t391.1');
6953    
6954            my $at = $token->{attributes};            my $at = $token->{attributes};
6955            my $form_attrs;            my $form_attrs;
6956            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5825  sub _tree_construction_main ($) { Line 6960  sub _tree_construction_main ($) {
6960            delete $at->{prompt};            delete $at->{prompt};
6961            my @tokens = (            my @tokens = (
6962                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6963                           attributes => $form_attrs},                           attributes => $form_attrs,
6964                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6965                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6966                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6967                            {type => START_TAG_TOKEN, tag_name => 'p',
6968                             line => $token->{line}, column => $token->{column}},
6969                            {type => START_TAG_TOKEN, tag_name => 'label',
6970                             line => $token->{line}, column => $token->{column}},
6971                         );                         );
6972            if ($prompt_attr) {            if ($prompt_attr) {
6973              !!!cp ('t390');              !!!cp ('t390');
6974              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6975                               #line => $token->{line}, column => $token->{column},
6976                              };
6977            } else {            } else {
6978              !!!cp ('t391');              !!!cp ('t391');
6979              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6980                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6981                               #line => $token->{line}, column => $token->{column},
6982                              }; # SHOULD
6983              ## TODO: make this configurable              ## TODO: make this configurable
6984            }            }
6985            push @tokens,            push @tokens,
6986                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6987                             line => $token->{line}, column => $token->{column}},
6988                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6989                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6990                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6991                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6992                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6993            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6994                             line => $token->{line}, column => $token->{column}},
6995                            {type => END_TAG_TOKEN, tag_name => 'form',
6996                             line => $token->{line}, column => $token->{column}};
6997            !!!back-token (@tokens);            !!!back-token (@tokens);
6998            redo B;            !!!next-token;
6999              next B;
7000          }          }
7001        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7002          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7003          my $el;          my $el;
7004          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7005                    
7006          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7007          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 7010  sub _tree_construction_main ($) {
7010          $insert->($el);          $insert->($el);
7011                    
7012          my $text = '';          my $text = '';
7013            !!!nack ('t392.1');
7014          !!!next-token;          !!!next-token;
7015          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7016            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 7041  sub _tree_construction_main ($) {
7041            ## Ignore the token            ## Ignore the token
7042          } else {          } else {
7043            !!!cp ('t398');            !!!cp ('t398');
7044            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7045          }          }
7046          !!!next-token;          !!!next-token;
7047            next B;
7048          } elsif ($token->{tag_name} eq 'rt' or
7049                   $token->{tag_name} eq 'rp') {
7050            ## has a |ruby| element in scope
7051            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7052              my $node = $self->{open_elements}->[$_];
7053              if ($node->[1] & RUBY_EL) {
7054                !!!cp ('t398.1');
7055                ## generate implied end tags
7056                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7057                  !!!cp ('t398.2');
7058                  pop @{$self->{open_elements}};
7059                }
7060                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7061                  !!!cp ('t398.3');
7062                  !!!parse-error (type => 'not closed',
7063                                  text => $self->{open_elements}->[-1]->[0]
7064                                      ->manakai_local_name,
7065                                  token => $token);
7066                  pop @{$self->{open_elements}}
7067                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7068                }
7069                last INSCOPE;
7070              } elsif ($node->[1] & SCOPING_EL) {
7071                !!!cp ('t398.4');
7072                last INSCOPE;
7073              }
7074            } # INSCOPE
7075    
7076            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7077    
7078            !!!nack ('t398.5');
7079            !!!next-token;
7080          redo B;          redo B;
7081        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7082                  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');  
7083          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7084    
7085          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7086    
7087            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7088    
7089            ## "adjust foreign attributes" - done in insert-element-f
7090                    
7091          $self->{insertion_mode} = IN_SELECT_IM;          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7092            
7093            if ($self->{self_closing}) {
7094              pop @{$self->{open_elements}};
7095              !!!ack ('t398.1');
7096            } else {
7097              !!!cp ('t398.2');
7098              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7099              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7100              ## mode, "in body" (not "in foreign content") secondary insertion
7101              ## mode, maybe.
7102            }
7103    
7104          !!!next-token;          !!!next-token;
7105          redo B;          next B;
7106        } elsif ({        } elsif ({
7107                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7108                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 7110  sub _tree_construction_main ($) {
7110                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7111                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7112          !!!cp ('t401');          !!!cp ('t401');
7113          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7114                            text => $token->{tag_name}, token => $token);
7115          ## Ignore the token          ## Ignore the token
7116            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7117          !!!next-token;          !!!next-token;
7118          redo B;          next B;
7119                    
7120          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7121        } else {        } else {
7122          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7123              !!!cp ('t384');
7124              !!!parse-error (type => 'image', token => $token);
7125              $token->{tag_name} = 'img';
7126            } else {
7127              !!!cp ('t385');
7128            }
7129    
7130            ## NOTE: There is an "as if <br>" code clone.
7131          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7132                    
7133          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7134    
7135            if ({
7136                 applet => 1, marquee => 1, object => 1,
7137                }->{$token->{tag_name}}) {
7138              !!!cp ('t380');
7139              push @$active_formatting_elements, ['#marker', ''];
7140              !!!nack ('t380.1');
7141            } elsif ({
7142                      b => 1, big => 1, em => 1, font => 1, i => 1,
7143                      s => 1, small => 1, strile => 1,
7144                      strong => 1, tt => 1, u => 1,
7145                     }->{$token->{tag_name}}) {
7146              !!!cp ('t375');
7147              push @$active_formatting_elements, $self->{open_elements}->[-1];
7148              !!!nack ('t375.1');
7149            } elsif ($token->{tag_name} eq 'input') {
7150              !!!cp ('t388');
7151              ## TODO: associate with $self->{form_element} if defined
7152              pop @{$self->{open_elements}};
7153              !!!ack ('t388.2');
7154            } elsif ({
7155                      area => 1, basefont => 1, bgsound => 1, br => 1,
7156                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7157                      #image => 1,
7158                     }->{$token->{tag_name}}) {
7159              !!!cp ('t388.1');
7160              pop @{$self->{open_elements}};
7161              !!!ack ('t388.3');
7162            } elsif ($token->{tag_name} eq 'select') {
7163              ## TODO: associate with $self->{form_element} if defined
7164            
7165              if ($self->{insertion_mode} & TABLE_IMS or
7166                  $self->{insertion_mode} & BODY_TABLE_IMS or
7167                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7168                !!!cp ('t400.1');
7169                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7170              } else {
7171                !!!cp ('t400.2');
7172                $self->{insertion_mode} = IN_SELECT_IM;
7173              }
7174              !!!nack ('t400.3');
7175            } else {
7176              !!!nack ('t402');
7177            }
7178                    
7179          !!!next-token;          !!!next-token;
7180          redo B;          next B;
7181        }        }
7182      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7183        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7184          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7185              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7186            for (@{$self->{open_elements}}) {          INSCOPE: {
7187              unless ({            for (reverse @{$self->{open_elements}}) {
7188                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7189                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7190                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7191                      }->{$_->[1]}) {                last INSCOPE;
7192                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7193                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7194              } else {                last;
               !!!cp ('t404');  
7195              }              }
7196            }            }
7197    
7198            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7199                              text => $token->{tag_name}, token => $token);
7200              ## NOTE: Ignore the token.
7201            !!!next-token;            !!!next-token;
7202            redo B;            next B;
7203          } else {          } # INSCOPE
7204            !!!cp ('t405');  
7205            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7206            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7207            !!!next-token;              !!!cp ('t403');
7208            redo B;              !!!parse-error (type => 'not closed',
7209                                text => $_->[0]->manakai_local_name,
7210                                token => $token);
7211                last;
7212              } else {
7213                !!!cp ('t404');
7214              }
7215          }          }
7216    
7217            $self->{insertion_mode} = AFTER_BODY_IM;
7218            !!!next-token;
7219            next B;
7220        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7221          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
7222            ## up-to-date, though it has same effect as speced.
7223            if (@{$self->{open_elements}} > 1 and
7224                $self->{open_elements}->[1]->[1] & BODY_EL) {
7225            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7226            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7227              !!!cp ('t406');              !!!cp ('t406');
7228              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7229                                text => $self->{open_elements}->[1]->[0]
7230                                    ->manakai_local_name,
7231                                token => $token);
7232            } else {            } else {
7233              !!!cp ('t407');              !!!cp ('t407');
7234            }            }
7235            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7236            ## reprocess            ## reprocess
7237            redo B;            next B;
7238          } else {          } else {
7239            !!!cp ('t408');            !!!cp ('t408');
7240            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7241                              text => $token->{tag_name}, token => $token);
7242            ## Ignore the token            ## Ignore the token
7243            !!!next-token;            !!!next-token;
7244            redo B;            next B;
7245          }          }
7246        } elsif ({        } elsif ({
7247                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7248                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7249                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7250                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7251                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7252                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7253          ## has an element in scope          ## has an element in scope
7254          my $i;          my $i;
7255          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7256            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7257            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => ($token->{tag_name} ne 'dd'),  
                     dt => ($token->{tag_name} ne 'dt'),  
                     li => ($token->{tag_name} ne 'li'),  
                     p => ($token->{tag_name} ne 'p'),  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               pop @{$self->{open_elements}};  
             }  
               
7258              !!!cp ('t410');              !!!cp ('t410');
7259              $i = $_;              $i = $_;
7260              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7261            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7262              !!!cp ('t411');              !!!cp ('t411');
7263              last INSCOPE;              last INSCOPE;
7264            }            }
7265          } # INSCOPE          } # INSCOPE
7266            
7267          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7268            if (defined $i) {            !!!cp ('t413');
7269              !!!parse-error (type => 'unmatched end tag',
7270                              text => $token->{tag_name}, token => $token);
7271              ## NOTE: Ignore the token.
7272            } else {
7273              ## Step 1. generate implied end tags
7274              while ({
7275                      ## END_TAG_OPTIONAL_EL
7276                      dd => ($token->{tag_name} ne 'dd'),
7277                      dt => ($token->{tag_name} ne 'dt'),
7278                      li => ($token->{tag_name} ne 'li'),
7279                      p => 1,
7280                      rt => 1,
7281                      rp => 1,
7282                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7283                !!!cp ('t409');
7284                pop @{$self->{open_elements}};
7285              }
7286    
7287              ## Step 2.
7288              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7289                      ne $token->{tag_name}) {
7290              !!!cp ('t412');              !!!cp ('t412');
7291              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7292                                text => $self->{open_elements}->[-1]->[0]
7293                                    ->manakai_local_name,
7294                                token => $token);
7295            } else {            } else {
7296              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7297            }            }
7298          }  
7299                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7300            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7301          } elsif ($token->{tag_name} eq 'p') {  
7302            !!!cp ('t415');            ## Step 4.
7303            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7304            my $el;                if {
7305            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7306            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7307          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7308          !!!next-token;          !!!next-token;
7309          redo B;          next B;
7310        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7311            undef $self->{form_element};
7312    
7313          ## has an element in scope          ## has an element in scope
7314            my $i;
7315          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7316            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7317            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
7318              !!!cp ('t418');              !!!cp ('t418');
7319                $i = $_;
7320              last INSCOPE;              last INSCOPE;
7321            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7322              !!!cp ('t419');              !!!cp ('t419');
7323              last INSCOPE;              last INSCOPE;
7324            }            }
7325          } # INSCOPE          } # INSCOPE
7326            
7327          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 {  
7328            !!!cp ('t421');            !!!cp ('t421');
7329            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7330                              text => $token->{tag_name}, token => $token);
7331              ## NOTE: Ignore the token.
7332            } else {
7333              ## Step 1. generate implied end tags
7334              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7335                !!!cp ('t417');
7336                pop @{$self->{open_elements}};
7337              }
7338              
7339              ## Step 2.
7340              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7341                      ne $token->{tag_name}) {
7342                !!!cp ('t417.1');
7343                !!!parse-error (type => 'not closed',
7344                                text => $self->{open_elements}->[-1]->[0]
7345                                    ->manakai_local_name,
7346                                token => $token);
7347              } else {
7348                !!!cp ('t420');
7349              }  
7350              
7351              ## Step 3.
7352              splice @{$self->{open_elements}}, $i;
7353          }          }
7354    
         undef $self->{form_element};  
7355          !!!next-token;          !!!next-token;
7356          redo B;          next B;
7357        } elsif ({        } elsif ({
7358                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7359                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6091  sub _tree_construction_main ($) { Line 7361  sub _tree_construction_main ($) {
7361          my $i;          my $i;
7362          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7363            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7364            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
7365              !!!cp ('t423');              !!!cp ('t423');
7366              $i = $_;              $i = $_;
7367              last INSCOPE;              last INSCOPE;
7368            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7369              !!!cp ('t424');              !!!cp ('t424');
7370              last INSCOPE;              last INSCOPE;
7371            }            }
7372          } # INSCOPE          } # INSCOPE
7373    
7374            unless (defined $i) { # has an element in scope
7375              !!!cp ('t425.1');
7376              !!!parse-error (type => 'unmatched end tag',
7377                              text => $token->{tag_name}, token => $token);
7378              ## NOTE: Ignore the token.
7379            } else {
7380              ## Step 1. generate implied end tags
7381              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7382                !!!cp ('t422');
7383                pop @{$self->{open_elements}};
7384              }
7385              
7386              ## Step 2.
7387              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7388                      ne $token->{tag_name}) {
7389                !!!cp ('t425');
7390                !!!parse-error (type => 'unmatched end tag',
7391                                text => $token->{tag_name}, token => $token);
7392              } else {
7393                !!!cp ('t426');
7394              }
7395    
7396              ## Step 3.
7397              splice @{$self->{open_elements}}, $i;
7398            }
7399                    
7400          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7401            !!!cp ('t425');          next B;
7402            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7403            ## has an element in scope
7404            my $i;
7405            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7406              my $node = $self->{open_elements}->[$_];
7407              if ($node->[1] & P_EL) {
7408                !!!cp ('t410.1');
7409                $i = $_;
7410                last INSCOPE;
7411              } elsif ($node->[1] & SCOPING_EL) {
7412                !!!cp ('t411.1');
7413                last INSCOPE;
7414              }
7415            } # INSCOPE
7416    
7417            if (defined $i) {
7418              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7419                      ne $token->{tag_name}) {
7420                !!!cp ('t412.1');
7421                !!!parse-error (type => 'not closed',
7422                                text => $self->{open_elements}->[-1]->[0]
7423                                    ->manakai_local_name,
7424                                token => $token);
7425              } else {
7426                !!!cp ('t414.1');
7427              }
7428    
7429              splice @{$self->{open_elements}}, $i;
7430          } else {          } else {
7431            !!!cp ('t426');            !!!cp ('t413.1');
7432              !!!parse-error (type => 'unmatched end tag',
7433                              text => $token->{tag_name}, token => $token);
7434    
7435              !!!cp ('t415.1');
7436              ## As if <p>, then reprocess the current token
7437              my $el;
7438              !!!create-element ($el, $HTML_NS, 'p',, $token);
7439              $insert->($el);
7440              ## NOTE: Not inserted into |$self->{open_elements}|.
7441          }          }
7442            
         splice @{$self->{open_elements}}, $i if defined $i;  
7443          !!!next-token;          !!!next-token;
7444          redo B;          next B;
7445        } elsif ({        } elsif ({
7446                  a => 1,                  a => 1,
7447                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6131  sub _tree_construction_main ($) { Line 7449  sub _tree_construction_main ($) {
7449                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7450                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7451          !!!cp ('t427');          !!!cp ('t427');
7452          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7453          redo B;          next B;
7454        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7455          !!!cp ('t428');          !!!cp ('t428');
7456          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7457                            text => 'br', token => $token);
7458    
7459          ## As if <br>          ## As if <br>
7460          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7461                    
7462          my $el;          my $el;
7463          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7464          $insert->($el);          $insert->($el);
7465                    
7466          ## Ignore the token.          ## Ignore the token.
7467          !!!next-token;          !!!next-token;
7468          redo B;          next B;
7469        } elsif ({        } elsif ({
7470                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7471                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6160  sub _tree_construction_main ($) { Line 7479  sub _tree_construction_main ($) {
7479                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7480                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7481          !!!cp ('t429');          !!!cp ('t429');
7482          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7483                            text => $token->{tag_name}, token => $token);
7484          ## Ignore the token          ## Ignore the token
7485          !!!next-token;          !!!next-token;
7486          redo B;          next B;
7487                    
7488          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7489                    
# Line 6174  sub _tree_construction_main ($) { Line 7494  sub _tree_construction_main ($) {
7494    
7495          ## Step 2          ## Step 2
7496          S2: {          S2: {
7497            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7498              ## Step 1              ## Step 1
7499              ## generate implied end tags              ## generate implied end tags
7500              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7501                !!!cp ('t430');                !!!cp ('t430');
7502                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7503                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7504                  ## which seems wrong.
7505                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7506                  $node_i++;
7507              }              }
7508                    
7509              ## Step 2              ## Step 2
7510              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7511                        ne $token->{tag_name}) {
7512                !!!cp ('t431');                !!!cp ('t431');
7513                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7514                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7515                                  text => $self->{open_elements}->[-1]->[0]
7516                                      ->manakai_local_name,
7517                                  token => $token);
7518              } else {              } else {
7519                !!!cp ('t432');                !!!cp ('t432');
7520              }              }
7521                            
7522              ## Step 3              ## Step 3
7523              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7524    
7525              !!!next-token;              !!!next-token;
7526              last S2;              last S2;
7527            } else {            } else {
7528              ## Step 3              ## Step 3
7529              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7530                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7531                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7532                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7533                !!!cp ('t433');                !!!cp ('t433');
7534                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7535                                  text => $token->{tag_name}, token => $token);
7536                ## Ignore the token                ## Ignore the token
7537                !!!next-token;                !!!next-token;
7538                last S2;                last S2;
# Line 6222  sub _tree_construction_main ($) { Line 7548  sub _tree_construction_main ($) {
7548            ## Step 5;            ## Step 5;
7549            redo S2;            redo S2;
7550          } # S2          } # S2
7551          redo B;          next B;
7552        }        }
7553      }      }
7554      redo B;      next B;
7555      } continue { # B
7556        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7557          ## NOTE: The code below is executed in cases where it does not have
7558          ## to be, but it it is harmless even in those cases.
7559          ## has an element in scope
7560          INSCOPE: {
7561            for (reverse 0..$#{$self->{open_elements}}) {
7562              my $node = $self->{open_elements}->[$_];
7563              if ($node->[1] & FOREIGN_EL) {
7564                last INSCOPE;
7565              } elsif ($node->[1] & SCOPING_EL) {
7566                last;
7567              }
7568            }
7569            
7570            ## NOTE: No foreign element in scope.
7571            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7572          } # INSCOPE
7573        }
7574    } # B    } # B
7575    
7576    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6233  sub _tree_construction_main ($) { Line 7578  sub _tree_construction_main ($) {
7578    ## TODO: script stuffs    ## TODO: script stuffs
7579  } # _tree_construct_main  } # _tree_construct_main
7580    
7581  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7582    my $class = shift;    my $class = shift;
7583    my $node = shift;    my $node = shift;
7584    my $s = \$_[0];    my $s = \$_[0];
7585    my $onerror = $_[1];    my $onerror = $_[1];
7586      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7587    
7588    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7589    
# Line 6256  sub set_inner_html ($$$) { Line 7602  sub set_inner_html ($$$) {
7602      }      }
7603    
7604      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7605      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7606    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7607      ## TODO: If non-html element      ## TODO: If non-html element
7608    
7609      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7610    
7611    ## TODO: Support for $get_wrapper
7612    
7613      ## Step 1 # MUST      ## Step 1 # MUST
7614      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7615      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6271  sub set_inner_html ($$$) { Line 7619  sub set_inner_html ($$$) {
7619    
7620      ## Step 8 # MUST      ## Step 8 # MUST
7621      my $i = 0;      my $i = 0;
7622      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7623      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7624      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7625        my $self = shift;        my $self = shift;
7626    
# Line 6281  sub set_inner_html ($$$) { Line 7629  sub set_inner_html ($$$) {
7629    
7630        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7631        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7632        $column++;  
7633          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7634          $p->{column}++;
7635    
7636        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7637          $line++;          $p->{line}++;
7638          $column = 0;          $p->{column} = 0;
7639          !!!cp ('i1');          !!!cp ('i1');
7640        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7641          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7642          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7643          $line++;          $p->{line}++;
7644          $column = 0;          $p->{column} = 0;
7645          !!!cp ('i2');          !!!cp ('i2');
7646        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7647          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6300  sub set_inner_html ($$$) { Line 7650  sub set_inner_html ($$$) {
7650          !!!cp ('i4');          !!!cp ('i4');
7651          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7652          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7653          } elsif ($self->{next_char} <= 0x0008 or
7654                   (0x000E <= $self->{next_char} and
7655                    $self->{next_char} <= 0x001F) or
7656                   (0x007F <= $self->{next_char} and
7657                    $self->{next_char} <= 0x009F) or
7658                   (0xD800 <= $self->{next_char} and
7659                    $self->{next_char} <= 0xDFFF) or
7660                   (0xFDD0 <= $self->{next_char} and
7661                    $self->{next_char} <= 0xFDDF) or
7662                   {
7663                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7664                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7665                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7666                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7667                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7668                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7669                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7670                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7671                    0x10FFFE => 1, 0x10FFFF => 1,
7672                   }->{$self->{next_char}}) {
7673            !!!cp ('i4.1');
7674            if ($self->{next_char} < 0x10000) {
7675              !!!parse-error (type => 'control char',
7676                              text => (sprintf 'U+%04X', $self->{next_char}));
7677            } else {
7678              !!!parse-error (type => 'control char',
7679                              text => (sprintf 'U-%08X', $self->{next_char}));
7680            }
7681        }        }
7682      };      };
7683      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6307  sub set_inner_html ($$$) { Line 7685  sub set_inner_html ($$$) {
7685            
7686      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7687        my (%opt) = @_;        my (%opt) = @_;
7688        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7689          my $column = $opt{column};
7690          if (defined $opt{token} and defined $opt{token}->{line}) {
7691            $line = $opt{token}->{line};
7692            $column = $opt{token}->{column};
7693          }
7694          warn "Parse error ($opt{type}) at line $line column $column\n";
7695      };      };
7696      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7697        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7698      };      };
7699            
7700      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6334  sub set_inner_html ($$$) { Line 7718  sub set_inner_html ($$$) {
7718          unless defined $p->{content_model};          unless defined $p->{content_model};
7719          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7720    
7721      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7722          ## TODO: Foreign element OK?
7723    
7724      ## Step 3      ## Step 3
7725      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 7729  sub set_inner_html ($$$) {
7729      $doc->append_child ($root);      $doc->append_child ($root);
7730    
7731      ## Step 5 # MUST      ## Step 5 # MUST
7732      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7733    
7734      undef $p->{head_element};      undef $p->{head_element};
7735    
# Line 6390  sub set_inner_html ($$$) { Line 7775  sub set_inner_html ($$$) {
7775      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7776    
7777      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7778    
7779        delete $p->{parse_error}; # delete loop
7780    } else {    } else {
7781      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";
7782    }    }

Legend:
Removed from v.1.86  
changed lines
  Added in v.1.167

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24