/[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.116 by wakaba, Mon Mar 17 13:23:39 2008 UTC revision 1.173 by wakaba, Sun Sep 14 03:59:08 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311  };  };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $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];
362    my $s;  
363        my $onerror = $_[2] || sub {
364    if (defined $charset) {      my (%opt) = @_;
365      require Encode; ## TODO: decode(utf8) don't delete BOM      warn "Parse error ($opt{type})\n";
366      $s = \ (Encode::decode ($charset, $$bytes_s));    };
367      $self->{input_encoding} = lc $charset; ## TODO: normalize name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368      $self->{confident} = 1;  
369    } else {    my $get_wrapper = $_[3] || sub ($) {
370      ## TODO: Implement HTML5 detection algorithm      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## 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
406        my $byte_buffer = '';
407        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;
420          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        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
445      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          (substr ($$bytes_s, 0, 1024));          ($byte_buffer);
447      $charset ||= 'windows-1252';      if (defined $charset_name) {
448      $s = \ (Encode::decode ($charset, $$bytes_s));        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449      $self->{input_encoding} = $charset;  
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;      $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 {    $self->{change_encoding} = sub {
511      my $self = shift;      my $self = shift;
512      my $charset = lc shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
     ## TODO: if $charset is supported  
     ## TODO: normalize charset name  
514    
515      ## "Change the encoding" algorithm:      $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            ## Step 1    
524      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
525        $charset = 'utf-8';            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      ## Step 2        !!!parse-error (type => 'charset label detected',
544      if (defined $self->{input_encoding} and                        text => $self->{input_encoding},
545          $self->{input_encoding} eq $charset) {                        value => $charset_name,
546        $self->{confident} = 1;                        level => $self->{level}->{warn},
547        return;                        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}
560    
561      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.    my $char_onerror = sub {
562          ':'.$charset, level => 'w', token => $token);      my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564      ## Step 3                      %opt, type => $type,
565      # if (can) {                      line => $self->{line}, column => $self->{column} + 1);
566        ## change the encoding on the fly.      if ($opt{octets}) {
567        #$self->{confident} = 1;        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        #return;      }
569      # }    };
570    
571      ## Step 4    my $wrapped_char_stream = $get_wrapper->($char_stream);
572      throw Whatpm::HTML::RestartParser (charset => $charset);    $wrapped_char_stream->onerror ($char_onerror);
   }; # $self->{change_encoding}  
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 163  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      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($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 186  sub parse_string ($$$;$) { Line 649  sub parse_string ($$$;$) {
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          $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})      ($self->{line_prev}, $self->{column_prev})
663          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
664      $self->{column}++;      $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667          !!!cp ('j1');
668        $self->{line}++;        $self->{line}++;
669        $self->{column} = 0;        $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    ## TODO: support for abort/streaming
673          my $next = $input->getc;
674          if (defined $next and $next ne "\x0A") {
675            $self->{next_next_char} = $next;
676          }
677        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
678        $self->{line}++;        $self->{line}++;
679        $self->{column} = 0;        $self->{column} = 0;
680      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
681          !!!cp ('j3');
682        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
683      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
684          !!!cp ('j4');
685        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
686        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
687        } elsif ($self->{next_char} <= 0x0008 or
688                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
689                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
690                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
691                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
692    ## ISSUE: U+FDE0-U+FDEF are not excluded
693                 {
694                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
695                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
696                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
697                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
698                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
699                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
700                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
701                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
702                  0x10FFFE => 1, 0x10FFFF => 1,
703                 }->{$self->{next_char}}) {
704          !!!cp ('j5');
705          if ($self->{next_char} < 0x10000) {
706            !!!parse-error (type => 'control char',
707                            text => (sprintf 'U+%04X', $self->{next_char}));
708          } else {
709            !!!parse-error (type => 'control char',
710                            text => (sprintf 'U-%08X', $self->{next_char}));
711          }
712      }      }
713    };    };
714    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
715    $self->{next_char} = -1;    $self->{next_char} = -1;
716    
717      $self->{read_until} = sub {
718        #my ($scalar, $specials_range, $offset) = @_;
719        my $specials_range = $_[1];
720        return 0 if defined $self->{next_next_char};
721        my $count = $input->manakai_read_until
722           ($_[0],
723            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
724            $_[2]);
725        if ($count) {
726          $self->{column} += $count;
727          $self->{column_prev} += $count;
728          $self->{prev_char} = [-1, -1, -1];
729          $self->{next_char} = -1;
730        }
731        return $count;
732      }; # $self->{read_until}
733    
734    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
735      my (%opt) = @_;      my (%opt) = @_;
736      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
# Line 229  sub parse_string ($$$;$) { Line 749  sub parse_string ($$$;$) {
749    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
750    
751    return $self->{document};    return $self->{document};
752  } # parse_string  } # parse_char_stream
753    
754  sub new ($) {  sub new ($) {
755    my $class = shift;    my $class = shift;
756    my $self = bless {}, $class;    my $self = bless {
757        level => {must => 'm',
758                  should => 's',
759                  warn => 'w',
760                  info => 'i',
761                  uncertain => 'u'},
762      }, $class;
763    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
764      $self->{next_char} = -1;      $self->{next_char} = -1;
765    };    };
# Line 262  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 788  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
788  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
789    
790  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
791  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
792  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
793  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
794  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 273  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 799  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
799  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
800  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
801  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
802  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
803  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
804  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
805  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 295  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 821  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
821  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
822  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
823  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
824    sub SELF_CLOSING_START_TAG_STATE () { 34 }
825    sub CDATA_SECTION_STATE () { 35 }
826    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
827    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
828    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
829    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
830    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
831    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
832    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
833    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
834    ## NOTE: "Entity data state", "entity in attribute value state", and
835    ## "consume a character reference" algorithm are jointly implemented
836    ## using the following six states:
837    sub ENTITY_STATE () { 44 }
838    sub ENTITY_HASH_STATE () { 45 }
839    sub NCR_NUM_STATE () { 46 }
840    sub HEXREF_X_STATE () { 47 }
841    sub HEXREF_HEX_STATE () { 48 }
842    sub ENTITY_NAME_STATE () { 49 }
843    
844  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
845  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 312  sub ROW_IMS ()        { 0b10000000 } Line 857  sub ROW_IMS ()        { 0b10000000 }
857  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
858  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
859  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
860    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
861        ## NOTE: "in foreign content" insertion mode is special; it is combined
862        ## with the secondary insertion mode.  In this parser, they are stored
863        ## together in the bit-or'ed form.
864    
865  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
866    
# Line 343  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 892  sub IN_COLUMN_GROUP_IM () { 0b10 }
892  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
893    my $self = shift;    my $self = shift;
894    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
895      #$self->{state_keyword}; # initialized when used
896      #$self->{entity__value}; # initialized when used
897      #$self->{entity__match}; # initialized when used
898    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
899    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
900    undef $self->{current_attribute};    undef $self->{current_attribute};
901    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
902    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
903    $self->{char} = [];    delete $self->{self_closing};
904    # $self->{next_char}    # $self->{next_char}
905    !!!next-input-character;    !!!next-input-character;
906    $self->{token} = [];    $self->{token} = [];
# Line 368  sub _initialize_tokenizer ($) { Line 920  sub _initialize_tokenizer ($) {
920  ##        ->{value}  ##        ->{value}
921  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
922  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
923    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
924    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
925    ##     while the token is pushed back to the stack.
926    
927  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
928    
# Line 377  sub _initialize_tokenizer ($) { Line 932  sub _initialize_tokenizer ($) {
932  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
933  ## and removed from the list.  ## and removed from the list.
934    
935  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
936  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
937    
938  sub _get_next_token ($) {  sub _get_next_token ($) {
939    my $self = shift;    my $self = shift;
940    
941      if ($self->{self_closing}) {
942        !!!parse-error (type => 'nestc', token => $self->{current_token});
943        ## NOTE: The |self_closing| flag is only set by start tag token.
944        ## In addition, when a start tag token is emitted, it is always set to
945        ## |current_token|.
946        delete $self->{self_closing};
947      }
948    
949    if (@{$self->{token}}) {    if (@{$self->{token}}) {
950        $self->{self_closing} = $self->{token}->[0]->{self_closing};
951      return shift @{$self->{token}};      return shift @{$self->{token}};
952    }    }
953    
# Line 404  sub _get_next_token ($) { Line 957  sub _get_next_token ($) {
957          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
958              not $self->{escape}) {              not $self->{escape}) {
959            !!!cp (1);            !!!cp (1);
960            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
961              ## "entity data state".  In this implementation, the tokenizer
962              ## is switched to the |ENTITY_STATE|, which is an implementation
963              ## of the "consume a character reference" algorithm.
964              $self->{entity_additional} = -1;
965              $self->{prev_state} = DATA_STATE;
966              $self->{state} = ENTITY_STATE;
967            !!!next-input-character;            !!!next-input-character;
968            redo A;            redo A;
969          } else {          } else {
# Line 466  sub _get_next_token ($) { Line 1025  sub _get_next_token ($) {
1025        # Anything else        # Anything else
1026        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1027                     data => chr $self->{next_char},                     data => chr $self->{next_char},
1028                     line => $self->{line}, column => $self->{column}};                     line => $self->{line}, column => $self->{column},
1029                      };
1030          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1031    
1032        ## Stay in the data state        ## Stay in the data state
1033        !!!next-input-character;        !!!next-input-character;
1034    
1035        !!!emit ($token);        !!!emit ($token);
1036    
1037        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev});  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&',  
                   line => $l, column => $c});  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1038      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1039        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1040          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 507  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049    
1050            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1051                      line => $self->{line_prev},                      line => $self->{line_prev},
1052                      column => $self->{column_prev}});                      column => $self->{column_prev},
1053                       });
1054    
1055            redo A;            redo A;
1056          }          }
# Line 553  sub _get_next_token ($) { Line 1096  sub _get_next_token ($) {
1096    
1097            !!!emit ({type => CHARACTER_TOKEN, data => '<>',            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1098                      line => $self->{line_prev},                      line => $self->{line_prev},
1099                      column => $self->{column_prev}});                      column => $self->{column_prev},
1100                       });
1101    
1102            redo A;            redo A;
1103          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
# Line 564  sub _get_next_token ($) { Line 1108  sub _get_next_token ($) {
1108            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1109            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1110                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1111                                      column => $self->{column_prev}};                                      column => $self->{column_prev},
1112                                       };
1113            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1114            redo A;            redo A;
1115          } else {          } else {
1116            !!!cp (23);            !!!cp (23);
1117            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1118                              line => $self->{line_prev},
1119                              column => $self->{column_prev});
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            ## reconsume            ## reconsume
1122    
1123            !!!emit ({type => CHARACTER_TOKEN, data => '<',            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1124                      line => $self->{line_prev},                      line => $self->{line_prev},
1125                      column => $self->{column_prev}});                      column => $self->{column_prev},
1126                       });
1127    
1128            redo A;            redo A;
1129          }          }
# Line 583  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1132        }        }
1133      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1134          ## NOTE: The "close tag open state" in the spec is implemented as
1135          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1136    
1137        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1138        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1139          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1140              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1141            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1142            my @next_char;            ## Reconsume.
1143            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            redo A;
             push @next_char, $self->{next_char};  
             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 => '</',  
                         line => $l, column => $c});  
     
               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 => '</',  
                       line => $l, column => $c});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1144          } else {          } else {
1145            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1146              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1147            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1148            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1149              ## Reconsume.
1150            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1151                      line => $l, column => $c});                      line => $l, column => $c,
1152                       });
1153            redo A;            redo A;
1154          }          }
1155        }        }
1156          
1157        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1158            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1159          !!!cp (29);          !!!cp (29);
# Line 677  sub _get_next_token ($) { Line 1188  sub _get_next_token ($) {
1188          # reconsume          # reconsume
1189    
1190          !!!emit ({type => CHARACTER_TOKEN, data => '</',          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1191                    line => $l, column => $c});                    line => $l, column => $c,
1192                     });
1193    
1194          redo A;          redo A;
1195        } else {        } else {
# Line 686  sub _get_next_token ($) { Line 1198  sub _get_next_token ($) {
1198          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1199          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1200                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1201                                    column => $self->{column_prev} - 1};                                    column => $self->{column_prev} - 1,
1202          ## $self->{next_char} is intentionally left as is                                   };
1203          redo A;          ## NOTE: $self->{next_char} is intentionally left as is.
1204            ## Although the "anything else" case of the spec not explicitly
1205            ## states that the next input character is to be reconsumed,
1206            ## it will be included to the |data| of the comment token
1207            ## generated from the bogus end tag, as defined in the
1208            ## "bogus comment state" entry.
1209            redo A;
1210          }
1211        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1212          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1213          if (length $ch) {
1214            my $CH = $ch;
1215            $ch =~ tr/a-z/A-Z/;
1216            my $nch = chr $self->{next_char};
1217            if ($nch eq $ch or $nch eq $CH) {
1218              !!!cp (24);
1219              ## Stay in the state.
1220              $self->{state_keyword} .= $nch;
1221              !!!next-input-character;
1222              redo A;
1223            } else {
1224              !!!cp (25);
1225              $self->{state} = DATA_STATE;
1226              ## Reconsume.
1227              !!!emit ({type => CHARACTER_TOKEN,
1228                        data => '</' . $self->{state_keyword},
1229                        line => $self->{line_prev},
1230                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1231                       });
1232              redo A;
1233            }
1234          } else { # after "<{tag-name}"
1235            unless ({
1236                     0x0009 => 1, # HT
1237                     0x000A => 1, # LF
1238                     0x000B => 1, # VT
1239                     0x000C => 1, # FF
1240                     0x0020 => 1, # SP
1241                     0x003E => 1, # >
1242                     0x002F => 1, # /
1243                     -1 => 1, # EOF
1244                    }->{$self->{next_char}}) {
1245              !!!cp (26);
1246              ## Reconsume.
1247              $self->{state} = DATA_STATE;
1248              !!!emit ({type => CHARACTER_TOKEN,
1249                        data => '</' . $self->{state_keyword},
1250                        line => $self->{line_prev},
1251                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1252                       });
1253              redo A;
1254            } else {
1255              !!!cp (27);
1256              $self->{current_token}
1257                  = {type => END_TAG_TOKEN,
1258                     tag_name => $self->{last_emitted_start_tag_name},
1259                     line => $self->{line_prev},
1260                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1261              $self->{state} = TAG_NAME_STATE;
1262              ## Reconsume.
1263              redo A;
1264            }
1265        }        }
1266      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1267        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 703  sub _get_next_token ($) { Line 1276  sub _get_next_token ($) {
1276        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1277          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1278            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1279            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1280          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1281            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 736  sub _get_next_token ($) { Line 1307  sub _get_next_token ($) {
1307          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1308          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1309            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1310            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1311          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1312            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 758  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327    
1328          redo A;          redo A;
1329        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1330            !!!cp (42);
1331            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1332          !!!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  
1333          redo A;          redo A;
1334        } else {        } else {
1335          !!!cp (44);          !!!cp (44);
# Line 793  sub _get_next_token ($) { Line 1352  sub _get_next_token ($) {
1352        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 816  sub _get_next_token ($) { Line 1373  sub _get_next_token ($) {
1373        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1374                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1375          !!!cp (49);          !!!cp (49);
1376          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1377                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1378                   value => '',
1379                   line => $self->{line}, column => $self->{column}};
1380          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (50);
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!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  
1387          redo A;          redo A;
1388        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1391            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1392            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1393          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1394            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 871  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          } else {          } else {
1419            !!!cp (56);            !!!cp (56);
1420          }          }
1421          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1422                                value => ''};              = {name => chr ($self->{next_char}),
1423                   value => '',
1424                   line => $self->{line}, column => $self->{column}};
1425          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1426          !!!next-input-character;          !!!next-input-character;
1427          redo A;          redo A;
# Line 882  sub _get_next_token ($) { Line 1431  sub _get_next_token ($) {
1431          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1432              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1433            !!!cp (57);            !!!cp (57);
1434            !!!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});
1435            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1436          } else {          } else {
1437            !!!cp (58);            !!!cp (58);
# Line 911  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460          $before_leave->();          $before_leave->();
1461          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1462            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1463            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1464          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1465            !!!cp (62);            !!!cp (62);
# Line 937  sub _get_next_token ($) { Line 1484  sub _get_next_token ($) {
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1487            !!!cp (64);
1488          $before_leave->();          $before_leave->();
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          $before_leave->();          $before_leave->();
1495          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1496            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1498          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1499            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1009  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1033  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1567                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1568          !!!cp (76);          !!!cp (76);
1569          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1570                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1571                   value => '',
1572                   line => $self->{line}, column => $self->{column}};
1573          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1577            !!!cp (77);
1578            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1579          !!!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  
1580          redo A;          redo A;
1581        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1582          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1583          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1584            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1585            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1586          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1587            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1080  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602    
1603          redo A;          redo A;
1604        } else {        } else {
1605          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1606          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1607                                value => ''};            !!!cp (78);
1608              !!!parse-error (type => 'bad attribute name');
1609            } else {
1610              !!!cp (82);
1611            }
1612            $self->{current_attribute}
1613                = {name => chr ($self->{next_char}),
1614                   value => '',
1615                   line => $self->{line}, column => $self->{column}};
1616          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;                  redo A;        
# Line 1113  sub _get_next_token ($) { Line 1643  sub _get_next_token ($) {
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1646            !!!parse-error (type => 'empty unquoted attribute value');
1647          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1648            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1649            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1650          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1651            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1140  sub _get_next_token ($) { Line 1669  sub _get_next_token ($) {
1669          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1670          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1671            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1672            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1673          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1674            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1708  sub _get_next_token ($) {
1708          redo A;          redo A;
1709        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1710          !!!cp (96);          !!!cp (96);
1711          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1712          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1713            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1714            ## implementation of the "consume a character reference" algorithm.
1715            $self->{prev_state} = $self->{state};
1716            $self->{entity_additional} = 0x0022; # "
1717            $self->{state} = ENTITY_STATE;
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1721          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1722          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1213  sub _get_next_token ($) { Line 1743  sub _get_next_token ($) {
1743        } else {        } else {
1744          !!!cp (100);          !!!cp (100);
1745          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1746            $self->{read_until}->($self->{current_attribute}->{value},
1747                                  q["&],
1748                                  length $self->{current_attribute}->{value});
1749    
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
# Line 1225  sub _get_next_token ($) { Line 1759  sub _get_next_token ($) {
1759          redo A;          redo A;
1760        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1761          !!!cp (102);          !!!cp (102);
1762          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1763          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1764            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1765            ## implementation of the "consume a character reference" algorithm.
1766            $self->{entity_additional} = 0x0027; # '
1767            $self->{prev_state} = $self->{state};
1768            $self->{state} = ENTITY_STATE;
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1772          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1773          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1774            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1776          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1777            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1257  sub _get_next_token ($) { Line 1794  sub _get_next_token ($) {
1794        } else {        } else {
1795          !!!cp (106);          !!!cp (106);
1796          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1797            $self->{read_until}->($self->{current_attribute}->{value},
1798                                  q['&],
1799                                  length $self->{current_attribute}->{value});
1800    
1801          ## Stay in the state          ## Stay in the state
1802          !!!next-input-character;          !!!next-input-character;
1803          redo A;          redo A;
# Line 1273  sub _get_next_token ($) { Line 1814  sub _get_next_token ($) {
1814          redo A;          redo A;
1815        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1816          !!!cp (108);          !!!cp (108);
1817          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1818          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1819            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1820            ## implementation of the "consume a character reference" algorithm.
1821            $self->{entity_additional} = -1;
1822            $self->{prev_state} = $self->{state};
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 1305  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 1338  sub _get_next_token ($) { Line 1880  sub _get_next_token ($) {
1880            !!!cp (116);            !!!cp (116);
1881          }          }
1882          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1883            $self->{read_until}->($self->{current_attribute}->{value},
1884                                  q["'=& >],
1885                                  length $self->{current_attribute}->{value});
1886    
1887          ## Stay in the state          ## Stay in the state
1888          !!!next-input-character;          !!!next-input-character;
1889          redo A;          redo A;
1890        }        }
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         !!!cp (117);  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         !!!cp (118);  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
1891      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1892        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1893            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1377  sub _get_next_token ($) { Line 1901  sub _get_next_token ($) {
1901        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1902          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1903            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1904            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1905          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1906            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1399  sub _get_next_token ($) { Line 1921  sub _get_next_token ($) {
1921    
1922          redo A;          redo A;
1923        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1924            !!!cp (122);
1925            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1926          !!!next-input-character;          !!!next-input-character;
1927          if ($self->{next_char} == 0x003E and # >          redo A;
1928              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1929              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1930            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1931            !!!cp (122);            !!!cp (122.3);
1932            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1933            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1934              if ($self->{current_token}->{attributes}) {
1935                !!!cp (122.1);
1936                !!!parse-error (type => 'end tag attribute');
1937              } else {
1938                ## NOTE: This state should never be reached.
1939                !!!cp (122.2);
1940              }
1941          } else {          } else {
1942            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1943          }          }
1944          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1945          # next-input-character is already done          ## Reconsume.
1946            !!!emit ($self->{current_token}); # start tag or end tag
1947          redo A;          redo A;
1948        } else {        } else {
1949          !!!cp (124);          !!!cp ('124.1');
1950          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1951          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1952          ## reconsume          ## reconsume
1953          redo A;          redo A;
1954        }        }
1955      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1956        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1957                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1958        ## NOTE: Set by the previous state            !!!cp ('124.2');
1959        #my $token = {type => COMMENT_TOKEN, data => ''};            !!!parse-error (type => 'nestc', token => $self->{current_token});
1960              ## TODO: Different type than slash in start tag
1961        BC: {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1962          if ($self->{next_char} == 0x003E) { # >            if ($self->{current_token}->{attributes}) {
1963            !!!cp (124);              !!!cp ('124.4');
1964            $self->{state} = DATA_STATE;              !!!parse-error (type => 'end tag attribute');
1965            !!!next-input-character;            } else {
1966                !!!cp ('124.5');
1967            !!!emit ($self->{current_token}); # comment            }
1968              ## TODO: Test |<title></title/>|
1969            } else {
1970              !!!cp ('124.3');
1971              $self->{self_closing} = 1;
1972            }
1973    
1974            redo A;          $self->{state} = DATA_STATE;
1975          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1976    
1977            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # start tag or end tag
1978    
1979            redo A;          redo A;
1980          } elsif ($self->{next_char} == -1) {
1981            !!!parse-error (type => 'unclosed tag');
1982            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1983              !!!cp (124.7);
1984              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1985            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1986              if ($self->{current_token}->{attributes}) {
1987                !!!cp (124.5);
1988                !!!parse-error (type => 'end tag attribute');
1989              } else {
1990                ## NOTE: This state should never be reached.
1991                !!!cp (124.6);
1992              }
1993          } else {          } else {
1994            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $self->{current_token}->{data} .= chr ($self->{next_char}); # comment  
           !!!next-input-character;  
           redo BC;  
1995          }          }
1996        } # BC          $self->{state} = DATA_STATE;
1997            ## Reconsume.
1998        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # start tag or end tag
1999      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {          redo A;
2000          } else {
2001            !!!cp ('124.4');
2002            !!!parse-error (type => 'nestc');
2003            ## TODO: This error type is wrong.
2004            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2005            ## Reconsume.
2006            redo A;
2007          }
2008        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2009        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2010    
2011        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);        ## NOTE: Unlike spec's "bogus comment state", this implementation
2012          ## consumes characters one-by-one basis.
2013          
2014          if ($self->{next_char} == 0x003E) { # >
2015            !!!cp (124);
2016            $self->{state} = DATA_STATE;
2017            !!!next-input-character;
2018    
2019            !!!emit ($self->{current_token}); # comment
2020            redo A;
2021          } elsif ($self->{next_char} == -1) {
2022            !!!cp (125);
2023            $self->{state} = DATA_STATE;
2024            ## reconsume
2025    
2026            !!!emit ($self->{current_token}); # comment
2027            redo A;
2028          } else {
2029            !!!cp (126);
2030            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2031            $self->{read_until}->($self->{current_token}->{data},
2032                                  q[>],
2033                                  length $self->{current_token}->{data});
2034    
2035        my @next_char;          ## Stay in the state.
2036        push @next_char, $self->{next_char};          !!!next-input-character;
2037            redo A;
2038          }
2039        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2040          ## (only happen if PCDATA state)
2041                
2042        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2043            !!!cp (133);
2044            $self->{state} = MD_HYPHEN_STATE;
2045          !!!next-input-character;          !!!next-input-character;
2046          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => '',  
                                     line => $l, column => $c};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2047        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2048                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2049            ## ASCII case-insensitive.
2050            !!!cp (130);
2051            $self->{state} = MD_DOCTYPE_STATE;
2052            $self->{state_keyword} = chr $self->{next_char};
2053          !!!next-input-character;          !!!next-input-character;
2054          push @next_char, $self->{next_char};          redo A;
2055          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2056              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2057            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2058            push @next_char, $self->{next_char};          !!!cp (135.4);                
2059            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2060                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2061              !!!next-input-character;          !!!next-input-character;
2062              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;  
                     $self->{current_token} = {type => DOCTYPE_TOKEN,  
                                               quirks => 1,  
                                               line => $l, column => $c};  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2063        } else {        } else {
2064          !!!cp (136);          !!!cp (136);
2065        }        }
2066    
2067        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2068        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2069        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2070          ## Reconsume.
2071        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2072        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073                                  line => $l, column => $c};                                  line => $self->{line_prev},
2074                                    column => $self->{column_prev} - 1,
2075                                   };
2076        redo A;        redo A;
2077              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2078        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2079        ## 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);
2080            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2081                                      line => $self->{line_prev},
2082                                      column => $self->{column_prev} - 2,
2083                                     };
2084            $self->{state} = COMMENT_START_STATE;
2085            !!!next-input-character;
2086            redo A;
2087          } else {
2088            !!!cp (128);
2089            !!!parse-error (type => 'bogus comment',
2090                            line => $self->{line_prev},
2091                            column => $self->{column_prev} - 2);
2092            $self->{state} = BOGUS_COMMENT_STATE;
2093            ## Reconsume.
2094            $self->{current_token} = {type => COMMENT_TOKEN,
2095                                      data => '-',
2096                                      line => $self->{line_prev},
2097                                      column => $self->{column_prev} - 2,
2098                                     };
2099            redo A;
2100          }
2101        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2102          ## ASCII case-insensitive.
2103          if ($self->{next_char} == [
2104                undef,
2105                0x004F, # O
2106                0x0043, # C
2107                0x0054, # T
2108                0x0059, # Y
2109                0x0050, # P
2110              ]->[length $self->{state_keyword}] or
2111              $self->{next_char} == [
2112                undef,
2113                0x006F, # o
2114                0x0063, # c
2115                0x0074, # t
2116                0x0079, # y
2117                0x0070, # p
2118              ]->[length $self->{state_keyword}]) {
2119            !!!cp (131);
2120            ## Stay in the state.
2121            $self->{state_keyword} .= chr $self->{next_char};
2122            !!!next-input-character;
2123            redo A;
2124          } elsif ((length $self->{state_keyword}) == 6 and
2125                   ($self->{next_char} == 0x0045 or # E
2126                    $self->{next_char} == 0x0065)) { # e
2127            !!!cp (129);
2128            $self->{state} = DOCTYPE_STATE;
2129            $self->{current_token} = {type => DOCTYPE_TOKEN,
2130                                      quirks => 1,
2131                                      line => $self->{line_prev},
2132                                      column => $self->{column_prev} - 7,
2133                                     };
2134            !!!next-input-character;
2135            redo A;
2136          } else {
2137            !!!cp (132);        
2138            !!!parse-error (type => 'bogus comment',
2139                            line => $self->{line_prev},
2140                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2141            $self->{state} = BOGUS_COMMENT_STATE;
2142            ## Reconsume.
2143            $self->{current_token} = {type => COMMENT_TOKEN,
2144                                      data => $self->{state_keyword},
2145                                      line => $self->{line_prev},
2146                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2147                                     };
2148            redo A;
2149          }
2150        } elsif ($self->{state} == MD_CDATA_STATE) {
2151          if ($self->{next_char} == {
2152                '[' => 0x0043, # C
2153                '[C' => 0x0044, # D
2154                '[CD' => 0x0041, # A
2155                '[CDA' => 0x0054, # T
2156                '[CDAT' => 0x0041, # A
2157              }->{$self->{state_keyword}}) {
2158            !!!cp (135.1);
2159            ## Stay in the state.
2160            $self->{state_keyword} .= chr $self->{next_char};
2161            !!!next-input-character;
2162            redo A;
2163          } elsif ($self->{state_keyword} eq '[CDATA' and
2164                   $self->{next_char} == 0x005B) { # [
2165            !!!cp (135.2);
2166            $self->{current_token} = {type => CHARACTER_TOKEN,
2167                                      data => '',
2168                                      line => $self->{line_prev},
2169                                      column => $self->{column_prev} - 7};
2170            $self->{state} = CDATA_SECTION_STATE;
2171            !!!next-input-character;
2172            redo A;
2173          } else {
2174            !!!cp (135.3);
2175            !!!parse-error (type => 'bogus comment',
2176                            line => $self->{line_prev},
2177                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2178            $self->{state} = BOGUS_COMMENT_STATE;
2179            ## Reconsume.
2180            $self->{current_token} = {type => COMMENT_TOKEN,
2181                                      data => $self->{state_keyword},
2182                                      line => $self->{line_prev},
2183                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2184                                     };
2185            redo A;
2186          }
2187      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2188        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2189          !!!cp (137);          !!!cp (137);
# Line 1621  sub _get_next_token ($) { Line 2266  sub _get_next_token ($) {
2266        } else {        } else {
2267          !!!cp (147);          !!!cp (147);
2268          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2269            $self->{read_until}->($self->{current_token}->{data},
2270                                  q[-],
2271                                  length $self->{current_token}->{data});
2272    
2273          ## Stay in the state          ## Stay in the state
2274          !!!next-input-character;          !!!next-input-character;
2275          redo A;          redo A;
# Line 1805  sub _get_next_token ($) { Line 2454  sub _get_next_token ($) {
2454          redo A;          redo A;
2455        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2456                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2457            $self->{state} = PUBLIC_STATE;
2458            $self->{state_keyword} = chr $self->{next_char};
2459          !!!next-input-character;          !!!next-input-character;
2460          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);  
         }  
   
         #  
2461        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2462                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2463            $self->{state} = SYSTEM_STATE;
2464            $self->{state_keyword} = chr $self->{next_char};
2465          !!!next-input-character;          !!!next-input-character;
2466          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);  
         }  
   
         #  
2467        } else {        } else {
2468          !!!cp (180);          !!!cp (180);
2469            !!!parse-error (type => 'string after DOCTYPE name');
2470            $self->{current_token}->{quirks} = 1;
2471    
2472            $self->{state} = BOGUS_DOCTYPE_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474          #          redo A;
2475        }        }
2476        } elsif ($self->{state} == PUBLIC_STATE) {
2477          ## ASCII case-insensitive
2478          if ($self->{next_char} == [
2479                undef,
2480                0x0055, # U
2481                0x0042, # B
2482                0x004C, # L
2483                0x0049, # I
2484              ]->[length $self->{state_keyword}] or
2485              $self->{next_char} == [
2486                undef,
2487                0x0075, # u
2488                0x0062, # b
2489                0x006C, # l
2490                0x0069, # i
2491              ]->[length $self->{state_keyword}]) {
2492            !!!cp (175);
2493            ## Stay in the state.
2494            $self->{state_keyword} .= chr $self->{next_char};
2495            !!!next-input-character;
2496            redo A;
2497          } elsif ((length $self->{state_keyword}) == 5 and
2498                   ($self->{next_char} == 0x0043 or # C
2499                    $self->{next_char} == 0x0063)) { # c
2500            !!!cp (168);
2501            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2502            !!!next-input-character;
2503            redo A;
2504          } else {
2505            !!!cp (169);
2506            !!!parse-error (type => 'string after DOCTYPE name',
2507                            line => $self->{line_prev},
2508                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2509            $self->{current_token}->{quirks} = 1;
2510    
2511        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2512        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2513            redo A;
2514          }
2515        } elsif ($self->{state} == SYSTEM_STATE) {
2516          ## ASCII case-insensitive
2517          if ($self->{next_char} == [
2518                undef,
2519                0x0059, # Y
2520                0x0053, # S
2521                0x0054, # T
2522                0x0045, # E
2523              ]->[length $self->{state_keyword}] or
2524              $self->{next_char} == [
2525                undef,
2526                0x0079, # y
2527                0x0073, # s
2528                0x0074, # t
2529                0x0065, # e
2530              ]->[length $self->{state_keyword}]) {
2531            !!!cp (170);
2532            ## Stay in the state.
2533            $self->{state_keyword} .= chr $self->{next_char};
2534            !!!next-input-character;
2535            redo A;
2536          } elsif ((length $self->{state_keyword}) == 5 and
2537                   ($self->{next_char} == 0x004D or # M
2538                    $self->{next_char} == 0x006D)) { # m
2539            !!!cp (171);
2540            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2541            !!!next-input-character;
2542            redo A;
2543          } else {
2544            !!!cp (172);
2545            !!!parse-error (type => 'string after DOCTYPE name',
2546                            line => $self->{line_prev},
2547                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2548            $self->{current_token}->{quirks} = 1;
2549    
2550        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2551        # next-input-character is already done          ## Reconsume.
2552        redo A;          redo A;
2553          }
2554      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2555        if ({        if ({
2556              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 1975  sub _get_next_token ($) { Line 2635  sub _get_next_token ($) {
2635          !!!cp (190);          !!!cp (190);
2636          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2637              .= chr $self->{next_char};              .= chr $self->{next_char};
2638            $self->{read_until}->($self->{current_token}->{public_identifier},
2639                                  q[">],
2640                                  length $self->{current_token}->{public_identifier});
2641    
2642          ## Stay in the state          ## Stay in the state
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
# Line 2011  sub _get_next_token ($) { Line 2675  sub _get_next_token ($) {
2675          !!!cp (194);          !!!cp (194);
2676          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2677              .= chr $self->{next_char};              .= chr $self->{next_char};
2678            $self->{read_until}->($self->{current_token}->{public_identifier},
2679                                  q['>],
2680                                  length $self->{current_token}->{public_identifier});
2681    
2682          ## Stay in the state          ## Stay in the state
2683          !!!next-input-character;          !!!next-input-character;
2684          redo A;          redo A;
# Line 2123  sub _get_next_token ($) { Line 2791  sub _get_next_token ($) {
2791          redo A;          redo A;
2792        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2793          !!!cp (208);          !!!cp (208);
2794          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2795    
2796          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2797          !!!next-input-character;          !!!next-input-character;
# Line 2147  sub _get_next_token ($) { Line 2815  sub _get_next_token ($) {
2815          !!!cp (210);          !!!cp (210);
2816          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2817              .= chr $self->{next_char};              .= chr $self->{next_char};
2818            $self->{read_until}->($self->{current_token}->{system_identifier},
2819                                  q[">],
2820                                  length $self->{current_token}->{system_identifier});
2821    
2822          ## Stay in the state          ## Stay in the state
2823          !!!next-input-character;          !!!next-input-character;
2824          redo A;          redo A;
# Line 2159  sub _get_next_token ($) { Line 2831  sub _get_next_token ($) {
2831          redo A;          redo A;
2832        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2833          !!!cp (212);          !!!cp (212);
2834          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2835    
2836          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2837          !!!next-input-character;          !!!next-input-character;
# Line 2183  sub _get_next_token ($) { Line 2855  sub _get_next_token ($) {
2855          !!!cp (214);          !!!cp (214);
2856          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2857              .= chr $self->{next_char};              .= chr $self->{next_char};
2858            $self->{read_until}->($self->{current_token}->{system_identifier},
2859                                  q['>],
2860                                  length $self->{current_token}->{system_identifier});
2861    
2862          ## Stay in the state          ## Stay in the state
2863          !!!next-input-character;          !!!next-input-character;
2864          redo A;          redo A;
# Line 2207  sub _get_next_token ($) { Line 2883  sub _get_next_token ($) {
2883        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2884          !!!cp (217);          !!!cp (217);
2885          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2886          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2887          ## reconsume          ## reconsume
2888    
# Line 2244  sub _get_next_token ($) { Line 2919  sub _get_next_token ($) {
2919          redo A;          redo A;
2920        } else {        } else {
2921          !!!cp (221);          !!!cp (221);
2922            my $s = '';
2923            $self->{read_until}->($s, q[>], 0);
2924    
2925          ## Stay in the state          ## Stay in the state
2926          !!!next-input-character;          !!!next-input-character;
2927          redo A;          redo A;
2928        }        }
2929      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2930        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2931      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2932    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2933          
2934          if ($self->{next_char} == 0x005D) { # ]
2935            !!!cp (221.1);
2936            $self->{state} = CDATA_SECTION_MSE1_STATE;
2937            !!!next-input-character;
2938            redo A;
2939          } elsif ($self->{next_char} == -1) {
2940            $self->{state} = DATA_STATE;
2941            !!!next-input-character;
2942            if (length $self->{current_token}->{data}) { # character
2943              !!!cp (221.2);
2944              !!!emit ($self->{current_token}); # character
2945            } else {
2946              !!!cp (221.3);
2947              ## No token to emit. $self->{current_token} is discarded.
2948            }        
2949            redo A;
2950          } else {
2951            !!!cp (221.4);
2952            $self->{current_token}->{data} .= chr $self->{next_char};
2953            $self->{read_until}->($self->{current_token}->{data},
2954                                  q<]>,
2955                                  length $self->{current_token}->{data});
2956    
2957    die "$0: _get_next_token: unexpected case";          ## Stay in the state.
2958  } # _get_next_token          !!!next-input-character;
2959            redo A;
2960          }
2961    
2962  sub _tokenize_attempt_to_consume_an_entity ($$$) {        ## ISSUE: "text tokens" in spec.
2963    my ($self, $in_attr, $additional) = @_;      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2964          if ($self->{next_char} == 0x005D) { # ]
2965            !!!cp (221.5);
2966            $self->{state} = CDATA_SECTION_MSE2_STATE;
2967            !!!next-input-character;
2968            redo A;
2969          } else {
2970            !!!cp (221.6);
2971            $self->{current_token}->{data} .= ']';
2972            $self->{state} = CDATA_SECTION_STATE;
2973            ## Reconsume.
2974            redo A;
2975          }
2976        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2977          if ($self->{next_char} == 0x003E) { # >
2978            $self->{state} = DATA_STATE;
2979            !!!next-input-character;
2980            if (length $self->{current_token}->{data}) { # character
2981              !!!cp (221.7);
2982              !!!emit ($self->{current_token}); # character
2983            } else {
2984              !!!cp (221.8);
2985              ## No token to emit. $self->{current_token} is discarded.
2986            }
2987            redo A;
2988          } elsif ($self->{next_char} == 0x005D) { # ]
2989            !!!cp (221.9); # character
2990            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2991            ## Stay in the state.
2992            !!!next-input-character;
2993            redo A;
2994          } else {
2995            !!!cp (221.11);
2996            $self->{current_token}->{data} .= ']]'; # character
2997            $self->{state} = CDATA_SECTION_STATE;
2998            ## Reconsume.
2999            redo A;
3000          }
3001        } elsif ($self->{state} == ENTITY_STATE) {
3002          if ({
3003            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3004            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3005            $self->{entity_additional} => 1,
3006          }->{$self->{next_char}}) {
3007            !!!cp (1001);
3008            ## Don't consume
3009            ## No error
3010            ## Return nothing.
3011            #
3012          } elsif ($self->{next_char} == 0x0023) { # #
3013            !!!cp (999);
3014            $self->{state} = ENTITY_HASH_STATE;
3015            $self->{state_keyword} = '#';
3016            !!!next-input-character;
3017            redo A;
3018          } elsif ((0x0041 <= $self->{next_char} and
3019                    $self->{next_char} <= 0x005A) or # A..Z
3020                   (0x0061 <= $self->{next_char} and
3021                    $self->{next_char} <= 0x007A)) { # a..z
3022            !!!cp (998);
3023            require Whatpm::_NamedEntityList;
3024            $self->{state} = ENTITY_NAME_STATE;
3025            $self->{state_keyword} = chr $self->{next_char};
3026            $self->{entity__value} = $self->{state_keyword};
3027            $self->{entity__match} = 0;
3028            !!!next-input-character;
3029            redo A;
3030          } else {
3031            !!!cp (1027);
3032            !!!parse-error (type => 'bare ero');
3033            ## Return nothing.
3034            #
3035          }
3036    
3037    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        ## NOTE: No character is consumed by the "consume a character
3038          ## reference" algorithm.  In other word, there is an "&" character
3039          ## that does not introduce a character reference, which would be
3040          ## appended to the parent element or the attribute value in later
3041          ## process of the tokenizer.
3042    
3043          if ($self->{prev_state} == DATA_STATE) {
3044            !!!cp (997);
3045            $self->{state} = $self->{prev_state};
3046            ## Reconsume.
3047            !!!emit ({type => CHARACTER_TOKEN, data => '&',
3048                      line => $self->{line_prev},
3049                      column => $self->{column_prev},
3050                     });
3051            redo A;
3052          } else {
3053            !!!cp (996);
3054            $self->{current_attribute}->{value} .= '&';
3055            $self->{state} = $self->{prev_state};
3056            ## Reconsume.
3057            redo A;
3058          }
3059        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3060          if ($self->{next_char} == 0x0078 or # x
3061              $self->{next_char} == 0x0058) { # X
3062            !!!cp (995);
3063            $self->{state} = HEXREF_X_STATE;
3064            $self->{state_keyword} .= chr $self->{next_char};
3065            !!!next-input-character;
3066            redo A;
3067          } elsif (0x0030 <= $self->{next_char} and
3068                   $self->{next_char} <= 0x0039) { # 0..9
3069            !!!cp (994);
3070            $self->{state} = NCR_NUM_STATE;
3071            $self->{state_keyword} = $self->{next_char} - 0x0030;
3072            !!!next-input-character;
3073            redo A;
3074          } else {
3075            !!!parse-error (type => 'bare nero',
3076                            line => $self->{line_prev},
3077                            column => $self->{column_prev} - 1);
3078    
3079    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3080         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3081         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3082         $additional => 1,  
3083        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3084      !!!cp (1001);            !!!cp (1019);
3085      ## Don't consume            $self->{state} = $self->{prev_state};
3086      ## No error            ## Reconsume.
3087      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3088    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3089      !!!next-input-character;                      line => $self->{line_prev},
3090      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3091          $self->{next_char} == 0x0058) { # X                     });
3092        my $code;            redo A;
       X: {  
         my $x_char = $self->{next_char};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_char} and  
             $self->{next_char} <= 0x0039) { # 0..9  
           !!!cp (1002);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro', line => $l, column => $c);  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
3093          } else {          } else {
3094            !!!cp (1007);            !!!cp (993);
3095            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{current_attribute}->{value} .= '&#';
3096              $self->{state} = $self->{prev_state};
3097              ## Reconsume.
3098              redo A;
3099          }          }
3100          }
3101          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3102            !!!cp (1008);        if (0x0030 <= $self->{next_char} and
3103            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            $self->{next_char} <= 0x0039) { # 0..9
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference', line => $l, column => $c);  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1, line => $l, column => $c};  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
3104          !!!cp (1012);          !!!cp (1012);
3105          $code *= 10;          $self->{state_keyword} *= 10;
3106          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3107                    
3108            ## Stay in the state.
3109          !!!next-input-character;          !!!next-input-character;
3110        }          redo A;
3111          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3112          !!!cp (1013);          !!!cp (1013);
3113          !!!next-input-character;          !!!next-input-character;
3114            #
3115        } else {        } else {
3116          !!!cp (1014);          !!!cp (1014);
3117          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3118            ## Reconsume.
3119            #
3120        }        }
3121    
3122          my $code = $self->{state_keyword};
3123          my $l = $self->{line_prev};
3124          my $c = $self->{column_prev};
3125        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3126          !!!cp (1015);          !!!cp (1015);
3127          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3128                            text => (sprintf 'U+%04X', $code),
3129                            line => $l, column => $c);
3130          $code = 0xFFFD;          $code = 0xFFFD;
3131        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3132          !!!cp (1016);          !!!cp (1016);
3133          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3134                            text => (sprintf 'U-%08X', $code),
3135                            line => $l, column => $c);
3136          $code = 0xFFFD;          $code = 0xFFFD;
3137        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3138          !!!cp (1017);          !!!cp (1017);
3139          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3140                            line => $l, column => $c);
3141          $code = 0x000A;          $code = 0x000A;
3142        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3143          !!!cp (1018);          !!!cp (1018);
3144          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3145                            text => (sprintf 'U+%04X', $code),
3146                            line => $l, column => $c);
3147          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3148        }        }
3149          
3150        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3151                line => $l, column => $c};          !!!cp (992);
3152      } else {          $self->{state} = $self->{prev_state};
3153        !!!cp (1019);          ## Reconsume.
3154        !!!parse-error (type => 'bare nero', line => $l, column => $c);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3155        !!!back-next-input-character ($self->{next_char});                    line => $l, column => $c,
3156        $self->{next_char} = 0x0023; # #                   });
3157        return undef;          redo A;
3158      }        } else {
3159    } elsif ((0x0041 <= $self->{next_char} and          !!!cp (991);
3160              $self->{next_char} <= 0x005A) or          $self->{current_attribute}->{value} .= chr $code;
3161             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{has_reference} = 1;
3162              $self->{next_char} <= 0x007A)) {          $self->{state} = $self->{prev_state};
3163      my $entity_name = chr $self->{next_char};          ## Reconsume.
3164      !!!next-input-character;          redo A;
3165          }
3166      my $value = $entity_name;      } elsif ($self->{state} == HEXREF_X_STATE) {
3167      my $match = 0;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3168      require Whatpm::_NamedEntityList;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3169      our $EntityChar;            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3170            # 0..9, A..F, a..f
3171      while (length $entity_name < 10 and          !!!cp (990);
3172             ## NOTE: Some number greater than the maximum length of entity name          $self->{state} = HEXREF_HEX_STATE;
3173             ((0x0041 <= $self->{next_char} and # a          $self->{state_keyword} = 0;
3174               $self->{next_char} <= 0x005A) or # x          ## Reconsume.
3175              (0x0061 <= $self->{next_char} and # a          redo A;
3176               $self->{next_char} <= 0x007A) or # z        } else {
3177              (0x0030 <= $self->{next_char} and # 0          !!!parse-error (type => 'bare hcro',
3178               $self->{next_char} <= 0x0039) or # 9                          line => $self->{line_prev},
3179              $self->{next_char} == 0x003B)) { # ;                          column => $self->{column_prev} - 2);
3180        $entity_name .= chr $self->{next_char};  
3181        if (defined $EntityChar->{$entity_name}) {          ## NOTE: According to the spec algorithm, nothing is returned,
3182          if ($self->{next_char} == 0x003B) { # ;          ## and then "&#" followed by "X" or "x" is appended to the parent
3183            !!!cp (1020);          ## element or the attribute value in the later processing.
3184            $value = $EntityChar->{$entity_name};  
3185            $match = 1;          if ($self->{prev_state} == DATA_STATE) {
3186            !!!next-input-character;            !!!cp (1005);
3187            last;            $self->{state} = $self->{prev_state};
3188              ## Reconsume.
3189              !!!emit ({type => CHARACTER_TOKEN,
3190                        data => '&' . $self->{state_keyword},
3191                        line => $self->{line_prev},
3192                        column => $self->{column_prev} - length $self->{state_keyword},
3193                       });
3194              redo A;
3195          } else {          } else {
3196            !!!cp (1021);            !!!cp (989);
3197            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3198            $match = -1;            $self->{state} = $self->{prev_state};
3199              ## Reconsume.
3200              redo A;
3201            }
3202          }
3203        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3204          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3205            # 0..9
3206            !!!cp (1002);
3207            $self->{state_keyword} *= 0x10;
3208            $self->{state_keyword} += $self->{next_char} - 0x0030;
3209            ## Stay in the state.
3210            !!!next-input-character;
3211            redo A;
3212          } elsif (0x0061 <= $self->{next_char} and
3213                   $self->{next_char} <= 0x0066) { # a..f
3214            !!!cp (1003);
3215            $self->{state_keyword} *= 0x10;
3216            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3217            ## Stay in the state.
3218            !!!next-input-character;
3219            redo A;
3220          } elsif (0x0041 <= $self->{next_char} and
3221                   $self->{next_char} <= 0x0046) { # A..F
3222            !!!cp (1004);
3223            $self->{state_keyword} *= 0x10;
3224            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3225            ## Stay in the state.
3226            !!!next-input-character;
3227            redo A;
3228          } elsif ($self->{next_char} == 0x003B) { # ;
3229            !!!cp (1006);
3230            !!!next-input-character;
3231            #
3232          } else {
3233            !!!cp (1007);
3234            !!!parse-error (type => 'no refc',
3235                            line => $self->{line},
3236                            column => $self->{column});
3237            ## Reconsume.
3238            #
3239          }
3240    
3241          my $code = $self->{state_keyword};
3242          my $l = $self->{line_prev};
3243          my $c = $self->{column_prev};
3244          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3245            !!!cp (1008);
3246            !!!parse-error (type => 'invalid character reference',
3247                            text => (sprintf 'U+%04X', $code),
3248                            line => $l, column => $c);
3249            $code = 0xFFFD;
3250          } elsif ($code > 0x10FFFF) {
3251            !!!cp (1009);
3252            !!!parse-error (type => 'invalid character reference',
3253                            text => (sprintf 'U-%08X', $code),
3254                            line => $l, column => $c);
3255            $code = 0xFFFD;
3256          } elsif ($code == 0x000D) {
3257            !!!cp (1010);
3258            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3259            $code = 0x000A;
3260          } elsif (0x80 <= $code and $code <= 0x9F) {
3261            !!!cp (1011);
3262            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3263            $code = $c1_entity_char->{$code};
3264          }
3265    
3266          if ($self->{prev_state} == DATA_STATE) {
3267            !!!cp (988);
3268            $self->{state} = $self->{prev_state};
3269            ## Reconsume.
3270            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3271                      line => $l, column => $c,
3272                     });
3273            redo A;
3274          } else {
3275            !!!cp (987);
3276            $self->{current_attribute}->{value} .= chr $code;
3277            $self->{current_attribute}->{has_reference} = 1;
3278            $self->{state} = $self->{prev_state};
3279            ## Reconsume.
3280            redo A;
3281          }
3282        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3283          if (length $self->{state_keyword} < 30 and
3284              ## NOTE: Some number greater than the maximum length of entity name
3285              ((0x0041 <= $self->{next_char} and # a
3286                $self->{next_char} <= 0x005A) or # x
3287               (0x0061 <= $self->{next_char} and # a
3288                $self->{next_char} <= 0x007A) or # z
3289               (0x0030 <= $self->{next_char} and # 0
3290                $self->{next_char} <= 0x0039) or # 9
3291               $self->{next_char} == 0x003B)) { # ;
3292            our $EntityChar;
3293            $self->{state_keyword} .= chr $self->{next_char};
3294            if (defined $EntityChar->{$self->{state_keyword}}) {
3295              if ($self->{next_char} == 0x003B) { # ;
3296                !!!cp (1020);
3297                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3298                $self->{entity__match} = 1;
3299                !!!next-input-character;
3300                #
3301              } else {
3302                !!!cp (1021);
3303                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3304                $self->{entity__match} = -1;
3305                ## Stay in the state.
3306                !!!next-input-character;
3307                redo A;
3308              }
3309            } else {
3310              !!!cp (1022);
3311              $self->{entity__value} .= chr $self->{next_char};
3312              $self->{entity__match} *= 2;
3313              ## Stay in the state.
3314            !!!next-input-character;            !!!next-input-character;
3315              redo A;
3316            }
3317          }
3318    
3319          my $data;
3320          my $has_ref;
3321          if ($self->{entity__match} > 0) {
3322            !!!cp (1023);
3323            $data = $self->{entity__value};
3324            $has_ref = 1;
3325            #
3326          } elsif ($self->{entity__match} < 0) {
3327            !!!parse-error (type => 'no refc');
3328            if ($self->{prev_state} != DATA_STATE and # in attribute
3329                $self->{entity__match} < -1) {
3330              !!!cp (1024);
3331              $data = '&' . $self->{state_keyword};
3332              #
3333            } else {
3334              !!!cp (1025);
3335              $data = $self->{entity__value};
3336              $has_ref = 1;
3337              #
3338          }          }
3339        } else {        } else {
3340          !!!cp (1022);          !!!cp (1026);
3341          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3342          $match *= 2;                          line => $self->{line_prev},
3343          !!!next-input-character;                          column => $self->{column_prev});
3344            $data = '&' . $self->{state_keyword};
3345            #
3346        }        }
3347      }    
3348              ## NOTE: In these cases, when a character reference is found,
3349      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3350        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3351        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3352                line => $l, column => $c};        ## tokenizer is appended to the parent element or the attribute value
3353      } elsif ($match < 0) {        ## as string, either literal string when no character reference or
3354        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## entity-replaced string otherwise, in this stage, since any characters
3355        if ($in_attr and $match < -1) {        ## that would not be consumed are appended in the data state or in an
3356          !!!cp (1024);        ## appropriate attribute value state anyway.
3357          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,  
3358                  line => $l, column => $c};        if ($self->{prev_state} == DATA_STATE) {
3359        } else {          !!!cp (986);
3360          !!!cp (1025);          $self->{state} = $self->{prev_state};
3361          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,          ## Reconsume.
3362                  line => $l, column => $c};          !!!emit ({type => CHARACTER_TOKEN,
3363                      data => $data,
3364                      line => $self->{line_prev},
3365                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3366                     });
3367            redo A;
3368          } else {
3369            !!!cp (985);
3370            $self->{current_attribute}->{value} .= $data;
3371            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3372            $self->{state} = $self->{prev_state};
3373            ## Reconsume.
3374            redo A;
3375        }        }
3376      } else {      } else {
3377        !!!cp (1026);        die "$0: $self->{state}: Unknown state";
       !!!parse-error (type => 'bare ero', line => $l, column => $c);  
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value,  
               line => $l, column => $c};  
3378      }      }
3379    } else {    } # A  
3380      !!!cp (1027);  
3381      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3382      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3383    
3384  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3385    my $self = shift;    my $self = shift;
# Line 2463  sub _initialize_tree_constructor ($) { Line 3388  sub _initialize_tree_constructor ($) {
3388    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3389    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3390    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3391      $self->{document}->set_user_data (manakai_source_line => 1);
3392      $self->{document}->set_user_data (manakai_source_column => 1);
3393  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3394    
3395  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2517  sub _tree_construction_initial ($) { Line 3444  sub _tree_construction_initial ($) {
3444        ## language.        ## language.
3445        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3446        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3447        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3448        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3449            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3450          !!!cp ('t1');          !!!cp ('t1');
3451          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3452        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3453          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3454          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3455          } elsif (defined $token->{public_identifier}) {
3456            if ($token->{public_identifier} eq 'XSLT-compat') {
3457              !!!cp ('t1.2');
3458              !!!parse-error (type => 'XSLT-compat', token => $token,
3459                              level => $self->{level}->{should});
3460            } else {
3461              !!!parse-error (type => 'not HTML5', token => $token);
3462            }
3463        } else {        } else {
3464          !!!cp ('t3');          !!!cp ('t3');
3465            #
3466        }        }
3467                
3468        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3469          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3470          ## NOTE: Default value for both |public_id| and |system_id| attributes
3471          ## are empty strings, so that we don't set any value in missing cases.
3472        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3473            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3474        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2547  sub _tree_construction_initial ($) { Line 3483  sub _tree_construction_initial ($) {
3483        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3484          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3485          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3486          if ({          my $prefix = [
3487            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3488            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3489            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3490            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3491            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3492            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3493            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3494            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3495            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3496            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3497            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3498            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3499            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3500            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3501            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3502            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3503            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3504            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3505            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3506            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3507            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3508            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3509            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3510            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3511            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3512            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3513            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3514            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3515            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3516            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3517            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3518            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3519            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3520            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3521            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3522            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3523            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3524            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3525            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3526            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3527            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3528            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3529            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3530            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3531            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3532            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3533            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3534            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3535            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3536            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3537            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3538            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3539            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3540            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3541            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3542            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3543            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3544            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3545            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3546            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3547            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3548            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3549            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3550            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3551            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3552            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3553            "-//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}) {  
3554            !!!cp ('t5');            !!!cp ('t5');
3555            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3556          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3557                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3558            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3559              !!!cp ('t6');              !!!cp ('t6');
3560              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2632  sub _tree_construction_initial ($) { Line 3562  sub _tree_construction_initial ($) {
3562              !!!cp ('t7');              !!!cp ('t7');
3563              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3564            }            }
3565          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3566                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3567            !!!cp ('t8');            !!!cp ('t8');
3568            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3569          } else {          } else {
# Line 2646  sub _tree_construction_initial ($) { Line 3576  sub _tree_construction_initial ($) {
3576          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3577          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3578          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") {
3579            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3580              ## marked as quirks.
3581            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3582            !!!cp ('t11');            !!!cp ('t11');
3583          } else {          } else {
# Line 2669  sub _tree_construction_initial ($) { Line 3600  sub _tree_construction_initial ($) {
3600        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3601        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3602        ## reprocess        ## reprocess
3603          !!!ack-later;
3604        return;        return;
3605      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3606        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2749  sub _tree_construction_root_element ($) Line 3681  sub _tree_construction_root_element ($)
3681        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3682          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3683            my $root_element;            my $root_element;
3684            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3685            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3686            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3687                  [$root_element, $el_category->{html}];
3688    
3689            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3690              !!!cp ('t24');              !!!cp ('t24');
3691              $self->{application_cache_selection}              $self->{application_cache_selection}
3692                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3693              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3694                ## According to Hixie (#whatwg 2008-03-19), it should be
3695                ## resolved against the base URI of the document in HTML
3696                ## or xml:base of the element in XHTML.
3697            } else {            } else {
3698              !!!cp ('t25');              !!!cp ('t25');
3699              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3700            }            }
3701    
3702              !!!nack ('t25c');
3703    
3704            !!!next-token;            !!!next-token;
3705            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3706          } else {          } else {
# Line 2779  sub _tree_construction_root_element ($) Line 3717  sub _tree_construction_root_element ($)
3717          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3718        }        }
3719    
3720      my $root_element; !!!create-element ($root_element, 'html',, $token);      my $root_element;
3721        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3722      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3723      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3724    
3725      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3726    
3727      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3728        !!!ack-later;
3729      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3730    
3731      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2809  sub _reset_insertion_mode ($) { Line 3749  sub _reset_insertion_mode ($) {
3749        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3750          $last = 1;          $last = 1;
3751          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3752            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3753                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3754              !!!cp ('t27');          } else {
3755              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3756          }          }
3757        }        }
3758              
3759        ## Step 4..13        ## Step 4..14
3760        my $new_mode = {        my $new_mode;
3761          if ($node->[1] & FOREIGN_EL) {
3762            !!!cp ('t28.1');
3763            ## NOTE: Strictly spaking, the line below only applies to MathML and
3764            ## SVG elements.  Currently the HTML syntax supports only MathML and
3765            ## SVG elements as foreigners.
3766            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3767          } elsif ($node->[1] & TABLE_CELL_EL) {
3768            if ($last) {
3769              !!!cp ('t28.2');
3770              #
3771            } else {
3772              !!!cp ('t28.3');
3773              $new_mode = IN_CELL_IM;
3774            }
3775          } else {
3776            !!!cp ('t28.4');
3777            $new_mode = {
3778                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3779                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3780                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3781                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3782                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3783                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2837  sub _reset_insertion_mode ($) { Line 3788  sub _reset_insertion_mode ($) {
3788                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3789                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3790                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3791                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3792          }
3793        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3794                
3795        ## Step 14        ## Step 15
3796        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3797          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3798            !!!cp ('t29');            !!!cp ('t29');
3799            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2855  sub _reset_insertion_mode ($) { Line 3807  sub _reset_insertion_mode ($) {
3807          !!!cp ('t31');          !!!cp ('t31');
3808        }        }
3809                
3810        ## Step 15        ## Step 16
3811        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3812                
3813        ## Step 16        ## Step 17
3814        $i--;        $i--;
3815        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3816                
3817        ## Step 17        ## Step 18
3818        redo S3;        redo S3;
3819      } # S3      } # S3
3820    
# Line 2974  sub _tree_construction_main ($) { Line 3926  sub _tree_construction_main ($) {
3926      ## Step 1      ## Step 1
3927      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3928      my $el;      my $el;
3929      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3930    
3931      ## Step 2      ## Step 2
3932      $insert->($el);      $insert->($el);
# Line 2985  sub _tree_construction_main ($) { Line 3937  sub _tree_construction_main ($) {
3937    
3938      ## Step 4      ## Step 4
3939      my $text = '';      my $text = '';
3940        !!!nack ('t40.1');
3941      !!!next-token;      !!!next-token;
3942      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3943        !!!cp ('t40');        !!!cp ('t40');
# Line 3011  sub _tree_construction_main ($) { Line 3964  sub _tree_construction_main ($) {
3964        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3965        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3966          !!!cp ('t43');          !!!cp ('t43');
3967          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3968        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3969          !!!cp ('t44');          !!!cp ('t44');
3970          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3971        } else {        } else {
3972          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3973        }        }
# Line 3024  sub _tree_construction_main ($) { Line 3977  sub _tree_construction_main ($) {
3977    
3978    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3979      my $script_el;      my $script_el;
3980      !!!create-element ($script_el, 'script', $token->{attributes}, $token);      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3981      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3982    
3983      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3984      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3985            
3986      my $text = '';      my $text = '';
3987        !!!nack ('t45.1');
3988      !!!next-token;      !!!next-token;
3989      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3990        !!!cp ('t45');        !!!cp ('t45');
# Line 3050  sub _tree_construction_main ($) { Line 4004  sub _tree_construction_main ($) {
4004        ## Ignore the token        ## Ignore the token
4005      } else {      } else {
4006        !!!cp ('t48');        !!!cp ('t48');
4007        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4008        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4009        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4010      }      }
# Line 3088  sub _tree_construction_main ($) { Line 4042  sub _tree_construction_main ($) {
4042        my $formatting_element;        my $formatting_element;
4043        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4044        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4045          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4046              !!!cp ('t52');
4047              last AFE;
4048            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4049                         eq $tag_name) {
4050            !!!cp ('t51');            !!!cp ('t51');
4051            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4052            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4053            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4054          }          }
4055        } # AFE        } # AFE
4056        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4057          !!!cp ('t53');          !!!cp ('t53');
4058          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4059          ## Ignore the token          ## Ignore the token
4060          !!!next-token;          !!!next-token;
4061          return;          return;
# Line 3117  sub _tree_construction_main ($) { Line 4072  sub _tree_construction_main ($) {
4072              last INSCOPE;              last INSCOPE;
4073            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4074              !!!cp ('t55');              !!!cp ('t55');
4075              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4076                                text => $token->{tag_name},
4077                              token => $end_tag_token);                              token => $end_tag_token);
4078              ## Ignore the token              ## Ignore the token
4079              !!!next-token;              !!!next-token;
4080              return;              return;
4081            }            }
4082          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4083            !!!cp ('t56');            !!!cp ('t56');
4084            $in_scope = 0;            $in_scope = 0;
4085          }          }
4086        } # INSCOPE        } # INSCOPE
4087        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4088          !!!cp ('t57');          !!!cp ('t57');
4089          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4090                            text => $token->{tag_name},
4091                          token => $end_tag_token);                          token => $end_tag_token);
4092          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4093          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3141  sub _tree_construction_main ($) { Line 4095  sub _tree_construction_main ($) {
4095        }        }
4096        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4097          !!!cp ('t58');          !!!cp ('t58');
4098          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],          !!!parse-error (type => 'not closed',
4099                            text => $self->{open_elements}->[-1]->[0]
4100                                ->manakai_local_name,
4101                          token => $end_tag_token);                          token => $end_tag_token);
4102        }        }
4103                
# Line 3150  sub _tree_construction_main ($) { Line 4106  sub _tree_construction_main ($) {
4106        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4107        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4108          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4109          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4110              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4111              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4112               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4113            !!!cp ('t59');            !!!cp ('t59');
4114            $furthest_block = $node;            $furthest_block = $node;
4115            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3239  sub _tree_construction_main ($) { Line 4195  sub _tree_construction_main ($) {
4195        } # S7          } # S7  
4196                
4197        ## Step 8        ## Step 8
4198        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
4199          my $foster_parent_element;          my $foster_parent_element;
4200          my $next_sibling;          my $next_sibling;
4201                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
4202                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4203                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4204                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4205                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3318  sub _tree_construction_main ($) { Line 4272  sub _tree_construction_main ($) {
4272    
4273    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4274      my $child = shift;      my $child = shift;
4275      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]}) {  
4276        # MUST        # MUST
4277        my $foster_parent_element;        my $foster_parent_element;
4278        my $next_sibling;        my $next_sibling;
4279                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4280                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4281                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4282                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4283                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3350  sub _tree_construction_main ($) { Line 4302  sub _tree_construction_main ($) {
4302      }      }
4303    }; # $insert_to_foster    }; # $insert_to_foster
4304    
4305    B: {    B: while (1) {
4306      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4307        !!!cp ('t73');        !!!cp ('t73');
4308        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4309        ## Ignore the token        ## Ignore the token
4310        ## Stay in the phase        ## Stay in the phase
4311        !!!next-token;        !!!next-token;
4312        redo B;        next B;
4313      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4314               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4315        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4316          !!!cp ('t79');          !!!cp ('t79');
4317          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4318          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4319        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4320          !!!cp ('t80');          !!!cp ('t80');
4321          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4322          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4323        } else {        } else {
4324          !!!cp ('t81');          !!!cp ('t81');
# Line 3383  sub _tree_construction_main ($) { Line 4335  sub _tree_construction_main ($) {
4335               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4336          }          }
4337        }        }
4338          !!!nack ('t84.1');
4339        !!!next-token;        !!!next-token;
4340        redo B;        next B;
4341      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4342        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4343        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3398  sub _tree_construction_main ($) { Line 4351  sub _tree_construction_main ($) {
4351          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4352        }        }
4353        !!!next-token;        !!!next-token;
4354        redo B;        next B;
4355      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4356          if ($token->{type} == CHARACTER_TOKEN) {
4357            !!!cp ('t87.1');
4358            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4359            !!!next-token;
4360            next B;
4361          } elsif ($token->{type} == START_TAG_TOKEN) {
4362            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4363                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4364                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4365                ($token->{tag_name} eq 'svg' and
4366                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4367              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4368              !!!cp ('t87.2');
4369              #
4370            } elsif ({
4371                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4372                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4373                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4374                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4375                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4376                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4377                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4378                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4379                     }->{$token->{tag_name}}) {
4380              !!!cp ('t87.2');
4381              !!!parse-error (type => 'not closed',
4382                              text => $self->{open_elements}->[-1]->[0]
4383                                  ->manakai_local_name,
4384                              token => $token);
4385    
4386              pop @{$self->{open_elements}}
4387                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4388    
4389              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4390              ## Reprocess.
4391              next B;
4392            } else {
4393              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4394              my $tag_name = $token->{tag_name};
4395              if ($nsuri eq $SVG_NS) {
4396                $tag_name = {
4397                   altglyph => 'altGlyph',
4398                   altglyphdef => 'altGlyphDef',
4399                   altglyphitem => 'altGlyphItem',
4400                   animatecolor => 'animateColor',
4401                   animatemotion => 'animateMotion',
4402                   animatetransform => 'animateTransform',
4403                   clippath => 'clipPath',
4404                   feblend => 'feBlend',
4405                   fecolormatrix => 'feColorMatrix',
4406                   fecomponenttransfer => 'feComponentTransfer',
4407                   fecomposite => 'feComposite',
4408                   feconvolvematrix => 'feConvolveMatrix',
4409                   fediffuselighting => 'feDiffuseLighting',
4410                   fedisplacementmap => 'feDisplacementMap',
4411                   fedistantlight => 'feDistantLight',
4412                   feflood => 'feFlood',
4413                   fefunca => 'feFuncA',
4414                   fefuncb => 'feFuncB',
4415                   fefuncg => 'feFuncG',
4416                   fefuncr => 'feFuncR',
4417                   fegaussianblur => 'feGaussianBlur',
4418                   feimage => 'feImage',
4419                   femerge => 'feMerge',
4420                   femergenode => 'feMergeNode',
4421                   femorphology => 'feMorphology',
4422                   feoffset => 'feOffset',
4423                   fepointlight => 'fePointLight',
4424                   fespecularlighting => 'feSpecularLighting',
4425                   fespotlight => 'feSpotLight',
4426                   fetile => 'feTile',
4427                   feturbulence => 'feTurbulence',
4428                   foreignobject => 'foreignObject',
4429                   glyphref => 'glyphRef',
4430                   lineargradient => 'linearGradient',
4431                   radialgradient => 'radialGradient',
4432                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4433                   textpath => 'textPath',  
4434                }->{$tag_name} || $tag_name;
4435              }
4436    
4437              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4438    
4439              ## "adjust foreign attributes" - done in insert-element-f
4440    
4441              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4442    
4443              if ($self->{self_closing}) {
4444                pop @{$self->{open_elements}};
4445                !!!ack ('t87.3');
4446              } else {
4447                !!!cp ('t87.4');
4448              }
4449    
4450              !!!next-token;
4451              next B;
4452            }
4453          } elsif ($token->{type} == END_TAG_TOKEN) {
4454            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4455            !!!cp ('t87.5');
4456            #
4457          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4458            !!!cp ('t87.6');
4459            !!!parse-error (type => 'not closed',
4460                            text => $self->{open_elements}->[-1]->[0]
4461                                ->manakai_local_name,
4462                            token => $token);
4463    
4464            pop @{$self->{open_elements}}
4465                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4466    
4467            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4468            ## Reprocess.
4469            next B;
4470          } else {
4471            die "$0: $token->{type}: Unknown token type";        
4472          }
4473        }
4474    
4475        if ($self->{insertion_mode} & HEAD_IMS) {
4476        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4477          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4478            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3409  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482              !!!cp ('t88.1');              !!!cp ('t88.1');
4483              ## Ignore the token.              ## Ignore the token.
4484              !!!next-token;              !!!next-token;
4485              redo B;              next B;
4486            }            }
4487            unless (length $token->{data}) {            unless (length $token->{data}) {
4488              !!!cp ('t88');              !!!cp ('t88');
4489              !!!next-token;              !!!next-token;
4490              redo B;              next B;
4491            }            }
4492          }          }
4493    
4494          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4495            !!!cp ('t89');            !!!cp ('t89');
4496            ## As if <head>            ## As if <head>
4497            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4498            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4499            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4500                  [$self->{head_element}, $el_category->{head}];
4501    
4502            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4503            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3433  sub _tree_construction_main ($) { Line 4507  sub _tree_construction_main ($) {
4507            !!!cp ('t90');            !!!cp ('t90');
4508            ## As if </noscript>            ## As if </noscript>
4509            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4510            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4511                        
4512            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4513            ## As if </head>            ## As if </head>
# Line 3449  sub _tree_construction_main ($) { Line 4523  sub _tree_construction_main ($) {
4523            !!!cp ('t92');            !!!cp ('t92');
4524          }          }
4525    
4526              ## "after head" insertion mode          ## "after head" insertion mode
4527              ## As if <body>          ## As if <body>
4528              !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
4529              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4530              ## reprocess          ## reprocess
4531              redo B;          next B;
4532            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4533              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4534                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4535                  !!!cp ('t93');              !!!cp ('t93');
4536                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4537                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4538                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4539                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4540                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4541                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4542                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4543                  !!!cp ('t94');              !!!next-token;
4544                  #              next B;
4545                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4546                  !!!cp ('t95');              !!!cp ('t93.2');
4547                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4548                  ## Ignore the token                              token => $token);
4549                  !!!next-token;              ## Ignore the token
4550                  redo B;              !!!nack ('t93.3');
4551                }              !!!next-token;
4552              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4553                !!!cp ('t96');            } else {
4554                ## As if <head>              !!!cp ('t95');
4555                !!!create-element ($self->{head_element}, 'head',, $token);              !!!parse-error (type => 'in head:head',
4556                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4557                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4558                !!!nack ('t95.1');
4559                !!!next-token;
4560                next B;
4561              }
4562            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4563              !!!cp ('t96');
4564              ## As if <head>
4565              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4566              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4567              push @{$self->{open_elements}},
4568                  [$self->{head_element}, $el_category->{head}];
4569    
4570                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4571                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4572              } else {          } else {
4573                !!!cp ('t97');            !!!cp ('t97');
4574              }          }
4575    
4576              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4577                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4578                  !!!cp ('t98');                  !!!cp ('t98');
4579                  ## As if </noscript>                  ## As if </noscript>
4580                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4581                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4582                                    token => $token);
4583                                
4584                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4585                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3504  sub _tree_construction_main ($) { Line 4590  sub _tree_construction_main ($) {
4590                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4591                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4592                  !!!cp ('t100');                  !!!cp ('t100');
4593                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4594                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4595                    push @{$self->{open_elements}},
4596                        [$self->{head_element}, $el_category->{head}];
4597                } else {                } else {
4598                  !!!cp ('t101');                  !!!cp ('t101');
4599                }                }
# Line 3513  sub _tree_construction_main ($) { Line 4601  sub _tree_construction_main ($) {
4601                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4602                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4603                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4604                  !!!nack ('t101.1');
4605                !!!next-token;                !!!next-token;
4606                redo B;                next B;
4607              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4608                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4609                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4610                  !!!cp ('t102');                  !!!cp ('t102');
4611                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4612                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4613                    push @{$self->{open_elements}},
4614                        [$self->{head_element}, $el_category->{head}];
4615                } else {                } else {
4616                  !!!cp ('t103');                  !!!cp ('t103');
4617                }                }
# Line 3528  sub _tree_construction_main ($) { Line 4619  sub _tree_construction_main ($) {
4619                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4620                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4621                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4622                  !!!ack ('t103.1');
4623                !!!next-token;                !!!next-token;
4624                redo B;                next B;
4625              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4626                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4627                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4628                  !!!cp ('t104');                  !!!cp ('t104');
4629                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4630                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4631                    push @{$self->{open_elements}},
4632                        [$self->{head_element}, $el_category->{head}];
4633                } else {                } else {
4634                  !!!cp ('t105');                  !!!cp ('t105');
4635                }                }
# Line 3543  sub _tree_construction_main ($) { Line 4637  sub _tree_construction_main ($) {
4637                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.
4638    
4639                unless ($self->{confident}) {                unless ($self->{confident}) {
4640                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4641                    !!!cp ('t106');                    !!!cp ('t106');
4642                      ## NOTE: Whether the encoding is supported or not is handled
4643                      ## in the {change_encoding} callback.
4644                    $self->{change_encoding}                    $self->{change_encoding}
4645                        ->($self, $token->{attributes}->{charset}->{value},                        ->($self, $token->{attributes}->{charset}->{value},
4646                           $token);                           $token);
# Line 3554  sub _tree_construction_main ($) { Line 4650  sub _tree_construction_main ($) {
4650                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4651                                                 ->{has_reference});                                                 ->{has_reference});
4652                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4653                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4654                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4655                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4656                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4657                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4658                      !!!cp ('t107');                      !!!cp ('t107');
4659                        ## NOTE: Whether the encoding is supported or not is handled
4660                        ## in the {change_encoding} callback.
4661                      $self->{change_encoding}                      $self->{change_encoding}
4662                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4663                             $token);                             $token);
# Line 3591  sub _tree_construction_main ($) { Line 4688  sub _tree_construction_main ($) {
4688    
4689                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4690                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4691                  !!!ack ('t110.1');
4692                !!!next-token;                !!!next-token;
4693                redo B;                next B;
4694              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4695                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4696                  !!!cp ('t111');                  !!!cp ('t111');
4697                  ## As if </noscript>                  ## As if </noscript>
4698                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4699                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4700                                    token => $token);
4701                                
4702                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4703                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4704                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4705                  !!!cp ('t112');                  !!!cp ('t112');
4706                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4707                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4708                    push @{$self->{open_elements}},
4709                        [$self->{head_element}, $el_category->{head}];
4710                } else {                } else {
4711                  !!!cp ('t113');                  !!!cp ('t113');
4712                }                }
# Line 3616  sub _tree_construction_main ($) { Line 4717  sub _tree_construction_main ($) {
4717                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4718                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4719                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4720                redo B;                next B;
4721              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4722                         $token->{tag_name} eq 'noframes') {
4723                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4724                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4725                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4726                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4727                  !!!cp ('t114');                  !!!cp ('t114');
4728                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4729                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4730                    push @{$self->{open_elements}},
4731                        [$self->{head_element}, $el_category->{head}];
4732                } else {                } else {
4733                  !!!cp ('t115');                  !!!cp ('t115');
4734                }                }
4735                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4736                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4737                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4738                redo B;                next B;
4739              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4740                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4741                  !!!cp ('t116');                  !!!cp ('t116');
4742                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4743                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4744                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4745                    !!!nack ('t116.1');
4746                  !!!next-token;                  !!!next-token;
4747                  redo B;                  next B;
4748                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4749                  !!!cp ('t117');                  !!!cp ('t117');
4750                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4751                                    token => $token);
4752                  ## Ignore the token                  ## Ignore the token
4753                    !!!nack ('t117.1');
4754                  !!!next-token;                  !!!next-token;
4755                  redo B;                  next B;
4756                } else {                } else {
4757                  !!!cp ('t118');                  !!!cp ('t118');
4758                  #                  #
# Line 3655  sub _tree_construction_main ($) { Line 4762  sub _tree_construction_main ($) {
4762                  !!!cp ('t119');                  !!!cp ('t119');
4763                  ## As if </noscript>                  ## As if </noscript>
4764                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4765                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4766                                    token => $token);
4767                                
4768                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4769                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4770                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4771                  !!!cp ('t120');                  !!!cp ('t120');
4772                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4773                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4774                    push @{$self->{open_elements}},
4775                        [$self->{head_element}, $el_category->{head}];
4776                } else {                } else {
4777                  !!!cp ('t121');                  !!!cp ('t121');
4778                }                }
# Line 3671  sub _tree_construction_main ($) { Line 4781  sub _tree_construction_main ($) {
4781                $script_start_tag->();                $script_start_tag->();
4782                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4783                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4784                redo B;                next B;
4785              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4786                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4787                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4788                  !!!cp ('t122');                  !!!cp ('t122');
4789                  ## As if </noscript>                  ## As if </noscript>
4790                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4791                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4792                                    text => $token->{tag_name}, token => $token);
4793                                    
4794                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4795                  ## As if </head>                  ## As if </head>
# Line 3705  sub _tree_construction_main ($) { Line 4816  sub _tree_construction_main ($) {
4816                } else {                } else {
4817                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4818                }                }
4819                  !!!nack ('t127.1');
4820                !!!next-token;                !!!next-token;
4821                redo B;                next B;
4822              } else {              } else {
4823                !!!cp ('t128');                !!!cp ('t128');
4824                #                #
# Line 3716  sub _tree_construction_main ($) { Line 4828  sub _tree_construction_main ($) {
4828                !!!cp ('t129');                !!!cp ('t129');
4829                ## As if </noscript>                ## As if </noscript>
4830                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4831                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4832                                  text => $token->{tag_name}, token => $token);
4833                                
4834                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4835                ## As if </head>                ## As if </head>
# Line 3738  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
4852              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4853              ## reprocess              ## reprocess
4854              redo B;              !!!ack-later;
4855                next B;
4856            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4857              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4858                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4859                  !!!cp ('t132');                  !!!cp ('t132');
4860                  ## As if <head>                  ## As if <head>
4861                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4862                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4863                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4864                        [$self->{head_element}, $el_category->{head}];
4865    
4866                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4867                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4868                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4872                  !!!cp ('t133');                  !!!cp ('t133');
4873                  ## As if </noscript>                  ## As if </noscript>
4874                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4875                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4876                                    text => 'head', token => $token);
4877                                    
4878                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4879                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4880                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4881                  !!!next-token;                  !!!next-token;
4882                  redo B;                  next B;
4883                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4884                  !!!cp ('t134');                  !!!cp ('t134');
4885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4886                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4887                  !!!next-token;                  !!!next-token;
4888                  redo B;                  next B;
4889                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4890                    !!!cp ('t134.1');
4891                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4892                                    token => $token);
4893                    ## Ignore the token
4894                    !!!next-token;
4895                    next B;
4896                } else {                } else {
4897                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4898                }                }
4899              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4900                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3780  sub _tree_construction_main ($) { Line 4902  sub _tree_construction_main ($) {
4902                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4903                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4904                  !!!next-token;                  !!!next-token;
4905                  redo B;                  next B;
4906                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4907                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4908                  !!!cp ('t137');                  !!!cp ('t137');
4909                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4910                                    text => 'noscript', token => $token);
4911                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4912                  !!!next-token;                  !!!next-token;
4913                  redo B;                  next B;
4914                } else {                } else {
4915                  !!!cp ('t138');                  !!!cp ('t138');
4916                  #                  #
# Line 3794  sub _tree_construction_main ($) { Line 4918  sub _tree_construction_main ($) {
4918              } elsif ({              } elsif ({
4919                        body => 1, html => 1,                        body => 1, html => 1,
4920                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4921                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4922                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4923                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head',, $token);  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4924                  !!!cp ('t140');                  !!!cp ('t140');
4925                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4926                                    text => $token->{tag_name}, token => $token);
4927                    ## Ignore the token
4928                    !!!next-token;
4929                    next B;
4930                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4931                    !!!cp ('t140.1');
4932                    !!!parse-error (type => 'unmatched end tag',
4933                                    text => $token->{tag_name}, token => $token);
4934                  ## Ignore the token                  ## Ignore the token
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4939                }                }
4940                              } elsif ($token->{tag_name} eq 'p') {
4941                #                !!!cp ('t142');
4942              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4943                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4944                       }->{$token->{tag_name}}) {                ## Ignore the token
4945                  !!!next-token;
4946                  next B;
4947                } elsif ($token->{tag_name} eq 'br') {
4948                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4949                  !!!cp ('t142');                  !!!cp ('t142.2');
4950                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4951                  !!!create-element ($self->{head_element}, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4952                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4953                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4954      
4955                    ## Reprocess in the "after head" insertion mode...
4956                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4957                    !!!cp ('t143.2');
4958                    ## As if </head>
4959                    pop @{$self->{open_elements}};
4960                    $self->{insertion_mode} = AFTER_HEAD_IM;
4961      
4962                    ## Reprocess in the "after head" insertion mode...
4963                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4964                    !!!cp ('t143.3');
4965                    ## ISSUE: Two parse errors for <head><noscript></br>
4966                    !!!parse-error (type => 'unmatched end tag',
4967                                    text => 'br', token => $token);
4968                    ## As if </noscript>
4969                    pop @{$self->{open_elements}};
4970                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4971    
4972                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4973                } else {                  ## As if </head>
4974                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4975                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4976    
4977                #                  ## Reprocess in the "after head" insertion mode...
4978              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4979                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4980                  #                  #
4981                } else {                } else {
4982                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4983                }                }
4984    
4985                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4986                  !!!parse-error (type => 'unmatched end tag',
4987                                  text => 'br', token => $token);
4988                  ## Ignore the token
4989                  !!!next-token;
4990                  next B;
4991                } else {
4992                  !!!cp ('t145');
4993                  !!!parse-error (type => 'unmatched end tag',
4994                                  text => $token->{tag_name}, token => $token);
4995                  ## Ignore the token
4996                  !!!next-token;
4997                  next B;
4998              }              }
4999    
5000              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5001                !!!cp ('t146');                !!!cp ('t146');
5002                ## As if </noscript>                ## As if </noscript>
5003                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5004                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5005                                  text => $token->{tag_name}, token => $token);
5006                                
5007                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5008                ## As if </head>                ## As if </head>
# Line 3864  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5019  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5020                !!!cp ('t148');                !!!cp ('t148');
5021                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5022                                  text => $token->{tag_name}, token => $token);
5023                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5024                !!!next-token;                !!!next-token;
5025                redo B;                next B;
5026              } else {              } else {
5027                !!!cp ('t149');                !!!cp ('t149');
5028              }              }
# Line 3877  sub _tree_construction_main ($) { Line 5032  sub _tree_construction_main ($) {
5032              !!!insert-element ('body',, $token);              !!!insert-element ('body',, $token);
5033              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5034              ## reprocess              ## reprocess
5035              redo B;              next B;
5036        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5037          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5038            !!!cp ('t149.1');            !!!cp ('t149.1');
5039    
5040            ## NOTE: As if <head>            ## NOTE: As if <head>
5041            !!!create-element ($self->{head_element}, 'head',, $token);            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5042            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
5043                ($self->{head_element});                ($self->{head_element});
5044            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
5045              #    [$self->{head_element}, $el_category->{head}];
5046            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
5047            ## NOTE: Reprocess.            ## NOTE: Reprocess.
5048    
# Line 3930  sub _tree_construction_main ($) { Line 5086  sub _tree_construction_main ($) {
5086          !!!insert-element ('body',, $token);          !!!insert-element ('body',, $token);
5087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5088          ## NOTE: Reprocess.          ## NOTE: Reprocess.
5089          redo B;          next B;
5090        } else {        } else {
5091          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5092        }        }
# Line 3945  sub _tree_construction_main ($) { Line 5101  sub _tree_construction_main ($) {
5101              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5102    
5103              !!!next-token;              !!!next-token;
5104              redo B;              next B;
5105            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5106              if ({              if ({
5107                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3955  sub _tree_construction_main ($) { Line 5111  sub _tree_construction_main ($) {
5111                  ## have an element in table scope                  ## have an element in table scope
5112                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5113                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5114                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5115                      !!!cp ('t151');                      !!!cp ('t151');
5116    
5117                      ## Close the cell                      ## Close the cell
5118                      !!!back-token; # <?>                      !!!back-token; # <x>
5119                      $token = {type => END_TAG_TOKEN, tag_name => $node->[1],                      $token = {type => END_TAG_TOKEN,
5120                                  tag_name => $node->[0]->manakai_local_name,
5121                                line => $token->{line},                                line => $token->{line},
5122                                column => $token->{column}};                                column => $token->{column}};
5123                      redo B;                      next B;
5124                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5125                      !!!cp ('t152');                      !!!cp ('t152');
5126                      ## ISSUE: This case can never be reached, maybe.                      ## ISSUE: This case can never be reached, maybe.
5127                      last;                      last;
# Line 3975  sub _tree_construction_main ($) { Line 5130  sub _tree_construction_main ($) {
5130    
5131                  !!!cp ('t153');                  !!!cp ('t153');
5132                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5133                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5134                  ## Ignore the token                  ## Ignore the token
5135                    !!!nack ('t153.1');
5136                  !!!next-token;                  !!!next-token;
5137                  redo B;                  next B;
5138                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5139                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5140                                    token => $token);
5141                                    
5142                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5143                  ## have a table element in table scope                  ## have a table element in table scope
# Line 3988  sub _tree_construction_main ($) { Line 5145  sub _tree_construction_main ($) {
5145                  INSCOPE: {                  INSCOPE: {
5146                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
5147                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
5148                      if ($node->[1] eq 'caption') {                      if ($node->[1] & CAPTION_EL) {
5149                        !!!cp ('t155');                        !!!cp ('t155');
5150                        $i = $_;                        $i = $_;
5151                        last INSCOPE;                        last INSCOPE;
5152                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5153                        !!!cp ('t156');                        !!!cp ('t156');
5154                        last;                        last;
5155                      }                      }
# Line 4002  sub _tree_construction_main ($) { Line 5157  sub _tree_construction_main ($) {
5157    
5158                    !!!cp ('t157');                    !!!cp ('t157');
5159                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5160                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5161                    ## Ignore the token                    ## Ignore the token
5162                      !!!nack ('t157.1');
5163                    !!!next-token;                    !!!next-token;
5164                    redo B;                    next B;
5165                  } # INSCOPE                  } # INSCOPE
5166                                    
5167                  ## generate implied end tags                  ## generate implied end tags
5168                  while ({                  while ($self->{open_elements}->[-1]->[1]
5169                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5170                    !!!cp ('t158');                    !!!cp ('t158');
5171                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5172                  }                  }
5173    
5174                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5175                    !!!cp ('t159');                    !!!cp ('t159');
5176                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5177                                      text => $self->{open_elements}->[-1]->[0]
5178                                          ->manakai_local_name,
5179                                      token => $token);
5180                  } else {                  } else {
5181                    !!!cp ('t160');                    !!!cp ('t160');
5182                  }                  }
# Line 4030  sub _tree_construction_main ($) { Line 5188  sub _tree_construction_main ($) {
5188                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5189                                    
5190                  ## reprocess                  ## reprocess
5191                  redo B;                  !!!ack-later;
5192                    next B;
5193                } else {                } else {
5194                  !!!cp ('t161');                  !!!cp ('t161');
5195                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 5205  sub _tree_construction_main ($) {
5205                  my $i;                  my $i;
5206                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5207                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5208                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5209                      !!!cp ('t163');                      !!!cp ('t163');
5210                      $i = $_;                      $i = $_;
5211                      last INSCOPE;                      last INSCOPE;
5212                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5213                      !!!cp ('t164');                      !!!cp ('t164');
5214                      last INSCOPE;                      last INSCOPE;
5215                    }                    }
5216                  } # INSCOPE                  } # INSCOPE
5217                    unless (defined $i) {                    unless (defined $i) {
5218                      !!!cp ('t165');                      !!!cp ('t165');
5219                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5220                                        text => $token->{tag_name},
5221                                        token => $token);
5222                      ## Ignore the token                      ## Ignore the token
5223                      !!!next-token;                      !!!next-token;
5224                      redo B;                      next B;
5225                    }                    }
5226                                    
5227                  ## generate implied end tags                  ## generate implied end tags
5228                  while ({                  while ($self->{open_elements}->[-1]->[1]
5229                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5230                    !!!cp ('t166');                    !!!cp ('t166');
5231                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5232                  }                  }
5233    
5234                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5235                            ne $token->{tag_name}) {
5236                    !!!cp ('t167');                    !!!cp ('t167');
5237                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5238                                      text => $self->{open_elements}->[-1]->[0]
5239                                          ->manakai_local_name,
5240                                      token => $token);
5241                  } else {                  } else {
5242                    !!!cp ('t168');                    !!!cp ('t168');
5243                  }                  }
# Line 4087  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5250                                    
5251                  !!!next-token;                  !!!next-token;
5252                  redo B;                  next B;
5253                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5254                  !!!cp ('t169');                  !!!cp ('t169');
5255                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5256                                    text => $token->{tag_name}, token => $token);
5257                  ## Ignore the token                  ## Ignore the token
5258                  !!!next-token;                  !!!next-token;
5259                  redo B;                  next B;
5260                } else {                } else {
5261                  !!!cp ('t170');                  !!!cp ('t170');
5262                  #                  #
# Line 4105  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268                  INSCOPE: {                  INSCOPE: {
5269                    for (reverse 0..$#{$self->{open_elements}}) {                    for (reverse 0..$#{$self->{open_elements}}) {
5270                      my $node = $self->{open_elements}->[$_];                      my $node = $self->{open_elements}->[$_];
5271                      if ($node->[1] eq $token->{tag_name}) {                      if ($node->[1] & CAPTION_EL) {
5272                        !!!cp ('t171');                        !!!cp ('t171');
5273                        $i = $_;                        $i = $_;
5274                        last INSCOPE;                        last INSCOPE;
5275                      } elsif ({                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
                               table => 1, html => 1,  
                              }->{$node->[1]}) {  
5276                        !!!cp ('t172');                        !!!cp ('t172');
5277                        last;                        last;
5278                      }                      }
# Line 4119  sub _tree_construction_main ($) { Line 5280  sub _tree_construction_main ($) {
5280    
5281                    !!!cp ('t173');                    !!!cp ('t173');
5282                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5283                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5284                    ## Ignore the token                    ## Ignore the token
5285                    !!!next-token;                    !!!next-token;
5286                    redo B;                    next B;
5287                  } # INSCOPE                  } # INSCOPE
5288                                    
5289                  ## generate implied end tags                  ## generate implied end tags
5290                  while ({                  while ($self->{open_elements}->[-1]->[1]
5291                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5292                    !!!cp ('t174');                    !!!cp ('t174');
5293                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5294                  }                  }
5295                                    
5296                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5297                    !!!cp ('t175');                    !!!cp ('t175');
5298                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                    !!!parse-error (type => 'not closed',
5299                                      text => $self->{open_elements}->[-1]->[0]
5300                                          ->manakai_local_name,
5301                                      token => $token);
5302                  } else {                  } else {
5303                    !!!cp ('t176');                    !!!cp ('t176');
5304                  }                  }
# Line 4147  sub _tree_construction_main ($) { Line 5310  sub _tree_construction_main ($) {
5310                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5311                                    
5312                  !!!next-token;                  !!!next-token;
5313                  redo B;                  next B;
5314                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5315                  !!!cp ('t177');                  !!!cp ('t177');
5316                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5317                                    text => $token->{tag_name}, token => $token);
5318                  ## Ignore the token                  ## Ignore the token
5319                  !!!next-token;                  !!!next-token;
5320                  redo B;                  next B;
5321                } else {                } else {
5322                  !!!cp ('t178');                  !!!cp ('t178');
5323                  #                  #
# Line 4169  sub _tree_construction_main ($) { Line 5333  sub _tree_construction_main ($) {
5333                INSCOPE: {                INSCOPE: {
5334                  for (reverse 0..$#{$self->{open_elements}}) {                  for (reverse 0..$#{$self->{open_elements}}) {
5335                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5336                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5337                      !!!cp ('t179');                      !!!cp ('t179');
5338                      $i = $_;                      $i = $_;
5339    
5340                      ## Close the cell                      ## Close the cell
5341                      !!!back-token; # </?>                      !!!back-token; # </x>
5342                      $token = {type => END_TAG_TOKEN, tag_name => $tn,                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5343                                line => $token->{line},                                line => $token->{line},
5344                                column => $token->{column}};                                column => $token->{column}};
5345                      redo B;                      next B;
5346                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5347                      !!!cp ('t180');                      !!!cp ('t180');
5348                      $tn = $node->[1];                      $tn = $node->[0]->manakai_local_name;
5349                      ## NOTE: There is exactly one |td| or |th| element                      ## NOTE: There is exactly one |td| or |th| element
5350                      ## in scope in the stack of open elements by definition.                      ## in scope in the stack of open elements by definition.
5351                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5352                      ## ISSUE: Can this be reached?                      ## ISSUE: Can this be reached?
5353                      !!!cp ('t181');                      !!!cp ('t181');
5354                      last;                      last;
# Line 4195  sub _tree_construction_main ($) { Line 5357  sub _tree_construction_main ($) {
5357    
5358                  !!!cp ('t182');                  !!!cp ('t182');
5359                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5360                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5361                  ## Ignore the token                  ## Ignore the token
5362                  !!!next-token;                  !!!next-token;
5363                  redo B;                  next B;
5364                } # INSCOPE                } # INSCOPE
5365              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5366                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5367                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5368                                  token => $token);
5369    
5370                ## As if </caption>                ## As if </caption>
5371                ## have a table element in table scope                ## have a table element in table scope
5372                my $i;                my $i;
5373                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5374                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5375                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5376                    !!!cp ('t184');                    !!!cp ('t184');
5377                    $i = $_;                    $i = $_;
5378                    last INSCOPE;                    last INSCOPE;
5379                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5380                    !!!cp ('t185');                    !!!cp ('t185');
5381                    last INSCOPE;                    last INSCOPE;
5382                  }                  }
5383                } # INSCOPE                } # INSCOPE
5384                unless (defined $i) {                unless (defined $i) {
5385                  !!!cp ('t186');                  !!!cp ('t186');
5386                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5387                                    text => 'caption', token => $token);
5388                  ## Ignore the token                  ## Ignore the token
5389                  !!!next-token;                  !!!next-token;
5390                  redo B;                  next B;
5391                }                }
5392                                
5393                ## generate implied end tags                ## generate implied end tags
5394                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5395                  !!!cp ('t187');                  !!!cp ('t187');
5396                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5397                }                }
5398    
5399                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5400                  !!!cp ('t188');                  !!!cp ('t188');
5401                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5402                                    text => $self->{open_elements}->[-1]->[0]
5403                                        ->manakai_local_name,
5404                                    token => $token);
5405                } else {                } else {
5406                  !!!cp ('t189');                  !!!cp ('t189');
5407                }                }
# Line 4250  sub _tree_construction_main ($) { Line 5413  sub _tree_construction_main ($) {
5413                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5414    
5415                ## reprocess                ## reprocess
5416                redo B;                next B;
5417              } elsif ({              } elsif ({
5418                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5419                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5420                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5421                  !!!cp ('t190');                  !!!cp ('t190');
5422                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5423                                    text => $token->{tag_name}, token => $token);
5424                  ## Ignore the token                  ## Ignore the token
5425                  !!!next-token;                  !!!next-token;
5426                  redo B;                  next B;
5427                } else {                } else {
5428                  !!!cp ('t191');                  !!!cp ('t191');
5429                  #                  #
# Line 4270  sub _tree_construction_main ($) { Line 5434  sub _tree_construction_main ($) {
5434                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5435                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5436                !!!cp ('t192');                !!!cp ('t192');
5437                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5438                                  text => $token->{tag_name}, token => $token);
5439                ## Ignore the token                ## Ignore the token
5440                !!!next-token;                !!!next-token;
5441                redo B;                next B;
5442              } else {              } else {
5443                !!!cp ('t193');                !!!cp ('t193');
5444                #                #
5445              }              }
5446        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5447          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5448            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
5449              !!!cp ('t75');              !!!cp ('t75');
5450              !!!parse-error (type => 'in body:#eof', token => $token);              !!!parse-error (type => 'in body:#eof', token => $token);
5451              last;              last;
# Line 4307  sub _tree_construction_main ($) { Line 5469  sub _tree_construction_main ($) {
5469            unless (length $token->{data}) {            unless (length $token->{data}) {
5470              !!!cp ('t194');              !!!cp ('t194');
5471              !!!next-token;              !!!next-token;
5472              redo B;              next B;
5473            } else {            } else {
5474              !!!cp ('t195');              !!!cp ('t195');
5475            }            }
5476          }          }
5477    
5478              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5479    
5480              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5481              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4321  sub _tree_construction_main ($) { Line 5483  sub _tree_construction_main ($) {
5483              ## result in a new Text node.              ## result in a new Text node.
5484              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5485                            
5486              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]}) {  
5487                # MUST                # MUST
5488                my $foster_parent_element;                my $foster_parent_element;
5489                my $next_sibling;                my $next_sibling;
5490                my $prev_sibling;                my $prev_sibling;
5491                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5492                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5493                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5494                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5495                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4365  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524          }          }
5525                            
5526          !!!next-token;          !!!next-token;
5527          redo B;          next B;
5528        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5529              if ({          if ({
5530                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5531                   th => 1, td => 1,               th => 1, td => 1,
5532                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5533                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5534                  ## Clear back to table context              ## Clear back to table context
5535                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5536                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5537                    !!!cp ('t201');                !!!cp ('t201');
5538                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5539                  }              }
5540                                
5541                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5542                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5543                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5544                }            }
5545              
5546                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5547                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5548                    !!!cp ('t202');                !!!cp ('t202');
5549                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5550                  }              }
5551                                    
5552                  ## Clear back to table body context              ## Clear back to table body context
5553                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5554                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5555                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5556                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5557                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5558                    pop @{$self->{open_elements}};              }
                 }  
5559                                    
5560                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5561                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5562                    !!!cp ('t204');                    !!!cp ('t204');
5563                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5564                      !!!nack ('t204');
5565                    !!!next-token;                    !!!next-token;
5566                    redo B;                    next B;
5567                  } else {                  } else {
5568                    !!!cp ('t205');                    !!!cp ('t205');
5569                    !!!insert-element ('tr',, $token);                    !!!insert-element ('tr',, $token);
# Line 4415  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574                }                }
5575    
5576                ## Clear back to table row context                ## Clear back to table row context
5577                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5578                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5579                  !!!cp ('t207');                  !!!cp ('t207');
5580                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5581                }                }
# Line 4427  sub _tree_construction_main ($) { Line 5585  sub _tree_construction_main ($) {
5585    
5586                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5587                                
5588                  !!!nack ('t207.1');
5589                !!!next-token;                !!!next-token;
5590                redo B;                next B;
5591              } elsif ({              } elsif ({
5592                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5593                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4440  sub _tree_construction_main ($) { Line 5599  sub _tree_construction_main ($) {
5599                  my $i;                  my $i;
5600                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5601                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5602                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5603                      !!!cp ('t208');                      !!!cp ('t208');
5604                      $i = $_;                      $i = $_;
5605                      last INSCOPE;                      last INSCOPE;
5606                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5607                      !!!cp ('t209');                      !!!cp ('t209');
5608                      last INSCOPE;                      last INSCOPE;
5609                    }                    }
5610                  } # INSCOPE                  } # INSCOPE
5611                  unless (defined $i) {                  unless (defined $i) {
5612                   !!!cp ('t210');                    !!!cp ('t210');
5613  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5614                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5615                                      text => $token->{tag_name}, token => $token);
5616                    ## Ignore the token                    ## Ignore the token
5617                      !!!nack ('t210.1');
5618                    !!!next-token;                    !!!next-token;
5619                    redo B;                    next B;
5620                  }                  }
5621                                    
5622                  ## Clear back to table row context                  ## Clear back to table row context
5623                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5624                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5625                    !!!cp ('t211');                    !!!cp ('t211');
5626                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5627                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4477  sub _tree_construction_main ($) { Line 5632  sub _tree_construction_main ($) {
5632                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5633                    !!!cp ('t212');                    !!!cp ('t212');
5634                    ## reprocess                    ## reprocess
5635                    redo B;                    !!!ack-later;
5636                      next B;
5637                  } else {                  } else {
5638                    !!!cp ('t213');                    !!!cp ('t213');
5639                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4489  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645                  my $i;                  my $i;
5646                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5647                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5648                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5649                      !!!cp ('t214');                      !!!cp ('t214');
5650                      $i = $_;                      $i = $_;
5651                      last INSCOPE;                      last INSCOPE;
5652                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5653                      !!!cp ('t215');                      !!!cp ('t215');
5654                      last INSCOPE;                      last INSCOPE;
5655                    }                    }
5656                  } # INSCOPE                  } # INSCOPE
5657                  unless (defined $i) {                  unless (defined $i) {
5658                    !!!cp ('t216');                    !!!cp ('t216');
5659  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5660                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5661                                      text => $token->{tag_name}, token => $token);
5662                    ## Ignore the token                    ## Ignore the token
5663                      !!!nack ('t216.1');
5664                    !!!next-token;                    !!!next-token;
5665                    redo B;                    next B;
5666                  }                  }
5667    
5668                  ## Clear back to table body context                  ## Clear back to table body context
5669                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5670                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5671                    !!!cp ('t217');                    !!!cp ('t217');
5672                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5673                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4536  sub _tree_construction_main ($) { Line 5689  sub _tree_construction_main ($) {
5689    
5690                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5691                  ## Clear back to table context                  ## Clear back to table context
5692                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5693                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5694                    !!!cp ('t219');                    !!!cp ('t219');
5695                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5696                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4546  sub _tree_construction_main ($) { Line 5699  sub _tree_construction_main ($) {
5699                  !!!insert-element ('colgroup',, $token);                  !!!insert-element ('colgroup',, $token);
5700                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5701                  ## reprocess                  ## reprocess
5702                  redo B;                  !!!ack-later;
5703                    next B;
5704                } elsif ({                } elsif ({
5705                          caption => 1,                          caption => 1,
5706                          colgroup => 1,                          colgroup => 1,
5707                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5708                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5709                  ## Clear back to table context                  ## Clear back to table context
5710                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5711                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5712                    !!!cp ('t220');                    !!!cp ('t220');
5713                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5714                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4572  sub _tree_construction_main ($) { Line 5726  sub _tree_construction_main ($) {
5726                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5727                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5728                  !!!next-token;                  !!!next-token;
5729                  redo B;                  !!!nack ('t220.1');
5730                    next B;
5731                } else {                } else {
5732                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5733                }                }
5734              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5735                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
5736                                  text => $self->{open_elements}->[-1]->[0]
5737                                      ->manakai_local_name,
5738                                  token => $token);
5739    
5740                ## As if </table>                ## As if </table>
5741                ## have a table element in table scope                ## have a table element in table scope
5742                my $i;                my $i;
5743                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5744                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5745                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5746                    !!!cp ('t221');                    !!!cp ('t221');
5747                    $i = $_;                    $i = $_;
5748                    last INSCOPE;                    last INSCOPE;
5749                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5750                    !!!cp ('t222');                    !!!cp ('t222');
5751                    last INSCOPE;                    last INSCOPE;
5752                  }                  }
# Line 4599  sub _tree_construction_main ($) { Line 5754  sub _tree_construction_main ($) {
5754                unless (defined $i) {                unless (defined $i) {
5755                  !!!cp ('t223');                  !!!cp ('t223');
5756  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5757                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5758                                    token => $token);
5759                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5760                    !!!nack ('t223.1');
5761                  !!!next-token;                  !!!next-token;
5762                  redo B;                  next B;
5763                }                }
5764                                
5765  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5766                ## generate implied end tags                ## generate implied end tags
5767                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5768                  !!!cp ('t224');                  !!!cp ('t224');
5769                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5770                }                }
5771    
5772                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5773                  !!!cp ('t225');                  !!!cp ('t225');
5774  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5775                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                  !!!parse-error (type => 'not closed',
5776                                    text => $self->{open_elements}->[-1]->[0]
5777                                        ->manakai_local_name,
5778                                    token => $token);
5779                } else {                } else {
5780                  !!!cp ('t226');                  !!!cp ('t226');
5781                }                }
# Line 4627  sub _tree_construction_main ($) { Line 5785  sub _tree_construction_main ($) {
5785    
5786                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5787    
5788                ## reprocess            ## reprocess
5789                redo B;            !!!ack-later;
5790              next B;
5791          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5792            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5793              !!!cp ('t227.8');              !!!cp ('t227.8');
5794              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5795              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5796              redo B;              next B;
5797            } else {            } else {
5798              !!!cp ('t227.7');              !!!cp ('t227.7');
5799              #              #
# Line 4644  sub _tree_construction_main ($) { Line 5803  sub _tree_construction_main ($) {
5803              !!!cp ('t227.6');              !!!cp ('t227.6');
5804              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5805              $script_start_tag->();              $script_start_tag->();
5806              redo B;              next B;
5807            } else {            } else {
5808              !!!cp ('t227.5');              !!!cp ('t227.5');
5809              #              #
# Line 4655  sub _tree_construction_main ($) { Line 5814  sub _tree_construction_main ($) {
5814                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5815                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5816                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5817                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5818                                    text => $token->{tag_name}, token => $token);
5819    
5820                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5821    
# Line 4664  sub _tree_construction_main ($) { Line 5824  sub _tree_construction_main ($) {
5824                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5825    
5826                  !!!next-token;                  !!!next-token;
5827                  redo B;                  !!!ack ('t227.2.1');
5828                    next B;
5829                } else {                } else {
5830                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5831                  #                  #
# Line 4682  sub _tree_construction_main ($) { Line 5843  sub _tree_construction_main ($) {
5843            #            #
5844          }          }
5845    
5846          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5847                            token => $token);
5848    
5849          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5850          #          #
# Line 4693  sub _tree_construction_main ($) { Line 5855  sub _tree_construction_main ($) {
5855                my $i;                my $i;
5856                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5857                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5858                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5859                    !!!cp ('t228');                    !!!cp ('t228');
5860                    $i = $_;                    $i = $_;
5861                    last INSCOPE;                    last INSCOPE;
5862                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5863                    !!!cp ('t229');                    !!!cp ('t229');
5864                    last INSCOPE;                    last INSCOPE;
5865                  }                  }
5866                } # INSCOPE                } # INSCOPE
5867                unless (defined $i) {                unless (defined $i) {
5868                  !!!cp ('t230');                  !!!cp ('t230');
5869                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5870                                    text => $token->{tag_name}, token => $token);
5871                  ## Ignore the token                  ## Ignore the token
5872                    !!!nack ('t230.1');
5873                  !!!next-token;                  !!!next-token;
5874                  redo B;                  next B;
5875                } else {                } else {
5876                  !!!cp ('t232');                  !!!cp ('t232');
5877                }                }
5878    
5879                ## Clear back to table row context                ## Clear back to table row context
5880                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5881                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5882                  !!!cp ('t231');                  !!!cp ('t231');
5883  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5884                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4726  sub _tree_construction_main ($) { Line 5887  sub _tree_construction_main ($) {
5887                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5888                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5889                !!!next-token;                !!!next-token;
5890                redo B;                !!!nack ('t231.1');
5891                  next B;
5892              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5893                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5894                  ## As if </tr>                  ## As if </tr>
# Line 4734  sub _tree_construction_main ($) { Line 5896  sub _tree_construction_main ($) {
5896                  my $i;                  my $i;
5897                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5898                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5899                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5900                      !!!cp ('t233');                      !!!cp ('t233');
5901                      $i = $_;                      $i = $_;
5902                      last INSCOPE;                      last INSCOPE;
5903                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5904                      !!!cp ('t234');                      !!!cp ('t234');
5905                      last INSCOPE;                      last INSCOPE;
5906                    }                    }
# Line 4748  sub _tree_construction_main ($) { Line 5908  sub _tree_construction_main ($) {
5908                  unless (defined $i) {                  unless (defined $i) {
5909                    !!!cp ('t235');                    !!!cp ('t235');
5910  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5911                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5912                                      text => $token->{type}, token => $token);
5913                    ## Ignore the token                    ## Ignore the token
5914                      !!!nack ('t236.1');
5915                    !!!next-token;                    !!!next-token;
5916                    redo B;                    next B;
5917                  }                  }
5918                                    
5919                  ## Clear back to table row context                  ## Clear back to table row context
5920                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5921                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5922                    !!!cp ('t236');                    !!!cp ('t236');
5923  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5924                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4773  sub _tree_construction_main ($) { Line 5934  sub _tree_construction_main ($) {
5934                  my $i;                  my $i;
5935                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5936                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5937                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5938                      !!!cp ('t237');                      !!!cp ('t237');
5939                      $i = $_;                      $i = $_;
5940                      last INSCOPE;                      last INSCOPE;
5941                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5942                      !!!cp ('t238');                      !!!cp ('t238');
5943                      last INSCOPE;                      last INSCOPE;
5944                    }                    }
5945                  } # INSCOPE                  } # INSCOPE
5946                  unless (defined $i) {                  unless (defined $i) {
5947                    !!!cp ('t239');                    !!!cp ('t239');
5948                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5949                                      text => $token->{tag_name}, token => $token);
5950                    ## Ignore the token                    ## Ignore the token
5951                      !!!nack ('t239.1');
5952                    !!!next-token;                    !!!next-token;
5953                    redo B;                    next B;
5954                  }                  }
5955                                    
5956                  ## Clear back to table body context                  ## Clear back to table body context
5957                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5958                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5959                    !!!cp ('t240');                    !!!cp ('t240');
5960                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5961                  }                  }
# Line 4823  sub _tree_construction_main ($) { Line 5981  sub _tree_construction_main ($) {
5981                my $i;                my $i;
5982                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5983                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5984                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5985                    !!!cp ('t241');                    !!!cp ('t241');
5986                    $i = $_;                    $i = $_;
5987                    last INSCOPE;                    last INSCOPE;
5988                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5989                    !!!cp ('t242');                    !!!cp ('t242');
5990                    last INSCOPE;                    last INSCOPE;
5991                  }                  }
5992                } # INSCOPE                } # INSCOPE
5993                unless (defined $i) {                unless (defined $i) {
5994                  !!!cp ('t243');                  !!!cp ('t243');
5995                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5996                                    text => $token->{tag_name}, token => $token);
5997                  ## Ignore the token                  ## Ignore the token
5998                    !!!nack ('t243.1');
5999                  !!!next-token;                  !!!next-token;
6000                  redo B;                  next B;
6001                }                }
6002                                    
6003                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4848  sub _tree_construction_main ($) { Line 6006  sub _tree_construction_main ($) {
6006                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6007                                
6008                !!!next-token;                !!!next-token;
6009                redo B;                next B;
6010              } elsif ({              } elsif ({
6011                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6012                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4858  sub _tree_construction_main ($) { Line 6016  sub _tree_construction_main ($) {
6016                  my $i;                  my $i;
6017                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6018                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6019                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6020                      !!!cp ('t247');                      !!!cp ('t247');
6021                      $i = $_;                      $i = $_;
6022                      last INSCOPE;                      last INSCOPE;
6023                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6024                      !!!cp ('t248');                      !!!cp ('t248');
6025                      last INSCOPE;                      last INSCOPE;
6026                    }                    }
6027                  } # INSCOPE                  } # INSCOPE
6028                    unless (defined $i) {                    unless (defined $i) {
6029                      !!!cp ('t249');                      !!!cp ('t249');
6030                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6031                                        text => $token->{tag_name}, token => $token);
6032                      ## Ignore the token                      ## Ignore the token
6033                        !!!nack ('t249.1');
6034                      !!!next-token;                      !!!next-token;
6035                      redo B;                      next B;
6036                    }                    }
6037                                    
6038                  ## As if </tr>                  ## As if </tr>
# Line 4882  sub _tree_construction_main ($) { Line 6040  sub _tree_construction_main ($) {
6040                  my $i;                  my $i;
6041                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6042                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6043                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6044                      !!!cp ('t250');                      !!!cp ('t250');
6045                      $i = $_;                      $i = $_;
6046                      last INSCOPE;                      last INSCOPE;
6047                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6048                      !!!cp ('t251');                      !!!cp ('t251');
6049                      last INSCOPE;                      last INSCOPE;
6050                    }                    }
6051                  } # INSCOPE                  } # INSCOPE
6052                    unless (defined $i) {                    unless (defined $i) {
6053                      !!!cp ('t252');                      !!!cp ('t252');
6054                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6055                                        text => 'tr', token => $token);
6056                      ## Ignore the token                      ## Ignore the token
6057                        !!!nack ('t252.1');
6058                      !!!next-token;                      !!!next-token;
6059                      redo B;                      next B;
6060                    }                    }
6061                                    
6062                  ## Clear back to table row context                  ## Clear back to table row context
6063                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6064                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6065                    !!!cp ('t253');                    !!!cp ('t253');
6066  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6067                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4919  sub _tree_construction_main ($) { Line 6076  sub _tree_construction_main ($) {
6076                my $i;                my $i;
6077                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6078                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6079                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6080                    !!!cp ('t254');                    !!!cp ('t254');
6081                    $i = $_;                    $i = $_;
6082                    last INSCOPE;                    last INSCOPE;
6083                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6084                    !!!cp ('t255');                    !!!cp ('t255');
6085                    last INSCOPE;                    last INSCOPE;
6086                  }                  }
6087                } # INSCOPE                } # INSCOPE
6088                unless (defined $i) {                unless (defined $i) {
6089                  !!!cp ('t256');                  !!!cp ('t256');
6090                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6091                                    text => $token->{tag_name}, token => $token);
6092                  ## Ignore the token                  ## Ignore the token
6093                    !!!nack ('t256.1');
6094                  !!!next-token;                  !!!next-token;
6095                  redo B;                  next B;
6096                }                }
6097    
6098                ## Clear back to table body context                ## Clear back to table body context
6099                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6100                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6101                  !!!cp ('t257');                  !!!cp ('t257');
6102  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6103                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4949  sub _tree_construction_main ($) { Line 6105  sub _tree_construction_main ($) {
6105    
6106                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6107                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6108                  !!!nack ('t257.1');
6109                !!!next-token;                !!!next-token;
6110                redo B;                next B;
6111              } elsif ({              } elsif ({
6112                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6113                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6114                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6115                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6116                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6117                !!!cp ('t258');            !!!cp ('t258');
6118                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6119                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6120                !!!next-token;            ## Ignore the token
6121                redo B;            !!!nack ('t258.1');
6122               !!!next-token;
6123              next B;
6124          } else {          } else {
6125            !!!cp ('t259');            !!!cp ('t259');
6126            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6127                              text => $token->{tag_name}, token => $token);
6128    
6129            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6130            #            #
6131          }          }
6132        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6133          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6134                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6135            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
6136            !!!cp ('t259.1');            !!!cp ('t259.1');
# Line 4992  sub _tree_construction_main ($) { Line 6152  sub _tree_construction_main ($) {
6152                unless (length $token->{data}) {                unless (length $token->{data}) {
6153                  !!!cp ('t260');                  !!!cp ('t260');
6154                  !!!next-token;                  !!!next-token;
6155                  redo B;                  next B;
6156                }                }
6157              }              }
6158                            
# Line 5003  sub _tree_construction_main ($) { Line 6163  sub _tree_construction_main ($) {
6163                !!!cp ('t262');                !!!cp ('t262');
6164                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6165                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6166                  !!!ack ('t262.1');
6167                !!!next-token;                !!!next-token;
6168                redo B;                next B;
6169              } else {              } else {
6170                !!!cp ('t263');                !!!cp ('t263');
6171                #                #
6172              }              }
6173            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6174              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6175                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6176                  !!!cp ('t264');                  !!!cp ('t264');
6177                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6178                                    text => 'colgroup', token => $token);
6179                  ## Ignore the token                  ## Ignore the token
6180                  !!!next-token;                  !!!next-token;
6181                  redo B;                  next B;
6182                } else {                } else {
6183                  !!!cp ('t265');                  !!!cp ('t265');
6184                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6185                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6186                  !!!next-token;                  !!!next-token;
6187                  redo B;                              next B;            
6188                }                }
6189              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6190                !!!cp ('t266');                !!!cp ('t266');
6191                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6192                                  text => 'col', token => $token);
6193                ## Ignore the token                ## Ignore the token
6194                !!!next-token;                !!!next-token;
6195                redo B;                next B;
6196              } else {              } else {
6197                !!!cp ('t267');                !!!cp ('t267');
6198                #                #
6199              }              }
6200        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6201          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6202              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
6203            !!!cp ('t270.2');            !!!cp ('t270.2');
6204            ## Stop parsing.            ## Stop parsing.
# Line 5046  sub _tree_construction_main ($) { Line 6209  sub _tree_construction_main ($) {
6209            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
6210            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6211            ## Reprocess.            ## Reprocess.
6212            redo B;            next B;
6213          }          }
6214        } else {        } else {
6215          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6216        }        }
6217    
6218            ## As if </colgroup>            ## As if </colgroup>
6219            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6220              !!!cp ('t269');              !!!cp ('t269');
6221  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6222              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6223                                text => 'colgroup', token => $token);
6224              ## Ignore the token              ## Ignore the token
6225                !!!nack ('t269.1');
6226              !!!next-token;              !!!next-token;
6227              redo B;              next B;
6228            } else {            } else {
6229              !!!cp ('t270');              !!!cp ('t270');
6230              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6231              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6232                !!!ack-later;
6233              ## reprocess              ## reprocess
6234              redo B;              next B;
6235            }            }
6236      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6237        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6238          !!!cp ('t271');          !!!cp ('t271');
6239          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6240          !!!next-token;          !!!next-token;
6241          redo B;          next B;
6242        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6243              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6244                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6245                  !!!cp ('t272');              !!!cp ('t272');
6246                  ## As if </option>              ## As if </option>
6247                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6248                } else {            } else {
6249                  !!!cp ('t273');              !!!cp ('t273');
6250                }            }
6251    
6252                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6253                !!!next-token;            !!!nack ('t273.1');
6254                redo B;            !!!next-token;
6255              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6256                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6257                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6258                  ## As if </option>              !!!cp ('t274');
6259                  pop @{$self->{open_elements}};              ## As if </option>
6260                } else {              pop @{$self->{open_elements}};
6261                  !!!cp ('t275');            } else {
6262                }              !!!cp ('t275');
6263              }
6264    
6265                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6266                  !!!cp ('t276');              !!!cp ('t276');
6267                  ## As if </optgroup>              ## As if </optgroup>
6268                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6269                } else {            } else {
6270                  !!!cp ('t277');              !!!cp ('t277');
6271                }            }
6272    
6273                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6274                !!!next-token;            !!!nack ('t277.1');
6275                redo B;            !!!next-token;
6276          } elsif ($token->{tag_name} eq 'select' or            next B;
6277                   $token->{tag_name} eq 'input' or          } elsif ({
6278                       select => 1, input => 1, textarea => 1,
6279                     }->{$token->{tag_name}} or
6280                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6281                    {                    {
6282                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5115  sub _tree_construction_main ($) { Line 6284  sub _tree_construction_main ($) {
6284                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6285                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6286            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6287            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6288                              token => $token);
6289            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6290            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6291                ## have an element in table scope            ## have an element in table scope
6292                my $i;            my $i;
6293                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6294                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6295                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6296                    !!!cp ('t278');                !!!cp ('t278');
6297                    $i = $_;                $i = $_;
6298                    last INSCOPE;                last INSCOPE;
6299                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6300                            table => 1, html => 1,                !!!cp ('t279');
6301                           }->{$node->[1]}) {                last INSCOPE;
6302                    !!!cp ('t279');              }
6303                    last INSCOPE;            } # INSCOPE
6304                  }            unless (defined $i) {
6305                } # INSCOPE              !!!cp ('t280');
6306                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6307                  !!!cp ('t280');                              text => 'select', token => $token);
6308                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              ## Ignore the token
6309                  ## Ignore the token              !!!nack ('t280.1');
6310                  !!!next-token;              !!!next-token;
6311                  redo B;              next B;
6312                }            }
6313                                
6314                !!!cp ('t281');            !!!cp ('t281');
6315                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6316    
6317                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6318    
6319            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6320              !!!cp ('t281.2');              !!!nack ('t281.2');
6321              !!!next-token;              !!!next-token;
6322              redo B;              next B;
6323            } else {            } else {
6324              !!!cp ('t281.1');              !!!cp ('t281.1');
6325                !!!ack-later;
6326              ## Reprocess the token.              ## Reprocess the token.
6327              redo B;              next B;
6328            }            }
6329          } else {          } else {
6330            !!!cp ('t282');            !!!cp ('t282');
6331            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6332                              text => $token->{tag_name}, token => $token);
6333            ## Ignore the token            ## Ignore the token
6334              !!!nack ('t282.1');
6335            !!!next-token;            !!!next-token;
6336            redo B;            next B;
6337          }          }
6338        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6339              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6340                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6341                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6342                  !!!cp ('t283');              !!!cp ('t283');
6343                  ## As if </option>              ## As if </option>
6344                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6345                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6346                  !!!cp ('t284');              !!!cp ('t284');
6347                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6348                } else {            } else {
6349                  !!!cp ('t285');              !!!cp ('t285');
6350                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6351                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6352                }              ## Ignore the token
6353                !!!next-token;            }
6354                redo B;            !!!nack ('t285.1');
6355              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6356                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6357                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6358                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6359                } else {              !!!cp ('t286');
6360                  !!!cp ('t287');              pop @{$self->{open_elements}};
6361                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            } else {
6362                  ## Ignore the token              !!!cp ('t287');
6363                }              !!!parse-error (type => 'unmatched end tag',
6364                !!!next-token;                              text => $token->{tag_name}, token => $token);
6365                redo B;              ## Ignore the token
6366              } elsif ($token->{tag_name} eq 'select') {            }
6367                ## have an element in table scope            !!!nack ('t287.1');
6368                my $i;            !!!next-token;
6369                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6370                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6371                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6372                    !!!cp ('t288');            my $i;
6373                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6374                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6375                  } elsif ({              if ($node->[1] & SELECT_EL) {
6376                            table => 1, html => 1,                !!!cp ('t288');
6377                           }->{$node->[1]}) {                $i = $_;
6378                    !!!cp ('t289');                last INSCOPE;
6379                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6380                  }                !!!cp ('t289');
6381                } # INSCOPE                last INSCOPE;
6382                unless (defined $i) {              }
6383                  !!!cp ('t290');            } # INSCOPE
6384                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            unless (defined $i) {
6385                  ## Ignore the token              !!!cp ('t290');
6386                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6387                  redo B;                              text => $token->{tag_name}, token => $token);
6388                }              ## Ignore the token
6389                !!!nack ('t290.1');
6390                !!!next-token;
6391                next B;
6392              }
6393                                
6394                !!!cp ('t291');            !!!cp ('t291');
6395                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6396    
6397                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6398    
6399                !!!next-token;            !!!nack ('t291.1');
6400                redo B;            !!!next-token;
6401              next B;
6402          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6403                   {                   {
6404                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6405                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6406                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6407  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6408                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6409                              text => $token->{tag_name}, token => $token);
6410                                
6411                ## have an element in table scope            ## have an element in table scope
6412                my $i;            my $i;
6413                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6414                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6415                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6416                    !!!cp ('t292');                !!!cp ('t292');
6417                    $i = $_;                $i = $_;
6418                    last INSCOPE;                last INSCOPE;
6419                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6420                            table => 1, html => 1,                !!!cp ('t293');
6421                           }->{$node->[1]}) {                last INSCOPE;
6422                    !!!cp ('t293');              }
6423                    last INSCOPE;            } # INSCOPE
6424                  }            unless (defined $i) {
6425                } # INSCOPE              !!!cp ('t294');
6426                unless (defined $i) {              ## Ignore the token
6427                  !!!cp ('t294');              !!!nack ('t294.1');
6428                  ## Ignore the token              !!!next-token;
6429                  !!!next-token;              next B;
6430                  redo B;            }
               }  
6431                                
6432                ## As if </select>            ## As if </select>
6433                ## have an element in table scope            ## have an element in table scope
6434                undef $i;            undef $i;
6435                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6436                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6437                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6438                    !!!cp ('t295');                !!!cp ('t295');
6439                    $i = $_;                $i = $_;
6440                    last INSCOPE;                last INSCOPE;
6441                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6442  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6443                    !!!cp ('t296');                !!!cp ('t296');
6444                    last INSCOPE;                last INSCOPE;
6445                  }              }
6446                } # INSCOPE            } # INSCOPE
6447                unless (defined $i) {            unless (defined $i) {
6448                  !!!cp ('t297');              !!!cp ('t297');
6449  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6450                  !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6451                  ## Ignore the </select> token                              text => 'select', token => $token);
6452                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6453                  redo B;              !!!nack ('t297.1');
6454                }              !!!next-token; ## TODO: ok?
6455                next B;
6456              }
6457                                
6458                !!!cp ('t298');            !!!cp ('t298');
6459                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6460    
6461                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6462    
6463                ## reprocess            !!!ack-later;
6464                redo B;            ## reprocess
6465              next B;
6466          } else {          } else {
6467            !!!cp ('t299');            !!!cp ('t299');
6468            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6469                              text => $token->{tag_name}, token => $token);
6470            ## Ignore the token            ## Ignore the token
6471              !!!nack ('t299.3');
6472            !!!next-token;            !!!next-token;
6473            redo B;            next B;
6474          }          }
6475        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6476          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6477                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6478            !!!cp ('t299.1');            !!!cp ('t299.1');
6479            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5317  sub _tree_construction_main ($) { Line 6498  sub _tree_construction_main ($) {
6498            unless (length $token->{data}) {            unless (length $token->{data}) {
6499              !!!cp ('t300');              !!!cp ('t300');
6500              !!!next-token;              !!!next-token;
6501              redo B;              next B;
6502            }            }
6503          }          }
6504                    
6505          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6506            !!!cp ('t301');            !!!cp ('t301');
6507            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6508    
6509            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6510          } else {          } else {
# Line 5331  sub _tree_construction_main ($) { Line 6512  sub _tree_construction_main ($) {
6512          }          }
6513                    
6514          ## "after body" insertion mode          ## "after body" insertion mode
6515          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6516    
6517          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6518          ## reprocess          ## reprocess
6519          redo B;          next B;
6520        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6521          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6522            !!!cp ('t303');            !!!cp ('t303');
6523            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6524                              text => $token->{tag_name}, token => $token);
6525                        
6526            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6527          } else {          } else {
# Line 5347  sub _tree_construction_main ($) { Line 6529  sub _tree_construction_main ($) {
6529          }          }
6530    
6531          ## "after body" insertion mode          ## "after body" insertion mode
6532          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6533                            text => $token->{tag_name}, token => $token);
6534    
6535          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6536            !!!ack-later;
6537          ## reprocess          ## reprocess
6538          redo B;          next B;
6539        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6540          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6541            !!!cp ('t305');            !!!cp ('t305');
6542            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6543                              text => $token->{tag_name}, token => $token);
6544                        
6545            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6546            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5367  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6553            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6554              !!!cp ('t307');              !!!cp ('t307');
6555              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6556                                text => 'html', token => $token);
6557              ## Ignore the token              ## Ignore the token
6558              !!!next-token;              !!!next-token;
6559              redo B;              next B;
6560            } else {            } else {
6561              !!!cp ('t308');              !!!cp ('t308');
6562              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6563              !!!next-token;              !!!next-token;
6564              redo B;              next B;
6565            }            }
6566          } else {          } else {
6567            !!!cp ('t309');            !!!cp ('t309');
6568            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6569                              text => $token->{tag_name}, token => $token);
6570    
6571            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6572            ## reprocess            ## reprocess
6573            redo B;            next B;
6574          }          }
6575        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6576          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5400  sub _tree_construction_main ($) { Line 6587  sub _tree_construction_main ($) {
6587            unless (length $token->{data}) {            unless (length $token->{data}) {
6588              !!!cp ('t310');              !!!cp ('t310');
6589              !!!next-token;              !!!next-token;
6590              redo B;              next B;
6591            }            }
6592          }          }
6593                    
6594          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6595            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6596              !!!cp ('t311');              !!!cp ('t311');
6597              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6598            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6599              !!!cp ('t312');              !!!cp ('t312');
6600              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6601            } else { # "after html frameset"            } else { # "after after frameset"
6602              !!!cp ('t313');              !!!cp ('t313');
6603              !!!parse-error (type => 'after html:#character', token => $token);              !!!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', token => $token);  
6604            }            }
6605                        
6606            ## Ignore the token.            ## Ignore the token.
# Line 5428  sub _tree_construction_main ($) { Line 6611  sub _tree_construction_main ($) {
6611              !!!cp ('t315');              !!!cp ('t315');
6612              !!!next-token;              !!!next-token;
6613            }            }
6614            redo B;            next B;
6615          }          }
6616                    
6617          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6618        } 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}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6619          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6620              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6621            !!!cp ('t318');            !!!cp ('t318');
6622            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6623              !!!nack ('t318.1');
6624            !!!next-token;            !!!next-token;
6625            redo B;            next B;
6626          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6627                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6628            !!!cp ('t319');            !!!cp ('t319');
6629            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6630            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6631              !!!ack ('t319.1');
6632            !!!next-token;            !!!next-token;
6633            redo B;            next B;
6634          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6635            !!!cp ('t320');            !!!cp ('t320');
6636            ## NOTE: As if in body.            ## NOTE: As if in head.
6637            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6638            redo B;            next B;
6639    
6640              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6641              ## has no parse error.
6642          } else {          } else {
6643            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6644              !!!cp ('t321');              !!!cp ('t321');
6645              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6646            } else {                              text => $token->{tag_name}, token => $token);
6647              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6648              !!!cp ('t322');              !!!cp ('t322');
6649              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6650                                text => $token->{tag_name}, token => $token);
6651              } else { # "after after frameset"
6652                !!!cp ('t322.2');
6653                !!!parse-error (type => 'after after frameset',
6654                                text => $token->{tag_name}, token => $token);
6655            }            }
6656            ## Ignore the token            ## Ignore the token
6657              !!!nack ('t322.1');
6658            !!!next-token;            !!!next-token;
6659            redo B;            next B;
6660          }          }
6661        } 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}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6662          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6663              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6664            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6665                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6666              !!!cp ('t325');              !!!cp ('t325');
6667              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6668                                text => $token->{tag_name}, token => $token);
6669              ## Ignore the token              ## Ignore the token
6670              !!!next-token;              !!!next-token;
6671            } else {            } else {
# Line 5499  sub _tree_construction_main ($) { Line 6675  sub _tree_construction_main ($) {
6675            }            }
6676    
6677            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6678                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6679              !!!cp ('t327');              !!!cp ('t327');
6680              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6681            } else {            } else {
6682              !!!cp ('t328');              !!!cp ('t328');
6683            }            }
6684            redo B;            next B;
6685          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6686                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6687            !!!cp ('t329');            !!!cp ('t329');
6688            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6689            !!!next-token;            !!!next-token;
6690            redo B;            next B;
6691          } else {          } else {
6692            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6693              !!!cp ('t330');              !!!cp ('t330');
6694              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6695            } else {                              text => $token->{tag_name}, token => $token);
6696              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6697                !!!cp ('t330.1');
6698                !!!parse-error (type => 'after frameset:/',
6699                                text => $token->{tag_name}, token => $token);
6700              } else { # "after after html"
6701              !!!cp ('t331');              !!!cp ('t331');
6702              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6703                                text => $token->{tag_name}, token => $token);
6704            }            }
6705            ## Ignore the token            ## Ignore the token
6706            !!!next-token;            !!!next-token;
6707            redo B;            next B;
6708          }          }
6709        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6710          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6711                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6712            !!!cp ('t331.1');            !!!cp ('t331.1');
6713            !!!parse-error (type => 'in body:#eof', token => $token);            !!!parse-error (type => 'in body:#eof', token => $token);
# Line 5550  sub _tree_construction_main ($) { Line 6732  sub _tree_construction_main ($) {
6732          !!!cp ('t332');          !!!cp ('t332');
6733          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6734          $script_start_tag->();          $script_start_tag->();
6735          redo B;          next B;
6736        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6737          !!!cp ('t333');          !!!cp ('t333');
6738          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6739          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6740          redo B;          next B;
6741        } elsif ({        } elsif ({
6742                  base => 1, link => 1,                  base => 1, link => 1,
6743                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5563  sub _tree_construction_main ($) { Line 6745  sub _tree_construction_main ($) {
6745          ## 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
6746          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6747          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6748            !!!ack ('t334.1');
6749          !!!next-token;          !!!next-token;
6750          redo B;          next B;
6751        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6752          ## 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
6753          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6754          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.
6755    
6756          unless ($self->{confident}) {          unless ($self->{confident}) {
6757            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6758              !!!cp ('t335');              !!!cp ('t335');
6759                ## NOTE: Whether the encoding is supported or not is handled
6760                ## in the {change_encoding} callback.
6761              $self->{change_encoding}              $self->{change_encoding}
6762                  ->($self, $token->{attributes}->{charset}->{value}, $token);                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6763                            
# Line 5581  sub _tree_construction_main ($) { Line 6766  sub _tree_construction_main ($) {
6766                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6767                                           ->{has_reference});                                           ->{has_reference});
6768            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6769              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6770                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6771                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6772                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6773                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6774                !!!cp ('t336');                !!!cp ('t336');
6775                  ## NOTE: Whether the encoding is supported or not is handled
6776                  ## in the {change_encoding} callback.
6777                $self->{change_encoding}                $self->{change_encoding}
6778                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6779                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 5613  sub _tree_construction_main ($) { Line 6799  sub _tree_construction_main ($) {
6799            }            }
6800          }          }
6801    
6802            !!!ack ('t338.1');
6803          !!!next-token;          !!!next-token;
6804          redo B;          next B;
6805        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6806          !!!cp ('t341');          !!!cp ('t341');
6807          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6808          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6809          redo B;          next B;
6810        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6811          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6812                                
6813          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6814              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6815            !!!cp ('t342');            !!!cp ('t342');
6816            ## Ignore the token            ## Ignore the token
6817          } else {          } else {
# Line 5638  sub _tree_construction_main ($) { Line 6825  sub _tree_construction_main ($) {
6825              }              }
6826            }            }
6827          }          }
6828            !!!nack ('t343.1');
6829          !!!next-token;          !!!next-token;
6830          redo B;          next B;
6831        } elsif ({        } elsif ({
6832                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6833                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
# Line 5654  sub _tree_construction_main ($) { Line 6842  sub _tree_construction_main ($) {
6842            !!!cp ('t350');            !!!cp ('t350');
6843            !!!parse-error (type => 'in form:form', token => $token);            !!!parse-error (type => 'in form:form', token => $token);
6844            ## Ignore the token            ## Ignore the token
6845              !!!nack ('t350.1');
6846            !!!next-token;            !!!next-token;
6847            redo B;            next B;
6848          }          }
6849    
6850          ## has a p element in scope          ## has a p element in scope
6851          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6852            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6853              !!!cp ('t344');              !!!cp ('t344');
6854              !!!back-token;              !!!back-token; # <form>
6855              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6856                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6857              redo B;              next B;
6858            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6859              !!!cp ('t345');              !!!cp ('t345');
6860              last INSCOPE;              last INSCOPE;
6861            }            }
# Line 5677  sub _tree_construction_main ($) { Line 6863  sub _tree_construction_main ($) {
6863                        
6864          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6865          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6866              !!!nack ('t346.1');
6867            !!!next-token;            !!!next-token;
6868            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6869              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5693  sub _tree_construction_main ($) { Line 6880  sub _tree_construction_main ($) {
6880            !!!cp ('t347.1');            !!!cp ('t347.1');
6881            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
6882    
6883              !!!nack ('t347.2');
6884            !!!next-token;            !!!next-token;
6885          } elsif ($token->{tag_name} eq 'table') {          } elsif ($token->{tag_name} eq 'table') {
6886            !!!cp ('t382');            !!!cp ('t382');
# Line 5700  sub _tree_construction_main ($) { Line 6888  sub _tree_construction_main ($) {
6888                        
6889            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6890    
6891              !!!nack ('t382.1');
6892            !!!next-token;            !!!next-token;
6893          } elsif ($token->{tag_name} eq 'hr') {          } elsif ($token->{tag_name} eq 'hr') {
6894            !!!cp ('t386');            !!!cp ('t386');
6895            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6896                    
6897              !!!nack ('t386.1');
6898            !!!next-token;            !!!next-token;
6899          } else {          } else {
6900            !!!cp ('t347');            !!!nack ('t347.1');
6901            !!!next-token;            !!!next-token;
6902          }          }
6903          redo B;          next B;
6904        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6905          ## has a p element in scope          ## has a p element in scope
6906          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6907            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6908              !!!cp ('t353');              !!!cp ('t353');
6909              !!!back-token;              !!!back-token; # <x>
6910              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6911                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6912              redo B;              next B;
6913            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6914              !!!cp ('t354');              !!!cp ('t354');
6915              last INSCOPE;              last INSCOPE;
6916            }            }
# Line 5737  sub _tree_construction_main ($) { Line 6924  sub _tree_construction_main ($) {
6924                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};                            dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6925          LI: {          LI: {
6926            ## Step 2            ## Step 2
6927            if ($li_or_dtdd->{$node->[1]}) {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6928              if ($i != -1) {              if ($i != -1) {
6929                !!!cp ('t355');                !!!cp ('t355');
6930                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6931                                $self->{open_elements}->[-1]->[1], token => $token);                                text => $self->{open_elements}->[-1]->[0]
6932                                      ->manakai_local_name,
6933                                  token => $token);
6934              } else {              } else {
6935                !!!cp ('t356');                !!!cp ('t356');
6936              }              }
# Line 5752  sub _tree_construction_main ($) { Line 6941  sub _tree_construction_main ($) {
6941            }            }
6942                        
6943            ## Step 3            ## Step 3
6944            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6945                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6946                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6947                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6948                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6949                  not ($node->[1] & DIV_EL)) {
6950              !!!cp ('t358');              !!!cp ('t358');
6951              last LI;              last LI;
6952            }            }
# Line 5769  sub _tree_construction_main ($) { Line 6959  sub _tree_construction_main ($) {
6959          } # LI          } # LI
6960                        
6961          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6962            !!!nack ('t359.1');
6963          !!!next-token;          !!!next-token;
6964          redo B;          next B;
6965        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6966          ## has a p element in scope          ## has a p element in scope
6967          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6968            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6969              !!!cp ('t367');              !!!cp ('t367');
6970              !!!back-token;              !!!back-token; # <plaintext>
6971              $token = {type => END_TAG_TOKEN, tag_name => 'p',              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6972                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6973              redo B;              next B;
6974            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6975              !!!cp ('t368');              !!!cp ('t368');
6976              last INSCOPE;              last INSCOPE;
6977            }            }
# Line 5793  sub _tree_construction_main ($) { Line 6981  sub _tree_construction_main ($) {
6981                        
6982          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6983                        
6984            !!!nack ('t368.1');
6985          !!!next-token;          !!!next-token;
6986          redo B;          next B;
6987        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6988          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6989            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6990            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6991              !!!cp ('t371');              !!!cp ('t371');
6992              !!!parse-error (type => 'in a:a', token => $token);              !!!parse-error (type => 'in a:a', token => $token);
6993                            
6994              !!!back-token;              !!!back-token; # <a>
6995              $token = {type => END_TAG_TOKEN, tag_name => 'a',              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6996                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
6997              $formatting_end_tag->($token);              $formatting_end_tag->($token);
# Line 5833  sub _tree_construction_main ($) { Line 7022  sub _tree_construction_main ($) {
7022          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7023          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7024    
7025            !!!nack ('t374.1');
7026          !!!next-token;          !!!next-token;
7027          redo B;          next B;
7028        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7029          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7030    
7031          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7032          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7033            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7034            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7035              !!!cp ('t376');              !!!cp ('t376');
7036              !!!parse-error (type => 'in nobr:nobr', token => $token);              !!!parse-error (type => 'in nobr:nobr', token => $token);
7037              !!!back-token;              !!!back-token; # <nobr>
7038              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7039                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
7040              redo B;              next B;
7041            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7042              !!!cp ('t377');              !!!cp ('t377');
7043              last INSCOPE;              last INSCOPE;
7044            }            }
# Line 5860  sub _tree_construction_main ($) { Line 7047  sub _tree_construction_main ($) {
7047          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7048          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7049                    
7050            !!!nack ('t377.1');
7051          !!!next-token;          !!!next-token;
7052          redo B;          next B;
7053        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7054          ## has a button element in scope          ## has a button element in scope
7055          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7056            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7057            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7058              !!!cp ('t378');              !!!cp ('t378');
7059              !!!parse-error (type => 'in button:button', token => $token);              !!!parse-error (type => 'in button:button', token => $token);
7060              !!!back-token;              !!!back-token; # <button>
7061              $token = {type => END_TAG_TOKEN, tag_name => 'button',              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7062                        line => $token->{line}, column => $token->{column}};                        line => $token->{line}, column => $token->{column}};
7063              redo B;              next B;
7064            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7065              !!!cp ('t379');              !!!cp ('t379');
7066              last INSCOPE;              last INSCOPE;
7067            }            }
# Line 5890  sub _tree_construction_main ($) { Line 7075  sub _tree_construction_main ($) {
7075    
7076          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7077    
7078            !!!nack ('t379.1');
7079          !!!next-token;          !!!next-token;
7080          redo B;          next B;
7081        } elsif ({        } elsif ({
7082                  xmp => 1,                  xmp => 1,
7083                  iframe => 1,                  iframe => 1,
7084                  noembed => 1,                  noembed => 1,
7085                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
7086                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
7087                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7088          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 5907  sub _tree_construction_main ($) { Line 7093  sub _tree_construction_main ($) {
7093          }          }
7094          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
7095          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
7096          redo B;          next B;
7097        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7098          !!!parse-error (type => 'isindex', token => $token);          !!!parse-error (type => 'isindex', token => $token);
7099                    
7100          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7101            !!!cp ('t389');            !!!cp ('t389');
7102            ## Ignore the token            ## Ignore the token
7103              !!!nack ('t389'); ## NOTE: Not acknowledged.
7104            !!!next-token;            !!!next-token;
7105            redo B;            next B;
7106          } else {          } else {
7107              !!!ack ('t391.1');
7108    
7109            my $at = $token->{attributes};            my $at = $token->{attributes};
7110            my $form_attrs;            my $form_attrs;
7111            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5938  sub _tree_construction_main ($) { Line 7127  sub _tree_construction_main ($) {
7127            if ($prompt_attr) {            if ($prompt_attr) {
7128              !!!cp ('t390');              !!!cp ('t390');
7129              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7130                             line => $token->{line}, column => $token->{column}};                             #line => $token->{line}, column => $token->{column},
7131                              };
7132            } else {            } else {
7133              !!!cp ('t391');              !!!cp ('t391');
7134              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7135                             data => 'This is a searchable index. Insert your search keywords here: ',                             data => 'This is a searchable index. Insert your search keywords here: ',
7136                             line => $token->{line}, column => $token->{column}}; # SHOULD                             #line => $token->{line}, column => $token->{column},
7137                              }; # SHOULD
7138              ## TODO: make this configurable              ## TODO: make this configurable
7139            }            }
7140            push @tokens,            push @tokens,
# Line 5958  sub _tree_construction_main ($) { Line 7149  sub _tree_construction_main ($) {
7149                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
7150                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
7151                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           $token = shift @tokens;  
7152            !!!back-token (@tokens);            !!!back-token (@tokens);
7153            redo B;            !!!next-token;
7154              next B;
7155          }          }
7156        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7157          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7158          my $el;          my $el;
7159          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7160                    
7161          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7162          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5974  sub _tree_construction_main ($) { Line 7165  sub _tree_construction_main ($) {
7165          $insert->($el);          $insert->($el);
7166                    
7167          my $text = '';          my $text = '';
7168            !!!nack ('t392.1');
7169          !!!next-token;          !!!next-token;
7170          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7171            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6004  sub _tree_construction_main ($) { Line 7196  sub _tree_construction_main ($) {
7196            ## Ignore the token            ## Ignore the token
7197          } else {          } else {
7198            !!!cp ('t398');            !!!cp ('t398');
7199            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7200          }          }
7201          !!!next-token;          !!!next-token;
7202            next B;
7203          } elsif ($token->{tag_name} eq 'rt' or
7204                   $token->{tag_name} eq 'rp') {
7205            ## has a |ruby| element in scope
7206            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7207              my $node = $self->{open_elements}->[$_];
7208              if ($node->[1] & RUBY_EL) {
7209                !!!cp ('t398.1');
7210                ## generate implied end tags
7211                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7212                  !!!cp ('t398.2');
7213                  pop @{$self->{open_elements}};
7214                }
7215                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7216                  !!!cp ('t398.3');
7217                  !!!parse-error (type => 'not closed',
7218                                  text => $self->{open_elements}->[-1]->[0]
7219                                      ->manakai_local_name,
7220                                  token => $token);
7221                  pop @{$self->{open_elements}}
7222                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7223                }
7224                last INSCOPE;
7225              } elsif ($node->[1] & SCOPING_EL) {
7226                !!!cp ('t398.4');
7227                last INSCOPE;
7228              }
7229            } # INSCOPE
7230    
7231            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7232    
7233            !!!nack ('t398.5');
7234            !!!next-token;
7235          redo B;          redo B;
7236          } elsif ($token->{tag_name} eq 'math' or
7237                   $token->{tag_name} eq 'svg') {
7238            $reconstruct_active_formatting_elements->($insert_to_current);
7239    
7240            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7241    
7242            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7243    
7244            ## "adjust foreign attributes" - done in insert-element-f
7245            
7246            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7247            
7248            if ($self->{self_closing}) {
7249              pop @{$self->{open_elements}};
7250              !!!ack ('t398.1');
7251            } else {
7252              !!!cp ('t398.2');
7253              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7254              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7255              ## mode, "in body" (not "in foreign content") secondary insertion
7256              ## mode, maybe.
7257            }
7258    
7259            !!!next-token;
7260            next B;
7261        } elsif ({        } elsif ({
7262                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7263                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6015  sub _tree_construction_main ($) { Line 7265  sub _tree_construction_main ($) {
7265                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7266                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7267          !!!cp ('t401');          !!!cp ('t401');
7268          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7269                            text => $token->{tag_name}, token => $token);
7270          ## Ignore the token          ## Ignore the token
7271            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7272          !!!next-token;          !!!next-token;
7273          redo B;          next B;
7274                    
7275          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7276        } else {        } else {
# Line 6040  sub _tree_construction_main ($) { Line 7292  sub _tree_construction_main ($) {
7292              }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
7293            !!!cp ('t380');            !!!cp ('t380');
7294            push @$active_formatting_elements, ['#marker', ''];            push @$active_formatting_elements, ['#marker', ''];
7295              !!!nack ('t380.1');
7296          } elsif ({          } elsif ({
7297                    b => 1, big => 1, em => 1, font => 1, i => 1,                    b => 1, big => 1, em => 1, font => 1, i => 1,
7298                    s => 1, small => 1, strile => 1,                    s => 1, small => 1, strile => 1,
# Line 6047  sub _tree_construction_main ($) { Line 7300  sub _tree_construction_main ($) {
7300                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7301            !!!cp ('t375');            !!!cp ('t375');
7302            push @$active_formatting_elements, $self->{open_elements}->[-1];            push @$active_formatting_elements, $self->{open_elements}->[-1];
7303              !!!nack ('t375.1');
7304          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
7305            !!!cp ('t388');            !!!cp ('t388');
7306            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7307            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7308              !!!ack ('t388.2');
7309          } elsif ({          } elsif ({
7310                    area => 1, basefont => 1, bgsound => 1, br => 1,                    area => 1, basefont => 1, bgsound => 1, br => 1,
7311                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 6058  sub _tree_construction_main ($) { Line 7313  sub _tree_construction_main ($) {
7313                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
7314            !!!cp ('t388.1');            !!!cp ('t388.1');
7315            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
7316              !!!ack ('t388.3');
7317          } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
7318            ## TODO: associate with $self->{form_element} if defined            ## TODO: associate with $self->{form_element} if defined
7319                    
# Line 6070  sub _tree_construction_main ($) { Line 7326  sub _tree_construction_main ($) {
7326              !!!cp ('t400.2');              !!!cp ('t400.2');
7327              $self->{insertion_mode} = IN_SELECT_IM;              $self->{insertion_mode} = IN_SELECT_IM;
7328            }            }
7329              !!!nack ('t400.3');
7330          } else {          } else {
7331            !!!cp ('t402');            !!!nack ('t402');
7332          }          }
7333                    
7334          !!!next-token;          !!!next-token;
7335          redo B;          next B;
7336        }        }
7337      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7338        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 6083  sub _tree_construction_main ($) { Line 7340  sub _tree_construction_main ($) {
7340          my $i;          my $i;
7341          INSCOPE: {          INSCOPE: {
7342            for (reverse @{$self->{open_elements}}) {            for (reverse @{$self->{open_elements}}) {
7343              if ($_->[1] eq 'body') {              if ($_->[1] & BODY_EL) {
7344                !!!cp ('t405');                !!!cp ('t405');
7345                $i = $_;                $i = $_;
7346                last INSCOPE;                last INSCOPE;
7347              } elsif ({              } elsif ($_->[1] & SCOPING_EL) {
                       applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
7348                !!!cp ('t405.1');                !!!cp ('t405.1');
7349                last;                last;
7350              }              }
7351            }            }
7352    
7353            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7354                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7355            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7356            !!!next-token;            !!!next-token;
7357            redo B;            next B;
7358          } # INSCOPE          } # INSCOPE
7359    
7360          for (@{$self->{open_elements}}) {          for (@{$self->{open_elements}}) {
7361            unless ({            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                    th => 1, tr => 1, body => 1, html => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$_->[1]}) {  
7362              !!!cp ('t403');              !!!cp ('t403');
7363              !!!parse-error (type => 'not closed:'.$_->[1], token => $token);              !!!parse-error (type => 'not closed',
7364                                text => $_->[0]->manakai_local_name,
7365                                token => $token);
7366              last;              last;
7367            } else {            } else {
7368              !!!cp ('t404');              !!!cp ('t404');
# Line 6119  sub _tree_construction_main ($) { Line 7371  sub _tree_construction_main ($) {
7371    
7372          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
7373          !!!next-token;          !!!next-token;
7374          redo B;          next B;
7375        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7376          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
7377            ## up-to-date, though it has same effect as speced.
7378            if (@{$self->{open_elements}} > 1 and
7379                $self->{open_elements}->[1]->[1] & BODY_EL) {
7380            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7381            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7382              !!!cp ('t406');              !!!cp ('t406');
7383              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7384                                text => $self->{open_elements}->[1]->[0]
7385                                    ->manakai_local_name,
7386                                token => $token);
7387            } else {            } else {
7388              !!!cp ('t407');              !!!cp ('t407');
7389            }            }
7390            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7391            ## reprocess            ## reprocess
7392            redo B;            next B;
7393          } else {          } else {
7394            !!!cp ('t408');            !!!cp ('t408');
7395            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7396                              text => $token->{tag_name}, token => $token);
7397            ## Ignore the token            ## Ignore the token
7398            !!!next-token;            !!!next-token;
7399            redo B;            next B;
7400          }          }
7401        } elsif ({        } elsif ({
7402                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6150  sub _tree_construction_main ($) { Line 7409  sub _tree_construction_main ($) {
7409          my $i;          my $i;
7410          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7411            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7412            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7413              !!!cp ('t410');              !!!cp ('t410');
7414              $i = $_;              $i = $_;
7415              last INSCOPE;              last INSCOPE;
7416            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7417              !!!cp ('t411');              !!!cp ('t411');
7418              last INSCOPE;              last INSCOPE;
7419            }            }
# Line 6165  sub _tree_construction_main ($) { Line 7421  sub _tree_construction_main ($) {
7421    
7422          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7423            !!!cp ('t413');            !!!cp ('t413');
7424            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7425                              text => $token->{tag_name}, token => $token);
7426              ## NOTE: Ignore the token.
7427          } else {          } else {
7428            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7429            while ({            while ({
7430                      ## END_TAG_OPTIONAL_EL
7431                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7432                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7433                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7434                    p => 1,                    p => 1,
7435                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7436                      rp => 1,
7437                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7438              !!!cp ('t409');              !!!cp ('t409');
7439              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7440            }            }
7441    
7442            ## Step 2.            ## Step 2.
7443            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7444                      ne $token->{tag_name}) {
7445              !!!cp ('t412');              !!!cp ('t412');
7446              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7447                                text => $self->{open_elements}->[-1]->[0]
7448                                    ->manakai_local_name,
7449                                token => $token);
7450            } else {            } else {
7451              !!!cp ('t414');              !!!cp ('t414');
7452            }            }
# Line 6196  sub _tree_construction_main ($) { Line 7461  sub _tree_construction_main ($) {
7461                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7462          }          }
7463          !!!next-token;          !!!next-token;
7464          redo B;          next B;
7465        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7466          undef $self->{form_element};          undef $self->{form_element};
7467    
# Line 6204  sub _tree_construction_main ($) { Line 7469  sub _tree_construction_main ($) {
7469          my $i;          my $i;
7470          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7471            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7472            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7473              !!!cp ('t418');              !!!cp ('t418');
7474              $i = $_;              $i = $_;
7475              last INSCOPE;              last INSCOPE;
7476            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7477              !!!cp ('t419');              !!!cp ('t419');
7478              last INSCOPE;              last INSCOPE;
7479            }            }
# Line 6219  sub _tree_construction_main ($) { Line 7481  sub _tree_construction_main ($) {
7481    
7482          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7483            !!!cp ('t421');            !!!cp ('t421');
7484            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7485                              text => $token->{tag_name}, token => $token);
7486              ## NOTE: Ignore the token.
7487          } else {          } else {
7488            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7489            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7490              !!!cp ('t417');              !!!cp ('t417');
7491              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7492            }            }
7493                        
7494            ## Step 2.            ## Step 2.
7495            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7496                      ne $token->{tag_name}) {
7497              !!!cp ('t417.1');              !!!cp ('t417.1');
7498              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7499                                text => $self->{open_elements}->[-1]->[0]
7500                                    ->manakai_local_name,
7501                                token => $token);
7502            } else {            } else {
7503              !!!cp ('t420');              !!!cp ('t420');
7504            }              }  
# Line 6242  sub _tree_construction_main ($) { Line 7508  sub _tree_construction_main ($) {
7508          }          }
7509    
7510          !!!next-token;          !!!next-token;
7511          redo B;          next B;
7512        } elsif ({        } elsif ({
7513                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7514                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6250  sub _tree_construction_main ($) { Line 7516  sub _tree_construction_main ($) {
7516          my $i;          my $i;
7517          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7518            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7519            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7520              !!!cp ('t423');              !!!cp ('t423');
7521              $i = $_;              $i = $_;
7522              last INSCOPE;              last INSCOPE;
7523            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7524              !!!cp ('t424');              !!!cp ('t424');
7525              last INSCOPE;              last INSCOPE;
7526            }            }
# Line 6267  sub _tree_construction_main ($) { Line 7528  sub _tree_construction_main ($) {
7528    
7529          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7530            !!!cp ('t425.1');            !!!cp ('t425.1');
7531            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7532                              text => $token->{tag_name}, token => $token);
7533              ## NOTE: Ignore the token.
7534          } else {          } else {
7535            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7536            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7537              !!!cp ('t422');              !!!cp ('t422');
7538              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7539            }            }
7540                        
7541            ## Step 2.            ## Step 2.
7542            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7543                      ne $token->{tag_name}) {
7544              !!!cp ('t425');              !!!cp ('t425');
7545              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7546                                text => $token->{tag_name}, token => $token);
7547            } else {            } else {
7548              !!!cp ('t426');              !!!cp ('t426');
7549            }            }
# Line 6290  sub _tree_construction_main ($) { Line 7553  sub _tree_construction_main ($) {
7553          }          }
7554                    
7555          !!!next-token;          !!!next-token;
7556          redo B;          next B;
7557        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7558          ## has an element in scope          ## has an element in scope
7559          my $i;          my $i;
7560          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7561            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7562            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7563              !!!cp ('t410.1');              !!!cp ('t410.1');
7564              $i = $_;              $i = $_;
7565              last INSCOPE;              last INSCOPE;
7566            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7567              !!!cp ('t411.1');              !!!cp ('t411.1');
7568              last INSCOPE;              last INSCOPE;
7569            }            }
7570          } # INSCOPE          } # INSCOPE
7571    
7572          if (defined $i) {          if (defined $i) {
7573            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7574                      ne $token->{tag_name}) {
7575              !!!cp ('t412.1');              !!!cp ('t412.1');
7576              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);              !!!parse-error (type => 'not closed',
7577                                text => $self->{open_elements}->[-1]->[0]
7578                                    ->manakai_local_name,
7579                                token => $token);
7580            } else {            } else {
7581              !!!cp ('t414.1');              !!!cp ('t414.1');
7582            }            }
# Line 6320  sub _tree_construction_main ($) { Line 7584  sub _tree_construction_main ($) {
7584            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7585          } else {          } else {
7586            !!!cp ('t413.1');            !!!cp ('t413.1');
7587            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7588                              text => $token->{tag_name}, token => $token);
7589    
7590            !!!cp ('t415.1');            !!!cp ('t415.1');
7591            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7592            my $el;            my $el;
7593            !!!create-element ($el, 'p',, $token);            !!!create-element ($el, $HTML_NS, 'p',, $token);
7594            $insert->($el);            $insert->($el);
7595            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7596          }          }
7597    
7598          !!!next-token;          !!!next-token;
7599          redo B;          next B;
7600        } elsif ({        } elsif ({
7601                  a => 1,                  a => 1,
7602                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6340  sub _tree_construction_main ($) { Line 7605  sub _tree_construction_main ($) {
7605                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7606          !!!cp ('t427');          !!!cp ('t427');
7607          $formatting_end_tag->($token);          $formatting_end_tag->($token);
7608          redo B;          next B;
7609        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7610          !!!cp ('t428');          !!!cp ('t428');
7611          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7612                            text => 'br', token => $token);
7613    
7614          ## As if <br>          ## As if <br>
7615          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7616                    
7617          my $el;          my $el;
7618          !!!create-element ($el, 'br',, $token);          !!!create-element ($el, $HTML_NS, 'br',, $token);
7619          $insert->($el);          $insert->($el);
7620                    
7621          ## Ignore the token.          ## Ignore the token.
7622          !!!next-token;          !!!next-token;
7623          redo B;          next B;
7624        } elsif ({        } elsif ({
7625                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7626                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6368  sub _tree_construction_main ($) { Line 7634  sub _tree_construction_main ($) {
7634                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7635                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7636          !!!cp ('t429');          !!!cp ('t429');
7637          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7638                            text => $token->{tag_name}, token => $token);
7639          ## Ignore the token          ## Ignore the token
7640          !!!next-token;          !!!next-token;
7641          redo B;          next B;
7642                    
7643          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7644                    
# Line 6382  sub _tree_construction_main ($) { Line 7649  sub _tree_construction_main ($) {
7649    
7650          ## Step 2          ## Step 2
7651          S2: {          S2: {
7652            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7653              ## Step 1              ## Step 1
7654              ## generate implied end tags              ## generate implied end tags
7655              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7656                !!!cp ('t430');                !!!cp ('t430');
7657                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7658                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7659                  ## which seems wrong.
7660                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7661                  $node_i++;
7662              }              }
7663                    
7664              ## Step 2              ## Step 2
7665              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7666                        ne $token->{tag_name}) {
7667                !!!cp ('t431');                !!!cp ('t431');
7668                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7669                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);                !!!parse-error (type => 'not closed',
7670                                  text => $self->{open_elements}->[-1]->[0]
7671                                      ->manakai_local_name,
7672                                  token => $token);
7673              } else {              } else {
7674                !!!cp ('t432');                !!!cp ('t432');
7675              }              }
7676                            
7677              ## Step 3              ## Step 3
7678              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7679    
7680              !!!next-token;              !!!next-token;
7681              last S2;              last S2;
7682            } else {            } else {
7683              ## Step 3              ## Step 3
7684              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7685                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7686                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7687                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7688                !!!cp ('t433');                !!!cp ('t433');
7689                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7690                                  text => $token->{tag_name}, token => $token);
7691                ## Ignore the token                ## Ignore the token
7692                !!!next-token;                !!!next-token;
7693                last S2;                last S2;
# Line 6430  sub _tree_construction_main ($) { Line 7703  sub _tree_construction_main ($) {
7703            ## Step 5;            ## Step 5;
7704            redo S2;            redo S2;
7705          } # S2          } # S2
7706          redo B;          next B;
7707        }        }
7708      }      }
7709      redo B;      next B;
7710      } continue { # B
7711        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7712          ## NOTE: The code below is executed in cases where it does not have
7713          ## to be, but it it is harmless even in those cases.
7714          ## has an element in scope
7715          INSCOPE: {
7716            for (reverse 0..$#{$self->{open_elements}}) {
7717              my $node = $self->{open_elements}->[$_];
7718              if ($node->[1] & FOREIGN_EL) {
7719                last INSCOPE;
7720              } elsif ($node->[1] & SCOPING_EL) {
7721                last;
7722              }
7723            }
7724            
7725            ## NOTE: No foreign element in scope.
7726            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7727          } # INSCOPE
7728        }
7729    } # B    } # B
7730    
7731    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6441  sub _tree_construction_main ($) { Line 7733  sub _tree_construction_main ($) {
7733    ## TODO: script stuffs    ## TODO: script stuffs
7734  } # _tree_construct_main  } # _tree_construct_main
7735    
7736  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7737    my $class = shift;    my $class = shift;
7738    my $node = shift;    my $node = shift;
7739    my $s = \$_[0];    my $s = \$_[0];
7740    my $onerror = $_[1];    my $onerror = $_[1];
7741      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7742    
7743    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7744    
# Line 6464  sub set_inner_html ($$$) { Line 7757  sub set_inner_html ($$$) {
7757      }      }
7758    
7759      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7760      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7761    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7762      ## TODO: If non-html element      ## TODO: If non-html element
7763    
7764      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7765    
7766    ## TODO: Support for $get_wrapper
7767    
7768      ## Step 1 # MUST      ## Step 1 # MUST
7769      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7770      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6479  sub set_inner_html ($$$) { Line 7774  sub set_inner_html ($$$) {
7774    
7775      ## Step 8 # MUST      ## Step 8 # MUST
7776      my $i = 0;      my $i = 0;
7777      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7778      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7779      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7780        my $self = shift;        my $self = shift;
7781    
# Line 6489  sub set_inner_html ($$$) { Line 7784  sub set_inner_html ($$$) {
7784    
7785        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7786        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7787        $column++;  
7788          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7789          $p->{column}++;
7790    
7791        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7792          $line++;          $p->{line}++;
7793          $column = 0;          $p->{column} = 0;
7794          !!!cp ('i1');          !!!cp ('i1');
7795        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7796          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7797          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7798          $line++;          $p->{line}++;
7799          $column = 0;          $p->{column} = 0;
7800          !!!cp ('i2');          !!!cp ('i2');
7801        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7802          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6508  sub set_inner_html ($$$) { Line 7805  sub set_inner_html ($$$) {
7805          !!!cp ('i4');          !!!cp ('i4');
7806          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7807          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7808          } elsif ($self->{next_char} <= 0x0008 or
7809                   (0x000E <= $self->{next_char} and
7810                    $self->{next_char} <= 0x001F) or
7811                   (0x007F <= $self->{next_char} and
7812                    $self->{next_char} <= 0x009F) or
7813                   (0xD800 <= $self->{next_char} and
7814                    $self->{next_char} <= 0xDFFF) or
7815                   (0xFDD0 <= $self->{next_char} and
7816                    $self->{next_char} <= 0xFDDF) or
7817                   {
7818                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7819                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7820                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7821                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7822                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7823                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7824                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7825                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7826                    0x10FFFE => 1, 0x10FFFF => 1,
7827                   }->{$self->{next_char}}) {
7828            !!!cp ('i4.1');
7829            if ($self->{next_char} < 0x10000) {
7830              !!!parse-error (type => 'control char',
7831                              text => (sprintf 'U+%04X', $self->{next_char}));
7832            } else {
7833              !!!parse-error (type => 'control char',
7834                              text => (sprintf 'U-%08X', $self->{next_char}));
7835            }
7836        }        }
7837      };      };
7838      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7839      $p->{next_char} = -1;      $p->{next_char} = -1;
7840        
7841        $p->{read_until} = sub {
7842          ## TODO: ...
7843          return 0;
7844        }; # $p->{read_until};
7845    
7846      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7847        my (%opt) = @_;        my (%opt) = @_;
7848        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7849          my $column = $opt{column};
7850          if (defined $opt{token} and defined $opt{token}->{line}) {
7851            $line = $opt{token}->{line};
7852            $column = $opt{token}->{column};
7853          }
7854          warn "Parse error ($opt{type}) at line $line column $column\n";
7855      };      };
7856      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7857        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7858      };      };
7859            
7860      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6542  sub set_inner_html ($$$) { Line 7878  sub set_inner_html ($$$) {
7878          unless defined $p->{content_model};          unless defined $p->{content_model};
7879          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7880    
7881      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7882          ## TODO: Foreign element OK?
7883    
7884      ## Step 3      ## Step 3
7885      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6552  sub set_inner_html ($$$) { Line 7889  sub set_inner_html ($$$) {
7889      $doc->append_child ($root);      $doc->append_child ($root);
7890    
7891      ## Step 5 # MUST      ## Step 5 # MUST
7892      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7893    
7894      undef $p->{head_element};      undef $p->{head_element};
7895    
# Line 6598  sub set_inner_html ($$$) { Line 7935  sub set_inner_html ($$$) {
7935      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7936    
7937      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7938    
7939        delete $p->{parse_error}; # delete loop
7940    } else {    } else {
7941      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";
7942    }    }

Legend:
Removed from v.1.116  
changed lines
  Added in v.1.173

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24