/[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.72 by wakaba, Sun Mar 2 14:32:26 2008 UTC revision 1.140 by wakaba, Sat May 24 09:59:52 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
 ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  
11  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
12  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
13    
14  my $permitted_slash_tag_name = {  require IO::Handle;
15    base => 1,  
16    link => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17    meta => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    hr => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    br => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    img => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    embed => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    param => 1,  
23    area => 1,  sub A_EL () { 0b1 }
24    col => 1,  sub ADDRESS_EL () { 0b10 }
25    input => 1,  sub BODY_EL () { 0b100 }
26    sub BUTTON_EL () { 0b1000 }
27    sub CAPTION_EL () { 0b10000 }
28    sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216    my $svg_attr_name = {
217      attributetype => 'attributeType',
218      basefrequency => 'baseFrequency',
219      baseprofile => 'baseProfile',
220      calcmode => 'calcMode',
221      clippathunits => 'clipPathUnits',
222      contentscripttype => 'contentScriptType',
223      contentstyletype => 'contentStyleType',
224      diffuseconstant => 'diffuseConstant',
225      edgemode => 'edgeMode',
226      externalresourcesrequired => 'externalResourcesRequired',
227      fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282  };  };
283    
284    my $foreign_attr_xname = {
285      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292      'xml:base' => [$XML_NS, ['xml', 'base']],
293      'xml:lang' => [$XML_NS, ['xml', 'lang']],
294      'xml:space' => [$XML_NS, ['xml', 'space']],
295      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297    };
298    
299    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
300    
301  my $c1_entity_char = {  my $c1_entity_char = {
302    0x80 => 0x20AC,    0x80 => 0x20AC,
303    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 333  my $c1_entity_char = {
333    0x9F => 0x0178,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
336  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
337      my $self = shift;
338      my $charset_name = shift;
339      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    } # parse_byte_string
342    
343    sub parse_byte_stream ($$$$;$) {
344    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
345    my $charset = shift;    my $charset_name = shift;
346    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
347    
348    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
349      my $self = shift;      my (%opt) = @_;
350      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
351      ## TODO: if $charset is supported    };
352      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
353    
354      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      ## Step 1        my $charset;
357      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
358        $charset = 'utf-8';    my ($char_stream, $e_status);
359    
360      SNIFFING: {
361    
362        ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376      }      }
377    
378      ## Step 2      ## Step 2
379      if (defined $self->{input_encoding} and      my $byte_buffer = '';
380          $self->{input_encoding} eq $charset) {      for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392        $self->{confident} = 1;        $self->{confident} = 1;
393        return;        last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408      }      }
409    
410      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
411          ':'.$charset, level => 'w');      ## TODO: <meta charset>
412    
413      ## Step 3      ## Step 5
414      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
415    
416      ## Step 4      ## Step 6
417      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521    }; # $self->{change_encoding}    }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
534    my $return;    my $return;
535    try {    try {
536      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
537    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
538      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
539      $s = \ (Encode::decode ($charset, $$bytes_s));      
540      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553      $self->{confident} = 1;      $self->{confident} = 1;
554      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556    };    };
557    return $return;    return $return;
558  } # parse_byte_string  } # parse_byte_stream
559    
560  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 566  sub parse_byte_string ($$$$;$) {
566  ## 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
567  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
568    
569  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
579    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
580    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
583    
# Line 177  sub parse_string ($$$;$) { Line 588  sub parse_string ($$$;$) {
588        if defined $self->{input_encoding};        if defined $self->{input_encoding};
589    
590    my $i = 0;    my $i = 0;
591    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
592    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
593    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
594      my $self = shift;      my $self = shift;
595    
596      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
597      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599        my $char;
600        if (defined $self->{next_next_char}) {
601          $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
610      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
611      $column++;      $self->{column}++;
612            
613      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614        $line++;        !!!cp ('j1');
615        $column = 0;        $self->{line}++;
616      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
617        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
618        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
619        $line++;        my $next = $input->getc;
620        $column = 0;        if (defined $next and $next ne "\x0A") {
621      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
622        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
623      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
624          $self->{line}++;
625          $self->{column} = 0;
626        } elsif ($self->{next_char} > 0x10FFFF) {
627          !!!cp ('j3');
628          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629        } elsif ($self->{next_char} == 0x0000) { # NULL
630          !!!cp ('j4');
631        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
632        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
655    $self->{next_input_character} = -1;    $self->{next_char} = -1;
656    
657    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
658      my (%opt) = @_;      my (%opt) = @_;
659      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
660        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
661        warn "Parse error ($opt{type}) at line $line column $column\n";
662    };    };
663    $self->{parse_error} = sub {    $self->{parse_error} = sub {
664      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
665    };    };
666    
667    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    $self->_construct_tree;    $self->_construct_tree;
670    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
671    
672      delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680    $self->{set_next_input_character} = sub {      must_level => 'm',
681      $self->{next_input_character} = -1;      should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687      $self->{set_next_char} = sub {
688        $self->{next_char} = -1;
689    };    };
690    $self->{parse_error} = sub {    $self->{parse_error} = sub {
691      #      #
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 745  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
745  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
747  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
752  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 763  sub TABLE_IMS ()      { 0b1000000 }
763  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
764  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
765  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
766    sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772    ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
774    ## NOTE: "after after body" insertion mode.
775  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
776    
777    ## NOTE: "after after frameset" insertion mode.
778  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
779    
780  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
781  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
782  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 790  sub IN_TABLE_IM () { TABLE_IMS }
790  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
791  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
792  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
793  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
794    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
795  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
796    
797  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 332  sub _initialize_tokenizer ($) { Line 804  sub _initialize_tokenizer ($) {
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_input_character}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
811    $self->{token} = [];    $self->{token} = [];
812    # $self->{escape}    # $self->{escape}
# Line 346  sub _initialize_tokenizer ($) { Line 819  sub _initialize_tokenizer ($) {
819  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
820  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
821  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
822  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
823  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
824  ##        ->{name}  ##        ->{name}
825  ##        ->{value}  ##        ->{value}
826  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
827  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 378  sub _initialize_tokenizer ($) { Line 858  sub _initialize_tokenizer ($) {
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
875    A: {    A: {
876      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
877        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
878          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
879              not $self->{escape}) {              not $self->{escape}) {
880              !!!cp (1);
881            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
882            !!!next-input-character;            !!!next-input-character;
883            redo A;            redo A;
884          } else {          } else {
885              !!!cp (2);
886            #            #
887          }          }
888        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
889          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
890            unless ($self->{escape}) {            unless ($self->{escape}) {
891              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
892                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
893                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
894                  !!!cp (3);
895                $self->{escape} = 1;                $self->{escape} = 1;
896                } else {
897                  !!!cp (4);
898              }              }
899              } else {
900                !!!cp (5);
901            }            }
902          }          }
903                    
904          #          #
905        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
906          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
907              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
908               not $self->{escape})) {               not $self->{escape})) {
909              !!!cp (6);
910            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
911            !!!next-input-character;            !!!next-input-character;
912            redo A;            redo A;
913          } else {          } else {
914              !!!cp (7);
915            #            #
916          }          }
917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
918          if ($self->{escape} and          if ($self->{escape} and
919              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
920            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
921                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
922                !!!cp (8);
923              delete $self->{escape};              delete $self->{escape};
924              } else {
925                !!!cp (9);
926            }            }
927            } else {
928              !!!cp (10);
929          }          }
930                    
931          #          #
932        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
933          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
934            !!!emit ({type => END_OF_FILE_TOKEN,
935                      line => $self->{line}, column => $self->{column}});
936          last A; ## TODO: ok?          last A; ## TODO: ok?
937          } else {
938            !!!cp (12);
939        }        }
940        # Anything else        # Anything else
941        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
943                       line => $self->{line}, column => $self->{column},
944                      };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
947    
# Line 440  sub _get_next_token ($) { Line 950  sub _get_next_token ($) {
950        redo A;        redo A;
951      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
# Line 447  sub _get_next_token ($) { Line 959  sub _get_next_token ($) {
959        # next-input-character is already done        # next-input-character is already done
960    
961        unless (defined $token) {        unless (defined $token) {
962          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
963            !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                      line => $l, column => $c,
965                     });
966        } else {        } else {
967            !!!cp (14);
968          !!!emit ($token);          !!!emit ($token);
969        }        }
970    
971        redo A;        redo A;
972      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
973        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
974          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
975              !!!cp (15);
976            !!!next-input-character;            !!!next-input-character;
977            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
978            redo A;            redo A;
979          } else {          } else {
980              !!!cp (16);
981            ## reconsume            ## reconsume
982            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                        line => $self->{line_prev},
986                        column => $self->{column_prev},
987                       });
988    
989            redo A;            redo A;
990          }          }
991        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
992          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
993              !!!cp (17);
994            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
995            !!!next-input-character;            !!!next-input-character;
996            redo A;            redo A;
997          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
998              !!!cp (18);
999            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1003                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1004              !!!cp (19);
1005            $self->{current_token}            $self->{current_token}
1006              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1007                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1008                   line => $self->{line_prev},
1009                   column => $self->{column_prev}};
1010            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1014                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1015              !!!cp (20);
1016            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1017                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1018                                        line => $self->{line_prev},
1019                                        column => $self->{column_prev}};
1020            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1021            !!!next-input-character;            !!!next-input-character;
1022            redo A;            redo A;
1023          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1024            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1025              !!!parse-error (type => 'empty start tag',
1026                              line => $self->{line_prev},
1027                              column => $self->{column_prev});
1028            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                        line => $self->{line_prev},
1033                        column => $self->{column_prev},
1034                       });
1035    
1036            redo A;            redo A;
1037          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1038            !!!parse-error (type => 'pio');            !!!cp (22);
1039              !!!parse-error (type => 'pio',
1040                              line => $self->{line_prev},
1041                              column => $self->{column_prev});
1042            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1043            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                        line => $self->{line_prev},
1045                                        column => $self->{column_prev},
1046                                       };
1047              ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!parse-error (type => 'bare stago');            !!!cp (23);
1051              !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                        line => $self->{line_prev},
1059                        column => $self->{column_prev},
1060                       });
1061    
1062            redo A;            redo A;
1063          }          }
# Line 517  sub _get_next_token ($) { Line 1065  sub _get_next_token ($) {
1065          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1066        }        }
1067      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1068          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1069        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1070          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1071    
1072            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1073            my @next_char;            my @next_char;
1074            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1075              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1076              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1077              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1078              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1079                  !!!cp (24);
1080                !!!next-input-character;                !!!next-input-character;
1081                next TAGNAME;                next TAGNAME;
1082              } else {              } else {
1083                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1084                  $self->{next_char} = shift @next_char; # reconsume
1085                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1086                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1087    
1088                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                            line => $l, column => $c,
1090                           });
1091        
1092                redo A;                redo A;
1093              }              }
1094            }            }
1095            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1096                
1097            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1098                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1099                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1100                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1101                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1102                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1103                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1104                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1105              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1106                $self->{next_char} = shift @next_char; # reconsume
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1109              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110                          line => $l, column => $c,
1111                         });
1112              redo A;              redo A;
1113            } else {            } else {
1114              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1115                $self->{next_char} = shift @next_char;
1116              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1117              # and consume...              # and consume...
1118            }            }
1119          } else {          } else {
1120            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1121              !!!cp (28);
1122            # next-input-character is already done            # next-input-character is already done
1123            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1124            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                        line => $l, column => $c,
1126                       });
1127            redo A;            redo A;
1128          }          }
1129        }        }
1130                
1131        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1132            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1133          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1134                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1135                = {type => END_TAG_TOKEN,
1136                   tag_name => chr ($self->{next_char} + 0x0020),
1137                   line => $l, column => $c};
1138          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1139          !!!next-input-character;          !!!next-input-character;
1140          redo A;          redo A;
1141        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1142                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1143            !!!cp (30);
1144          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1145                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1146                                      line => $l, column => $c};
1147          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1148          !!!next-input-character;          !!!next-input-character;
1149          redo A;          redo A;
1150        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1151          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1152            !!!parse-error (type => 'empty end tag',
1153                            line => $self->{line_prev}, ## "<" in "</>"
1154                            column => $self->{column_prev} - 1);
1155          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1156          !!!next-input-character;          !!!next-input-character;
1157          redo A;          redo A;
1158        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1159            !!!cp (32);
1160          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1161          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                      line => $l, column => $c,
1166                     });
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170            !!!cp (33);
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1173          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174                                      line => $self->{line_prev}, # "<" of "</"
1175                                      column => $self->{column_prev} - 1,
1176                                     };
1177            ## $self->{next_char} is intentionally left as is
1178          redo A;          redo A;
1179        }        }
1180      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1181        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1182            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1183            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1184            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1185            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1186            !!!cp (34);
1187          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1188          !!!next-input-character;          !!!next-input-character;
1189          redo A;          redo A;
1190        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1191          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1192            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1193            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1194          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1195            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1196            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1197              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1198            }            #  !!! cp (36);
1199              #  !!! parse-error (type => 'end tag attribute');
1200              #} else {
1201                !!!cp (37);
1202              #}
1203          } else {          } else {
1204            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1205          }          }
# Line 628  sub _get_next_token ($) { Line 1209  sub _get_next_token ($) {
1209          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1210    
1211          redo A;          redo A;
1212        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1213                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1214          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1215            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1216            # start tag or end tag            # start tag or end tag
1217          ## Stay in this state          ## Stay in this state
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1221          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1222          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1223            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1227            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1228              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1229            }            #  !!! cp (40);
1230              #  !!! parse-error (type => 'end tag attribute');
1231              #} else {
1232                !!!cp (41);
1233              #}
1234          } else {          } else {
1235            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1236          }          }
# Line 655  sub _get_next_token ($) { Line 1240  sub _get_next_token ($) {
1240          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1247          redo A;          redo A;
1248        } else {        } else {
1249          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1250            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1251            # start tag or end tag            # start tag or end tag
1252          ## Stay in the state          ## Stay in the state
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        }        }
1256      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1257        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1258            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1259            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1260            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1261            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1262            !!!cp (45);
1263          ## Stay in the state          ## Stay in the state
1264          !!!next-input-character;          !!!next-input-character;
1265          redo A;          redo A;
1266        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1267          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1268            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1269            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1270          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1271            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1272            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1273                !!!cp (47);
1274              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1275              } else {
1276                !!!cp (48);
1277            }            }
1278          } else {          } else {
1279            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 703  sub _get_next_token ($) { Line 1284  sub _get_next_token ($) {
1284          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1285    
1286          redo A;          redo A;
1287        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1290                                value => ''};          $self->{current_attribute}
1291                = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1310                !!!cp (53);
1311              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1312              } else {
1313                !!!cp (54);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 748  sub _get_next_token ($) { Line 1326  sub _get_next_token ($) {
1326               0x0022 => 1, # "               0x0022 => 1, # "
1327               0x0027 => 1, # '               0x0027 => 1, # '
1328               0x003D => 1, # =               0x003D => 1, # =
1329              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1330              !!!cp (55);
1331            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1332            } else {
1333              !!!cp (56);
1334          }          }
1335          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute}
1336                                value => ''};              = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
# Line 761  sub _get_next_token ($) { Line 1344  sub _get_next_token ($) {
1344        my $before_leave = sub {        my $before_leave = sub {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1348              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351              !!!cp (58);
1352            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1353              = $self->{current_attribute};              = $self->{current_attribute};
1354          }          }
1355        }; # $before_leave        }; # $before_leave
1356    
1357        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1358            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1359            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1360            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1361            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1362            !!!cp (59);
1363          $before_leave->();          $before_leave->();
1364          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366          redo A;          redo A;
1367        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1368            !!!cp (60);
1369          $before_leave->();          $before_leave->();
1370          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1371          !!!next-input-character;          !!!next-input-character;
1372          redo A;          redo A;
1373        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1374          $before_leave->();          $before_leave->();
1375          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379              !!!cp (62);
1380            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1381            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1382              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 803  sub _get_next_token ($) { Line 1390  sub _get_next_token ($) {
1390          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1391    
1392          redo A;          redo A;
1393        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1394                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1395          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1396            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1397          ## Stay in the state          ## Stay in the state
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1405          redo A;          redo A;
1406        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1408          $before_leave->();          $before_leave->();
1409          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1410            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1411            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1412          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1413            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1414            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1415                !!!cp (67);
1416              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1417              } else {
1418                ## NOTE: This state should never be reached.
1419                !!!cp (68);
1420            }            }
1421          } else {          } else {
1422            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 845  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428    
1429          redo A;          redo A;
1430        } else {        } else {
1431          if ($self->{next_input_character} == 0x0022 or # "          if ($self->{next_char} == 0x0022 or # "
1432              $self->{next_input_character} == 0x0027) { # '              $self->{next_char} == 0x0027) { # '
1433              !!!cp (69);
1434            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1435            } else {
1436              !!!cp (70);
1437          }          }
1438          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          $self->{current_attribute}->{name} .= chr ($self->{next_char});
1439          ## Stay in the state          ## Stay in the state
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        }        }
1443      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1444        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1445            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1446            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1447            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1448            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1449            !!!cp (71);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1454            !!!cp (72);
1455          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1456          !!!next-input-character;          !!!next-input-character;
1457          redo A;          redo A;
1458        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1459          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1460            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1461            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1462          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1463            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1464            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1465                !!!cp (74);
1466              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1467              } else {
1468                ## NOTE: This state should never be reached.
1469                !!!cp (75);
1470            }            }
1471          } else {          } else {
1472            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 886  sub _get_next_token ($) { Line 1477  sub _get_next_token ($) {
1477          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1478    
1479          redo A;          redo A;
1480        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1483                                value => ''};          $self->{current_attribute}
1484                = {name => chr ($self->{next_char} + 0x0020),
1485                   value => '',
1486                   line => $self->{line}, column => $self->{column}};
1487          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!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  
1494          redo A;          redo A;
1495        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1497          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1502            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1503                !!!cp (80);
1504              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1505              } else {
1506                ## NOTE: This state should never be reached.
1507                !!!cp (81);
1508            }            }
1509          } else {          } else {
1510            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 928  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516    
1517          redo A;          redo A;
1518        } else {        } else {
1519          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1520                                value => ''};          $self->{current_attribute}
1521                = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
1527        }        }
1528      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1529        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1530            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1531            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1532            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1533            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1534            !!!cp (83);
1535          ## Stay in the state          ## Stay in the state
1536          !!!next-input-character;          !!!next-input-character;
1537          redo A;          redo A;
1538        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1539            !!!cp (84);
1540          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1544            !!!cp (85);
1545          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1546          ## reconsume          ## reconsume
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1549            !!!cp (86);
1550          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1551          !!!next-input-character;          !!!next-input-character;
1552          redo A;          redo A;
1553        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1554          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1559            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1560                !!!cp (88);
1561              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1562              } else {
1563                ## NOTE: This state should never be reached.
1564                !!!cp (89);
1565            }            }
1566          } else {          } else {
1567            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 974  sub _get_next_token ($) { Line 1572  sub _get_next_token ($) {
1572          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1573    
1574          redo A;          redo A;
1575        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1577          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1582            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1583                !!!cp (91);
1584              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1585              } else {
1586                ## NOTE: This state should never be reached.
1587                !!!cp (92);
1588            }            }
1589          } else {          } else {
1590            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 995  sub _get_next_token ($) { Line 1596  sub _get_next_token ($) {
1596    
1597          redo A;          redo A;
1598        } else {        } else {
1599          if ($self->{next_input_character} == 0x003D) { # =          if ($self->{next_char} == 0x003D) { # =
1600              !!!cp (93);
1601            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1602            } else {
1603              !!!cp (94);
1604          }          }
1605          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1606          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        }        }
1610      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1611        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1612            !!!cp (95);
1613          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1617            !!!cp (96);
1618          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1619          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1620          !!!next-input-character;          !!!next-input-character;
1621          redo A;          redo A;
1622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1623          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1624          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1629            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1630                !!!cp (98);
1631              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1632              } else {
1633                ## NOTE: This state should never be reached.
1634                !!!cp (99);
1635            }            }
1636          } else {          } else {
1637            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1034  sub _get_next_token ($) { Line 1643  sub _get_next_token ($) {
1643    
1644          redo A;          redo A;
1645        } else {        } else {
1646          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1647            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1648          ## Stay in the state          ## Stay in the state
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;          redo A;
1651        }        }
1652      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1653        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1654            !!!cp (101);
1655          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1656          !!!next-input-character;          !!!next-input-character;
1657          redo A;          redo A;
1658        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1659            !!!cp (102);
1660          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1661          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1666          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1671            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1672                !!!cp (104);
1673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1674              } else {
1675                ## NOTE: This state should never be reached.
1676                !!!cp (105);
1677            }            }
1678          } else {          } else {
1679            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1070  sub _get_next_token ($) { Line 1685  sub _get_next_token ($) {
1685    
1686          redo A;          redo A;
1687        } else {        } else {
1688          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1689            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1690          ## Stay in the state          ## Stay in the state
1691          !!!next-input-character;          !!!next-input-character;
1692          redo A;          redo A;
1693        }        }
1694      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1695        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1696            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1697            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1698            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1699            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1700            !!!cp (107);
1701          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1702          !!!next-input-character;          !!!next-input-character;
1703          redo A;          redo A;
1704        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1705            !!!cp (108);
1706          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1707          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1708          !!!next-input-character;          !!!next-input-character;
1709          redo A;          redo A;
1710        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1711          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1716            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1717                !!!cp (110);
1718              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1719              } else {
1720                ## NOTE: This state should never be reached.
1721                !!!cp (111);
1722            }            }
1723          } else {          } else {
1724            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1108  sub _get_next_token ($) { Line 1729  sub _get_next_token ($) {
1729          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1730    
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1733          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1734          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1735            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1736            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1737          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1738            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1739            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1740                !!!cp (113);
1741              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1742              } else {
1743                ## NOTE: This state should never be reached.
1744                !!!cp (114);
1745            }            }
1746          } else {          } else {
1747            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1133  sub _get_next_token ($) { Line 1757  sub _get_next_token ($) {
1757               0x0022 => 1, # "               0x0022 => 1, # "
1758               0x0027 => 1, # '               0x0027 => 1, # '
1759               0x003D => 1, # =               0x003D => 1, # =
1760              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1761              !!!cp (115);
1762            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1763            } else {
1764              !!!cp (116);
1765          }          }
1766          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1767          ## Stay in the state          ## Stay in the state
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
# Line 1151  sub _get_next_token ($) { Line 1778  sub _get_next_token ($) {
1778             -1);             -1);
1779    
1780        unless (defined $token) {        unless (defined $token) {
1781            !!!cp (117);
1782          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1783        } else {        } else {
1784            !!!cp (118);
1785          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1786          $self->{current_attribute}->{has_reference} = $token->{has_reference};          $self->{current_attribute}->{has_reference} = $token->{has_reference};
1787          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
# Line 1162  sub _get_next_token ($) { Line 1791  sub _get_next_token ($) {
1791        # next-input-character is already done        # next-input-character is already done
1792        redo A;        redo A;
1793      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1794        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1795            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1796            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1797            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1798            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1799            !!!cp (118);
1800          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1801          !!!next-input-character;          !!!next-input-character;
1802          redo A;          redo A;
1803        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1804          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1805            $self->{current_token}->{first_start_tag}            !!!cp (119);
               = not defined $self->{last_emitted_start_tag_name};  
1806            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1807          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1808            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1809            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1810                !!!cp (120);
1811              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1812              } else {
1813                ## NOTE: This state should never be reached.
1814                !!!cp (121);
1815            }            }
1816          } else {          } else {
1817            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1189  sub _get_next_token ($) { Line 1822  sub _get_next_token ($) {
1822          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1823    
1824          redo A;          redo A;
1825        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1829          redo A;          redo A;
1830        } else {        } else {
1831            !!!cp ('124.1');
1832          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1833          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1834          ## reconsume          ## reconsume
1835          redo A;          redo A;
1836        }        }
1837        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1838          if ($self->{next_char} == 0x003E) { # >
1839            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1840              !!!cp ('124.2');
1841              !!!parse-error (type => 'nestc', token => $self->{current_token});
1842              ## TODO: Different type than slash in start tag
1843              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1844              if ($self->{current_token}->{attributes}) {
1845                !!!cp ('124.4');
1846                !!!parse-error (type => 'end tag attribute');
1847              } else {
1848                !!!cp ('124.5');
1849              }
1850              ## TODO: Test |<title></title/>|
1851            } else {
1852              !!!cp ('124.3');
1853              $self->{self_closing} = 1;
1854            }
1855    
1856            $self->{state} = DATA_STATE;
1857            !!!next-input-character;
1858    
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860    
1861            redo A;
1862          } else {
1863            !!!cp ('124.4');
1864            !!!parse-error (type => 'nestc');
1865            ## TODO: This error type is wrong.
1866            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1867            ## Reconsume.
1868            redo A;
1869          }
1870      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1871        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1872                
1873        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1874          #my $token = {type => COMMENT_TOKEN, data => ''};
1875    
1876        BC: {        BC: {
1877          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1878              !!!cp (124);
1879            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1880            !!!next-input-character;            !!!next-input-character;
1881    
1882            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1883    
1884            redo A;            redo A;
1885          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1886              !!!cp (125);
1887            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1888            ## reconsume            ## reconsume
1889    
1890            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1891    
1892            redo A;            redo A;
1893          } else {          } else {
1894            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1895              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1896            !!!next-input-character;            !!!next-input-character;
1897            redo BC;            redo BC;
1898          }          }
1899        } # BC        } # BC
1900    
1901          die "$0: _get_next_token: unexpected case [BC]";
1902      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1903        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1904    
1905          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1906    
1907        my @next_char;        my @next_char;
1908        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1909                
1910        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1911          !!!next-input-character;          !!!next-input-character;
1912          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1913          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1914            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1915              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1916                                        line => $l, column => $c,
1917                                       };
1918            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1919            !!!next-input-character;            !!!next-input-character;
1920            redo A;            redo A;
1921            } else {
1922              !!!cp (128);
1923          }          }
1924        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1925                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1926          !!!next-input-character;          !!!next-input-character;
1927          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1928          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1929              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1930            !!!next-input-character;            !!!next-input-character;
1931            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1932            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1933                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1934              !!!next-input-character;              !!!next-input-character;
1935              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1936              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1937                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1938                !!!next-input-character;                !!!next-input-character;
1939                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1940                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1941                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1942                  !!!next-input-character;                  !!!next-input-character;
1943                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1944                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1945                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1946                    !!!next-input-character;                    !!!next-input-character;
1947                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1948                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1949                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1950                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1951                        ## TODO: What a stupid code this is!
1952                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1953                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1954                                                  quirks => 1,
1955                                                  line => $l, column => $c,
1956                                                 };
1957                      !!!next-input-character;                      !!!next-input-character;
1958                      redo A;                      redo A;
1959                      } else {
1960                        !!!cp (130);
1961                    }                    }
1962                    } else {
1963                      !!!cp (131);
1964                  }                  }
1965                  } else {
1966                    !!!cp (132);
1967                }                }
1968                } else {
1969                  !!!cp (133);
1970              }              }
1971              } else {
1972                !!!cp (134);
1973            }            }
1974            } else {
1975              !!!cp (135);
1976          }          }
1977          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1978                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1979                   $self->{next_char} == 0x005B) { # [
1980            !!!next-input-character;
1981            push @next_char, $self->{next_char};
1982            if ($self->{next_char} == 0x0043) { # C
1983              !!!next-input-character;
1984              push @next_char, $self->{next_char};
1985              if ($self->{next_char} == 0x0044) { # D
1986                !!!next-input-character;
1987                push @next_char, $self->{next_char};
1988                if ($self->{next_char} == 0x0041) { # A
1989                  !!!next-input-character;
1990                  push @next_char, $self->{next_char};
1991                  if ($self->{next_char} == 0x0054) { # T
1992                    !!!next-input-character;
1993                    push @next_char, $self->{next_char};
1994                    if ($self->{next_char} == 0x0041) { # A
1995                      !!!next-input-character;
1996                      push @next_char, $self->{next_char};
1997                      if ($self->{next_char} == 0x005B) { # [
1998                        !!!cp (135.1);
1999                        $self->{state} = CDATA_BLOCK_STATE;
2000                        !!!next-input-character;
2001                        redo A;
2002                      } else {
2003                        !!!cp (135.2);
2004                      }
2005                    } else {
2006                      !!!cp (135.3);
2007                    }
2008                  } else {
2009                    !!!cp (135.4);                
2010                  }
2011                } else {
2012                  !!!cp (135.5);
2013                }
2014              } else {
2015                !!!cp (135.6);
2016              }
2017            } else {
2018              !!!cp (135.7);
2019            }
2020          } else {
2021            !!!cp (136);
2022        }        }
2023    
2024        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2025        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2026        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2027        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2028          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2029                                    line => $l, column => $c,
2030                                   };
2031        redo A;        redo A;
2032                
2033        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2034        ## 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?        ## 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?
2035      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2036        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2037            !!!cp (137);
2038          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2039          !!!next-input-character;          !!!next-input-character;
2040          redo A;          redo A;
2041        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2042            !!!cp (138);
2043          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2044          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2045          !!!next-input-character;          !!!next-input-character;
# Line 1308  sub _get_next_token ($) { Line 2047  sub _get_next_token ($) {
2047          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2048    
2049          redo A;          redo A;
2050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2051            !!!cp (139);
2052          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2053          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2054          ## reconsume          ## reconsume
# Line 1317  sub _get_next_token ($) { Line 2057  sub _get_next_token ($) {
2057    
2058          redo A;          redo A;
2059        } else {        } else {
2060            !!!cp (140);
2061          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2062              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2063          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2064          !!!next-input-character;          !!!next-input-character;
2065          redo A;          redo A;
2066        }        }
2067      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2068        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2069            !!!cp (141);
2070          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2071          !!!next-input-character;          !!!next-input-character;
2072          redo A;          redo A;
2073        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2074            !!!cp (142);
2075          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2076          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2077          !!!next-input-character;          !!!next-input-character;
# Line 1336  sub _get_next_token ($) { Line 2079  sub _get_next_token ($) {
2079          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2080    
2081          redo A;          redo A;
2082        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2083            !!!cp (143);
2084          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2085          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2086          ## reconsume          ## reconsume
# Line 1345  sub _get_next_token ($) { Line 2089  sub _get_next_token ($) {
2089    
2090          redo A;          redo A;
2091        } else {        } else {
2092            !!!cp (144);
2093          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2094              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2095          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2096          !!!next-input-character;          !!!next-input-character;
2097          redo A;          redo A;
2098        }        }
2099      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2100        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2101            !!!cp (145);
2102          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2103          !!!next-input-character;          !!!next-input-character;
2104          redo A;          redo A;
2105        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2106            !!!cp (146);
2107          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2108          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2109          ## reconsume          ## reconsume
# Line 1365  sub _get_next_token ($) { Line 2112  sub _get_next_token ($) {
2112    
2113          redo A;          redo A;
2114        } else {        } else {
2115          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2116            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2117          ## Stay in the state          ## Stay in the state
2118          !!!next-input-character;          !!!next-input-character;
2119          redo A;          redo A;
2120        }        }
2121      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2122        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2123            !!!cp (148);
2124          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2125          !!!next-input-character;          !!!next-input-character;
2126          redo A;          redo A;
2127        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2128            !!!cp (149);
2129          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2130          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2131          ## reconsume          ## reconsume
# Line 1384  sub _get_next_token ($) { Line 2134  sub _get_next_token ($) {
2134    
2135          redo A;          redo A;
2136        } else {        } else {
2137          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2138            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2139          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        }        }
2143      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2144        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2145            !!!cp (151);
2146          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2147          !!!next-input-character;          !!!next-input-character;
2148    
2149          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2150    
2151          redo A;          redo A;
2152        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2153          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2154            !!!parse-error (type => 'dash in comment',
2155                            line => $self->{line_prev},
2156                            column => $self->{column_prev});
2157          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2158          ## Stay in the state          ## Stay in the state
2159          !!!next-input-character;          !!!next-input-character;
2160          redo A;          redo A;
2161        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2162            !!!cp (153);
2163          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2164          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2165          ## reconsume          ## reconsume
# Line 1412  sub _get_next_token ($) { Line 2168  sub _get_next_token ($) {
2168    
2169          redo A;          redo A;
2170        } else {        } else {
2171          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2172          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2173                            line => $self->{line_prev},
2174                            column => $self->{column_prev});
2175            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2176          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178          redo A;          redo A;
2179        }        }
2180      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2181        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2182            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2183            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2184            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2185            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2186            !!!cp (155);
2187          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2188          !!!next-input-character;          !!!next-input-character;
2189          redo A;          redo A;
2190        } else {        } else {
2191            !!!cp (156);
2192          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2193          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2194          ## reconsume          ## reconsume
2195          redo A;          redo A;
2196        }        }
2197      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2198        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2199            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2200            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2201            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2202            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2203            !!!cp (157);
2204          ## Stay in the state          ## Stay in the state
2205          !!!next-input-character;          !!!next-input-character;
2206          redo A;          redo A;
2207        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2208            !!!cp (158);
2209          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2210          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2211          !!!next-input-character;          !!!next-input-character;
2212    
2213          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2214    
2215          redo A;          redo A;
2216        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2217            !!!cp (159);
2218          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2219          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2220          ## reconsume          ## reconsume
2221    
2222          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2223    
2224          redo A;          redo A;
2225        } else {        } else {
2226          $self->{current_token}          !!!cp (160);
2227              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2228                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2229  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2230          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2231          !!!next-input-character;          !!!next-input-character;
# Line 1470  sub _get_next_token ($) { Line 2233  sub _get_next_token ($) {
2233        }        }
2234      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2235  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2236        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2237            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2238            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2239            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2240            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2241            !!!cp (161);
2242          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2243          !!!next-input-character;          !!!next-input-character;
2244          redo A;          redo A;
2245        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2246            !!!cp (162);
2247          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249    
2250          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2251    
2252          redo A;          redo A;
2253        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2254            !!!cp (163);
2255          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2256          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2257          ## reconsume          ## reconsume
2258    
2259          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2260          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2261    
2262          redo A;          redo A;
2263        } else {        } else {
2264            !!!cp (164);
2265          $self->{current_token}->{name}          $self->{current_token}->{name}
2266            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2267          ## Stay in the state          ## Stay in the state
2268          !!!next-input-character;          !!!next-input-character;
2269          redo A;          redo A;
2270        }        }
2271      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2272        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2273            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2274            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2275            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2276            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2277            !!!cp (165);
2278          ## Stay in the state          ## Stay in the state
2279          !!!next-input-character;          !!!next-input-character;
2280          redo A;          redo A;
2281        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2282            !!!cp (166);
2283          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2284          !!!next-input-character;          !!!next-input-character;
2285    
2286          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2287    
2288          redo A;          redo A;
2289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2290            !!!cp (167);
2291          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2292          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2293          ## reconsume          ## reconsume
2294    
2295          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2296          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2297    
2298          redo A;          redo A;
2299        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2300                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2301          !!!next-input-character;          !!!next-input-character;
2302          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2303              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2304            !!!next-input-character;            !!!next-input-character;
2305            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2306                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2307              !!!next-input-character;              !!!next-input-character;
2308              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2309                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2310                !!!next-input-character;                !!!next-input-character;
2311                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2312                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2313                  !!!next-input-character;                  !!!next-input-character;
2314                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2315                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2316                      !!!cp (168);
2317                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2318                    !!!next-input-character;                    !!!next-input-character;
2319                    redo A;                    redo A;
2320                    } else {
2321                      !!!cp (169);
2322                  }                  }
2323                  } else {
2324                    !!!cp (170);
2325                }                }
2326                } else {
2327                  !!!cp (171);
2328              }              }
2329              } else {
2330                !!!cp (172);
2331            }            }
2332            } else {
2333              !!!cp (173);
2334          }          }
2335    
2336          #          #
2337        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2338                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2339          !!!next-input-character;          !!!next-input-character;
2340          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2341              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2342            !!!next-input-character;            !!!next-input-character;
2343            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2344                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2345              !!!next-input-character;              !!!next-input-character;
2346              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2347                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2348                !!!next-input-character;                !!!next-input-character;
2349                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2350                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2351                  !!!next-input-character;                  !!!next-input-character;
2352                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2353                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2354                      !!!cp (174);
2355                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2356                    !!!next-input-character;                    !!!next-input-character;
2357                    redo A;                    redo A;
2358                    } else {
2359                      !!!cp (175);
2360                  }                  }
2361                  } else {
2362                    !!!cp (176);
2363                }                }
2364                } else {
2365                  !!!cp (177);
2366              }              }
2367              } else {
2368                !!!cp (178);
2369            }            }
2370            } else {
2371              !!!cp (179);
2372          }          }
2373    
2374          #          #
2375        } else {        } else {
2376            !!!cp (180);
2377          !!!next-input-character;          !!!next-input-character;
2378          #          #
2379        }        }
2380    
2381        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2382          $self->{current_token}->{quirks} = 1;
2383    
2384        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2385        # next-input-character is already done        # next-input-character is already done
2386        redo A;        redo A;
# Line 1593  sub _get_next_token ($) { Line 2388  sub _get_next_token ($) {
2388        if ({        if ({
2389              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2390              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2391            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2392            !!!cp (181);
2393          ## Stay in the state          ## Stay in the state
2394          !!!next-input-character;          !!!next-input-character;
2395          redo A;          redo A;
2396        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2397            !!!cp (182);
2398          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2399          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401          redo A;          redo A;
2402        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2403            !!!cp (183);
2404          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2405          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2406          !!!next-input-character;          !!!next-input-character;
2407          redo A;          redo A;
2408        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2409            !!!cp (184);
2410          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2411    
2412          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2413          !!!next-input-character;          !!!next-input-character;
2414    
2415          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2416          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2417    
2418          redo A;          redo A;
2419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2420            !!!cp (185);
2421          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2422    
2423          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2424          ## reconsume          ## reconsume
2425    
2426          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2427          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2428    
2429          redo A;          redo A;
2430        } else {        } else {
2431            !!!cp (186);
2432          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2433            $self->{current_token}->{quirks} = 1;
2434    
2435          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2436          !!!next-input-character;          !!!next-input-character;
2437          redo A;          redo A;
2438        }        }
2439      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2440        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2441            !!!cp (187);
2442          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2443          !!!next-input-character;          !!!next-input-character;
2444          redo A;          redo A;
2445        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2446            !!!cp (188);
2447          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2448    
2449          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2450          !!!next-input-character;          !!!next-input-character;
2451    
2452          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2453          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2454    
2455          redo A;          redo A;
2456        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2457            !!!cp (189);
2458          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2459    
2460          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2461          ## reconsume          ## reconsume
2462    
2463          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2464          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2465    
2466          redo A;          redo A;
2467        } else {        } else {
2468            !!!cp (190);
2469          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2470              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2471          ## Stay in the state          ## Stay in the state
2472          !!!next-input-character;          !!!next-input-character;
2473          redo A;          redo A;
2474        }        }
2475      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2476        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2477            !!!cp (191);
2478          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2479          !!!next-input-character;          !!!next-input-character;
2480          redo A;          redo A;
2481        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2482            !!!cp (192);
2483          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2484    
2485          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2486          !!!next-input-character;          !!!next-input-character;
2487    
2488          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2489          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2490    
2491          redo A;          redo A;
2492        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2493            !!!cp (193);
2494          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2495    
2496          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2497          ## reconsume          ## reconsume
2498    
2499          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2500          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2501    
2502          redo A;          redo A;
2503        } else {        } else {
2504            !!!cp (194);
2505          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2506              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2507          ## Stay in the state          ## Stay in the state
2508          !!!next-input-character;          !!!next-input-character;
2509          redo A;          redo A;
# Line 1701  sub _get_next_token ($) { Line 2512  sub _get_next_token ($) {
2512        if ({        if ({
2513              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2514              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2515            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2516            !!!cp (195);
2517          ## Stay in the state          ## Stay in the state
2518          !!!next-input-character;          !!!next-input-character;
2519          redo A;          redo A;
2520        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2521            !!!cp (196);
2522          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2523          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2524          !!!next-input-character;          !!!next-input-character;
2525          redo A;          redo A;
2526        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2527            !!!cp (197);
2528          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2529          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2530          !!!next-input-character;          !!!next-input-character;
2531          redo A;          redo A;
2532        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2533            !!!cp (198);
2534          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2535          !!!next-input-character;          !!!next-input-character;
2536    
2537          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2538    
2539          redo A;          redo A;
2540        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2541            !!!cp (199);
2542          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2543    
2544          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2545          ## reconsume          ## reconsume
2546    
2547          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2548          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2549    
2550          redo A;          redo A;
2551        } else {        } else {
2552            !!!cp (200);
2553          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2554            $self->{current_token}->{quirks} = 1;
2555    
2556          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2557          !!!next-input-character;          !!!next-input-character;
2558          redo A;          redo A;
# Line 1742  sub _get_next_token ($) { Line 2561  sub _get_next_token ($) {
2561        if ({        if ({
2562              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2563              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2564            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2565            !!!cp (201);
2566          ## Stay in the state          ## Stay in the state
2567          !!!next-input-character;          !!!next-input-character;
2568          redo A;          redo A;
2569        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2570            !!!cp (202);
2571          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2572          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2573          !!!next-input-character;          !!!next-input-character;
2574          redo A;          redo A;
2575        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2576            !!!cp (203);
2577          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2578          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2579          !!!next-input-character;          !!!next-input-character;
2580          redo A;          redo A;
2581        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (204);
2583          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2584          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2585          !!!next-input-character;          !!!next-input-character;
2586    
2587          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2588          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2589    
2590          redo A;          redo A;
2591        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2592            !!!cp (205);
2593          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2594    
2595          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2596          ## reconsume          ## reconsume
2597    
2598          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2599          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2600    
2601          redo A;          redo A;
2602        } else {        } else {
2603            !!!cp (206);
2604          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2605            $self->{current_token}->{quirks} = 1;
2606    
2607          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        }        }
2611      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2612        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2613            !!!cp (207);
2614          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2615          !!!next-input-character;          !!!next-input-character;
2616          redo A;          redo A;
2617        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2618            !!!cp (208);
2619          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2620    
2621          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2622          !!!next-input-character;          !!!next-input-character;
2623    
2624          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2625          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2626    
2627          redo A;          redo A;
2628        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2629            !!!cp (209);
2630          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2631    
2632          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2633          ## reconsume          ## reconsume
2634    
2635          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2636          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2637    
2638          redo A;          redo A;
2639        } else {        } else {
2640            !!!cp (210);
2641          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2642              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2643          ## Stay in the state          ## Stay in the state
2644          !!!next-input-character;          !!!next-input-character;
2645          redo A;          redo A;
2646        }        }
2647      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2648        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2649            !!!cp (211);
2650          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2651          !!!next-input-character;          !!!next-input-character;
2652          redo A;          redo A;
2653        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2654            !!!cp (212);
2655          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2656    
2657          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2658          !!!next-input-character;          !!!next-input-character;
2659    
2660          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2661          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2662    
2663          redo A;          redo A;
2664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2665            !!!cp (213);
2666          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2667    
2668          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2669          ## reconsume          ## reconsume
2670    
2671          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2672          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2673    
2674          redo A;          redo A;
2675        } else {        } else {
2676            !!!cp (214);
2677          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2678              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2679          ## Stay in the state          ## Stay in the state
2680          !!!next-input-character;          !!!next-input-character;
2681          redo A;          redo A;
# Line 1849  sub _get_next_token ($) { Line 2684  sub _get_next_token ($) {
2684        if ({        if ({
2685              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2686              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2687            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2688            !!!cp (215);
2689          ## Stay in the state          ## Stay in the state
2690          !!!next-input-character;          !!!next-input-character;
2691          redo A;          redo A;
2692        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2693            !!!cp (216);
2694          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2695          !!!next-input-character;          !!!next-input-character;
2696    
2697          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2698    
2699          redo A;          redo A;
2700        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2701            !!!cp (217);
2702          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2703    
2704          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2705          ## reconsume          ## reconsume
2706    
2707          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2708          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2709    
2710          redo A;          redo A;
2711        } else {        } else {
2712            !!!cp (218);
2713          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2714            #$self->{current_token}->{quirks} = 1;
2715    
2716          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2717          !!!next-input-character;          !!!next-input-character;
2718          redo A;          redo A;
2719        }        }
2720      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2721        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2722            !!!cp (219);
2723          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2724          !!!next-input-character;          !!!next-input-character;
2725    
         delete $self->{current_token}->{correct};  
2726          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2727    
2728          redo A;          redo A;
2729        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2730            !!!cp (220);
2731          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2732          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2733          ## reconsume          ## reconsume
2734    
         delete $self->{current_token}->{correct};  
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2736    
2737          redo A;          redo A;
2738        } else {        } else {
2739            !!!cp (221);
2740          ## Stay in the state          ## Stay in the state
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        }        }
2744        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2745          my $s = '';
2746          
2747          my ($l, $c) = ($self->{line}, $self->{column});
2748    
2749          CS: while ($self->{next_char} != -1) {
2750            if ($self->{next_char} == 0x005D) { # ]
2751              !!!next-input-character;
2752              if ($self->{next_char} == 0x005D) { # ]
2753                !!!next-input-character;
2754                MDC: {
2755                  if ($self->{next_char} == 0x003E) { # >
2756                    !!!cp (221.1);
2757                    !!!next-input-character;
2758                    last CS;
2759                  } elsif ($self->{next_char} == 0x005D) { # ]
2760                    !!!cp (221.2);
2761                    $s .= ']';
2762                    !!!next-input-character;
2763                    redo MDC;
2764                  } else {
2765                    !!!cp (221.3);
2766                    $s .= ']]';
2767                    #
2768                  }
2769                } # MDC
2770              } else {
2771                !!!cp (221.4);
2772                $s .= ']';
2773                #
2774              }
2775            } else {
2776              !!!cp (221.5);
2777              #
2778            }
2779            $s .= chr $self->{next_char};
2780            !!!next-input-character;
2781          } # CS
2782    
2783          $self->{state} = DATA_STATE;
2784          ## next-input-character done or EOF, which is reconsumed.
2785    
2786          if (length $s) {
2787            !!!cp (221.6);
2788            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2789                      line => $l, column => $c});
2790          } else {
2791            !!!cp (221.7);
2792          }
2793    
2794          redo A;
2795    
2796          ## ISSUE: "text tokens" in spec.
2797          ## TODO: Streaming support
2798      } else {      } else {
2799        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2800      }      }
# Line 1910  sub _get_next_token ($) { Line 2806  sub _get_next_token ($) {
2806  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2807    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2808    
2809      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2810    
2811    if ({    if ({
2812         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2813         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2814         $additional => 1,         $additional => 1,
2815        }->{$self->{next_input_character}}) {        }->{$self->{next_char}}) {
2816        !!!cp (1001);
2817      ## Don't consume      ## Don't consume
2818      ## No error      ## No error
2819      return undef;      return undef;
2820    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2821      !!!next-input-character;      !!!next-input-character;
2822      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2823          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2824        my $code;        my $code;
2825        X: {        X: {
2826          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2827          !!!next-input-character;          !!!next-input-character;
2828          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2829              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2830              !!!cp (1002);
2831            $code ||= 0;            $code ||= 0;
2832            $code *= 0x10;            $code *= 0x10;
2833            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2834            redo X;            redo X;
2835          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2836                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2837              !!!cp (1003);
2838            $code ||= 0;            $code ||= 0;
2839            $code *= 0x10;            $code *= 0x10;
2840            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2841            redo X;            redo X;
2842          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2843                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2844              !!!cp (1004);
2845            $code ||= 0;            $code ||= 0;
2846            $code *= 0x10;            $code *= 0x10;
2847            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2848            redo X;            redo X;
2849          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2850            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2851            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2852            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2853              $self->{next_char} = 0x0023; # #
2854            return undef;            return undef;
2855          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2856              !!!cp (1006);
2857            !!!next-input-character;            !!!next-input-character;
2858          } else {          } else {
2859            !!!parse-error (type => 'no refc');            !!!cp (1007);
2860              !!!parse-error (type => 'no refc', line => $l, column => $c);
2861          }          }
2862    
2863          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2864            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2865              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2866            $code = 0xFFFD;            $code = 0xFFFD;
2867          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2868            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2869              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2870            $code = 0xFFFD;            $code = 0xFFFD;
2871          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2872            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2873              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2874            $code = 0x000A;            $code = 0x000A;
2875          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2876            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2877              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2878            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2879          }          }
2880    
2881          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2882                  has_reference => 1};                  has_reference => 1,
2883                    line => $l, column => $c,
2884                   };
2885        } # X        } # X
2886      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2887               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2888        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2889        !!!next-input-character;        !!!next-input-character;
2890                
2891        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2892                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2893            !!!cp (1012);
2894          $code *= 10;          $code *= 10;
2895          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2896                    
2897          !!!next-input-character;          !!!next-input-character;
2898        }        }
2899    
2900        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2901            !!!cp (1013);
2902          !!!next-input-character;          !!!next-input-character;
2903        } else {        } else {
2904          !!!parse-error (type => 'no refc');          !!!cp (1014);
2905            !!!parse-error (type => 'no refc', line => $l, column => $c);
2906        }        }
2907    
2908        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2909          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2910            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2911          $code = 0xFFFD;          $code = 0xFFFD;
2912        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2913          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2914            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2915          $code = 0xFFFD;          $code = 0xFFFD;
2916        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2917          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2918            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2919          $code = 0x000A;          $code = 0x000A;
2920        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2921          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2922            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2923          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2924        }        }
2925                
2926        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2927                  line => $l, column => $c,
2928                 };
2929      } else {      } else {
2930        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2931        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2932        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2933          $self->{next_char} = 0x0023; # #
2934        return undef;        return undef;
2935      }      }
2936    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2937              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2938             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2939              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2940      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2941      !!!next-input-character;      !!!next-input-character;
2942    
2943      my $value = $entity_name;      my $value = $entity_name;
# Line 2024  sub _tokenize_attempt_to_consume_an_enti Line 2945  sub _tokenize_attempt_to_consume_an_enti
2945      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2946      our $EntityChar;      our $EntityChar;
2947    
2948      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2949             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2950             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2951               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2952              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2953               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2954              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2955               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2956              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2957        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2958        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2959          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2960              !!!cp (1020);
2961            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2962            $match = 1;            $match = 1;
2963            !!!next-input-character;            !!!next-input-character;
2964            last;            last;
2965          } else {          } else {
2966              !!!cp (1021);
2967            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2968            $match = -1;            $match = -1;
2969            !!!next-input-character;            !!!next-input-character;
2970          }          }
2971        } else {        } else {
2972          $value .= chr $self->{next_input_character};          !!!cp (1022);
2973            $value .= chr $self->{next_char};
2974          $match *= 2;          $match *= 2;
2975          !!!next-input-character;          !!!next-input-character;
2976        }        }
2977      }      }
2978            
2979      if ($match > 0) {      if ($match > 0) {
2980        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        !!!cp (1023);
2981          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2982                  line => $l, column => $c,
2983                 };
2984      } elsif ($match < 0) {      } elsif ($match < 0) {
2985        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2986        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2987          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2988        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2989          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};                  line => $l, column => $c,
2990                   };
2991          } else {
2992            !!!cp (1025);
2993            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2994                    line => $l, column => $c,
2995                   };
2996        }        }
2997      } else {      } else {
2998        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2999          !!!parse-error (type => 'bare ero', line => $l, column => $c);
3000        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
3001        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
3002                  line => $l, column => $c,
3003                 };
3004      }      }
3005    } else {    } else {
3006        !!!cp (1027);
3007      ## no characters are consumed      ## no characters are consumed
3008      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3009      return undef;      return undef;
3010    }    }
3011  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2106  sub _construct_tree ($) { Line 3043  sub _construct_tree ($) {
3043        
3044    !!!next-token;    !!!next-token;
3045    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3046    undef $self->{form_element};    undef $self->{form_element};
3047    undef $self->{head_element};    undef $self->{head_element};
3048    $self->{open_elements} = [];    $self->{open_elements} = [];
3049    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3050    
3051      ## NOTE: The "initial" insertion mode.
3052    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3053    
3054      ## NOTE: The "before html" insertion mode.
3055    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3056      $self->{insertion_mode} = BEFORE_HEAD_IM;
3057    
3058      ## NOTE: The "before head" insertion mode and so on.
3059    $self->_tree_construction_main;    $self->_tree_construction_main;
3060  } # _construct_tree  } # _construct_tree
3061    
3062  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3063    my $self = shift;    my $self = shift;
3064    
3065      ## NOTE: "initial" insertion mode
3066    
3067    INITIAL: {    INITIAL: {
3068      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3069        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2130  sub _tree_construction_initial ($) { Line 3075  sub _tree_construction_initial ($) {
3075        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3076            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3077            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3078          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3079            !!!parse-error (type => 'not HTML5', token => $token);
3080        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3081            !!!cp ('t2');
3082          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3083          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3084          } else {
3085            !!!cp ('t3');
3086        }        }
3087                
3088        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3089          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3090          ## NOTE: Default value for both |public_id| and |system_id| attributes
3091          ## are empty strings, so that we don't set any value in missing cases.
3092        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3093            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3094        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2146  sub _tree_construction_initial ($) { Line 3097  sub _tree_construction_initial ($) {
3097        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3098        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3099                
3100        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3101            !!!cp ('t4');
3102          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3103        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3104          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2225  sub _tree_construction_initial ($) { Line 3177  sub _tree_construction_initial ($) {
3177            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3178            "HTML" => 1,            "HTML" => 1,
3179          }->{$pubid}) {          }->{$pubid}) {
3180              !!!cp ('t5');
3181            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3182          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3183                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3184            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3185                !!!cp ('t6');
3186              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3187            } else {            } else {
3188                !!!cp ('t7');
3189              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3190            }            }
3191          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3192                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3193              !!!cp ('t8');
3194            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3195            } else {
3196              !!!cp ('t9');
3197          }          }
3198          } else {
3199            !!!cp ('t10');
3200        }        }
3201        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3202          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3203          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3204          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") {
3205              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3206            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3207              !!!cp ('t11');
3208            } else {
3209              !!!cp ('t12');
3210          }          }
3211          } else {
3212            !!!cp ('t13');
3213        }        }
3214                
3215        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3216        !!!next-token;        !!!next-token;
3217        return;        return;
3218      } elsif ({      } elsif ({
# Line 2254  sub _tree_construction_initial ($) { Line 3220  sub _tree_construction_initial ($) {
3220                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3221                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3222               }->{$token->{type}}) {               }->{$token->{type}}) {
3223        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3224          !!!parse-error (type => 'no DOCTYPE', token => $token);
3225        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3226        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3227        ## reprocess        ## reprocess
3228          !!!ack-later;
3229        return;        return;
3230      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3231        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3232          ## Ignore the token          ## Ignore the token
3233    
3234          unless (length $token->{data}) {          unless (length $token->{data}) {
3235            ## Stay in the phase            !!!cp ('t15');
3236              ## Stay in the insertion mode.
3237            !!!next-token;            !!!next-token;
3238            redo INITIAL;            redo INITIAL;
3239            } else {
3240              !!!cp ('t16');
3241          }          }
3242          } else {
3243            !!!cp ('t17');
3244        }        }
3245    
3246        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3247        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3248        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3249        ## reprocess        ## reprocess
3250        return;        return;
3251      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3252          !!!cp ('t18');
3253        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3254        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3255                
3256        ## Stay in the phase.        ## Stay in the insertion mode.
3257        !!!next-token;        !!!next-token;
3258        redo INITIAL;        redo INITIAL;
3259      } else {      } else {
3260        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3261      }      }
3262    } # INITIAL    } # INITIAL
3263    
3264      die "$0: _tree_construction_initial: This should be never reached";
3265  } # _tree_construction_initial  } # _tree_construction_initial
3266    
3267  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3268    my $self = shift;    my $self = shift;
3269    
3270      ## NOTE: "before html" insertion mode.
3271        
3272    B: {    B: {
3273        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3274          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3275            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3276          ## Ignore the token          ## Ignore the token
3277          ## Stay in the phase          ## Stay in the insertion mode.
3278          !!!next-token;          !!!next-token;
3279          redo B;          redo B;
3280        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3281            !!!cp ('t20');
3282          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3283          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3284          ## Stay in the phase          ## Stay in the insertion mode.
3285          !!!next-token;          !!!next-token;
3286          redo B;          redo B;
3287        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2309  sub _tree_construction_root_element ($) Line 3289  sub _tree_construction_root_element ($)
3289            ## Ignore the token.            ## Ignore the token.
3290    
3291            unless (length $token->{data}) {            unless (length $token->{data}) {
3292              ## Stay in the phase              !!!cp ('t21');
3293                ## Stay in the insertion mode.
3294              !!!next-token;              !!!next-token;
3295              redo B;              redo B;
3296              } else {
3297                !!!cp ('t22');
3298            }            }
3299            } else {
3300              !!!cp ('t23');
3301          }          }
3302    
3303          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3304    
3305          #          #
3306        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3307          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3308              $token->{attributes}->{manifest}) {            my $root_element;
3309            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3310                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3311            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3312                  [$root_element, $el_category->{html}];
3313    
3314              if ($token->{attributes}->{manifest}) {
3315                !!!cp ('t24');
3316                $self->{application_cache_selection}
3317                    ->($token->{attributes}->{manifest}->{value});
3318                ## ISSUE: Spec is unclear on relative references.
3319                ## According to Hixie (#whatwg 2008-03-19), it should be
3320                ## resolved against the base URI of the document in HTML
3321                ## or xml:base of the element in XHTML.
3322              } else {
3323                !!!cp ('t25');
3324                $self->{application_cache_selection}->(undef);
3325              }
3326    
3327              !!!nack ('t25c');
3328    
3329              !!!next-token;
3330              return; ## Go to the "before head" insertion mode.
3331          } else {          } else {
3332            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3333              #
3334          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3335        } elsif ({        } elsif ({
3336                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3337                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3338                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3339          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3340          #          #
3341        } else {        } else {
3342          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3343        }        }
3344    
3345        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3346        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3347        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3348        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3349        #redo B;  
3350        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3351    
3352        ## NOTE: Reprocess the token.
3353        !!!ack-later;
3354        return; ## Go to the "before head" insertion mode.
3355    
3356        ## ISSUE: There is an issue in the spec
3357    } # B    } # B
3358    
3359      die "$0: _tree_construction_root_element: This should never be reached";
3360  } # _tree_construction_root_element  } # _tree_construction_root_element
3361    
3362  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2363  sub _reset_insertion_mode ($) { Line 3371  sub _reset_insertion_mode ($) {
3371            
3372      ## Step 3      ## Step 3
3373      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3374        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3375          $last = 1;          $last = 1;
3376          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3377            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3378                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3379              #          } else {
3380            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3381          }          }
3382        }        }
3383              
3384        ## Step 4..13        ## Step 4..14
3385        my $new_mode = {        my $new_mode;
3386          if ($node->[1] & FOREIGN_EL) {
3387            !!!cp ('t28.1');
3388            ## NOTE: Strictly spaking, the line below only applies to MathML and
3389            ## SVG elements.  Currently the HTML syntax supports only MathML and
3390            ## SVG elements as foreigners.
3391            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3392            ## ISSUE: What is set as the secondary insertion mode?
3393          } elsif ($node->[1] & TABLE_CELL_EL) {
3394            if ($last) {
3395              !!!cp ('t28.2');
3396              #
3397            } else {
3398              !!!cp ('t28.3');
3399              $new_mode = IN_CELL_IM;
3400            }
3401          } else {
3402            !!!cp ('t28.4');
3403            $new_mode = {
3404                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3405                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3406                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3407                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3408                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3409                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2395  sub _reset_insertion_mode ($) { Line 3414  sub _reset_insertion_mode ($) {
3414                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3415                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3416                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3417                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3418          }
3419        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3420                
3421        ## Step 14        ## Step 15
3422        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3423          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3424              !!!cp ('t29');
3425            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3426          } else {          } else {
3427              ## ISSUE: Can this state be reached?
3428              !!!cp ('t30');
3429            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3430          }          }
3431          return;          return;
3432          } else {
3433            !!!cp ('t31');
3434        }        }
3435                
3436        ## Step 15        ## Step 16
3437        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3438                
3439        ## Step 16        ## Step 17
3440        $i--;        $i--;
3441        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3442                
3443        ## Step 17        ## Step 18
3444        redo S3;        redo S3;
3445      } # S3      } # S3
3446    
3447      die "$0: _reset_insertion_mode: This line should never be reached";
3448  } # _reset_insertion_mode  } # _reset_insertion_mode
3449    
3450  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2439  sub _tree_construction_main ($) { Line 3466  sub _tree_construction_main ($) {
3466      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3467      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3468        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3469            !!!cp ('t32');
3470          return;          return;
3471        }        }
3472      }      }
# Line 2453  sub _tree_construction_main ($) { Line 3481  sub _tree_construction_main ($) {
3481    
3482        ## Step 6        ## Step 6
3483        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3484            !!!cp ('t33_1');
3485          #          #
3486        } else {        } else {
3487          my $in_open_elements;          my $in_open_elements;
3488          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3489            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3490                !!!cp ('t33');
3491              $in_open_elements = 1;              $in_open_elements = 1;
3492              last OE;              last OE;
3493            }            }
3494          }          }
3495          if ($in_open_elements) {          if ($in_open_elements) {
3496              !!!cp ('t34');
3497            #            #
3498          } else {          } else {
3499              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3500              !!!cp ('t35');
3501            redo S4;            redo S4;
3502          }          }
3503        }        }
# Line 2487  sub _tree_construction_main ($) { Line 3520  sub _tree_construction_main ($) {
3520    
3521        ## Step 11        ## Step 11
3522        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3523            !!!cp ('t36');
3524          ## Step 7'          ## Step 7'
3525          $i++;          $i++;
3526          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3527                    
3528          redo S7;          redo S7;
3529        }        }
3530    
3531          !!!cp ('t37');
3532      } # S7      } # S7
3533    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3534    
3535    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3536      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3537        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3538            !!!cp ('t38');
3539          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3540          return;          return;
3541        }        }
3542      }      }
3543    
3544        !!!cp ('t39');
3545    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3546    
3547    my $parse_rcdata = sub ($$) {    my $insert;
3548      my ($content_model_flag, $insert) = @_;  
3549      my $parse_rcdata = sub ($) {
3550        my ($content_model_flag) = @_;
3551    
3552      ## Step 1      ## Step 1
3553      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3554      my $el;      my $el;
3555      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3556    
3557      ## Step 2      ## Step 2
3558      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3559    
3560      ## Step 3      ## Step 3
3561      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2522  sub _tree_construction_main ($) { Line 3563  sub _tree_construction_main ($) {
3563    
3564      ## Step 4      ## Step 4
3565      my $text = '';      my $text = '';
3566        !!!nack ('t40.1');
3567      !!!next-token;      !!!next-token;
3568      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3569          !!!cp ('t40');
3570        $text .= $token->{data};        $text .= $token->{data};
3571        !!!next-token;        !!!next-token;
3572      }      }
3573    
3574      ## Step 5      ## Step 5
3575      if (length $text) {      if (length $text) {
3576          !!!cp ('t41');
3577        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3578        $el->append_child ($text);        $el->append_child ($text);
3579      }      }
# Line 2538  sub _tree_construction_main ($) { Line 3582  sub _tree_construction_main ($) {
3582      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3583    
3584      ## Step 7      ## Step 7
3585      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3586            $token->{tag_name} eq $start_tag_name) {
3587          !!!cp ('t42');
3588        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3589      } else {      } else {
3590        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3591          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3592            !!!cp ('t43');
3593            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3594          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3595            !!!cp ('t44');
3596            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3597          } else {
3598            die "$0: $content_model_flag in parse_rcdata";
3599          }
3600      }      }
3601      !!!next-token;      !!!next-token;
3602    }; # $parse_rcdata    }; # $parse_rcdata
3603    
3604    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3605      my $script_el;      my $script_el;
3606      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3607      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3608    
3609      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3610      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3611            
3612      my $text = '';      my $text = '';
3613        !!!nack ('t45.1');
3614      !!!next-token;      !!!next-token;
3615      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3616          !!!cp ('t45');
3617        $text .= $token->{data};        $text .= $token->{data};
3618        !!!next-token;        !!!next-token;
3619      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3620      if (length $text) {      if (length $text) {
3621          !!!cp ('t46');
3622        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3623      }      }
3624                                
# Line 2573  sub _tree_construction_main ($) { Line 3626  sub _tree_construction_main ($) {
3626    
3627      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3628          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3629          !!!cp ('t47');
3630        ## Ignore the token        ## Ignore the token
3631      } else {      } else {
3632        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3633          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3634        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3635        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3636      }      }
3637            
3638      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3639          !!!cp ('t49');
3640        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3641      } else {      } else {
3642          !!!cp ('t50');
3643        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3644        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3645    
# Line 2596  sub _tree_construction_main ($) { Line 3653  sub _tree_construction_main ($) {
3653      !!!next-token;      !!!next-token;
3654    }; # $script_start_tag    }; # $script_start_tag
3655    
3656      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3657      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3658      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3659    
3660    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3661      my $tag_name = shift;      my $end_tag_token = shift;
3662        my $tag_name = $end_tag_token->{tag_name};
3663    
3664        ## NOTE: The adoption agency algorithm (AAA).
3665    
3666      FET: {      FET: {
3667        ## Step 1        ## Step 1
3668        my $formatting_element;        my $formatting_element;
3669        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3670        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3671          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3672              !!!cp ('t52');
3673              last AFE;
3674            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3675                         eq $tag_name) {
3676              !!!cp ('t51');
3677            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3678            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3679            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3680          }          }
3681        } # AFE        } # AFE
3682        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3683          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3684            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3685          ## Ignore the token          ## Ignore the token
3686          !!!next-token;          !!!next-token;
3687          return;          return;
# Line 2625  sub _tree_construction_main ($) { Line 3693  sub _tree_construction_main ($) {
3693          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3694          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3695            if ($in_scope) {            if ($in_scope) {
3696                !!!cp ('t54');
3697              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3698              last INSCOPE;              last INSCOPE;
3699            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3700              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3701                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3702                                token => $end_tag_token);
3703              ## Ignore the token              ## Ignore the token
3704              !!!next-token;              !!!next-token;
3705              return;              return;
3706            }            }
3707          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3708                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3709            $in_scope = 0;            $in_scope = 0;
3710          }          }
3711        } # INSCOPE        } # INSCOPE
3712        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3713          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3714            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3715                            token => $end_tag_token);
3716          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3717          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3718          return;          return;
3719        }        }
3720        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3721          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3722            !!!parse-error (type => 'not closed',
3723                            value => $self->{open_elements}->[-1]->[0]
3724                                ->manakai_local_name,
3725                            token => $end_tag_token);
3726        }        }
3727                
3728        ## Step 2        ## Step 2
# Line 2655  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3731        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3732          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3733          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3734              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3735              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3736               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3737              !!!cp ('t59');
3738            $furthest_block = $node;            $furthest_block = $node;
3739            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3740          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3741              !!!cp ('t60');
3742            last OE;            last OE;
3743          }          }
3744        } # OE        } # OE
3745                
3746        ## Step 3        ## Step 3
3747        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3748            !!!cp ('t61');
3749          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3750          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3751          !!!next-token;          !!!next-token;
# Line 2680  sub _tree_construction_main ($) { Line 3758  sub _tree_construction_main ($) {
3758        ## Step 5        ## Step 5
3759        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3760        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3761            !!!cp ('t62');
3762          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3763        }        }
3764                
# Line 2702  sub _tree_construction_main ($) { Line 3781  sub _tree_construction_main ($) {
3781          S7S2: {          S7S2: {
3782            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3783              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3784                  !!!cp ('t63');
3785                $node_i_in_active = $_;                $node_i_in_active = $_;
3786                last S7S2;                last S7S2;
3787              }              }
# Line 2715  sub _tree_construction_main ($) { Line 3795  sub _tree_construction_main ($) {
3795                    
3796          ## Step 4          ## Step 4
3797          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3798              !!!cp ('t64');
3799            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3800          }          }
3801                    
3802          ## Step 5          ## Step 5
3803          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3804              !!!cp ('t65');
3805            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3806            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3807            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2737  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819        } # S7          } # S7  
3820                
3821        ## Step 8        ## Step 8
3822        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3823            my $foster_parent_element;
3824            my $next_sibling;
3825            OE: for (reverse 0..$#{$self->{open_elements}}) {
3826              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3827                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3828                                 if (defined $parent and $parent->node_type == 1) {
3829                                   !!!cp ('t65.1');
3830                                   $foster_parent_element = $parent;
3831                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3832                                 } else {
3833                                   !!!cp ('t65.2');
3834                                   $foster_parent_element
3835                                     = $self->{open_elements}->[$_ - 1]->[0];
3836                                 }
3837                                 last OE;
3838                               }
3839                             } # OE
3840                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3841                               unless defined $foster_parent_element;
3842            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3843            $open_tables->[-1]->[1] = 1; # tainted
3844          } else {
3845            !!!cp ('t65.3');
3846            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3847          }
3848                
3849        ## Step 9        ## Step 9
3850        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2754  sub _tree_construction_main ($) { Line 3861  sub _tree_construction_main ($) {
3861        my $i;        my $i;
3862        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3863          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3864              !!!cp ('t66');
3865            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3866            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3867          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3868              !!!cp ('t67');
3869            $i = $_;            $i = $_;
3870          }          }
3871        } # AFE        } # AFE
# Line 2766  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875        undef $i;        undef $i;
3876        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3877          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3878              !!!cp ('t68');
3879            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3880            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3881          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3882              !!!cp ('t69');
3883            $i = $_;            $i = $_;
3884          }          }
3885        } # OE        } # OE
# Line 2779  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890      } # FET      } # FET
3891    }; # $formatting_end_tag    }; # $formatting_end_tag
3892    
3893    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3894      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3895    }; # $insert_to_current    }; # $insert_to_current
3896    
3897    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3898                         my $child = shift;      my $child = shift;
3899                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3900                              table => 1, tbody => 1, tfoot => 1,        # MUST
3901                              thead => 1, tr => 1,        my $foster_parent_element;
3902                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3903                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3904                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3905                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3906                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3907                                   !!!cp ('t70');
3908                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3909                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3910                               } else {                               } else {
3911                                   !!!cp ('t71');
3912                                 $foster_parent_element                                 $foster_parent_element
3913                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3914                               }                               }
# Line 2809  sub _tree_construction_main ($) { Line 3919  sub _tree_construction_main ($) {
3919                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3920                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3921                             ($child, $next_sibling);                             ($child, $next_sibling);
3922                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3923                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3924                         }        !!!cp ('t72');
3925          $self->{open_elements}->[-1]->[0]->append_child ($child);
3926        }
3927    }; # $insert_to_foster    }; # $insert_to_foster
3928    
3929    my $insert;    B: while (1) {
   
   B: {  
3930      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3931        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3932          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3933        ## Ignore the token        ## Ignore the token
3934        ## Stay in the phase        ## Stay in the phase
3935        !!!next-token;        !!!next-token;
3936        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3937      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3938               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3939        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3940          ## Turn into the main phase          !!!cp ('t79');
3941          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3942          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3943        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3944          ## Turn into the main phase          !!!cp ('t80');
3945          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3946          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3947          } else {
3948            !!!cp ('t81');
3949        }        }
3950    
3951  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3952  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3953        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3954        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3955          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3956              !!!cp ('t84');
3957            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3958              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3959               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3960          }          }
3961        }        }
3962          !!!nack ('t84.1');
3963        !!!next-token;        !!!next-token;
3964        redo B;        next B;
3965      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3966        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3967        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3968            !!!cp ('t85');
3969          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3970        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3971            !!!cp ('t86');
3972          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3973        } else {        } else {
3974            !!!cp ('t87');
3975          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3976        }        }
3977        !!!next-token;        !!!next-token;
3978        redo B;        next B;
3979      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3980          if ($token->{type} == CHARACTER_TOKEN) {
3981            !!!cp ('t87.1');
3982            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3983            !!!next-token;
3984            next B;
3985          } elsif ($token->{type} == START_TAG_TOKEN) {
3986            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3987                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3988                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3989                ($token->{tag_name} eq 'svg' and
3990                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3991              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3992              !!!cp ('t87.2');
3993              #
3994            } elsif ({
3995                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3996                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3997                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3998                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3999                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4000                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4001                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4002                      var => 1,
4003                     }->{$token->{tag_name}}) {
4004              !!!cp ('t87.2');
4005              !!!parse-error (type => 'not closed',
4006                              value => $self->{open_elements}->[-1]->[0]
4007                                  ->manakai_local_name,
4008                              token => $token);
4009    
4010              pop @{$self->{open_elements}}
4011                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4012    
4013              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4014              ## Reprocess.
4015              next B;
4016            } else {
4017              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4018              my $tag_name = $token->{tag_name};
4019              if ($nsuri eq $SVG_NS) {
4020                $tag_name = {
4021                   altglyph => 'altGlyph',
4022                   altglyphdef => 'altGlyphDef',
4023                   altglyphitem => 'altGlyphItem',
4024                   animatecolor => 'animateColor',
4025                   animatemotion => 'animateMotion',
4026                   animatetransform => 'animateTransform',
4027                   clippath => 'clipPath',
4028                   feblend => 'feBlend',
4029                   fecolormatrix => 'feColorMatrix',
4030                   fecomponenttransfer => 'feComponentTransfer',
4031                   fecomposite => 'feComposite',
4032                   feconvolvematrix => 'feConvolveMatrix',
4033                   fediffuselighting => 'feDiffuseLighting',
4034                   fedisplacementmap => 'feDisplacementMap',
4035                   fedistantlight => 'feDistantLight',
4036                   feflood => 'feFlood',
4037                   fefunca => 'feFuncA',
4038                   fefuncb => 'feFuncB',
4039                   fefuncg => 'feFuncG',
4040                   fefuncr => 'feFuncR',
4041                   fegaussianblur => 'feGaussianBlur',
4042                   feimage => 'feImage',
4043                   femerge => 'feMerge',
4044                   femergenode => 'feMergeNode',
4045                   femorphology => 'feMorphology',
4046                   feoffset => 'feOffset',
4047                   fepointlight => 'fePointLight',
4048                   fespecularlighting => 'feSpecularLighting',
4049                   fespotlight => 'feSpotLight',
4050                   fetile => 'feTile',
4051                   feturbulence => 'feTurbulence',
4052                   foreignobject => 'foreignObject',
4053                   glyphref => 'glyphRef',
4054                   lineargradient => 'linearGradient',
4055                   radialgradient => 'radialGradient',
4056                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4057                   textpath => 'textPath',  
4058                }->{$tag_name} || $tag_name;
4059              }
4060    
4061              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4062    
4063              ## "adjust foreign attributes" - done in insert-element-f
4064    
4065              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4066    
4067              if ($self->{self_closing}) {
4068                pop @{$self->{open_elements}};
4069                !!!ack ('t87.3');
4070              } else {
4071                !!!cp ('t87.4');
4072              }
4073    
4074              !!!next-token;
4075              next B;
4076            }
4077          } elsif ($token->{type} == END_TAG_TOKEN) {
4078            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4079            !!!cp ('t87.5');
4080            #
4081          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4082            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4083            !!!cp ('t87.6');
4084            #
4085            ## TODO: ...
4086          } else {
4087            die "$0: $token->{type}: Unknown token type";        
4088          }
4089        }
4090    
4091        if ($self->{insertion_mode} & HEAD_IMS) {
4092        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4093          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4094            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4095                !!!cp ('t88.2');
4096                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4097              } else {
4098                !!!cp ('t88.1');
4099                ## Ignore the token.
4100                !!!next-token;
4101                next B;
4102              }
4103            unless (length $token->{data}) {            unless (length $token->{data}) {
4104                !!!cp ('t88');
4105              !!!next-token;              !!!next-token;
4106              redo B;              next B;
4107            }            }
4108          }          }
4109    
4110          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4111              !!!cp ('t89');
4112            ## As if <head>            ## As if <head>
4113            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4114            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4115            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4116                  [$self->{head_element}, $el_category->{head}];
4117    
4118            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4119            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4120    
4121            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4122          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4123              !!!cp ('t90');
4124            ## As if </noscript>            ## As if </noscript>
4125            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4126            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4127                        
4128            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4129            ## As if </head>            ## As if </head>
# Line 2920  sub _tree_construction_main ($) { Line 4131  sub _tree_construction_main ($) {
4131    
4132            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4133          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4134              !!!cp ('t91');
4135            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4136    
4137            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4138            } else {
4139              !!!cp ('t92');
4140          }          }
4141    
4142              ## "after head" insertion mode          ## "after head" insertion mode
4143              ## As if <body>          ## As if <body>
4144              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4145              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4146              ## reprocess          ## reprocess
4147              redo B;          next B;
4148            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4149              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4150                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4151                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4152                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4153                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4154                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4155                  !!!next-token;              push @{$self->{open_elements}},
4156                  redo B;                  [$self->{head_element}, $el_category->{head}];
4157                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4158                  #              !!!nack ('t93.1');
4159                } else {              !!!next-token;
4160                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4161                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4162                  !!!next-token;              !!!cp ('t93.2');
4163                  redo B;              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4164                }              ## Ignore the token
4165              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t93.3');
4166                ## As if <head>              !!!next-token;
4167                !!!create-element ($self->{head_element}, 'head');              next B;
4168                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            } else {
4169                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!cp ('t95');
4170                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4171                ## Ignore the token
4172                !!!nack ('t95.1');
4173                !!!next-token;
4174                next B;
4175              }
4176            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4177              !!!cp ('t96');
4178              ## As if <head>
4179              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4180              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4181              push @{$self->{open_elements}},
4182                  [$self->{head_element}, $el_category->{head}];
4183    
4184                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4185                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4186              }          } else {
4187              !!!cp ('t97');
4188            }
4189    
4190              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4191                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4192                    !!!cp ('t98');
4193                  ## As if </noscript>                  ## As if </noscript>
4194                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4195                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4196                                
4197                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4198                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4199                  } else {
4200                    !!!cp ('t99');
4201                }                }
4202    
4203                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4204                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4205                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4206                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4207                    push @{$self->{open_elements}},
4208                        [$self->{head_element}, $el_category->{head}];
4209                  } else {
4210                    !!!cp ('t101');
4211                }                }
4212                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4213                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4214                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4215                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4216                  !!!nack ('t101.1');
4217                !!!next-token;                !!!next-token;
4218                redo B;                next B;
4219              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4220                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4221                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4222                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4223                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4224                    push @{$self->{open_elements}},
4225                        [$self->{head_element}, $el_category->{head}];
4226                  } else {
4227                    !!!cp ('t103');
4228                }                }
4229                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4230                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4231                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4232                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4233                  !!!ack ('t103.1');
4234                !!!next-token;                !!!next-token;
4235                redo B;                next B;
4236              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4237                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4238                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4240                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241                    push @{$self->{open_elements}},
4242                        [$self->{head_element}, $el_category->{head}];
4243                  } else {
4244                    !!!cp ('t105');
4245                }                }
4246                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4247                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.
4248    
4249                unless ($self->{confident}) {                unless ($self->{confident}) {
4250                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4251                      !!!cp ('t106');
4252                      ## NOTE: Whether the encoding is supported or not is handled
4253                      ## in the {change_encoding} callback.
4254                    $self->{change_encoding}                    $self->{change_encoding}
4255                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4256                             $token);
4257                                        
4258                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4259                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4260                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4261                                                 ->{has_reference});                                                 ->{has_reference});
4262                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4263                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4264                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4265                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4266                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4267                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4268                        !!!cp ('t107');
4269                        ## NOTE: Whether the encoding is supported or not is handled
4270                        ## in the {change_encoding} callback.
4271                      $self->{change_encoding}                      $self->{change_encoding}
4272                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4273                               $token);
4274                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4275                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4276                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
4277                                                     ->{has_reference});                                                     ->{has_reference});
4278                      } else {
4279                        !!!cp ('t108');
4280                    }                    }
4281                  }                  }
4282                } else {                } else {
4283                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
4284                      !!!cp ('t109');
4285                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4286                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4287                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4288                                                 ->{has_reference});                                                 ->{has_reference});
4289                  }                  }
4290                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
4291                      !!!cp ('t110');
4292                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4293                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4294                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3039  sub _tree_construction_main ($) { Line 4296  sub _tree_construction_main ($) {
4296                  }                  }
4297                }                }
4298    
4299                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4300                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4301                  !!!ack ('t110.1');
4302                !!!next-token;                !!!next-token;
4303                redo B;                next B;
4304              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4305                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4306                    !!!cp ('t111');
4307                  ## As if </noscript>                  ## As if </noscript>
4308                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4309                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4310                                
4311                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4312                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4313                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4314                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4315                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4316                    push @{$self->{open_elements}},
4317                        [$self->{head_element}, $el_category->{head}];
4318                  } else {
4319                    !!!cp ('t113');
4320                }                }
4321    
4322                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4323                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4324                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4325                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4326                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4327                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4328                redo B;                next B;
4329              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4330                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4331                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4332                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4333                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4334                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4335                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4336                    push @{$self->{open_elements}},
4337                        [$self->{head_element}, $el_category->{head}];
4338                  } else {
4339                    !!!cp ('t115');
4340                }                }
4341                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4342                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4343                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4344                redo B;                next B;
4345              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4346                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4347                    !!!cp ('t116');
4348                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4349                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4350                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4351                    !!!nack ('t116.1');
4352                  !!!next-token;                  !!!next-token;
4353                  redo B;                  next B;
4354                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4355                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4356                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4357                  ## Ignore the token                  ## Ignore the token
4358                    !!!nack ('t117.1');
4359                  !!!next-token;                  !!!next-token;
4360                  redo B;                  next B;
4361                } else {                } else {
4362                    !!!cp ('t118');
4363                  #                  #
4364                }                }
4365              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4366                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4367                    !!!cp ('t119');
4368                  ## As if </noscript>                  ## As if </noscript>
4369                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4370                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4371                                
4372                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4373                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4374                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4375                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4376                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4377                    push @{$self->{open_elements}},
4378                        [$self->{head_element}, $el_category->{head}];
4379                  } else {
4380                    !!!cp ('t121');
4381                }                }
4382    
4383                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4384                $script_start_tag->($insert_to_current);                $script_start_tag->();
4385                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4386                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4387                redo B;                next B;
4388              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4389                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4390                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4391                    !!!cp ('t122');
4392                  ## As if </noscript>                  ## As if </noscript>
4393                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4394                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4395                                    
4396                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4397                  ## As if </head>                  ## As if </head>
# Line 3122  sub _tree_construction_main ($) { Line 4399  sub _tree_construction_main ($) {
4399                                    
4400                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4401                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4402                    !!!cp ('t124');
4403                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4404                                    
4405                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4406                  } else {
4407                    !!!cp ('t125');
4408                }                }
4409    
4410                ## "after head" insertion mode                ## "after head" insertion mode
4411                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4412                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4413                    !!!cp ('t126');
4414                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4415                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4416                    !!!cp ('t127');
4417                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4418                } else {                } else {
4419                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4420                }                }
4421                  !!!nack ('t127.1');
4422                !!!next-token;                !!!next-token;
4423                redo B;                next B;
4424              } else {              } else {
4425                  !!!cp ('t128');
4426                #                #
4427              }              }
4428    
4429              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4430                  !!!cp ('t129');
4431                ## As if </noscript>                ## As if </noscript>
4432                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4433                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4434                                
4435                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4436                ## As if </head>                ## As if </head>
# Line 3153  sub _tree_construction_main ($) { Line 4438  sub _tree_construction_main ($) {
4438    
4439                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4440              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4441                  !!!cp ('t130');
4442                ## As if </head>                ## As if </head>
4443                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4444    
4445                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4446                } else {
4447                  !!!cp ('t131');
4448              }              }
4449    
4450              ## "after head" insertion mode              ## "after head" insertion mode
4451              ## As if <body>              ## As if <body>
4452              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4453              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4454              ## reprocess              ## reprocess
4455              redo B;              !!!ack-later;
4456                next B;
4457            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4458              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4459                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4460                    !!!cp ('t132');
4461                  ## As if <head>                  ## As if <head>
4462                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4463                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4464                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4465                        [$self->{head_element}, $el_category->{head}];
4466    
4467                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4468                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4469                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4470                  !!!next-token;                  !!!next-token;
4471                  redo B;                  next B;
4472                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4473                    !!!cp ('t133');
4474                  ## As if </noscript>                  ## As if </noscript>
4475                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4476                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4477                                    
4478                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4479                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4480                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4481                  !!!next-token;                  !!!next-token;
4482                  redo B;                  next B;
4483                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4484                    !!!cp ('t134');
4485                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4486                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4487                  !!!next-token;                  !!!next-token;
4488                  redo B;                  next B;
4489                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4490                    !!!cp ('t134.1');
4491                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4492                    ## Ignore the token
4493                    !!!next-token;
4494                    next B;
4495                } else {                } else {
4496                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4497                }                }
4498              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4499                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4500                    !!!cp ('t136');
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4503                  !!!next-token;                  !!!next-token;
4504                  redo B;                  next B;
4505                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4506                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4507                    !!!cp ('t137');
4508                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4509                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4510                  !!!next-token;                  !!!next-token;
4511                  redo B;                  next B;
4512                } else {                } else {
4513                    !!!cp ('t138');
4514                  #                  #
4515                }                }
4516              } elsif ({              } elsif ({
4517                        body => 1, html => 1,                        body => 1, html => 1,
4518                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4519                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4520                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4521                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4522                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4523                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4524                  ## Ignore the token                  ## Ignore the token
4525                  !!!next-token;                  !!!next-token;
4526                  redo B;                  next B;
4527                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4528                    !!!cp ('t140.1');
4529                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4530                    ## Ignore the token
4531                    !!!next-token;
4532                    next B;
4533                  } else {
4534                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4535                }                }
4536                              } elsif ($token->{tag_name} eq 'p') {
4537                #                !!!cp ('t142');
4538              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4539                        p => 1, br => 1,                ## Ignore the token
4540                       }->{$token->{tag_name}}) {                !!!next-token;
4541                  next B;
4542                } elsif ($token->{tag_name} eq 'br') {
4543                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4544                  ## As if <head>                  !!!cp ('t142.2');
4545                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4546                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4547                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4548                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4549      
4550                    ## Reprocess in the "after head" insertion mode...
4551                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4552                    !!!cp ('t143.2');
4553                    ## As if </head>
4554                    pop @{$self->{open_elements}};
4555                    $self->{insertion_mode} = AFTER_HEAD_IM;
4556      
4557                    ## Reprocess in the "after head" insertion mode...
4558                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4559                    !!!cp ('t143.3');
4560                    ## ISSUE: Two parse errors for <head><noscript></br>
4561                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4562                    ## As if </noscript>
4563                    pop @{$self->{open_elements}};
4564                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4565    
4566                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4567                }                  ## As if </head>
4568                    pop @{$self->{open_elements}};
4569                    $self->{insertion_mode} = AFTER_HEAD_IM;
4570    
4571                #                  ## Reprocess in the "after head" insertion mode...
4572              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4573                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4574                  #                  #
4575                } else {                } else {
4576                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4577                }                }
4578    
4579                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4580                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4581                  ## Ignore the token
4582                  !!!next-token;
4583                  next B;
4584                } else {
4585                  !!!cp ('t145');
4586                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4587                  ## Ignore the token
4588                  !!!next-token;
4589                  next B;
4590              }              }
4591    
4592              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4593                  !!!cp ('t146');
4594                ## As if </noscript>                ## As if </noscript>
4595                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4596                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4597                                
4598                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4599                ## As if </head>                ## As if </head>
# Line 3265  sub _tree_construction_main ($) { Line 4601  sub _tree_construction_main ($) {
4601    
4602                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4603              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4604                  !!!cp ('t147');
4605                ## As if </head>                ## As if </head>
4606                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4607    
4608                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4609              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4610                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4611                  !!!cp ('t148');
4612                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4613                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4614                !!!next-token;                !!!next-token;
4615                redo B;                next B;
4616                } else {
4617                  !!!cp ('t149');
4618              }              }
4619    
4620              ## "after head" insertion mode              ## "after head" insertion mode
4621              ## As if <body>              ## As if <body>
4622              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4623              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4624              ## reprocess              ## reprocess
4625              redo B;              next B;
4626            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4627              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4628            }            !!!cp ('t149.1');
4629    
4630              ## NOTE: As if <head>
4631              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4632              $self->{open_elements}->[-1]->[0]->append_child
4633                  ($self->{head_element});
4634              #push @{$self->{open_elements}},
4635              #    [$self->{head_element}, $el_category->{head}];
4636              #$self->{insertion_mode} = IN_HEAD_IM;
4637              ## NOTE: Reprocess.
4638    
4639              ## NOTE: As if </head>
4640              #pop @{$self->{open_elements}};
4641              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4642              ## NOTE: Reprocess.
4643              
4644              #
4645            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4646              !!!cp ('t149.2');
4647    
4648              ## NOTE: As if </head>
4649              pop @{$self->{open_elements}};
4650              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4651              ## NOTE: Reprocess.
4652    
4653              #
4654            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4655              !!!cp ('t149.3');
4656    
4657              !!!parse-error (type => 'in noscript:#eof', token => $token);
4658    
4659              ## As if </noscript>
4660              pop @{$self->{open_elements}};
4661              #$self->{insertion_mode} = IN_HEAD_IM;
4662              ## NOTE: Reprocess.
4663    
4664              ## NOTE: As if </head>
4665              pop @{$self->{open_elements}};
4666              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4667              ## NOTE: Reprocess.
4668    
4669              #
4670            } else {
4671              !!!cp ('t149.4');
4672              #
4673            }
4674    
4675            ## NOTE: As if <body>
4676            !!!insert-element ('body',, $token);
4677            $self->{insertion_mode} = IN_BODY_IM;
4678            ## NOTE: Reprocess.
4679            next B;
4680          } else {
4681            die "$0: $token->{type}: Unknown token type";
4682          }
4683    
4684            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4685      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4686            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4687                !!!cp ('t150');
4688              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4689              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4690                            
4691              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4692    
4693              !!!next-token;              !!!next-token;
4694              redo B;              next B;
4695            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4696              if ({              if ({
4697                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3303  sub _tree_construction_main ($) { Line 4699  sub _tree_construction_main ($) {
4699                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4700                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4701                  ## have an element in table scope                  ## have an element in table scope
4702                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4705                      $tn = $node->[1];                      !!!cp ('t151');
4706                      last INSCOPE;  
4707                    } elsif ({                      ## Close the cell
4708                              table => 1, html => 1,                      !!!back-token; # <x>
4709                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4710                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4711                    }                                line => $token->{line},
4712                  } # INSCOPE                                column => $token->{column}};
4713                    unless (defined $tn) {                      next B;
4714                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4715                      ## Ignore the token                      !!!cp ('t152');
4716                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4717                      redo B;                      last;
4718                    }                    }
4719                                    }
4720                  ## Close the cell  
4721                  !!!back-token; # <?>                  !!!cp ('t153');
4722                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4723                  redo B;                      value => $token->{tag_name}, token => $token);
4724                    ## Ignore the token
4725                    !!!nack ('t153.1');
4726                    !!!next-token;
4727                    next B;
4728                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4729                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4730                                    
4731                  ## As if </caption>                  ## NOTE: As if </caption>.
4732                  ## have a table element in table scope                  ## have a table element in table scope
4733                  my $i;                  my $i;
4734                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4735                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4736                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4737                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4738                      last INSCOPE;                        !!!cp ('t155');
4739                    } elsif ({                        $i = $_;
4740                              table => 1, html => 1,                        last INSCOPE;
4741                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4742                      last INSCOPE;                        !!!cp ('t156');
4743                          last;
4744                        }
4745                    }                    }
4746    
4747                      !!!cp ('t157');
4748                      !!!parse-error (type => 'start tag not allowed',
4749                                      value => $token->{tag_name}, token => $token);
4750                      ## Ignore the token
4751                      !!!nack ('t157.1');
4752                      !!!next-token;
4753                      next B;
4754                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4755                                    
4756                  ## generate implied end tags                  ## generate implied end tags
4757                  if ({                  while ($self->{open_elements}->[-1]->[1]
4758                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4759                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4760                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4761                  }                  }
4762    
4763                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4764                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4765                      !!!parse-error (type => 'not closed',
4766                                      value => $self->{open_elements}->[-1]->[0]
4767                                          ->manakai_local_name,
4768                                      token => $token);
4769                    } else {
4770                      !!!cp ('t160');
4771                  }                  }
4772                                    
4773                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3375  sub _tree_construction_main ($) { Line 4777  sub _tree_construction_main ($) {
4777                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4778                                    
4779                  ## reprocess                  ## reprocess
4780                  redo B;                  !!!ack-later;
4781                    next B;
4782                } else {                } else {
4783                    !!!cp ('t161');
4784                  #                  #
4785                }                }
4786              } else {              } else {
4787                  !!!cp ('t162');
4788                #                #
4789              }              }
4790            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3389  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794                  my $i;                  my $i;
4795                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4796                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4797                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4798                        !!!cp ('t163');
4799                      $i = $_;                      $i = $_;
4800                      last INSCOPE;                      last INSCOPE;
4801                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4802                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4803                      last INSCOPE;                      last INSCOPE;
4804                    }                    }
4805                  } # INSCOPE                  } # INSCOPE
4806                    unless (defined $i) {                    unless (defined $i) {
4807                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4808                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4809                      ## Ignore the token                      ## Ignore the token
4810                      !!!next-token;                      !!!next-token;
4811                      redo B;                      next B;
4812                    }                    }
4813                                    
4814                  ## generate implied end tags                  ## generate implied end tags
4815                  if ({                  while ($self->{open_elements}->[-1]->[1]
4816                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4817                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4818                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4819                  }                  }
4820                    
4821                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4822                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4823                      !!!cp ('t167');
4824                      !!!parse-error (type => 'not closed',
4825                                      value => $self->{open_elements}->[-1]->[0]
4826                                          ->manakai_local_name,
4827                                      token => $token);
4828                    } else {
4829                      !!!cp ('t168');
4830                  }                  }
4831                                    
4832                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3430  sub _tree_construction_main ($) { Line 4836  sub _tree_construction_main ($) {
4836                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4837                                    
4838                  !!!next-token;                  !!!next-token;
4839                  redo B;                  next B;
4840                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4841                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4842                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                  ## Ignore the token                  ## Ignore the token
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                } else {                } else {
4847                    !!!cp ('t170');
4848                  #                  #
4849                }                }
4850              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4851                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4852                  ## have a table element in table scope                  ## have a table element in table scope
4853                  my $i;                  my $i;
4854                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4855                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4856                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4857                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4858                      last INSCOPE;                        !!!cp ('t171');
4859                    } elsif ({                        $i = $_;
4860                              table => 1, html => 1,                        last INSCOPE;
4861                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4862                      last INSCOPE;                        !!!cp ('t172');
4863                          last;
4864                        }
4865                    }                    }
4866    
4867                      !!!cp ('t173');
4868                      !!!parse-error (type => 'unmatched end tag',
4869                                      value => $token->{tag_name}, token => $token);
4870                      ## Ignore the token
4871                      !!!next-token;
4872                      next B;
4873                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4874                                    
4875                  ## generate implied end tags                  ## generate implied end tags
4876                  if ({                  while ($self->{open_elements}->[-1]->[1]
4877                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4878                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4879                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4880                  }                  }
4881                                    
4882                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4883                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4884                      !!!parse-error (type => 'not closed',
4885                                      value => $self->{open_elements}->[-1]->[0]
4886                                          ->manakai_local_name,
4887                                      token => $token);
4888                    } else {
4889                      !!!cp ('t176');
4890                  }                  }
4891                                    
4892                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3484  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4897                                    
4898                  !!!next-token;                  !!!next-token;
4899                  redo B;                  next B;
4900                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4901                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4902                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4903                  ## Ignore the token                  ## Ignore the token
4904                  !!!next-token;                  !!!next-token;
4905                  redo B;                  next B;
4906                } else {                } else {
4907                    !!!cp ('t178');
4908                  #                  #
4909                }                }
4910              } elsif ({              } elsif ({
# Line 3501  sub _tree_construction_main ($) { Line 4915  sub _tree_construction_main ($) {
4915                ## have an element in table scope                ## have an element in table scope
4916                my $i;                my $i;
4917                my $tn;                my $tn;
4918                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4919                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4920                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4921                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4922                    last INSCOPE;                      !!!cp ('t179');
4923                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4924                    $tn = $node->[1];  
4925                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4926                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4927                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4928                            table => 1, html => 1,                                line => $token->{line},
4929                           }->{$node->[1]}) {                                column => $token->{column}};
4930                    last INSCOPE;                      next B;
4931                      } elsif ($node->[1] & TABLE_CELL_EL) {
4932                        !!!cp ('t180');
4933                        $tn = $node->[0]->manakai_local_name;
4934                        ## NOTE: There is exactly one |td| or |th| element
4935                        ## in scope in the stack of open elements by definition.
4936                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4937                        ## ISSUE: Can this be reached?
4938                        !!!cp ('t181');
4939                        last;
4940                      }
4941                  }                  }
4942                } # INSCOPE  
4943                unless (defined $i) {                  !!!cp ('t182');
4944                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4945                        value => $token->{tag_name}, token => $token);
4946                  ## Ignore the token                  ## Ignore the token
4947                  !!!next-token;                  !!!next-token;
4948                  redo B;                  next B;
4949                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4950              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4951                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4952                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4953    
4954                ## As if </caption>                ## As if </caption>
4955                ## have a table element in table scope                ## have a table element in table scope
4956                my $i;                my $i;
4957                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4958                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4959                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4960                      !!!cp ('t184');
4961                    $i = $_;                    $i = $_;
4962                    last INSCOPE;                    last INSCOPE;
4963                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4964                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4965                    last INSCOPE;                    last INSCOPE;
4966                  }                  }
4967                } # INSCOPE                } # INSCOPE
4968                unless (defined $i) {                unless (defined $i) {
4969                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4970                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4971                  ## Ignore the token                  ## Ignore the token
4972                  !!!next-token;                  !!!next-token;
4973                  redo B;                  next B;
4974                }                }
4975                                
4976                ## generate implied end tags                ## generate implied end tags
4977                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4978                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4979                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4980                }                }
4981    
4982                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4984                    !!!parse-error (type => 'not closed',
4985                                    value => $self->{open_elements}->[-1]->[0]
4986                                        ->manakai_local_name,
4987                                    token => $token);
4988                  } else {
4989                    !!!cp ('t189');
4990                }                }
4991    
4992                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3577  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4997    
4998                ## reprocess                ## reprocess
4999                redo B;                next B;
5000              } elsif ({              } elsif ({
5001                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5002                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5003                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5004                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5005                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5006                  ## Ignore the token                  ## Ignore the token
5007                  !!!next-token;                  !!!next-token;
5008                  redo B;                  next B;
5009                } else {                } else {
5010                    !!!cp ('t191');
5011                  #                  #
5012                }                }
5013              } elsif ({              } elsif ({
# Line 3594  sub _tree_construction_main ($) { Line 5015  sub _tree_construction_main ($) {
5015                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5016                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5017                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5018                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5019                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5020                ## Ignore the token                ## Ignore the token
5021                !!!next-token;                !!!next-token;
5022                redo B;                next B;
5023              } else {              } else {
5024                  !!!cp ('t193');
5025                #                #
5026              }              }
5027          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5028            for my $entry (@{$self->{open_elements}}) {
5029              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5030                !!!cp ('t75');
5031                !!!parse-error (type => 'in body:#eof', token => $token);
5032                last;
5033              }
5034            }
5035    
5036            ## Stop parsing.
5037            last B;
5038        } else {        } else {
5039          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5040        }        }
# Line 3609  sub _tree_construction_main ($) { Line 5043  sub _tree_construction_main ($) {
5043        #        #
5044      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5045        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5046              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5047                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5048              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5049                                
5050                unless (length $token->{data}) {            unless (length $token->{data}) {
5051                  !!!next-token;              !!!cp ('t194');
5052                  redo B;              !!!next-token;
5053                }              next B;
5054              }            } else {
5055                !!!cp ('t195');
5056              }
5057            }
5058    
5059              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5060    
5061              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5062              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3626  sub _tree_construction_main ($) { Line 5064  sub _tree_construction_main ($) {
5064              ## result in a new Text node.              ## result in a new Text node.
5065              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5066                            
5067              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]}) {  
5068                # MUST                # MUST
5069                my $foster_parent_element;                my $foster_parent_element;
5070                my $next_sibling;                my $next_sibling;
5071                my $prev_sibling;                my $prev_sibling;
5072                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5073                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5074                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5075                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5076                        !!!cp ('t196');
5077                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5078                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5079                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5080                    } else {                    } else {
5081                        !!!cp ('t197');
5082                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5083                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5084                    }                    }
# Line 3653  sub _tree_construction_main ($) { Line 5090  sub _tree_construction_main ($) {
5090                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5091                if (defined $prev_sibling and                if (defined $prev_sibling and
5092                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5093                    !!!cp ('t198');
5094                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5095                } else {                } else {
5096                    !!!cp ('t199');
5097                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5098                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5099                     $next_sibling);                     $next_sibling);
5100                }                }
5101              } else {            $open_tables->[-1]->[1] = 1; # tainted
5102                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5103              }            !!!cp ('t200');
5104              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5105            }
5106                            
5107              !!!next-token;          !!!next-token;
5108              redo B;          next B;
5109        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5110              if ({              if ({
5111                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3672  sub _tree_construction_main ($) { Line 5113  sub _tree_construction_main ($) {
5113                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5114                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5115                  ## Clear back to table context                  ## Clear back to table context
5116                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5117                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5118                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5119                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5120                  }                  }
5121                                    
5122                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5123                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5124                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5125                }                }
5126    
5127                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5128                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5129                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5130                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5131                  }                  }
5132                                    
5133                  ## Clear back to table body context                  ## Clear back to table body context
5134                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5135                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5136                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5137                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5138                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5139                  }                  }
5140                                    
5141                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5142                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5143                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5144                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5145                      !!!nack ('t204');
5146                    !!!next-token;                    !!!next-token;
5147                    redo B;                    next B;
5148                  } else {                  } else {
5149                    !!!insert-element ('tr');                    !!!cp ('t205');
5150                      !!!insert-element ('tr',, $token);
5151                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5152                  }                  }
5153                  } else {
5154                    !!!cp ('t206');
5155                }                }
5156    
5157                ## Clear back to table row context                ## Clear back to table row context
5158                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5159                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5160                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5161                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5162                }                }
5163                                
5164                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5165                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5166    
5167                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5168                                
5169                  !!!nack ('t207.1');
5170                !!!next-token;                !!!next-token;
5171                redo B;                next B;
5172              } elsif ({              } elsif ({
5173                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5174                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3733  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                  my $i;                  my $i;
5181                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5183                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5184                        !!!cp ('t208');
5185                      $i = $_;                      $i = $_;
5186                      last INSCOPE;                      last INSCOPE;
5187                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5188                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5189                      last INSCOPE;                      last INSCOPE;
5190                    }                    }
5191                  } # INSCOPE                  } # INSCOPE
5192                  unless (defined $i) {                  unless (defined $i) {
5193                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5194    ## TODO: This type is wrong.
5195                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5196                    ## Ignore the token                    ## Ignore the token
5197                      !!!nack ('t210.1');
5198                    !!!next-token;                    !!!next-token;
5199                    redo B;                    next B;
5200                  }                  }
5201                                    
5202                  ## Clear back to table row context                  ## Clear back to table row context
5203                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5204                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5205                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5206                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5207                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5208                  }                  }
5209                                    
5210                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5211                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5212                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5213                      !!!cp ('t212');
5214                    ## reprocess                    ## reprocess
5215                    redo B;                    !!!ack-later;
5216                      next B;
5217                  } else {                  } else {
5218                      !!!cp ('t213');
5219                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5220                  }                  }
5221                }                }
# Line 3772  sub _tree_construction_main ($) { Line 5225  sub _tree_construction_main ($) {
5225                  my $i;                  my $i;
5226                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5227                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5228                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5229                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5230                      $i = $_;                      $i = $_;
5231                      last INSCOPE;                      last INSCOPE;
5232                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5233                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5234                      last INSCOPE;                      last INSCOPE;
5235                    }                    }
5236                  } # INSCOPE                  } # INSCOPE
5237                  unless (defined $i) {                  unless (defined $i) {
5238                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5239    ## TODO: This erorr type ios wrong.
5240                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5241                    ## Ignore the token                    ## Ignore the token
5242                      !!!nack ('t216.1');
5243                    !!!next-token;                    !!!next-token;
5244                    redo B;                    next B;
5245                  }                  }
5246    
5247                  ## Clear back to table body context                  ## Clear back to table body context
5248                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5249                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5250                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5251                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5252                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5253                  }                  }
5254                                    
# Line 3808  sub _tree_construction_main ($) { Line 5262  sub _tree_construction_main ($) {
5262                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5263                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5264                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5265                  } else {
5266                    !!!cp ('t218');
5267                }                }
5268    
5269                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5270                  ## Clear back to table context                  ## Clear back to table context
5271                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5272                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5273                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5274                      ## ISSUE: Can this state be reached?
5275                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5276                  }                  }
5277                                    
5278                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5279                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5280                  ## reprocess                  ## reprocess
5281                  redo B;                  !!!ack-later;
5282                    next B;
5283                } elsif ({                } elsif ({
5284                          caption => 1,                          caption => 1,
5285                          colgroup => 1,                          colgroup => 1,
5286                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5287                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5288                  ## Clear back to table context                  ## Clear back to table context
5289                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5290                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5291                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5292                      ## ISSUE: Can this state be reached?
5293                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5294                  }                  }
5295                                    
5296                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5297                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5298                                    
5299                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5300                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5301                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5302                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3846  sub _tree_construction_main ($) { Line 5305  sub _tree_construction_main ($) {
5305                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5306                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5307                  !!!next-token;                  !!!next-token;
5308                  redo B;                  !!!nack ('t220.1');
5309                    next B;
5310                } else {                } else {
5311                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5312                }                }
5313              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5314                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5315                                  value => $self->{open_elements}->[-1]->[0]
5316                                      ->manakai_local_name,
5317                                  token => $token);
5318    
5319                ## As if </table>                ## As if </table>
5320                ## have a table element in table scope                ## have a table element in table scope
5321                my $i;                my $i;
5322                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5323                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5324                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5325                      !!!cp ('t221');
5326                    $i = $_;                    $i = $_;
5327                    last INSCOPE;                    last INSCOPE;
5328                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5329                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5330                    last INSCOPE;                    last INSCOPE;
5331                  }                  }
5332                } # INSCOPE                } # INSCOPE
5333                unless (defined $i) {                unless (defined $i) {
5334                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5335    ## TODO: The following is wrong, maybe.
5336                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5337                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5338                    !!!nack ('t223.1');
5339                  !!!next-token;                  !!!next-token;
5340                  redo B;                  next B;
5341                }                }
5342                                
5343    ## TODO: Followings are removed from the latest spec.
5344                ## generate implied end tags                ## generate implied end tags
5345                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5346                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5347                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5348                }                }
5349    
5350                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5351                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5352                    ## NOTE: |<table><tr><table>|
5353                    !!!parse-error (type => 'not closed',
5354                                    value => $self->{open_elements}->[-1]->[0]
5355                                        ->manakai_local_name,
5356                                    token => $token);
5357                  } else {
5358                    !!!cp ('t226');
5359                }                }
5360    
5361                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5362                  pop @{$open_tables};
5363    
5364                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5365    
5366                ## reprocess            ## reprocess
5367                redo B;            !!!ack-later;
5368          } else {            next B;
5369            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5370              if (not $open_tables->[-1]->[1]) { # tainted
5371                !!!cp ('t227.8');
5372                ## NOTE: This is a "as if in head" code clone.
5373                $parse_rcdata->(CDATA_CONTENT_MODEL);
5374                next B;
5375              } else {
5376                !!!cp ('t227.7');
5377                #
5378              }
5379            } elsif ($token->{tag_name} eq 'script') {
5380              if (not $open_tables->[-1]->[1]) { # tainted
5381                !!!cp ('t227.6');
5382                ## NOTE: This is a "as if in head" code clone.
5383                $script_start_tag->();
5384                next B;
5385              } else {
5386                !!!cp ('t227.5');
5387                #
5388              }
5389            } elsif ($token->{tag_name} eq 'input') {
5390              if (not $open_tables->[-1]->[1]) { # tainted
5391                if ($token->{attributes}->{type}) { ## TODO: case
5392                  my $type = lc $token->{attributes}->{type}->{value};
5393                  if ($type eq 'hidden') {
5394                    !!!cp ('t227.3');
5395                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5396    
5397            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5398    
5399                    ## TODO: form element pointer
5400    
5401                    pop @{$self->{open_elements}};
5402    
5403                    !!!next-token;
5404                    !!!ack ('t227.2.1');
5405                    next B;
5406                  } else {
5407                    !!!cp ('t227.2');
5408                    #
5409                  }
5410                } else {
5411                  !!!cp ('t227.1');
5412                  #
5413                }
5414              } else {
5415                !!!cp ('t227.4');
5416                #
5417              }
5418            } else {
5419              !!!cp ('t227');
5420            #            #
5421          }          }
5422    
5423            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5424    
5425            $insert = $insert_to_foster;
5426            #
5427        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5428              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5429                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3911  sub _tree_construction_main ($) { Line 5431  sub _tree_construction_main ($) {
5431                my $i;                my $i;
5432                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5433                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5434                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5435                      !!!cp ('t228');
5436                    $i = $_;                    $i = $_;
5437                    last INSCOPE;                    last INSCOPE;
5438                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5439                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5440                    last INSCOPE;                    last INSCOPE;
5441                  }                  }
5442                } # INSCOPE                } # INSCOPE
5443                unless (defined $i) {                unless (defined $i) {
5444                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5445                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                  ## Ignore the token                  ## Ignore the token
5447                    !!!nack ('t230.1');
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                  } else {
5451                    !!!cp ('t232');
5452                }                }
5453    
5454                ## Clear back to table row context                ## Clear back to table row context
5455                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5456                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5457                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5458                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5459                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5460                }                }
5461    
5462                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5463                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5464                !!!next-token;                !!!next-token;
5465                redo B;                !!!nack ('t231.1');
5466                  next B;
5467              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5468                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5469                  ## As if </tr>                  ## As if </tr>
# Line 3946  sub _tree_construction_main ($) { Line 5471  sub _tree_construction_main ($) {
5471                  my $i;                  my $i;
5472                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5473                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5474                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5475                        !!!cp ('t233');
5476                      $i = $_;                      $i = $_;
5477                      last INSCOPE;                      last INSCOPE;
5478                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5479                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5480                      last INSCOPE;                      last INSCOPE;
5481                    }                    }
5482                  } # INSCOPE                  } # INSCOPE
5483                  unless (defined $i) {                  unless (defined $i) {
5484                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5485    ## TODO: The following is wrong.
5486                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5487                    ## Ignore the token                    ## Ignore the token
5488                      !!!nack ('t236.1');
5489                    !!!next-token;                    !!!next-token;
5490                    redo B;                    next B;
5491                  }                  }
5492                                    
5493                  ## Clear back to table row context                  ## Clear back to table row context
5494                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5495                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5496                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5497                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5498                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5499                  }                  }
5500                                    
# Line 3980  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508                  my $i;                  my $i;
5509                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5511                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5512                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5513                      $i = $_;                      $i = $_;
5514                      last INSCOPE;                      last INSCOPE;
5515                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5516                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5517                      last INSCOPE;                      last INSCOPE;
5518                    }                    }
5519                  } # INSCOPE                  } # INSCOPE
5520                  unless (defined $i) {                  unless (defined $i) {
5521                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5522                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                    ## Ignore the token                    ## Ignore the token
5524                      !!!nack ('t239.1');
5525                    !!!next-token;                    !!!next-token;
5526                    redo B;                    next B;
5527                  }                  }
5528                                    
5529                  ## Clear back to table body context                  ## Clear back to table body context
5530                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5531                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5532                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5533                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5534                  }                  }
5535                                    
# Line 4018  sub _tree_construction_main ($) { Line 5545  sub _tree_construction_main ($) {
5545                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5546                }                }
5547    
5548                  ## NOTE: </table> in the "in table" insertion mode.
5549                  ## When you edit the code fragment below, please ensure that
5550                  ## the code for <table> in the "in table" insertion mode
5551                  ## is synced with it.
5552    
5553                ## have a table element in table scope                ## have a table element in table scope
5554                my $i;                my $i;
5555                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5556                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5557                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5558                      !!!cp ('t241');
5559                    $i = $_;                    $i = $_;
5560                    last INSCOPE;                    last INSCOPE;
5561                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5562                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5563                    last INSCOPE;                    last INSCOPE;
5564                  }                  }
5565                } # INSCOPE                } # INSCOPE
5566                unless (defined $i) {                unless (defined $i) {
5567                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5568                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5569                  ## Ignore the token                  ## Ignore the token
5570                    !!!nack ('t243.1');
5571                  !!!next-token;                  !!!next-token;
5572                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5573                }                }
5574                                    
5575                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5576                  pop @{$open_tables};
5577                                
5578                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5579                                
5580                !!!next-token;                !!!next-token;
5581                redo B;                next B;
5582              } elsif ({              } elsif ({
5583                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5584                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4069  sub _tree_construction_main ($) { Line 5588  sub _tree_construction_main ($) {
5588                  my $i;                  my $i;
5589                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5591                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5592                        !!!cp ('t247');
5593                      $i = $_;                      $i = $_;
5594                      last INSCOPE;                      last INSCOPE;
5595                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5596                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5597                      last INSCOPE;                      last INSCOPE;
5598                    }                    }
5599                  } # INSCOPE                  } # INSCOPE
5600                    unless (defined $i) {                    unless (defined $i) {
5601                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5602                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                      ## Ignore the token                      ## Ignore the token
5604                        !!!nack ('t249.1');
5605                      !!!next-token;                      !!!next-token;
5606                      redo B;                      next B;
5607                    }                    }
5608                                    
5609                  ## As if </tr>                  ## As if </tr>
# Line 4090  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611                  my $i;                  my $i;
5612                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5613                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5614                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5615                        !!!cp ('t250');
5616                      $i = $_;                      $i = $_;
5617                      last INSCOPE;                      last INSCOPE;
5618                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5619                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5620                      last INSCOPE;                      last INSCOPE;
5621                    }                    }
5622                  } # INSCOPE                  } # INSCOPE
5623                    unless (defined $i) {                    unless (defined $i) {
5624                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5625                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5626                      ## Ignore the token                      ## Ignore the token
5627                        !!!nack ('t252.1');
5628                      !!!next-token;                      !!!next-token;
5629                      redo B;                      next B;
5630                    }                    }
5631                                    
5632                  ## Clear back to table row context                  ## Clear back to table row context
5633                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5634                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5635                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5636                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5637                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5638                  }                  }
5639                                    
# Line 4123  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646                my $i;                my $i;
5647                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5648                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5649                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5650                      !!!cp ('t254');
5651                    $i = $_;                    $i = $_;
5652                    last INSCOPE;                    last INSCOPE;
5653                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5654                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5655                    last INSCOPE;                    last INSCOPE;
5656                  }                  }
5657                } # INSCOPE                } # INSCOPE
5658                unless (defined $i) {                unless (defined $i) {
5659                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5660                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5661                  ## Ignore the token                  ## Ignore the token
5662                    !!!nack ('t256.1');
5663                  !!!next-token;                  !!!next-token;
5664                  redo B;                  next B;
5665                }                }
5666    
5667                ## Clear back to table body context                ## Clear back to table body context
5668                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5669                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5670                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5671                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5672                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5673                }                }
5674    
5675                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5676                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5677                  !!!nack ('t257.1');
5678                !!!next-token;                !!!next-token;
5679                redo B;                next B;
5680              } elsif ({              } elsif ({
5681                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5682                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5683                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5684                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5685                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5686                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5687                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5688                !!!next-token;            ## Ignore the token
5689                redo B;            !!!nack ('t258.1');
5690               !!!next-token;
5691              next B;
5692          } else {          } else {
5693            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5694              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5695    
5696            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5697            #            #
5698          }          }
5699          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5700            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5701                    @{$self->{open_elements}} == 1) { # redundant, maybe
5702              !!!parse-error (type => 'in body:#eof', token => $token);
5703              !!!cp ('t259.1');
5704              #
5705            } else {
5706              !!!cp ('t259.2');
5707              #
5708            }
5709    
5710            ## Stop parsing
5711            last B;
5712        } else {        } else {
5713          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5714        }        }
# Line 4175  sub _tree_construction_main ($) { Line 5717  sub _tree_construction_main ($) {
5717              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5718                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5719                unless (length $token->{data}) {                unless (length $token->{data}) {
5720                    !!!cp ('t260');
5721                  !!!next-token;                  !!!next-token;
5722                  redo B;                  next B;
5723                }                }
5724              }              }
5725                            
5726                !!!cp ('t261');
5727              #              #
5728            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5729              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5730                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5731                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5732                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5733                  !!!ack ('t262.1');
5734                !!!next-token;                !!!next-token;
5735                redo B;                next B;
5736              } else {              } else {
5737                  !!!cp ('t263');
5738                #                #
5739              }              }
5740            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5741              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5742                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5743                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5744                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5745                  ## Ignore the token                  ## Ignore the token
5746                  !!!next-token;                  !!!next-token;
5747                  redo B;                  next B;
5748                } else {                } else {
5749                    !!!cp ('t265');
5750                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5751                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                              next B;            
5754                }                }
5755              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5756                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5757                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5758                ## Ignore the token                ## Ignore the token
5759                !!!next-token;                !!!next-token;
5760                redo B;                next B;
5761              } else {              } else {
5762                  !!!cp ('t267');
5763                #                #
5764              }              }
5765            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5766              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5767            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5768              !!!cp ('t270.2');
5769              ## Stop parsing.
5770              last B;
5771            } else {
5772              ## NOTE: As if </colgroup>.
5773              !!!cp ('t270.1');
5774              pop @{$self->{open_elements}}; # colgroup
5775              $self->{insertion_mode} = IN_TABLE_IM;
5776              ## Reprocess.
5777              next B;
5778            }
5779          } else {
5780            die "$0: $token->{type}: Unknown token type";
5781          }
5782    
5783            ## As if </colgroup>            ## As if </colgroup>
5784            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5785              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5786    ## TODO: Wrong error type?
5787                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5788              ## Ignore the token              ## Ignore the token
5789                !!!nack ('t269.1');
5790              !!!next-token;              !!!next-token;
5791              redo B;              next B;
5792            } else {            } else {
5793                !!!cp ('t270');
5794              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5795              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5796                !!!ack-later;
5797              ## reprocess              ## reprocess
5798              redo B;              next B;
5799            }            }
5800      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5801        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5802            !!!cp ('t271');
5803          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5804          !!!next-token;          !!!next-token;
5805          redo B;          next B;
5806        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5807              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5808                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5809                  ## As if </option>              !!!cp ('t272');
5810                  pop @{$self->{open_elements}};              ## As if </option>
5811                }              pop @{$self->{open_elements}};
5812              } else {
5813                !!!cp ('t273');
5814              }
5815    
5816                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5817                !!!next-token;            !!!nack ('t273.1');
5818                redo B;            !!!next-token;
5819              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5820                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5821                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5822                  pop @{$self->{open_elements}};              !!!cp ('t274');
5823                }              ## As if </option>
5824                pop @{$self->{open_elements}};
5825              } else {
5826                !!!cp ('t275');
5827              }
5828    
5829                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5830                  ## As if </optgroup>              !!!cp ('t276');
5831                  pop @{$self->{open_elements}};              ## As if </optgroup>
5832                }              pop @{$self->{open_elements}};
5833              } else {
5834                !!!cp ('t277');
5835              }
5836    
5837                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5838                !!!next-token;            !!!nack ('t277.1');
5839                redo B;            !!!next-token;
5840              } elsif ($token->{tag_name} eq 'select') {            next B;
5841                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5842                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5843                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5844                my $i;                    {
5845                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5846                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5847                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5848                    $i = $_;                    }->{$token->{tag_name}})) {
5849                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5850                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5851                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5852                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5853                    last INSCOPE;            ## have an element in table scope
5854                  }            my $i;
5855                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5856                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5857                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5858                  ## Ignore the token                !!!cp ('t278');
5859                  !!!next-token;                $i = $_;
5860                  redo B;                last INSCOPE;
5861                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5862                  !!!cp ('t279');
5863                  last INSCOPE;
5864                }
5865              } # INSCOPE
5866              unless (defined $i) {
5867                !!!cp ('t280');
5868                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5869                ## Ignore the token
5870                !!!nack ('t280.1');
5871                !!!next-token;
5872                next B;
5873              }
5874                                
5875                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5876              splice @{$self->{open_elements}}, $i;
5877    
5878                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5879    
5880                !!!next-token;            if ($token->{tag_name} eq 'select') {
5881                redo B;              !!!nack ('t281.2');
5882                !!!next-token;
5883                next B;
5884              } else {
5885                !!!cp ('t281.1');
5886                !!!ack-later;
5887                ## Reprocess the token.
5888                next B;
5889              }
5890          } else {          } else {
5891            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5892              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5893            ## Ignore the token            ## Ignore the token
5894              !!!nack ('t282.1');
5895            !!!next-token;            !!!next-token;
5896            redo B;            next B;
5897          }          }
5898        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5899              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5900                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5901                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5902                  ## As if </option>              !!!cp ('t283');
5903                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5904                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5905                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5906                } else {              !!!cp ('t284');
5907                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5908                  ## Ignore the token            } else {
5909                }              !!!cp ('t285');
5910                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5911                redo B;              ## Ignore the token
5912              } elsif ($token->{tag_name} eq 'option') {            }
5913                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5914                  pop @{$self->{open_elements}};            !!!next-token;
5915                } else {            next B;
5916                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5917                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5918                }              !!!cp ('t286');
5919                !!!next-token;              pop @{$self->{open_elements}};
5920                redo B;            } else {
5921              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5922                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5923                my $i;              ## Ignore the token
5924                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5925                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5926                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5927                    $i = $_;            next B;
5928                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5929                  } elsif ({            ## have an element in table scope
5930                            table => 1, html => 1,            my $i;
5931                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5932                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5933                  }              if ($node->[1] & SELECT_EL) {
5934                } # INSCOPE                !!!cp ('t288');
5935                unless (defined $i) {                $i = $_;
5936                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5937                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5938                  !!!next-token;                !!!cp ('t289');
5939                  redo B;                last INSCOPE;
5940                }              }
5941              } # INSCOPE
5942              unless (defined $i) {
5943                !!!cp ('t290');
5944                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                ## Ignore the token
5946                !!!nack ('t290.1');
5947                !!!next-token;
5948                next B;
5949              }
5950                                
5951                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5952              splice @{$self->{open_elements}}, $i;
5953    
5954                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5955    
5956                !!!next-token;            !!!nack ('t291.1');
5957                redo B;            !!!next-token;
5958              } elsif ({            next B;
5959                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5960                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5961                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5963                     }->{$token->{tag_name}}) {
5964    ## TODO: The following is wrong?
5965              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5966                                
5967                ## have an element in table scope            ## have an element in table scope
5968                my $i;            my $i;
5969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5970                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5971                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5972                    $i = $_;                !!!cp ('t292');
5973                    last INSCOPE;                $i = $_;
5974                  } elsif ({                last INSCOPE;
5975                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5976                           }->{$node->[1]}) {                !!!cp ('t293');
5977                    last INSCOPE;                last INSCOPE;
5978                  }              }
5979                } # INSCOPE            } # INSCOPE
5980                unless (defined $i) {            unless (defined $i) {
5981                  ## Ignore the token              !!!cp ('t294');
5982                  !!!next-token;              ## Ignore the token
5983                  redo B;              !!!nack ('t294.1');
5984                }              !!!next-token;
5985                next B;
5986              }
5987                                
5988                ## As if </select>            ## As if </select>
5989                ## have an element in table scope            ## have an element in table scope
5990                undef $i;            undef $i;
5991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5992                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5993                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5994                    $i = $_;                !!!cp ('t295');
5995                    last INSCOPE;                $i = $_;
5996                  } elsif ({                last INSCOPE;
5997                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5998                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5999                    last INSCOPE;                !!!cp ('t296');
6000                  }                last INSCOPE;
6001                } # INSCOPE              }
6002                unless (defined $i) {            } # INSCOPE
6003                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6004                  ## Ignore the </select> token              !!!cp ('t297');
6005                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6006                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6007                }              ## Ignore the </select> token
6008                !!!nack ('t297.1');
6009                !!!next-token; ## TODO: ok?
6010                next B;
6011              }
6012                                
6013                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6014              splice @{$self->{open_elements}}, $i;
6015    
6016                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6017    
6018                ## reprocess            !!!ack-later;
6019                redo B;            ## reprocess
6020              next B;
6021          } else {          } else {
6022            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6023              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6024            ## Ignore the token            ## Ignore the token
6025              !!!nack ('t299.3');
6026            !!!next-token;            !!!next-token;
6027            redo B;            next B;
6028          }          }
6029          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6030            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6031                    @{$self->{open_elements}} == 1) { # redundant, maybe
6032              !!!cp ('t299.1');
6033              !!!parse-error (type => 'in body:#eof', token => $token);
6034            } else {
6035              !!!cp ('t299.2');
6036            }
6037    
6038            ## Stop parsing.
6039            last B;
6040        } else {        } else {
6041          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6042        }        }
# Line 4412  sub _tree_construction_main ($) { Line 6050  sub _tree_construction_main ($) {
6050            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6051                        
6052            unless (length $token->{data}) {            unless (length $token->{data}) {
6053                !!!cp ('t300');
6054              !!!next-token;              !!!next-token;
6055              redo B;              next B;
6056            }            }
6057          }          }
6058                    
6059          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6060            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6061              !!!parse-error (type => 'after html:#character', token => $token);
6062    
6063            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6064            } else {
6065              !!!cp ('t302');
6066          }          }
6067                    
6068          ## "after body" insertion mode          ## "after body" insertion mode
6069          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6070    
6071          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6072          ## reprocess          ## reprocess
6073          redo B;          next B;
6074        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6075          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6076            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6077              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6078                        
6079            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6080            } else {
6081              !!!cp ('t304');
6082          }          }
6083    
6084          ## "after body" insertion mode          ## "after body" insertion mode
6085          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6086    
6087          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6088            !!!ack-later;
6089          ## reprocess          ## reprocess
6090          redo B;          next B;
6091        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6092          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6093            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6094              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6095                        
6096            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6097            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6098            } else {
6099              !!!cp ('t306');
6100          }          }
6101    
6102          ## "after body" insertion mode          ## "after body" insertion mode
6103          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6104            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6105              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6106                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6107              ## Ignore the token              ## Ignore the token
6108              !!!next-token;              !!!next-token;
6109              redo B;              next B;
6110            } else {            } else {
6111                !!!cp ('t308');
6112              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6113              !!!next-token;              !!!next-token;
6114              redo B;              next B;
6115            }            }
6116          } else {          } else {
6117            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6118              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6119    
6120            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6121            ## reprocess            ## reprocess
6122            redo B;            next B;
6123          }          }
6124          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6125            !!!cp ('t309.2');
6126            ## Stop parsing
6127            last B;
6128        } else {        } else {
6129          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6130        }        }
# Line 4478  sub _tree_construction_main ($) { Line 6134  sub _tree_construction_main ($) {
6134            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6135                        
6136            unless (length $token->{data}) {            unless (length $token->{data}) {
6137                !!!cp ('t310');
6138              !!!next-token;              !!!next-token;
6139              redo B;              next B;
6140            }            }
6141          }          }
6142                    
6143          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6144            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6145              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6146                !!!parse-error (type => 'in frameset:#character', token => $token);
6147            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6148              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6149                !!!parse-error (type => 'after frameset:#character', token => $token);
6150            } else { # "after html frameset"            } else { # "after html frameset"
6151              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6152                !!!parse-error (type => 'after html:#character', token => $token);
6153    
6154              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6155              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6156              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6157            }            }
6158                        
6159            ## Ignore the token.            ## Ignore the token.
6160            if (length $token->{data}) {            if (length $token->{data}) {
6161                !!!cp ('t314');
6162              ## reprocess the rest of characters              ## reprocess the rest of characters
6163            } else {            } else {
6164                !!!cp ('t315');
6165              !!!next-token;              !!!next-token;
6166            }            }
6167            redo B;            next B;
6168          }          }
6169                    
6170          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6171        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6172          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6173            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6174              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6175    
6176            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6177            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6178          }          } else {
6179              !!!cp ('t317');
6180            }
6181    
6182          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6183              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6184            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6185              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6186              !!!nack ('t318.1');
6187            !!!next-token;            !!!next-token;
6188            redo B;            next B;
6189          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6190                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6191            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6192              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6194              !!!ack ('t319.1');
6195            !!!next-token;            !!!next-token;
6196            redo B;            next B;
6197          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6198              !!!cp ('t320');
6199            ## NOTE: As if in body.            ## NOTE: As if in body.
6200            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6201            redo B;            next B;
6202          } else {          } else {
6203            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6204              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6205                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6206            } else {            } else {
6207              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6208                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6209            }            }
6210            ## Ignore the token            ## Ignore the token
6211              !!!nack ('t322.1');
6212            !!!next-token;            !!!next-token;
6213            redo B;            next B;
6214          }          }
6215        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6216          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6217            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6218              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6219    
6220            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6221            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6222            } else {
6223              !!!cp ('t324');
6224          }          }
6225    
6226          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6227              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6228            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6229                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6230              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6231                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6232              ## Ignore the token              ## Ignore the token
6233              !!!next-token;              !!!next-token;
6234            } else {            } else {
6235                !!!cp ('t326');
6236              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6237              !!!next-token;              !!!next-token;
6238            }            }
6239    
6240            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6241                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6242                !!!cp ('t327');
6243              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6244              } else {
6245                !!!cp ('t328');
6246            }            }
6247            redo B;            next B;
6248          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6249                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6250              !!!cp ('t329');
6251            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6252            !!!next-token;            !!!next-token;
6253            redo B;            next B;
6254          } else {          } else {
6255            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6256              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6257                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6258            } else {            } else {
6259              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6260                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6261            }            }
6262            ## Ignore the token            ## Ignore the token
6263            !!!next-token;            !!!next-token;
6264            redo B;            next B;
6265            }
6266          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6267            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6268                    @{$self->{open_elements}} == 1) { # redundant, maybe
6269              !!!cp ('t331.1');
6270              !!!parse-error (type => 'in body:#eof', token => $token);
6271            } else {
6272              !!!cp ('t331.2');
6273          }          }
6274            
6275            ## Stop parsing
6276            last B;
6277        } else {        } else {
6278          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6279        }        }
# Line 4591  sub _tree_construction_main ($) { Line 6286  sub _tree_construction_main ($) {
6286      ## "in body" insertion mode      ## "in body" insertion mode
6287      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6288        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6289            !!!cp ('t332');
6290          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6291          $script_start_tag->($insert);          $script_start_tag->();
6292          redo B;          next B;
6293        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6294            !!!cp ('t333');
6295          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6296          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6297          redo B;          next B;
6298        } elsif ({        } elsif ({
6299                  base => 1, link => 1,                  base => 1, link => 1,
6300                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6301            !!!cp ('t334');
6302          ## 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
6303          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6304          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6305            !!!ack ('t334.1');
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6309          ## 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
6310          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6311          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.
6312    
6313          unless ($self->{confident}) {          unless ($self->{confident}) {
6314            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6315                !!!cp ('t335');
6316                ## NOTE: Whether the encoding is supported or not is handled
6317                ## in the {change_encoding} callback.
6318              $self->{change_encoding}              $self->{change_encoding}
6319                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6320                            
6321              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6322                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6323                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6324                                           ->{has_reference});                                           ->{has_reference});
6325            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6326              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6327                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6328                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6329                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6330                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6331                  !!!cp ('t336');
6332                  ## NOTE: Whether the encoding is supported or not is handled
6333                  ## in the {change_encoding} callback.
6334                $self->{change_encoding}                $self->{change_encoding}
6335                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6336                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6337                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6338                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 4637  sub _tree_construction_main ($) { Line 6341  sub _tree_construction_main ($) {
6341            }            }
6342          } else {          } else {
6343            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
6344                !!!cp ('t337');
6345              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6346                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6347                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6348                                           ->{has_reference});                                           ->{has_reference});
6349            }            }
6350            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
6351                !!!cp ('t338');
6352              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6353                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6354                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4650  sub _tree_construction_main ($) { Line 6356  sub _tree_construction_main ($) {
6356            }            }
6357          }          }
6358    
6359            !!!ack ('t338.1');
6360          !!!next-token;          !!!next-token;
6361          redo B;          next B;
6362        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6363          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6364          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6365          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6366            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6367        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6368          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6369                                
6370          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6371              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6372              !!!cp ('t342');
6373            ## Ignore the token            ## Ignore the token
6374          } else {          } else {
6375            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6376            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6377              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6378                  !!!cp ('t343');
6379                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6380                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6381                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6382              }              }
6383            }            }
6384          }          }
6385            !!!nack ('t343.1');
6386          !!!next-token;          !!!next-token;
6387          redo B;          next B;
6388        } elsif ({        } elsif ({
6389                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6390                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6391                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6392                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6393                  pre => 1,                  pre => 1, listing => 1,
6394                    form => 1,
6395                    table => 1,
6396                    hr => 1,
6397                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6398            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6399              !!!cp ('t350');
6400              !!!parse-error (type => 'in form:form', token => $token);
6401              ## Ignore the token
6402              !!!nack ('t350.1');
6403              !!!next-token;
6404              next B;
6405            }
6406    
6407          ## has a p element in scope          ## has a p element in scope
6408          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6409            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6410              !!!back-token;              !!!cp ('t344');
6411              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6412              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6413            } elsif ({                        line => $token->{line}, column => $token->{column}};
6414                      table => 1, caption => 1, td => 1, th => 1,              next B;
6415                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6416                     }->{$_->[1]}) {              !!!cp ('t345');
6417              last INSCOPE;              last INSCOPE;
6418            }            }
6419          } # INSCOPE          } # INSCOPE
6420                        
6421          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6422          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6423              !!!nack ('t346.1');
6424            !!!next-token;            !!!next-token;
6425            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6426              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6427              unless (length $token->{data}) {              unless (length $token->{data}) {
6428                  !!!cp ('t346');
6429                !!!next-token;                !!!next-token;
6430                } else {
6431                  !!!cp ('t349');
6432              }              }
6433              } else {
6434                !!!cp ('t348');
6435            }            }
6436          } else {          } elsif ($token->{tag_name} eq 'form') {
6437              !!!cp ('t347.1');
6438              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6439    
6440              !!!nack ('t347.2');
6441            !!!next-token;            !!!next-token;
6442          }          } elsif ($token->{tag_name} eq 'table') {
6443          redo B;            !!!cp ('t382');
6444        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6445          if (defined $self->{form_element}) {            
6446            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6447            ## Ignore the token  
6448              !!!nack ('t382.1');
6449              !!!next-token;
6450            } elsif ($token->{tag_name} eq 'hr') {
6451              !!!cp ('t386');
6452              pop @{$self->{open_elements}};
6453            
6454              !!!nack ('t386.1');
6455            !!!next-token;            !!!next-token;
           redo B;  
6456          } else {          } else {
6457            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6458            !!!next-token;            !!!next-token;
           redo B;  
6459          }          }
6460        } elsif ($token->{tag_name} eq 'li') {          next B;
6461          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6462          ## has a p element in scope          ## has a p element in scope
6463          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6464            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6465              !!!back-token;              !!!cp ('t353');
6466              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6467              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6468            } elsif ({                        line => $token->{line}, column => $token->{column}};
6469                      table => 1, caption => 1, td => 1, th => 1,              next B;
6470                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6471                     }->{$_->[1]}) {              !!!cp ('t354');
6472              last INSCOPE;              last INSCOPE;
6473            }            }
6474          } # INSCOPE          } # INSCOPE
# Line 4758  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476          ## Step 1          ## Step 1
6477          my $i = -1;          my $i = -1;
6478          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6479            my $li_or_dtdd = {li => {li => 1},
6480                              dt => {dt => 1, dd => 1},
6481                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6482          LI: {          LI: {
6483            ## Step 2            ## Step 2
6484            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6485              if ($i != -1) {              if ($i != -1) {
6486                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6487                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6488              }                                value => $self->{open_elements}->[-1]->[0]
6489              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6490              last LI;                                token => $token);
6491            }              } else {
6492                            !!!cp ('t356');
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
6493              }              }
6494              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6495              last LI;              last LI;
6496              } else {
6497                !!!cp ('t357');
6498            }            }
6499                        
6500            ## Step 3            ## Step 3
6501            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6502                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6503                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6504                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6505                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6506                  not ($node->[1] & DIV_EL)) {
6507                !!!cp ('t358');
6508              last LI;              last LI;
6509            }            }
6510                        
6511              !!!cp ('t359');
6512            ## Step 4            ## Step 4
6513            $i--;            $i--;
6514            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6515            redo LI;            redo LI;
6516          } # LI          } # LI
6517                        
6518          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6519            !!!nack ('t359.1');
6520          !!!next-token;          !!!next-token;
6521          redo B;          next B;
6522        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6523          ## has a p element in scope          ## has a p element in scope
6524          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6525            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6526              !!!back-token;              !!!cp ('t367');
6527              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6528              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6529            } elsif ({                        line => $token->{line}, column => $token->{column}};
6530                      table => 1, caption => 1, td => 1, th => 1,              next B;
6531                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6532                     }->{$_->[1]}) {              !!!cp ('t368');
6533              last INSCOPE;              last INSCOPE;
6534            }            }
6535          } # INSCOPE          } # INSCOPE
6536                        
6537          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6538                        
6539          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6540                        
6541            !!!nack ('t368.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6544        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6545          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6546            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6547            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6548              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6549                !!!parse-error (type => 'in a:a', token => $token);
6550                            
6551              !!!back-token;              !!!back-token; # <a>
6552              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6553              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6554                $formatting_end_tag->($token);
6555                            
6556              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6557                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6558                    !!!cp ('t372');
6559                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6560                  last AFE2;                  last AFE2;
6561                }                }
6562              } # AFE2              } # AFE2
6563              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6564                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6565                    !!!cp ('t373');
6566                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6567                  last OE;                  last OE;
6568                }                }
6569              } # OE              } # OE
6570              last AFE;              last AFE;
6571            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6572                !!!cp ('t374');
6573              last AFE;              last AFE;
6574            }            }
6575          } # AFE          } # AFE
6576                        
6577          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6578    
6579          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6580          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6581    
6582            !!!nack ('t374.1');
6583          !!!next-token;          !!!next-token;
6584          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6585        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6586          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6587    
6588          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6589          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6590            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6591            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6592              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6593              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6594              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6595              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6596            } elsif ({                        line => $token->{line}, column => $token->{column}};
6597                      table => 1, caption => 1, td => 1, th => 1,              next B;
6598                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6599                     }->{$node->[1]}) {              !!!cp ('t377');
6600              last INSCOPE;              last INSCOPE;
6601            }            }
6602          } # INSCOPE          } # INSCOPE
6603                    
6604          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6605          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6606                    
6607            !!!nack ('t377.1');
6608          !!!next-token;          !!!next-token;
6609          redo B;          next B;
6610        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6611          ## has a button element in scope          ## has a button element in scope
6612          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6613            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6614            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6615              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6616              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6617              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6618              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6619            } elsif ({                        line => $token->{line}, column => $token->{column}};
6620                      table => 1, caption => 1, td => 1, th => 1,              next B;
6621                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6622                     }->{$node->[1]}) {              !!!cp ('t379');
6623              last INSCOPE;              last INSCOPE;
6624            }            }
6625          } # INSCOPE          } # INSCOPE
6626                        
6627          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6628                        
6629          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6630          push @$active_formatting_elements, ['#marker', ''];  
6631            ## TODO: associate with $self->{form_element} if defined
6632    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6633          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6634            
6635          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6636          !!!next-token;          !!!next-token;
6637          redo B;          next B;
6638        } elsif ({        } elsif ({
6639                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6640                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6641                  image => 1,                  noembed => 1,
6642                    noframes => 1,
6643                    noscript => 0, ## TODO: 1 if scripting is enabled
6644                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6645          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6646            !!!parse-error (type => 'image');            !!!cp ('t381');
6647            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6648            } else {
6649              !!!cp ('t399');
6650          }          }
6651            ## NOTE: There is an "as if in body" code clone.
6652          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6653          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6654        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6655          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6656                    
6657          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6658              !!!cp ('t389');
6659            ## Ignore the token            ## Ignore the token
6660              !!!nack ('t389'); ## NOTE: Not acknowledged.
6661            !!!next-token;            !!!next-token;
6662            redo B;            next B;
6663          } else {          } else {
6664            my $at = $token->{attributes};            my $at = $token->{attributes};
6665            my $form_attrs;            my $form_attrs;
# Line 5093  sub _tree_construction_main ($) { Line 6670  sub _tree_construction_main ($) {
6670            delete $at->{prompt};            delete $at->{prompt};
6671            my @tokens = (            my @tokens = (
6672                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6673                           attributes => $form_attrs},                           attributes => $form_attrs,
6674                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6675                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6676                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6677                            {type => START_TAG_TOKEN, tag_name => 'p',
6678                             line => $token->{line}, column => $token->{column}},
6679                            {type => START_TAG_TOKEN, tag_name => 'label',
6680                             line => $token->{line}, column => $token->{column}},
6681                         );                         );
6682            if ($prompt_attr) {            if ($prompt_attr) {
6683              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6684                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6685                               #line => $token->{line}, column => $token->{column},
6686                              };
6687            } else {            } else {
6688                !!!cp ('t391');
6689              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6690                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6691                               #line => $token->{line}, column => $token->{column},
6692                              }; # SHOULD
6693              ## TODO: make this configurable              ## TODO: make this configurable
6694            }            }
6695            push @tokens,            push @tokens,
6696                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6697                             line => $token->{line}, column => $token->{column}},
6698                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6699                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6700                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6701                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6702                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6703            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6704                             line => $token->{line}, column => $token->{column}},
6705                            {type => END_TAG_TOKEN, tag_name => 'form',
6706                             line => $token->{line}, column => $token->{column}};
6707              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6708            !!!back-token (@tokens);            !!!back-token (@tokens);
6709            redo B;            !!!next-token;
6710              next B;
6711          }          }
6712        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6713          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6714          my $el;          my $el;
6715          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6716                    
6717          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6718          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5128  sub _tree_construction_main ($) { Line 6721  sub _tree_construction_main ($) {
6721          $insert->($el);          $insert->($el);
6722                    
6723          my $text = '';          my $text = '';
6724            !!!nack ('t392.1');
6725          !!!next-token;          !!!next-token;
6726          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6727            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6728            unless (length $token->{data}) {            unless (length $token->{data}) {
6729                !!!cp ('t392');
6730              !!!next-token;              !!!next-token;
6731              } else {
6732                !!!cp ('t393');
6733            }            }
6734            } else {
6735              !!!cp ('t394');
6736          }          }
6737          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6738              !!!cp ('t395');
6739            $text .= $token->{data};            $text .= $token->{data};
6740            !!!next-token;            !!!next-token;
6741          }          }
6742          if (length $text) {          if (length $text) {
6743              !!!cp ('t396');
6744            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6745          }          }
6746                    
# Line 5147  sub _tree_construction_main ($) { Line 6748  sub _tree_construction_main ($) {
6748                    
6749          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6750              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6751              !!!cp ('t397');
6752            ## Ignore the token            ## Ignore the token
6753          } else {          } else {
6754            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6755              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6756          }          }
6757          !!!next-token;          !!!next-token;
6758          redo B;          next B;
6759        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6760                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6761          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6762    
6763            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6764    
6765            ## "adjust foreign attributes" - done in insert-element-f
6766                    
6767          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6768                    
6769          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6770              pop @{$self->{open_elements}};
6771              !!!ack ('t398.1');
6772            } else {
6773              !!!cp ('t398.2');
6774              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6775              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6776              ## mode, "in body" (not "in foreign content") secondary insertion
6777              ## mode, maybe.
6778            }
6779    
6780          !!!next-token;          !!!next-token;
6781          redo B;          next B;
6782        } elsif ({        } elsif ({
6783                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6784                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6785                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6786                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6787                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6788          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6789            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6790          ## Ignore the token          ## Ignore the token
6791            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6792          !!!next-token;          !!!next-token;
6793          redo B;          next B;
6794                    
6795          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6796        } else {        } else {
6797            if ($token->{tag_name} eq 'image') {
6798              !!!cp ('t384');
6799              !!!parse-error (type => 'image', token => $token);
6800              $token->{tag_name} = 'img';
6801            } else {
6802              !!!cp ('t385');
6803            }
6804    
6805            ## NOTE: There is an "as if <br>" code clone.
6806          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6807                    
6808          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6809    
6810            if ({
6811                 applet => 1, marquee => 1, object => 1,
6812                }->{$token->{tag_name}}) {
6813              !!!cp ('t380');
6814              push @$active_formatting_elements, ['#marker', ''];
6815              !!!nack ('t380.1');
6816            } elsif ({
6817                      b => 1, big => 1, em => 1, font => 1, i => 1,
6818                      s => 1, small => 1, strile => 1,
6819                      strong => 1, tt => 1, u => 1,
6820                     }->{$token->{tag_name}}) {
6821              !!!cp ('t375');
6822              push @$active_formatting_elements, $self->{open_elements}->[-1];
6823              !!!nack ('t375.1');
6824            } elsif ($token->{tag_name} eq 'input') {
6825              !!!cp ('t388');
6826              ## TODO: associate with $self->{form_element} if defined
6827              pop @{$self->{open_elements}};
6828              !!!ack ('t388.2');
6829            } elsif ({
6830                      area => 1, basefont => 1, bgsound => 1, br => 1,
6831                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6832                      #image => 1,
6833                     }->{$token->{tag_name}}) {
6834              !!!cp ('t388.1');
6835              pop @{$self->{open_elements}};
6836              !!!ack ('t388.3');
6837            } elsif ($token->{tag_name} eq 'select') {
6838              ## TODO: associate with $self->{form_element} if defined
6839            
6840              if ($self->{insertion_mode} & TABLE_IMS or
6841                  $self->{insertion_mode} & BODY_TABLE_IMS or
6842                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6843                !!!cp ('t400.1');
6844                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6845              } else {
6846                !!!cp ('t400.2');
6847                $self->{insertion_mode} = IN_SELECT_IM;
6848              }
6849              !!!nack ('t400.3');
6850            } else {
6851              !!!nack ('t402');
6852            }
6853                    
6854          !!!next-token;          !!!next-token;
6855          redo B;          next B;
6856        }        }
6857      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6858        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6859          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6860              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6861            for (@{$self->{open_elements}}) {          INSCOPE: {
6862              unless ({            for (reverse @{$self->{open_elements}}) {
6863                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6864                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6865                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6866                      }->{$_->[1]}) {                last INSCOPE;
6867                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6868                  !!!cp ('t405.1');
6869                  last;
6870              }              }
6871            }            }
6872    
6873            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6874                              value => $token->{tag_name}, token => $token);
6875              ## NOTE: Ignore the token.
6876            !!!next-token;            !!!next-token;
6877            redo B;            next B;
6878          } else {          } # INSCOPE
6879            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6880            ## Ignore the token          for (@{$self->{open_elements}}) {
6881            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6882            redo B;              !!!cp ('t403');
6883                !!!parse-error (type => 'not closed',
6884                                value => $_->[0]->manakai_local_name,
6885                                token => $token);
6886                last;
6887              } else {
6888                !!!cp ('t404');
6889              }
6890          }          }
6891    
6892            $self->{insertion_mode} = AFTER_BODY_IM;
6893            !!!next-token;
6894            next B;
6895        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6896          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
6897            ## up-to-date, though it has same effect as speced.
6898            if (@{$self->{open_elements}} > 1 and
6899                $self->{open_elements}->[1]->[1] & BODY_EL) {
6900            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6901            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6902              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6903                !!!parse-error (type => 'not closed',
6904                                value => $self->{open_elements}->[1]->[0]
6905                                    ->manakai_local_name,
6906                                token => $token);
6907              } else {
6908                !!!cp ('t407');
6909            }            }
6910            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6911            ## reprocess            ## reprocess
6912            redo B;            next B;
6913          } else {          } else {
6914            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6915              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6916            ## Ignore the token            ## Ignore the token
6917            !!!next-token;            !!!next-token;
6918            redo B;            next B;
6919          }          }
6920        } elsif ({        } elsif ({
6921                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6922                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6923                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6924                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6925                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6926                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6927          ## has an element in scope          ## has an element in scope
6928          my $i;          my $i;
6929          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6930            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6931            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6932              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6933              $i = $_;              $i = $_;
6934              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6935            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6936                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6937              last INSCOPE;              last INSCOPE;
6938            }            }
6939          } # INSCOPE          } # INSCOPE
6940            
6941          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6942            if (defined $i) {            !!!cp ('t413');
6943              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6944            } else {
6945              ## Step 1. generate implied end tags
6946              while ({
6947                      dd => ($token->{tag_name} ne 'dd'),
6948                      dt => ($token->{tag_name} ne 'dt'),
6949                      li => ($token->{tag_name} ne 'li'),
6950                      p => 1,
6951                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6952                !!!cp ('t409');
6953                pop @{$self->{open_elements}};
6954              }
6955    
6956              ## Step 2.
6957              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6958                      ne $token->{tag_name}) {
6959                !!!cp ('t412');
6960                !!!parse-error (type => 'not closed',
6961                                value => $self->{open_elements}->[-1]->[0]
6962                                    ->manakai_local_name,
6963                                token => $token);
6964            } else {            } else {
6965              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6966            }            }
6967          }  
6968                      ## Step 3.
         if (defined $i) {  
6969            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6970          } elsif ($token->{tag_name} eq 'p') {  
6971            ## As if <p>, then reprocess the current token            ## Step 4.
6972            my $el;            $clear_up_to_marker->()
6973            !!!create-element ($el, 'p');                if {
6974            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6975                  }->{$token->{tag_name}};
6976          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6977          !!!next-token;          !!!next-token;
6978          redo B;          next B;
6979        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6980            undef $self->{form_element};
6981    
6982          ## has an element in scope          ## has an element in scope
6983            my $i;
6984          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6985            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6986            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6987              ## generate implied end tags              !!!cp ('t418');
6988              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6989              last INSCOPE;              last INSCOPE;
6990            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6991                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6992              last INSCOPE;              last INSCOPE;
6993            }            }
6994          } # INSCOPE          } # INSCOPE
6995            
6996          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6997            pop @{$self->{open_elements}};            !!!cp ('t421');
6998          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6999            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
7000              ## Step 1. generate implied end tags
7001              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7002                !!!cp ('t417');
7003                pop @{$self->{open_elements}};
7004              }
7005              
7006              ## Step 2.
7007              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7008                      ne $token->{tag_name}) {
7009                !!!cp ('t417.1');
7010                !!!parse-error (type => 'not closed',
7011                                value => $self->{open_elements}->[-1]->[0]
7012                                    ->manakai_local_name,
7013                                token => $token);
7014              } else {
7015                !!!cp ('t420');
7016              }  
7017              
7018              ## Step 3.
7019              splice @{$self->{open_elements}}, $i;
7020          }          }
7021    
         undef $self->{form_element};  
7022          !!!next-token;          !!!next-token;
7023          redo B;          next B;
7024        } elsif ({        } elsif ({
7025                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7026                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5328  sub _tree_construction_main ($) { Line 7028  sub _tree_construction_main ($) {
7028          my $i;          my $i;
7029          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7030            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7031            if ({            if ($node->[1] & HEADING_EL) {
7032                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7033              $i = $_;              $i = $_;
7034              last INSCOPE;              last INSCOPE;
7035            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7036                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7037              last INSCOPE;              last INSCOPE;
7038            }            }
7039          } # INSCOPE          } # INSCOPE
7040            
7041          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7042            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7043              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7044            } else {
7045              ## Step 1. generate implied end tags
7046              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7047                !!!cp ('t422');
7048                pop @{$self->{open_elements}};
7049              }
7050              
7051              ## Step 2.
7052              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7053                      ne $token->{tag_name}) {
7054                !!!cp ('t425');
7055                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7056              } else {
7057                !!!cp ('t426');
7058              }
7059    
7060              ## Step 3.
7061              splice @{$self->{open_elements}}, $i;
7062          }          }
7063                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7064          !!!next-token;          !!!next-token;
7065          redo B;          next B;
7066          } elsif ($token->{tag_name} eq 'p') {
7067            ## has an element in scope
7068            my $i;
7069            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7070              my $node = $self->{open_elements}->[$_];
7071              if ($node->[1] & P_EL) {
7072                !!!cp ('t410.1');
7073                $i = $_;
7074                last INSCOPE;
7075              } elsif ($node->[1] & SCOPING_EL) {
7076                !!!cp ('t411.1');
7077                last INSCOPE;
7078              }
7079            } # INSCOPE
7080    
7081            if (defined $i) {
7082              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7083                      ne $token->{tag_name}) {
7084                !!!cp ('t412.1');
7085                !!!parse-error (type => 'not closed',
7086                                value => $self->{open_elements}->[-1]->[0]
7087                                    ->manakai_local_name,
7088                                token => $token);
7089              } else {
7090                !!!cp ('t414.1');
7091              }
7092    
7093              splice @{$self->{open_elements}}, $i;
7094            } else {
7095              !!!cp ('t413.1');
7096              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7097    
7098              !!!cp ('t415.1');
7099              ## As if <p>, then reprocess the current token
7100              my $el;
7101              !!!create-element ($el, $HTML_NS, 'p',, $token);
7102              $insert->($el);
7103              ## NOTE: Not inserted into |$self->{open_elements}|.
7104            }
7105    
7106            !!!next-token;
7107            next B;
7108        } elsif ({        } elsif ({
7109                  a => 1,                  a => 1,
7110                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7111                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7112                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7113                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7114          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7115          redo B;          $formatting_end_tag->($token);
7116            next B;
7117        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7118          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7119            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7120    
7121          ## As if <br>          ## As if <br>
7122          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7123                    
7124          my $el;          my $el;
7125          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7126          $insert->($el);          $insert->($el);
7127                    
7128          ## Ignore the token.          ## Ignore the token.
7129          !!!next-token;          !!!next-token;
7130          redo B;          next B;
7131        } elsif ({        } elsif ({
7132                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7133                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5392  sub _tree_construction_main ($) { Line 7140  sub _tree_construction_main ($) {
7140                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7141                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7142                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7143          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7144            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7145          ## Ignore the token          ## Ignore the token
7146          !!!next-token;          !!!next-token;
7147          redo B;          next B;
7148                    
7149          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7150                    
# Line 5406  sub _tree_construction_main ($) { Line 7155  sub _tree_construction_main ($) {
7155    
7156          ## Step 2          ## Step 2
7157          S2: {          S2: {
7158            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7159              ## Step 1              ## Step 1
7160              ## generate implied end tags              ## generate implied end tags
7161              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7162                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7163                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7164                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7165              }              }
7166                    
7167              ## Step 2              ## Step 2
7168              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7169                        ne $token->{tag_name}) {
7170                  !!!cp ('t431');
7171                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7172                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7173                                  value => $self->{open_elements}->[-1]->[0]
7174                                      ->manakai_local_name,
7175                                  token => $token);
7176                } else {
7177                  !!!cp ('t432');
7178              }              }
7179                            
7180              ## Step 3              ## Step 3
# Line 5433  sub _tree_construction_main ($) { Line 7184  sub _tree_construction_main ($) {
7184              last S2;              last S2;
7185            } else {            } else {
7186              ## Step 3              ## Step 3
7187              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7188                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7189                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7190                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7191                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7192                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7193                ## Ignore the token                ## Ignore the token
7194                !!!next-token;                !!!next-token;
7195                last S2;                last S2;
7196              }              }
7197    
7198                !!!cp ('t434');
7199            }            }
7200                        
7201            ## Step 4            ## Step 4
# Line 5451  sub _tree_construction_main ($) { Line 7205  sub _tree_construction_main ($) {
7205            ## Step 5;            ## Step 5;
7206            redo S2;            redo S2;
7207          } # S2          } # S2
7208          redo B;          next B;
7209        }        }
7210      }      }
7211      redo B;      next B;
7212      } continue { # B
7213        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7214          ## NOTE: The code below is executed in cases where it does not have
7215          ## to be, but it it is harmless even in those cases.
7216          ## has an element in scope
7217          INSCOPE: {
7218            for (reverse 0..$#{$self->{open_elements}}) {
7219              my $node = $self->{open_elements}->[$_];
7220              if ($node->[1] & FOREIGN_EL) {
7221                last INSCOPE;
7222              } elsif ($node->[1] & SCOPING_EL) {
7223                last;
7224              }
7225            }
7226            
7227            ## NOTE: No foreign element in scope.
7228            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7229          } # INSCOPE
7230        }
7231    } # B    } # B
7232    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7233    ## Stop parsing # MUST    ## Stop parsing # MUST
7234        
7235    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5503  sub set_inner_html ($$$) { Line 7271  sub set_inner_html ($$$) {
7271      my $p = $class->new;      my $p = $class->new;
7272      $p->{document} = $doc;      $p->{document} = $doc;
7273    
7274      ## Step 9 # MUST      ## Step 8 # MUST
7275      my $i = 0;      my $i = 0;
7276      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7277      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7278      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7279        my $self = shift;        my $self = shift;
7280    
7281        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7282        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7283    
7284          $self->{next_char} = -1 and return if $i >= length $$s;
7285          $self->{next_char} = ord substr $$s, $i++, 1;
7286    
7287        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7288        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7289        $column++;  
7290          if ($self->{next_char} == 0x000A) { # LF
7291        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7292          $line++;          $p->{column} = 0;
7293          $column = 0;          !!!cp ('i1');
7294        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7295          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7296          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7297          $line++;          $p->{line}++;
7298          $column = 0;          $p->{column} = 0;
7299        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7300          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7301        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7302            !!!cp ('i3');
7303          } elsif ($self->{next_char} == 0x0000) { # NULL
7304            !!!cp ('i4');
7305          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7306          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7307          } elsif ($self->{next_char} <= 0x0008 or
7308                   (0x000E <= $self->{next_char} and
7309                    $self->{next_char} <= 0x001F) or
7310                   (0x007F <= $self->{next_char} and
7311                    $self->{next_char} <= 0x009F) or
7312                   (0xD800 <= $self->{next_char} and
7313                    $self->{next_char} <= 0xDFFF) or
7314                   (0xFDD0 <= $self->{next_char} and
7315                    $self->{next_char} <= 0xFDDF) or
7316                   {
7317                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7318                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7319                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7320                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7321                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7322                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7323                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7324                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7325                    0x10FFFE => 1, 0x10FFFF => 1,
7326                   }->{$self->{next_char}}) {
7327            !!!cp ('i4.1');
7328            !!!parse-error (type => 'control char', level => $self->{must_level});
7329    ## TODO: error type documentation
7330        }        }
7331      };      };
7332      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7333      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7334            
7335      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7336        my (%opt) = @_;        my (%opt) = @_;
7337        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7338          my $column = $opt{column};
7339          if (defined $opt{token} and defined $opt{token}->{line}) {
7340            $line = $opt{token}->{line};
7341            $column = $opt{token}->{column};
7342          }
7343          warn "Parse error ($opt{type}) at line $line column $column\n";
7344      };      };
7345      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7346        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7347      };      };
7348            
7349      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 5564  sub set_inner_html ($$$) { Line 7367  sub set_inner_html ($$$) {
7367          unless defined $p->{content_model};          unless defined $p->{content_model};
7368          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7369    
7370      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7371          ## TODO: Foreign element OK?
7372    
7373      ## Step 4      ## Step 3
7374      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7375        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7376    
7377      ## Step 5 # MUST      ## Step 4 # MUST
7378      $doc->append_child ($root);      $doc->append_child ($root);
7379    
7380      ## Step 6 # MUST      ## Step 5 # MUST
7381      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7382    
7383      undef $p->{head_element};      undef $p->{head_element};
7384    
7385      ## Step 7 # MUST      ## Step 6 # MUST
7386      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7387    
7388      ## Step 8 # MUST      ## Step 7 # MUST
7389      my $anode = $node;      my $anode = $node;
7390      AN: while (defined $anode) {      AN: while (defined $anode) {
7391        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7392          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7393          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7394            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
7395                !!!cp ('i5');
7396              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7397              last AN;              last AN;
7398            }            }
# Line 5596  sub set_inner_html ($$$) { Line 7401  sub set_inner_html ($$$) {
7401        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7402      } # AN      } # AN
7403            
7404      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7405      {      {
7406        my $self = $p;        my $self = $p;
7407        !!!next-token;        !!!next-token;
7408      }      }
7409      $p->_tree_construction_main;      $p->_tree_construction_main;
7410    
7411      ## Step 11 # MUST      ## Step 10 # MUST
7412      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7413      for (@cn) {      for (@cn) {
7414        $node->remove_child ($_);        $node->remove_child ($_);
7415      }      }
7416      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7417    
7418      ## Step 12 # MUST      ## Step 11 # MUST
7419      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7420      for (@cn) {      for (@cn) {
7421        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5620  sub set_inner_html ($$$) { Line 7424  sub set_inner_html ($$$) {
7424      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7425    
7426      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7427    
7428        delete $p->{parse_error}; # delete loop
7429    } else {    } else {
7430      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";
7431    }    }

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.140

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24