/[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.65 by wakaba, Mon Nov 19 12:18:26 2007 UTC revision 1.145 by wakaba, Sat May 24 11:57:47 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributetype => 'attributeType',
215      basefrequency => 'baseFrequency',
216      baseprofile => 'baseProfile',
217      calcmode => 'calcMode',
218      clippathunits => 'clipPathUnits',
219      contentscripttype => 'contentScriptType',
220      contentstyletype => 'contentStyleType',
221      diffuseconstant => 'diffuseConstant',
222      edgemode => 'edgeMode',
223      externalresourcesrequired => 'externalResourcesRequired',
224      fecolormatrix => 'feColorMatrix',
225      fecomposite => 'feComposite',
226      fegaussianblur => 'feGaussianBlur',
227      femorphology => 'feMorphology',
228      fetile => 'feTile',
229      filterres => 'filterRes',
230      filterunits => 'filterUnits',
231      glyphref => 'glyphRef',
232      gradienttransform => 'gradientTransform',
233      gradientunits => 'gradientUnits',
234      kernelmatrix => 'kernelMatrix',
235      kernelunitlength => 'kernelUnitLength',
236      keypoints => 'keyPoints',
237      keysplines => 'keySplines',
238      keytimes => 'keyTimes',
239      lengthadjust => 'lengthAdjust',
240      limitingconeangle => 'limitingConeAngle',
241      markerheight => 'markerHeight',
242      markerunits => 'markerUnits',
243      markerwidth => 'markerWidth',
244      maskcontentunits => 'maskContentUnits',
245      maskunits => 'maskUnits',
246      numoctaves => 'numOctaves',
247      pathlength => 'pathLength',
248      patterncontentunits => 'patternContentUnits',
249      patterntransform => 'patternTransform',
250      patternunits => 'patternUnits',
251      pointsatx => 'pointsAtX',
252      pointsaty => 'pointsAtY',
253      pointsatz => 'pointsAtZ',
254      preservealpha => 'preserveAlpha',
255      preserveaspectratio => 'preserveAspectRatio',
256      primitiveunits => 'primitiveUnits',
257      refx => 'refX',
258      refy => 'refY',
259      repeatcount => 'repeatCount',
260      repeatdur => 'repeatDur',
261      requiredextensions => 'requiredExtensions',
262      specularconstant => 'specularConstant',
263      specularexponent => 'specularExponent',
264      spreadmethod => 'spreadMethod',
265      startoffset => 'startOffset',
266      stddeviation => 'stdDeviation',
267      stitchtiles => 'stitchTiles',
268      surfacescale => 'surfaceScale',
269      systemlanguage => 'systemLanguage',
270      tablevalues => 'tableValues',
271      targetx => 'targetX',
272      targety => 'targetY',
273      textlength => 'textLength',
274      viewbox => 'viewBox',
275      viewtarget => 'viewTarget',
276      xchannelselector => 'xChannelSelector',
277      ychannelselector => 'yChannelSelector',
278      zoomandpan => 'zoomAndPan',
279  };  };
280    
281    my $foreign_attr_xname = {
282      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
283      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
284      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
285      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
286      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
287      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
288      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
289      'xml:base' => [$XML_NS, ['xml', 'base']],
290      'xml:lang' => [$XML_NS, ['xml', 'lang']],
291      'xml:space' => [$XML_NS, ['xml', 'space']],
292      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
293      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
294    };
295    
296    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
297    
298  my $c1_entity_char = {  my $c1_entity_char = {
299    0x80 => 0x20AC,    0x80 => 0x20AC,
300    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 63  my $c1_entity_char = { Line 330  my $c1_entity_char = {
330    0x9F => 0x0178,    0x9F => 0x0178,
331  }; # $c1_entity_char  }; # $c1_entity_char
332    
 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  
   
333  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
334      my $self = shift;
335      my $charset_name = shift;
336      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
337      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
338    } # parse_byte_string
339    
340    sub parse_byte_stream ($$$$;$) {
341    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
342    my $charset = shift;    my $charset_name = shift;
343    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;  
   }  
344    
345    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
346      my $self = shift;      my (%opt) = @_;
347      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
348      ## TODO: if $charset is supported    };
349      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
350    
351      ## "Change the encoding" algorithm:    ## HTML5 encoding sniffing algorithm
352      require Message::Charset::Info;
353      ## Step 1        my $charset;
354      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?    my $buffer;
355        $charset = 'utf-8';    my ($char_stream, $e_status);
356    
357      SNIFFING: {
358    
359        ## Step 1
360        if (defined $charset_name) {
361          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
362    
363          ## ISSUE: Unsupported encoding is not ignored according to the spec.
364          ($char_stream, $e_status) = $charset->get_decode_handle
365              ($byte_stream, allow_error_reporting => 1,
366               allow_fallback => 1);
367          if ($char_stream) {
368            $self->{confident} = 1;
369            last SNIFFING;
370          } else {
371            ## TODO: unsupported error
372          }
373      }      }
374    
375      ## Step 2      ## Step 2
376      if (defined $self->{input_encoding} and      my $byte_buffer = '';
377          $self->{input_encoding} eq $charset) {      for (1..1024) {
378          my $char = $byte_stream->getc;
379          last unless defined $char;
380          $byte_buffer .= $char;
381        } ## TODO: timeout
382    
383        ## Step 3
384        if ($byte_buffer =~ /^\xFE\xFF/) {
385          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
386          ($char_stream, $e_status) = $charset->get_decode_handle
387              ($byte_stream, allow_error_reporting => 1,
388               allow_fallback => 1, byte_buffer => \$byte_buffer);
389        $self->{confident} = 1;        $self->{confident} = 1;
390        return;        last SNIFFING;
391        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
392          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
393          ($char_stream, $e_status) = $charset->get_decode_handle
394              ($byte_stream, allow_error_reporting => 1,
395               allow_fallback => 1, byte_buffer => \$byte_buffer);
396          $self->{confident} = 1;
397          last SNIFFING;
398        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
399          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
400          ($char_stream, $e_status) = $charset->get_decode_handle
401              ($byte_stream, allow_error_reporting => 1,
402               allow_fallback => 1, byte_buffer => \$byte_buffer);
403          $self->{confident} = 1;
404          last SNIFFING;
405      }      }
406    
407      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
408          ':'.$charset, level => 'w');      ## TODO: <meta charset>
409    
410      ## Step 3      ## Step 5
411      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
412    
413      ## Step 4      ## Step 6
414      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
415        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
416            ($byte_buffer);
417        if (defined $charset_name) {
418          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
419    
420          ## ISSUE: Unsupported encoding is not ignored according to the spec.
421          require Whatpm::Charset::DecodeHandle;
422          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
423              ($byte_stream);
424          ($char_stream, $e_status) = $charset->get_decode_handle
425              ($buffer, allow_error_reporting => 1,
426               allow_fallback => 1, byte_buffer => \$byte_buffer);
427          if ($char_stream) {
428            $buffer->{buffer} = $byte_buffer;
429            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
430                            value => $charset_name,
431                            level => $self->{info_level},
432                            line => 1, column => 1);
433            $self->{confident} = 0;
434            last SNIFFING;
435          }
436        }
437    
438        ## Step 7: default
439        ## TODO: Make this configurable.
440        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
441            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
442            ## detectable in the step 6.
443        require Whatpm::Charset::DecodeHandle;
444        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
445            ($byte_stream);
446        ($char_stream, $e_status)
447            = $charset->get_decode_handle ($buffer,
448                                           allow_error_reporting => 1,
449                                           allow_fallback => 1,
450                                           byte_buffer => \$byte_buffer);
451        $buffer->{buffer} = $byte_buffer;
452        !!!parse-error (type => 'sniffing:default', ## TODO: type name
453                        value => 'windows-1252',
454                        level => $self->{info_level},
455                        line => 1, column => 1);
456        $self->{confident} = 0;
457      } # SNIFFING
458    
459      $self->{input_encoding} = $charset->get_iana_name;
460      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
461        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      } elsif (not ($e_status &
466                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
467        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
468                        value => $self->{input_encoding},
469                        level => $self->{unsupported_level},
470                        line => 1, column => 1);
471      }
472    
473      $self->{change_encoding} = sub {
474        my $self = shift;
475        $charset_name = shift;
476        my $token = shift;
477    
478        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
479        ($char_stream, $e_status) = $charset->get_decode_handle
480            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
481             byte_buffer => \ $buffer->{buffer});
482        
483        if ($char_stream) { # if supported
484          ## "Change the encoding" algorithm:
485    
486          ## Step 1    
487          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
488            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
489            ($char_stream, $e_status) = $charset->get_decode_handle
490                ($byte_stream,
491                 byte_buffer => \ $buffer->{buffer});
492          }
493          $charset_name = $charset->get_iana_name;
494          
495          ## Step 2
496          if (defined $self->{input_encoding} and
497              $self->{input_encoding} eq $charset_name) {
498            !!!parse-error (type => 'charset label:matching', ## TODO: type
499                            value => $charset_name,
500                            level => $self->{info_level});
501            $self->{confident} = 1;
502            return;
503          }
504    
505          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
506              ':'.$charset_name, level => 'w', token => $token);
507          
508          ## Step 3
509          # if (can) {
510            ## change the encoding on the fly.
511            #$self->{confident} = 1;
512            #return;
513          # }
514          
515          ## Step 4
516          throw Whatpm::HTML::RestartParser ();
517        }
518    }; # $self->{change_encoding}    }; # $self->{change_encoding}
519    
520      my $char_onerror = sub {
521        my (undef, $type, %opt) = @_;
522        !!!parse-error (%opt, type => $type,
523                        line => $self->{line}, column => $self->{column} + 1);
524        if ($opt{octets}) {
525          ${$opt{octets}} = "\x{FFFD}"; # relacement character
526        }
527      };
528      $char_stream->onerror ($char_onerror);
529    
530    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
531    my $return;    my $return;
532    try {    try {
533      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($char_stream, @args);  
534    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
535      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
536      $s = \ (Encode::decode ($charset, $$bytes_s));      
537      $self->{input_encoding} = $charset; ## TODO: normalize      $self->{input_encoding} = $charset->get_iana_name;
538        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
539          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        } elsif (not ($e_status &
544                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
545          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
546                          value => $self->{input_encoding},
547                          level => $self->{unsupported_level},
548                          line => 1, column => 1);
549        }
550      $self->{confident} = 1;      $self->{confident} = 1;
551      $return = $self->parse_char_string ($s, @args);      $char_stream->onerror ($char_onerror);
552        $return = $self->parse_char_stream ($char_stream, @args);
553    };    };
554    return $return;    return $return;
555  } # parse_byte_string  } # parse_byte_stream
556    
557  *parse_char_string = \&parse_string;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
558    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
559    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
560    ## because the core part of our HTML parser expects a string of character,
561    ## not a string of bytes or code units or anything which might contain a BOM.
562    ## Therefore, any parser interface that accepts a string of bytes,
563    ## such as |parse_byte_string| in this module, must ensure that it does
564    ## strip the BOM and never strip any ZWNBSP.
565    
566  sub parse_string ($$$;$) {  sub parse_char_string ($$$;$) {
567    my $self = ref $_[0] ? shift : shift->new;    my $self = shift;
568      require utf8;
569    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
570      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
571      return $self->parse_char_stream ($input, @_[1..$#_]);
572    } # parse_char_string
573    *parse_string = \&parse_char_string;
574    
575    sub parse_char_stream ($$$;$) {
576      my $self = ref $_[0] ? shift : shift->new;
577      my $input = $_[0];
578    $self->{document} = $_[1];    $self->{document} = $_[1];
579    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
580    
# Line 170  sub parse_string ($$$;$) { Line 585  sub parse_string ($$$;$) {
585        if defined $self->{input_encoding};        if defined $self->{input_encoding};
586    
587    my $i = 0;    my $i = 0;
588    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
589    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
590    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
591      my $self = shift;      my $self = shift;
592    
593      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
594      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
595    
596        my $char;
597        if (defined $self->{next_next_char}) {
598          $char = $self->{next_next_char};
599          delete $self->{next_next_char};
600        } else {
601          $char = $input->getc;
602        }
603        $self->{next_char} = -1 and return unless defined $char;
604        $self->{next_char} = ord $char;
605    
606      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
607      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
608      $column++;      $self->{column}++;
609            
610      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
611        $line++;        !!!cp ('j1');
612        $column = 0;        $self->{line}++;
613      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
614        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
615        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
616        $line++;        my $next = $input->getc;
617        $column = 0;        if (defined $next and $next ne "\x0A") {
618      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
619        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
620      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
621          $self->{line}++;
622          $self->{column} = 0;
623        } elsif ($self->{next_char} > 0x10FFFF) {
624          !!!cp ('j3');
625          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
626        } elsif ($self->{next_char} == 0x0000) { # NULL
627          !!!cp ('j4');
628        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
629        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
630        } elsif ($self->{next_char} <= 0x0008 or
631                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
632                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
633                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
634                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
635                 {
636                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
637                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
638                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
639                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
640                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
641                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
642                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
643                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
644                  0x10FFFE => 1, 0x10FFFF => 1,
645                 }->{$self->{next_char}}) {
646          !!!cp ('j5');
647          !!!parse-error (type => 'control char', level => $self->{must_level});
648    ## TODO: error type documentation
649      }      }
650    };    };
651    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
652    $self->{next_input_character} = -1;    $self->{next_char} = -1;
653    
654    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
655      my (%opt) = @_;      my (%opt) = @_;
656      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
657        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
658        warn "Parse error ($opt{type}) at line $line column $column\n";
659    };    };
660    $self->{parse_error} = sub {    $self->{parse_error} = sub {
661      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
662    };    };
663    
664    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 213  sub parse_string ($$$;$) { Line 666  sub parse_string ($$$;$) {
666    $self->_construct_tree;    $self->_construct_tree;
667    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
668    
669      delete $self->{parse_error}; # remove loop
670    
671    return $self->{document};    return $self->{document};
672  } # parse_string  } # parse_char_stream
673    
674  sub new ($) {  sub new ($) {
675    my $class = shift;    my $class = shift;
676    my $self = bless {}, $class;    my $self = bless {
677    $self->{set_next_input_character} = sub {      must_level => 'm',
678      $self->{next_input_character} = -1;      should_level => 's',
679        good_level => 'w',
680        warn_level => 'w',
681        info_level => 'i',
682        unsupported_level => 'u',
683      }, $class;
684      $self->{set_next_char} = sub {
685        $self->{next_char} = -1;
686    };    };
687    $self->{parse_error} = sub {    $self->{parse_error} = sub {
688      #      #
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 741  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
741  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
742  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
743  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
744    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
745    sub SELF_CLOSING_START_TAG_STATE () { 34 }
746    sub CDATA_BLOCK_STATE () { 35 }
747    
748  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
749  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 760  sub TABLE_IMS ()      { 0b1000000 }
760  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
761  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
762  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
763    sub SELECT_IMS ()     { 0b10000000000 }
764    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
765        ## NOTE: "in foreign content" insertion mode is special; it is combined
766        ## with the secondary insertion mode.  In this parser, they are stored
767        ## together in the bit-or'ed form.
768    
769    ## NOTE: "initial" and "before html" insertion modes have no constants.
770    
771    ## NOTE: "after after body" insertion mode.
772  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
773    
774    ## NOTE: "after after frameset" insertion mode.
775  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
776    
777  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
778  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
779  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 787  sub IN_TABLE_IM () { TABLE_IMS }
787  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
788  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
789  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
790  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
791    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
792  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
793    
794  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 324  sub _initialize_tokenizer ($) { Line 801  sub _initialize_tokenizer ($) {
801    undef $self->{current_attribute};    undef $self->{current_attribute};
802    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
803    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
804      delete $self->{self_closing};
805    $self->{char} = [];    $self->{char} = [];
806    # $self->{next_input_character}    # $self->{next_char}
807    !!!next-input-character;    !!!next-input-character;
808    $self->{token} = [];    $self->{token} = [];
809    # $self->{escape}    # $self->{escape}
# Line 338  sub _initialize_tokenizer ($) { Line 816  sub _initialize_tokenizer ($) {
816  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
817  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
818  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
819  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
820  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
821    ##        ->{name}
822    ##        ->{value}
823    ##        ->{has_reference} == 1 or 0
824  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
825    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
826    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
827    ##     while the token is pushed back to the stack.
828    
829    ## ISSUE: "When a DOCTYPE token is created, its
830    ## <i>self-closing flag</i> must be unset (its other state is that it
831    ## be set), and its attributes list must be empty.": Wrong subject?
832    
833  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
834    
# Line 367  sub _initialize_tokenizer ($) { Line 855  sub _initialize_tokenizer ($) {
855    
856  sub _get_next_token ($) {  sub _get_next_token ($) {
857    my $self = shift;    my $self = shift;
858    
859      if ($self->{self_closing}) {
860        !!!parse-error (type => 'nestc', token => $self->{current_token});
861        ## NOTE: The |self_closing| flag is only set by start tag token.
862        ## In addition, when a start tag token is emitted, it is always set to
863        ## |current_token|.
864        delete $self->{self_closing};
865      }
866    
867    if (@{$self->{token}}) {    if (@{$self->{token}}) {
868        $self->{self_closing} = $self->{token}->[0]->{self_closing};
869      return shift @{$self->{token}};      return shift @{$self->{token}};
870    }    }
871    
872    A: {    A: {
873      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
874        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
875          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
876                not $self->{escape}) {
877              !!!cp (1);
878            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
879            !!!next-input-character;            !!!next-input-character;
880            redo A;            redo A;
881          } else {          } else {
882              !!!cp (2);
883            #            #
884          }          }
885        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
886          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
887            unless ($self->{escape}) {            unless ($self->{escape}) {
888              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
889                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
890                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
891                  !!!cp (3);
892                $self->{escape} = 1;                $self->{escape} = 1;
893                } else {
894                  !!!cp (4);
895              }              }
896              } else {
897                !!!cp (5);
898            }            }
899          }          }
900                    
901          #          #
902        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
903          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
904              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
905               not $self->{escape})) {               not $self->{escape})) {
906              !!!cp (6);
907            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
908            !!!next-input-character;            !!!next-input-character;
909            redo A;            redo A;
910          } else {          } else {
911              !!!cp (7);
912            #            #
913          }          }
914        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
915          if ($self->{escape} and          if ($self->{escape} and
916              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
917            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
918                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
919                !!!cp (8);
920              delete $self->{escape};              delete $self->{escape};
921              } else {
922                !!!cp (9);
923            }            }
924            } else {
925              !!!cp (10);
926          }          }
927                    
928          #          #
929        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
930          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
931            !!!emit ({type => END_OF_FILE_TOKEN,
932                      line => $self->{line}, column => $self->{column}});
933          last A; ## TODO: ok?          last A; ## TODO: ok?
934          } else {
935            !!!cp (12);
936        }        }
937        # Anything else        # Anything else
938        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
939                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
940                       line => $self->{line}, column => $self->{column},
941                      };
942        ## Stay in the data state        ## Stay in the data state
943        !!!next-input-character;        !!!next-input-character;
944    
# Line 428  sub _get_next_token ($) { Line 947  sub _get_next_token ($) {
947        redo A;        redo A;
948      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
949        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
950    
951          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
952                
953        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
954    
955        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
956        # next-input-character is already done        # next-input-character is already done
957    
958        unless (defined $token) {        unless (defined $token) {
959          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
960            !!!emit ({type => CHARACTER_TOKEN, data => '&',
961                      line => $l, column => $c,
962                     });
963        } else {        } else {
964            !!!cp (14);
965          !!!emit ($token);          !!!emit ($token);
966        }        }
967    
968        redo A;        redo A;
969      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
970        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
971          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
972              !!!cp (15);
973            !!!next-input-character;            !!!next-input-character;
974            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
975            redo A;            redo A;
976          } else {          } else {
977              !!!cp (16);
978            ## reconsume            ## reconsume
979            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
980    
981            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
982                        line => $self->{line_prev},
983                        column => $self->{column_prev},
984                       });
985    
986            redo A;            redo A;
987          }          }
988        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
989          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
990              !!!cp (17);
991            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
992            !!!next-input-character;            !!!next-input-character;
993            redo A;            redo A;
994          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
995              !!!cp (18);
996            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
997            !!!next-input-character;            !!!next-input-character;
998            redo A;            redo A;
999          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1000                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1001              !!!cp (19);
1002            $self->{current_token}            $self->{current_token}
1003              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1004                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1005                   line => $self->{line_prev},
1006                   column => $self->{column_prev}};
1007            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1008            !!!next-input-character;            !!!next-input-character;
1009            redo A;            redo A;
1010          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1011                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1012              !!!cp (20);
1013            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1014                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1015                                        line => $self->{line_prev},
1016                                        column => $self->{column_prev}};
1017            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1018            !!!next-input-character;            !!!next-input-character;
1019            redo A;            redo A;
1020          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1021            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1022              !!!parse-error (type => 'empty start tag',
1023                              line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1026            !!!next-input-character;            !!!next-input-character;
1027    
1028            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1029                        line => $self->{line_prev},
1030                        column => $self->{column_prev},
1031                       });
1032    
1033            redo A;            redo A;
1034          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1035            !!!parse-error (type => 'pio');            !!!cp (22);
1036              !!!parse-error (type => 'pio',
1037                              line => $self->{line_prev},
1038                              column => $self->{column_prev});
1039            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1040            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1041                                        line => $self->{line_prev},
1042                                        column => $self->{column_prev},
1043                                       };
1044              ## $self->{next_char} is intentionally left as is
1045            redo A;            redo A;
1046          } else {          } else {
1047            !!!parse-error (type => 'bare stago');            !!!cp (23);
1048              !!!parse-error (type => 'bare stago',
1049                              line => $self->{line_prev},
1050                              column => $self->{column_prev});
1051            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1055                        line => $self->{line_prev},
1056                        column => $self->{column_prev},
1057                       });
1058    
1059            redo A;            redo A;
1060          }          }
# Line 505  sub _get_next_token ($) { Line 1062  sub _get_next_token ($) {
1062          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1063        }        }
1064      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1065          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1066        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1067          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1068    
1069            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1070            my @next_char;            my @next_char;
1071            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++) {
1072              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1073              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1074              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1075              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1076                  !!!cp (24);
1077                !!!next-input-character;                !!!next-input-character;
1078                next TAGNAME;                next TAGNAME;
1079              } else {              } else {
1080                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1081                  $self->{next_char} = shift @next_char; # reconsume
1082                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1083                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1084    
1085                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1086                            line => $l, column => $c,
1087                           });
1088        
1089                redo A;                redo A;
1090              }              }
1091            }            }
1092            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1093                
1094            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1095                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1096                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1097                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1098                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1099                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1100                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1101                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1102              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1103                $self->{next_char} = shift @next_char; # reconsume
1104              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1105              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1106              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                          line => $l, column => $c,
1108                         });
1109              redo A;              redo A;
1110            } else {            } else {
1111              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1112                $self->{next_char} = shift @next_char;
1113              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1114              # and consume...              # and consume...
1115            }            }
1116          } else {          } else {
1117            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1118              !!!cp (28);
1119            # next-input-character is already done            # next-input-character is already done
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1122                        line => $l, column => $c,
1123                       });
1124            redo A;            redo A;
1125          }          }
1126        }        }
1127                
1128        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1129            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1130          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1131                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1132                = {type => END_TAG_TOKEN,
1133                   tag_name => chr ($self->{next_char} + 0x0020),
1134                   line => $l, column => $c};
1135          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1136          !!!next-input-character;          !!!next-input-character;
1137          redo A;          redo A;
1138        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1139                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1140            !!!cp (30);
1141          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1142                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1143                                      line => $l, column => $c};
1144          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1145          !!!next-input-character;          !!!next-input-character;
1146          redo A;          redo A;
1147        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1148          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1149            !!!parse-error (type => 'empty end tag',
1150                            line => $self->{line_prev}, ## "<" in "</>"
1151                            column => $self->{column_prev} - 1);
1152          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1153          !!!next-input-character;          !!!next-input-character;
1154          redo A;          redo A;
1155        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1156            !!!cp (32);
1157          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1158          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1159          # reconsume          # reconsume
1160    
1161          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1162                      line => $l, column => $c,
1163                     });
1164    
1165          redo A;          redo A;
1166        } else {        } else {
1167            !!!cp (33);
1168          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1169          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1170          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1171                                      line => $self->{line_prev}, # "<" of "</"
1172                                      column => $self->{column_prev} - 1,
1173                                     };
1174            ## $self->{next_char} is intentionally left as is
1175          redo A;          redo A;
1176        }        }
1177      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1178        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1179            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1180            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1181            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1182            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1183            !!!cp (34);
1184          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1185          !!!next-input-character;          !!!next-input-character;
1186          redo A;          redo A;
1187        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1188          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1190            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1191          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1192            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1193            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1194              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1195            }            #  !!! cp (36);
1196              #  !!! parse-error (type => 'end tag attribute');
1197              #} else {
1198                !!!cp (37);
1199              #}
1200          } else {          } else {
1201            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1202          }          }
# Line 616  sub _get_next_token ($) { Line 1206  sub _get_next_token ($) {
1206          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1207    
1208          redo A;          redo A;
1209        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1210                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1211          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1212            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1213            # start tag or end tag            # start tag or end tag
1214          ## Stay in this state          ## Stay in this state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1218          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1219          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1220            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1221            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1222          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1223            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1224            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1225              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1226            }            #  !!! cp (40);
1227              #  !!! parse-error (type => 'end tag attribute');
1228              #} else {
1229                !!!cp (41);
1230              #}
1231          } else {          } else {
1232            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1233          }          }
# Line 643  sub _get_next_token ($) { Line 1237  sub _get_next_token ($) {
1237          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1238    
1239          redo A;          redo A;
1240        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (42);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!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  
1244          redo A;          redo A;
1245        } else {        } else {
1246          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1247            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1248            # start tag or end tag            # start tag or end tag
1249          ## Stay in the state          ## Stay in the state
1250          !!!next-input-character;          !!!next-input-character;
1251          redo A;          redo A;
1252        }        }
1253      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1254        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1255            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1256            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1257            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1258            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1259            !!!cp (45);
1260          ## Stay in the state          ## Stay in the state
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1264          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1265            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1266            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1267          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1268            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1269            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1270                !!!cp (47);
1271              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1272              } else {
1273                !!!cp (48);
1274            }            }
1275          } else {          } else {
1276            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 1281  sub _get_next_token ($) {
1281          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1282    
1283          redo A;          redo A;
1284        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1285                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1286          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1287                                value => ''};          $self->{current_attribute}
1288                = {name => chr ($self->{next_char} + 0x0020),
1289                   value => '',
1290                   line => $self->{line}, column => $self->{column}};
1291          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1292          !!!next-input-character;          !!!next-input-character;
1293          redo A;          redo A;
1294        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (50);
1296            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1297          !!!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  
1298          redo A;          redo A;
1299        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1300          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1301          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1302            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1303            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1304          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1305            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1306            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1307                !!!cp (53);
1308              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1309              } else {
1310                !!!cp (54);
1311            }            }
1312          } else {          } else {
1313            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 732  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
1319    
1320          redo A;          redo A;
1321        } else {        } else {
1322          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1323                                value => ''};               0x0022 => 1, # "
1324                 0x0027 => 1, # '
1325                 0x003D => 1, # =
1326                }->{$self->{next_char}}) {
1327              !!!cp (55);
1328              !!!parse-error (type => 'bad attribute name');
1329            } else {
1330              !!!cp (56);
1331            }
1332            $self->{current_attribute}
1333                = {name => chr ($self->{next_char}),
1334                   value => '',
1335                   line => $self->{line}, column => $self->{column}};
1336          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
# Line 742  sub _get_next_token ($) { Line 1341  sub _get_next_token ($) {
1341        my $before_leave = sub {        my $before_leave = sub {
1342          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1343              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1344            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1345              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1346            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1347          } else {          } else {
1348              !!!cp (58);
1349            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1350              = $self->{current_attribute};              = $self->{current_attribute};
1351          }          }
1352        }; # $before_leave        }; # $before_leave
1353    
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1357            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1358            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1359            !!!cp (59);
1360          $before_leave->();          $before_leave->();
1361          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1362          !!!next-input-character;          !!!next-input-character;
1363          redo A;          redo A;
1364        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1365            !!!cp (60);
1366          $before_leave->();          $before_leave->();
1367          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1368          !!!next-input-character;          !!!next-input-character;
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1371          $before_leave->();          $before_leave->();
1372          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1373            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1374            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1375          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1376              !!!cp (62);
1377            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1378            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1379              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 784  sub _get_next_token ($) { Line 1387  sub _get_next_token ($) {
1387          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1388    
1389          redo A;          redo A;
1390        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1391                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1392          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1393            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1394          ## Stay in the state          ## Stay in the state
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1398            !!!cp (64);
1399          $before_leave->();          $before_leave->();
1400            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1401          !!!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  
1402          redo A;          redo A;
1403        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1404          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1405          $before_leave->();          $before_leave->();
1406          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1407            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1408            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1409          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1410            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1411            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1412                !!!cp (67);
1413              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1414              } else {
1415                ## NOTE: This state should never be reached.
1416                !!!cp (68);
1417            }            }
1418          } else {          } else {
1419            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 826  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425    
1426          redo A;          redo A;
1427        } else {        } else {
1428          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1429                $self->{next_char} == 0x0027) { # '
1430              !!!cp (69);
1431              !!!parse-error (type => 'bad attribute name');
1432            } else {
1433              !!!cp (70);
1434            }
1435            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1436          ## Stay in the state          ## Stay in the state
1437          !!!next-input-character;          !!!next-input-character;
1438          redo A;          redo A;
1439        }        }
1440      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1441        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1442            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1443            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1444            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1445            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1446            !!!cp (71);
1447          ## Stay in the state          ## Stay in the state
1448          !!!next-input-character;          !!!next-input-character;
1449          redo A;          redo A;
1450        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1451            !!!cp (72);
1452          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1453          !!!next-input-character;          !!!next-input-character;
1454          redo A;          redo A;
1455        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1456          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1457            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1458            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1459          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1460            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1461            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1462                !!!cp (74);
1463              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1464              } else {
1465                ## NOTE: This state should never be reached.
1466                !!!cp (75);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 863  sub _get_next_token ($) { Line 1474  sub _get_next_token ($) {
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1478                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1479          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1480                                value => ''};          $self->{current_attribute}
1481                = {name => chr ($self->{next_char} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1488            !!!cp (77);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1495            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1496            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1497          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1500                !!!cp (80);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502              } else {
1503                ## NOTE: This state should never be reached.
1504                !!!cp (81);
1505            }            }
1506          } else {          } else {
1507            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 905  sub _get_next_token ($) { Line 1513  sub _get_next_token ($) {
1513    
1514          redo A;          redo A;
1515        } else {        } else {
1516          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1517                                value => ''};          $self->{current_attribute}
1518                = {name => chr ($self->{next_char}),
1519                   value => '',
1520                   line => $self->{line}, column => $self->{column}};
1521          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;                  redo A;        
1524        }        }
1525      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1526        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1527            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1528            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1529            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1530            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1531            !!!cp (83);
1532          ## Stay in the state          ## Stay in the state
1533          !!!next-input-character;          !!!next-input-character;
1534          redo A;          redo A;
1535        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1536            !!!cp (84);
1537          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539          redo A;          redo A;
1540        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1541            !!!cp (85);
1542          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1543          ## reconsume          ## reconsume
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1546            !!!cp (86);
1547          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1551          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1552            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1553            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1554          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1555            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1556            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1557                !!!cp (88);
1558              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1559              } else {
1560                ## NOTE: This state should never be reached.
1561                !!!cp (89);
1562            }            }
1563          } else {          } else {
1564            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 951  sub _get_next_token ($) { Line 1569  sub _get_next_token ($) {
1569          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1570    
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1573          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1574          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1575            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1576            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1577          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1578            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1579            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1580                !!!cp (91);
1581              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1582              } else {
1583                ## NOTE: This state should never be reached.
1584                !!!cp (92);
1585            }            }
1586          } else {          } else {
1587            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 972  sub _get_next_token ($) { Line 1593  sub _get_next_token ($) {
1593    
1594          redo A;          redo A;
1595        } else {        } else {
1596          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1597              !!!cp (93);
1598              !!!parse-error (type => 'bad attribute value');
1599            } else {
1600              !!!cp (94);
1601            }
1602            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1603          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605          redo A;          redo A;
1606        }        }
1607      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1608        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1609          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1610            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1611          !!!next-input-character;          !!!next-input-character;
1612          redo A;          redo A;
1613        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1614            !!!cp (96);
1615          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1616          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;          redo A;
1619        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1620          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1621          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1622            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1623            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1624          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1625            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1626            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1627                !!!cp (98);
1628              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1629              } else {
1630                ## NOTE: This state should never be reached.
1631                !!!cp (99);
1632            }            }
1633          } else {          } else {
1634            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1008  sub _get_next_token ($) { Line 1640  sub _get_next_token ($) {
1640    
1641          redo A;          redo A;
1642        } else {        } else {
1643          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1644            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1645          ## Stay in the state          ## Stay in the state
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        }        }
1649      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1650        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1651          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1652            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1653          !!!next-input-character;          !!!next-input-character;
1654          redo A;          redo A;
1655        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1656            !!!cp (102);
1657          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1658          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660          redo A;          redo A;
1661        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1662          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1663          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1664            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1665            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1666          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1667            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1668            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1669                !!!cp (104);
1670              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1671              } else {
1672                ## NOTE: This state should never be reached.
1673                !!!cp (105);
1674            }            }
1675          } else {          } else {
1676            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1044  sub _get_next_token ($) { Line 1682  sub _get_next_token ($) {
1682    
1683          redo A;          redo A;
1684        } else {        } else {
1685          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1686            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1687          ## Stay in the state          ## Stay in the state
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        }        }
1691      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1692        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1693            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1694            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1695            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1696            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1697            !!!cp (107);
1698          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1699          !!!next-input-character;          !!!next-input-character;
1700          redo A;          redo A;
1701        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1702            !!!cp (108);
1703          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1704          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1705          !!!next-input-character;          !!!next-input-character;
1706          redo A;          redo A;
1707        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1708          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1709            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1711          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1713            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1714                !!!cp (110);
1715              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1716              } else {
1717                ## NOTE: This state should never be reached.
1718                !!!cp (111);
1719            }            }
1720          } else {          } else {
1721            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1082  sub _get_next_token ($) { Line 1726  sub _get_next_token ($) {
1726          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1727    
1728          redo A;          redo A;
1729        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1730          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1731          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1732            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1733            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1734          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1735            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1736            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1737                !!!cp (113);
1738              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1739              } else {
1740                ## NOTE: This state should never be reached.
1741                !!!cp (114);
1742            }            }
1743          } else {          } else {
1744            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1103  sub _get_next_token ($) { Line 1750  sub _get_next_token ($) {
1750    
1751          redo A;          redo A;
1752        } else {        } else {
1753          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1754                 0x0022 => 1, # "
1755                 0x0027 => 1, # '
1756                 0x003D => 1, # =
1757                }->{$self->{next_char}}) {
1758              !!!cp (115);
1759              !!!parse-error (type => 'bad attribute value');
1760            } else {
1761              !!!cp (116);
1762            }
1763            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1764          ## Stay in the state          ## Stay in the state
1765          !!!next-input-character;          !!!next-input-character;
1766          redo A;          redo A;
1767        }        }
1768      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1769        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1770              (1,
1771               $self->{last_attribute_value_state}
1772                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1773               $self->{last_attribute_value_state}
1774                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1775               -1);
1776    
1777        unless (defined $token) {        unless (defined $token) {
1778            !!!cp (117);
1779          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1780        } else {        } else {
1781            !!!cp (118);
1782          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1783            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1784          ## 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"
1785        }        }
1786    
1787        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1788        # next-input-character is already done        # next-input-character is already done
1789        redo A;        redo A;
1790        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1791          if ($self->{next_char} == 0x0009 or # HT
1792              $self->{next_char} == 0x000A or # LF
1793              $self->{next_char} == 0x000B or # VT
1794              $self->{next_char} == 0x000C or # FF
1795              $self->{next_char} == 0x0020) { # SP
1796            !!!cp (118);
1797            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1798            !!!next-input-character;
1799            redo A;
1800          } elsif ($self->{next_char} == 0x003E) { # >
1801            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1802              !!!cp (119);
1803              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1804            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1805              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1806              if ($self->{current_token}->{attributes}) {
1807                !!!cp (120);
1808                !!!parse-error (type => 'end tag attribute');
1809              } else {
1810                ## NOTE: This state should never be reached.
1811                !!!cp (121);
1812              }
1813            } else {
1814              die "$0: $self->{current_token}->{type}: Unknown token type";
1815            }
1816            $self->{state} = DATA_STATE;
1817            !!!next-input-character;
1818    
1819            !!!emit ($self->{current_token}); # start tag or end tag
1820    
1821            redo A;
1822          } elsif ($self->{next_char} == 0x002F) { # /
1823            !!!cp (122);
1824            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1825            !!!next-input-character;
1826            redo A;
1827          } elsif ($self->{next_char} == -1) {
1828            !!!parse-error (type => 'unclosed tag');
1829            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1830              !!!cp (122.3);
1831              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1832            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (122.1);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (122.2);
1839              }
1840            } else {
1841              die "$0: $self->{current_token}->{type}: Unknown token type";
1842            }
1843            $self->{state} = DATA_STATE;
1844            ## Reconsume.
1845            !!!emit ($self->{current_token}); # start tag or end tag
1846            redo A;
1847          } else {
1848            !!!cp ('124.1');
1849            !!!parse-error (type => 'no space between attributes');
1850            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1851            ## reconsume
1852            redo A;
1853          }
1854        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1855          if ($self->{next_char} == 0x003E) { # >
1856            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1857              !!!cp ('124.2');
1858              !!!parse-error (type => 'nestc', token => $self->{current_token});
1859              ## TODO: Different type than slash in start tag
1860              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1861              if ($self->{current_token}->{attributes}) {
1862                !!!cp ('124.4');
1863                !!!parse-error (type => 'end tag attribute');
1864              } else {
1865                !!!cp ('124.5');
1866              }
1867              ## TODO: Test |<title></title/>|
1868            } else {
1869              !!!cp ('124.3');
1870              $self->{self_closing} = 1;
1871            }
1872    
1873            $self->{state} = DATA_STATE;
1874            !!!next-input-character;
1875    
1876            !!!emit ($self->{current_token}); # start tag or end tag
1877    
1878            redo A;
1879          } elsif ($self->{next_char} == -1) {
1880            !!!parse-error (type => 'unclosed tag');
1881            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1882              !!!cp (124.7);
1883              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1884            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1885              if ($self->{current_token}->{attributes}) {
1886                !!!cp (124.5);
1887                !!!parse-error (type => 'end tag attribute');
1888              } else {
1889                ## NOTE: This state should never be reached.
1890                !!!cp (124.6);
1891              }
1892            } else {
1893              die "$0: $self->{current_token}->{type}: Unknown token type";
1894            }
1895            $self->{state} = DATA_STATE;
1896            ## Reconsume.
1897            !!!emit ($self->{current_token}); # start tag or end tag
1898            redo A;
1899          } else {
1900            !!!cp ('124.4');
1901            !!!parse-error (type => 'nestc');
1902            ## TODO: This error type is wrong.
1903            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1904            ## Reconsume.
1905            redo A;
1906          }
1907      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1908        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1909                
1910        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1911          #my $token = {type => COMMENT_TOKEN, data => ''};
1912    
1913        BC: {        BC: {
1914          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1915              !!!cp (124);
1916            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1917            !!!next-input-character;            !!!next-input-character;
1918    
1919            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1920    
1921            redo A;            redo A;
1922          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1923              !!!cp (125);
1924            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1925            ## reconsume            ## reconsume
1926    
1927            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1928    
1929            redo A;            redo A;
1930          } else {          } else {
1931            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1932              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1933            !!!next-input-character;            !!!next-input-character;
1934            redo BC;            redo BC;
1935          }          }
1936        } # BC        } # BC
1937    
1938          die "$0: _get_next_token: unexpected case [BC]";
1939      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1940        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1941    
1942          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1943    
1944        my @next_char;        my @next_char;
1945        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1946                
1947        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1948          !!!next-input-character;          !!!next-input-character;
1949          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1950          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1951            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1952              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1953                                        line => $l, column => $c,
1954                                       };
1955            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1956            !!!next-input-character;            !!!next-input-character;
1957            redo A;            redo A;
1958            } else {
1959              !!!cp (128);
1960          }          }
1961        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1962                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1963          !!!next-input-character;          !!!next-input-character;
1964          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1965          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1966              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1967            !!!next-input-character;            !!!next-input-character;
1968            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1969            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1970                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1971              !!!next-input-character;              !!!next-input-character;
1972              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1973              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1974                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1975                !!!next-input-character;                !!!next-input-character;
1976                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1977                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1978                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1979                  !!!next-input-character;                  !!!next-input-character;
1980                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1981                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1982                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1983                    !!!next-input-character;                    !!!next-input-character;
1984                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1985                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1986                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1987                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1988                        ## TODO: What a stupid code this is!
1989                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1990                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1991                                                  quirks => 1,
1992                                                  line => $l, column => $c,
1993                                                 };
1994                      !!!next-input-character;                      !!!next-input-character;
1995                      redo A;                      redo A;
1996                      } else {
1997                        !!!cp (130);
1998                    }                    }
1999                    } else {
2000                      !!!cp (131);
2001                  }                  }
2002                  } else {
2003                    !!!cp (132);
2004                }                }
2005                } else {
2006                  !!!cp (133);
2007              }              }
2008              } else {
2009                !!!cp (134);
2010            }            }
2011            } else {
2012              !!!cp (135);
2013          }          }
2014          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2015                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2016                   $self->{next_char} == 0x005B) { # [
2017            !!!next-input-character;
2018            push @next_char, $self->{next_char};
2019            if ($self->{next_char} == 0x0043) { # C
2020              !!!next-input-character;
2021              push @next_char, $self->{next_char};
2022              if ($self->{next_char} == 0x0044) { # D
2023                !!!next-input-character;
2024                push @next_char, $self->{next_char};
2025                if ($self->{next_char} == 0x0041) { # A
2026                  !!!next-input-character;
2027                  push @next_char, $self->{next_char};
2028                  if ($self->{next_char} == 0x0054) { # T
2029                    !!!next-input-character;
2030                    push @next_char, $self->{next_char};
2031                    if ($self->{next_char} == 0x0041) { # A
2032                      !!!next-input-character;
2033                      push @next_char, $self->{next_char};
2034                      if ($self->{next_char} == 0x005B) { # [
2035                        !!!cp (135.1);
2036                        $self->{state} = CDATA_BLOCK_STATE;
2037                        !!!next-input-character;
2038                        redo A;
2039                      } else {
2040                        !!!cp (135.2);
2041                      }
2042                    } else {
2043                      !!!cp (135.3);
2044                    }
2045                  } else {
2046                    !!!cp (135.4);                
2047                  }
2048                } else {
2049                  !!!cp (135.5);
2050                }
2051              } else {
2052                !!!cp (135.6);
2053              }
2054            } else {
2055              !!!cp (135.7);
2056            }
2057          } else {
2058            !!!cp (136);
2059        }        }
2060    
2061        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2062        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2063        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2064        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2065          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2066                                    line => $l, column => $c,
2067                                   };
2068        redo A;        redo A;
2069                
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2071        ## 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?
2072      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2073        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2074            !!!cp (137);
2075          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2076          !!!next-input-character;          !!!next-input-character;
2077          redo A;          redo A;
2078        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2079            !!!cp (138);
2080          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2081          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2082          !!!next-input-character;          !!!next-input-character;
# Line 1221  sub _get_next_token ($) { Line 2084  sub _get_next_token ($) {
2084          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2085    
2086          redo A;          redo A;
2087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2088            !!!cp (139);
2089          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2090          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2091          ## reconsume          ## reconsume
# Line 1230  sub _get_next_token ($) { Line 2094  sub _get_next_token ($) {
2094    
2095          redo A;          redo A;
2096        } else {        } else {
2097            !!!cp (140);
2098          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2099              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2100          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2101          !!!next-input-character;          !!!next-input-character;
2102          redo A;          redo A;
2103        }        }
2104      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2105        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2106            !!!cp (141);
2107          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2111            !!!cp (142);
2112          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2113          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2114          !!!next-input-character;          !!!next-input-character;
# Line 1249  sub _get_next_token ($) { Line 2116  sub _get_next_token ($) {
2116          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2117    
2118          redo A;          redo A;
2119        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2120            !!!cp (143);
2121          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2122          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2123          ## reconsume          ## reconsume
# Line 1258  sub _get_next_token ($) { Line 2126  sub _get_next_token ($) {
2126    
2127          redo A;          redo A;
2128        } else {        } else {
2129            !!!cp (144);
2130          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2131              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2132          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2133          !!!next-input-character;          !!!next-input-character;
2134          redo A;          redo A;
2135        }        }
2136      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2137        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2138            !!!cp (145);
2139          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2143            !!!cp (146);
2144          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2145          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2146          ## reconsume          ## reconsume
# Line 1278  sub _get_next_token ($) { Line 2149  sub _get_next_token ($) {
2149    
2150          redo A;          redo A;
2151        } else {        } else {
2152          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2153            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2154          ## Stay in the state          ## Stay in the state
2155          !!!next-input-character;          !!!next-input-character;
2156          redo A;          redo A;
2157        }        }
2158      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2159        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2160            !!!cp (148);
2161          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2162          !!!next-input-character;          !!!next-input-character;
2163          redo A;          redo A;
2164        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2165            !!!cp (149);
2166          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2167          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2168          ## reconsume          ## reconsume
# Line 1297  sub _get_next_token ($) { Line 2171  sub _get_next_token ($) {
2171    
2172          redo A;          redo A;
2173        } else {        } else {
2174          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
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} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2181        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2182            !!!cp (151);
2183          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2184          !!!next-input-character;          !!!next-input-character;
2185    
2186          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2187    
2188          redo A;          redo A;
2189        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2190          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2191            !!!parse-error (type => 'dash in comment',
2192                            line => $self->{line_prev},
2193                            column => $self->{column_prev});
2194          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2195          ## Stay in the state          ## Stay in the state
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2199            !!!cp (153);
2200          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2201          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2202          ## reconsume          ## reconsume
# Line 1325  sub _get_next_token ($) { Line 2205  sub _get_next_token ($) {
2205    
2206          redo A;          redo A;
2207        } else {        } else {
2208          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2209          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2210                            line => $self->{line_prev},
2211                            column => $self->{column_prev});
2212            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2213          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2214          !!!next-input-character;          !!!next-input-character;
2215          redo A;          redo A;
2216        }        }
2217      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2218        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2219            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2220            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2221            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2222            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2223            !!!cp (155);
2224          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } else {        } else {
2228            !!!cp (156);
2229          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2230          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2231          ## reconsume          ## reconsume
2232          redo A;          redo A;
2233        }        }
2234      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2235        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2236            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2237            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2238            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2239            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2240            !!!cp (157);
2241          ## Stay in the state          ## Stay in the state
2242          !!!next-input-character;          !!!next-input-character;
2243          redo A;          redo A;
2244        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2245            !!!cp (158);
2246          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2247          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249    
2250          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2251    
2252          redo A;          redo A;
2253        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2254            !!!cp (159);
2255          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2256          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2257          ## reconsume          ## reconsume
2258    
2259          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2260    
2261          redo A;          redo A;
2262        } else {        } else {
2263          $self->{current_token}          !!!cp (160);
2264              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2265                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2266  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2267          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2268          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 2270  sub _get_next_token ($) {
2270        }        }
2271      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2272  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2273        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2274            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2275            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2276            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2277            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2278            !!!cp (161);
2279          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2280          !!!next-input-character;          !!!next-input-character;
2281          redo A;          redo A;
2282        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2283            !!!cp (162);
2284          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2285          !!!next-input-character;          !!!next-input-character;
2286    
2287          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2288    
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2291            !!!cp (163);
2292          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2293          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2294          ## reconsume          ## reconsume
2295    
2296          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2297          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2298    
2299          redo A;          redo A;
2300        } else {        } else {
2301            !!!cp (164);
2302          $self->{current_token}->{name}          $self->{current_token}->{name}
2303            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        }        }
2308      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2309        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2310            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2311            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2312            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2313            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2314            !!!cp (165);
2315          ## Stay in the state          ## Stay in the state
2316          !!!next-input-character;          !!!next-input-character;
2317          redo A;          redo A;
2318        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2319            !!!cp (166);
2320          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2321          !!!next-input-character;          !!!next-input-character;
2322    
2323          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2324    
2325          redo A;          redo A;
2326        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2327            !!!cp (167);
2328          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2329          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2330          ## reconsume          ## reconsume
2331    
2332          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2333          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2334    
2335          redo A;          redo A;
2336        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2337                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2338          !!!next-input-character;          !!!next-input-character;
2339          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2340              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2341            !!!next-input-character;            !!!next-input-character;
2342            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2343                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2344              !!!next-input-character;              !!!next-input-character;
2345              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2346                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2347                !!!next-input-character;                !!!next-input-character;
2348                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2349                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2350                  !!!next-input-character;                  !!!next-input-character;
2351                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2352                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2353                      !!!cp (168);
2354                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2355                    !!!next-input-character;                    !!!next-input-character;
2356                    redo A;                    redo A;
2357                    } else {
2358                      !!!cp (169);
2359                  }                  }
2360                  } else {
2361                    !!!cp (170);
2362                }                }
2363                } else {
2364                  !!!cp (171);
2365              }              }
2366              } else {
2367                !!!cp (172);
2368            }            }
2369            } else {
2370              !!!cp (173);
2371          }          }
2372    
2373          #          #
2374        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2375                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2376          !!!next-input-character;          !!!next-input-character;
2377          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2378              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2379            !!!next-input-character;            !!!next-input-character;
2380            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2381                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2382              !!!next-input-character;              !!!next-input-character;
2383              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2384                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2385                !!!next-input-character;                !!!next-input-character;
2386                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2387                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2388                  !!!next-input-character;                  !!!next-input-character;
2389                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2390                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2391                      !!!cp (174);
2392                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2393                    !!!next-input-character;                    !!!next-input-character;
2394                    redo A;                    redo A;
2395                    } else {
2396                      !!!cp (175);
2397                  }                  }
2398                  } else {
2399                    !!!cp (176);
2400                }                }
2401                } else {
2402                  !!!cp (177);
2403              }              }
2404              } else {
2405                !!!cp (178);
2406            }            }
2407            } else {
2408              !!!cp (179);
2409          }          }
2410    
2411          #          #
2412        } else {        } else {
2413            !!!cp (180);
2414          !!!next-input-character;          !!!next-input-character;
2415          #          #
2416        }        }
2417    
2418        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2419          $self->{current_token}->{quirks} = 1;
2420    
2421        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2422        # next-input-character is already done        # next-input-character is already done
2423        redo A;        redo A;
# Line 1506  sub _get_next_token ($) { Line 2425  sub _get_next_token ($) {
2425        if ({        if ({
2426              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2427              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2428            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2429            !!!cp (181);
2430          ## Stay in the state          ## Stay in the state
2431          !!!next-input-character;          !!!next-input-character;
2432          redo A;          redo A;
2433        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2434            !!!cp (182);
2435          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2436          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2440            !!!cp (183);
2441          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2442          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2443          !!!next-input-character;          !!!next-input-character;
2444          redo A;          redo A;
2445        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2446            !!!cp (184);
2447          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no 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 (185);
2458          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
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 (186);
2469          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2470            $self->{current_token}->{quirks} = 1;
2471    
2472          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474          redo A;          redo A;
2475        }        }
2476      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2477        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2478            !!!cp (187);
2479          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2480          !!!next-input-character;          !!!next-input-character;
2481          redo A;          redo A;
2482        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2483            !!!cp (188);
2484            !!!parse-error (type => 'unclosed PUBLIC literal');
2485    
2486            $self->{state} = DATA_STATE;
2487            !!!next-input-character;
2488    
2489            $self->{current_token}->{quirks} = 1;
2490            !!!emit ($self->{current_token}); # DOCTYPE
2491    
2492            redo A;
2493          } elsif ($self->{next_char} == -1) {
2494            !!!cp (189);
2495          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2496    
2497          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2498          ## reconsume          ## reconsume
2499    
2500          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2501          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2502    
2503          redo A;          redo A;
2504        } else {        } else {
2505            !!!cp (190);
2506          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2507              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2508          ## Stay in the state          ## Stay in the state
2509          !!!next-input-character;          !!!next-input-character;
2510          redo A;          redo A;
2511        }        }
2512      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2513        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2514            !!!cp (191);
2515          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2516          !!!next-input-character;          !!!next-input-character;
2517          redo A;          redo A;
2518        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2519            !!!cp (192);
2520            !!!parse-error (type => 'unclosed PUBLIC literal');
2521    
2522            $self->{state} = DATA_STATE;
2523            !!!next-input-character;
2524    
2525            $self->{current_token}->{quirks} = 1;
2526            !!!emit ($self->{current_token}); # DOCTYPE
2527    
2528            redo A;
2529          } elsif ($self->{next_char} == -1) {
2530            !!!cp (193);
2531          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2532    
2533          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2534          ## reconsume          ## reconsume
2535    
2536          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2537          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2538    
2539          redo A;          redo A;
2540        } else {        } else {
2541            !!!cp (194);
2542          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2543              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2544          ## Stay in the state          ## Stay in the state
2545          !!!next-input-character;          !!!next-input-character;
2546          redo A;          redo A;
# Line 1594  sub _get_next_token ($) { Line 2549  sub _get_next_token ($) {
2549        if ({        if ({
2550              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2551              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2552            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2553            !!!cp (195);
2554          ## Stay in the state          ## Stay in the state
2555          !!!next-input-character;          !!!next-input-character;
2556          redo A;          redo A;
2557        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2558            !!!cp (196);
2559          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2560          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2561          !!!next-input-character;          !!!next-input-character;
2562          redo A;          redo A;
2563        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2564            !!!cp (197);
2565          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2566          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2567          !!!next-input-character;          !!!next-input-character;
2568          redo A;          redo A;
2569        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2570            !!!cp (198);
2571          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2572          !!!next-input-character;          !!!next-input-character;
2573    
2574          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2575    
2576          redo A;          redo A;
2577        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2578            !!!cp (199);
2579          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2580    
2581          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2582          ## reconsume          ## reconsume
2583    
2584          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2585          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2586    
2587          redo A;          redo A;
2588        } else {        } else {
2589            !!!cp (200);
2590          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2591            $self->{current_token}->{quirks} = 1;
2592    
2593          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2598  sub _get_next_token ($) {
2598        if ({        if ({
2599              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2600              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2601            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2602            !!!cp (201);
2603          ## Stay in the state          ## Stay in the state
2604          !!!next-input-character;          !!!next-input-character;
2605          redo A;          redo A;
2606        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2607            !!!cp (202);
2608          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2609          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2610          !!!next-input-character;          !!!next-input-character;
2611          redo A;          redo A;
2612        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2613            !!!cp (203);
2614          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2615          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2616          !!!next-input-character;          !!!next-input-character;
2617          redo A;          redo A;
2618        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2619            !!!cp (204);
2620          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
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 (205);
2630          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
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 (206);
2641          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2642            $self->{current_token}->{quirks} = 1;
2643    
2644          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646          redo A;          redo A;
2647        }        }
2648      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2649        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2650            !!!cp (207);
2651          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2652          !!!next-input-character;          !!!next-input-character;
2653          redo A;          redo A;
2654        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2655            !!!cp (208);
2656            !!!parse-error (type => 'unclosed PUBLIC literal');
2657    
2658            $self->{state} = DATA_STATE;
2659            !!!next-input-character;
2660    
2661            $self->{current_token}->{quirks} = 1;
2662            !!!emit ($self->{current_token}); # DOCTYPE
2663    
2664            redo A;
2665          } elsif ($self->{next_char} == -1) {
2666            !!!cp (209);
2667          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2668    
2669          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2670          ## reconsume          ## reconsume
2671    
2672          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2673          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2674    
2675          redo A;          redo A;
2676        } else {        } else {
2677            !!!cp (210);
2678          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2679              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2680          ## Stay in the state          ## Stay in the state
2681          !!!next-input-character;          !!!next-input-character;
2682          redo A;          redo A;
2683        }        }
2684      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2685        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2686            !!!cp (211);
2687          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2688          !!!next-input-character;          !!!next-input-character;
2689          redo A;          redo A;
2690        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2691            !!!cp (212);
2692            !!!parse-error (type => 'unclosed PUBLIC literal');
2693    
2694            $self->{state} = DATA_STATE;
2695            !!!next-input-character;
2696    
2697            $self->{current_token}->{quirks} = 1;
2698            !!!emit ($self->{current_token}); # DOCTYPE
2699    
2700            redo A;
2701          } elsif ($self->{next_char} == -1) {
2702            !!!cp (213);
2703          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2704    
2705          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2706          ## reconsume          ## reconsume
2707    
2708          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2709          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2710    
2711          redo A;          redo A;
2712        } else {        } else {
2713            !!!cp (214);
2714          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2715              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2716          ## Stay in the state          ## Stay in the state
2717          !!!next-input-character;          !!!next-input-character;
2718          redo A;          redo A;
# Line 1722  sub _get_next_token ($) { Line 2721  sub _get_next_token ($) {
2721        if ({        if ({
2722              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2723              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2724            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2725            !!!cp (215);
2726          ## Stay in the state          ## Stay in the state
2727          !!!next-input-character;          !!!next-input-character;
2728          redo A;          redo A;
2729        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2730            !!!cp (216);
2731          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733    
2734          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2735    
2736          redo A;          redo A;
2737        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2738          !!!parse-error (type => 'unclosed DOCTYPE');          !!!cp (217);
2739    
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          ## reconsume          ## reconsume
2742    
2743          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2744          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2745    
2746          redo A;          redo A;
2747        } else {        } else {
2748            !!!cp (218);
2749          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2750            #$self->{current_token}->{quirks} = 1;
2751    
2752          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2753          !!!next-input-character;          !!!next-input-character;
2754          redo A;          redo A;
2755        }        }
2756      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2757        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2758            !!!cp (219);
2759          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2760          !!!next-input-character;          !!!next-input-character;
2761    
         delete $self->{current_token}->{correct};  
2762          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2763    
2764          redo A;          redo A;
2765        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2766            !!!cp (220);
2767          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2768          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2769          ## reconsume          ## reconsume
2770    
         delete $self->{current_token}->{correct};  
2771          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2772    
2773          redo A;          redo A;
2774        } else {        } else {
2775            !!!cp (221);
2776          ## Stay in the state          ## Stay in the state
2777          !!!next-input-character;          !!!next-input-character;
2778          redo A;          redo A;
2779        }        }
2780        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2781          my $s = '';
2782          
2783          my ($l, $c) = ($self->{line}, $self->{column});
2784    
2785          CS: while ($self->{next_char} != -1) {
2786            if ($self->{next_char} == 0x005D) { # ]
2787              !!!next-input-character;
2788              if ($self->{next_char} == 0x005D) { # ]
2789                !!!next-input-character;
2790                MDC: {
2791                  if ($self->{next_char} == 0x003E) { # >
2792                    !!!cp (221.1);
2793                    !!!next-input-character;
2794                    last CS;
2795                  } elsif ($self->{next_char} == 0x005D) { # ]
2796                    !!!cp (221.2);
2797                    $s .= ']';
2798                    !!!next-input-character;
2799                    redo MDC;
2800                  } else {
2801                    !!!cp (221.3);
2802                    $s .= ']]';
2803                    #
2804                  }
2805                } # MDC
2806              } else {
2807                !!!cp (221.4);
2808                $s .= ']';
2809                #
2810              }
2811            } else {
2812              !!!cp (221.5);
2813              #
2814            }
2815            $s .= chr $self->{next_char};
2816            !!!next-input-character;
2817          } # CS
2818    
2819          $self->{state} = DATA_STATE;
2820          ## next-input-character done or EOF, which is reconsumed.
2821    
2822          if (length $s) {
2823            !!!cp (221.6);
2824            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2825                      line => $l, column => $c});
2826          } else {
2827            !!!cp (221.7);
2828          }
2829    
2830          redo A;
2831    
2832          ## ISSUE: "text tokens" in spec.
2833          ## TODO: Streaming support
2834      } else {      } else {
2835        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2836      }      }
# Line 1780  sub _get_next_token ($) { Line 2839  sub _get_next_token ($) {
2839    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2840  } # _get_next_token  } # _get_next_token
2841    
2842  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2843    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2844    
2845      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2846    
2847    if ({    if ({
2848         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2849         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2850        }->{$self->{next_input_character}}) {         $additional => 1,
2851          }->{$self->{next_char}}) {
2852        !!!cp (1001);
2853      ## Don't consume      ## Don't consume
2854      ## No error      ## No error
2855      return undef;      return undef;
2856    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2857      !!!next-input-character;      !!!next-input-character;
2858      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2859          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2860        my $code;        my $code;
2861        X: {        X: {
2862          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2863          !!!next-input-character;          !!!next-input-character;
2864          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2865              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2866              !!!cp (1002);
2867            $code ||= 0;            $code ||= 0;
2868            $code *= 0x10;            $code *= 0x10;
2869            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2870            redo X;            redo X;
2871          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2872                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2873              !!!cp (1003);
2874            $code ||= 0;            $code ||= 0;
2875            $code *= 0x10;            $code *= 0x10;
2876            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2877            redo X;            redo X;
2878          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2879                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2880              !!!cp (1004);
2881            $code ||= 0;            $code ||= 0;
2882            $code *= 0x10;            $code *= 0x10;
2883            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2884            redo X;            redo X;
2885          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2886            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2887            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2888            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2889              $self->{next_char} = 0x0023; # #
2890            return undef;            return undef;
2891          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2892              !!!cp (1006);
2893            !!!next-input-character;            !!!next-input-character;
2894          } else {          } else {
2895            !!!parse-error (type => 'no refc');            !!!cp (1007);
2896              !!!parse-error (type => 'no refc', line => $l, column => $c);
2897          }          }
2898    
2899          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2900            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2901              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2902            $code = 0xFFFD;            $code = 0xFFFD;
2903          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2904            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2905              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2906            $code = 0xFFFD;            $code = 0xFFFD;
2907          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2908            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2909              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2910            $code = 0x000A;            $code = 0x000A;
2911          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2912            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2913              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2914            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2915          }          }
2916    
2917          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2918                    has_reference => 1,
2919                    line => $l, column => $c,
2920                   };
2921        } # X        } # X
2922      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2923               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2924        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2925        !!!next-input-character;        !!!next-input-character;
2926                
2927        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2928                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2929            !!!cp (1012);
2930          $code *= 10;          $code *= 10;
2931          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2932                    
2933          !!!next-input-character;          !!!next-input-character;
2934        }        }
2935    
2936        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2937            !!!cp (1013);
2938          !!!next-input-character;          !!!next-input-character;
2939        } else {        } else {
2940          !!!parse-error (type => 'no refc');          !!!cp (1014);
2941            !!!parse-error (type => 'no refc', line => $l, column => $c);
2942        }        }
2943    
2944        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2945          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2946            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2947          $code = 0xFFFD;          $code = 0xFFFD;
2948        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2949          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2950            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2951          $code = 0xFFFD;          $code = 0xFFFD;
2952        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2953          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2954            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2955          $code = 0x000A;          $code = 0x000A;
2956        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2957          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2958            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2959          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2960        }        }
2961                
2962        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2963                  line => $l, column => $c,
2964                 };
2965      } else {      } else {
2966        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2967        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2968        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2969          $self->{next_char} = 0x0023; # #
2970        return undef;        return undef;
2971      }      }
2972    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2973              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2974             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2975              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2976      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2977      !!!next-input-character;      !!!next-input-character;
2978    
2979      my $value = $entity_name;      my $value = $entity_name;
# Line 1895  sub _tokenize_attempt_to_consume_an_enti Line 2981  sub _tokenize_attempt_to_consume_an_enti
2981      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2982      our $EntityChar;      our $EntityChar;
2983    
2984      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2985             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2986             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2987               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2988              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2989               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2990              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2991               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2992              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2993        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2994        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2995          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2996              !!!cp (1020);
2997            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2998            $match = 1;            $match = 1;
2999            !!!next-input-character;            !!!next-input-character;
3000            last;            last;
3001          } else {          } else {
3002              !!!cp (1021);
3003            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3004            $match = -1;            $match = -1;
3005            !!!next-input-character;            !!!next-input-character;
3006          }          }
3007        } else {        } else {
3008          $value .= chr $self->{next_input_character};          !!!cp (1022);
3009            $value .= chr $self->{next_char};
3010          $match *= 2;          $match *= 2;
3011          !!!next-input-character;          !!!next-input-character;
3012        }        }
3013      }      }
3014            
3015      if ($match > 0) {      if ($match > 0) {
3016        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
3017          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3018                  line => $l, column => $c,
3019                 };
3020      } elsif ($match < 0) {      } elsif ($match < 0) {
3021        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3022        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3023          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
3024        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3025          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
3026                   };
3027          } else {
3028            !!!cp (1025);
3029            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3030                    line => $l, column => $c,
3031                   };
3032        }        }
3033      } else {      } else {
3034        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3035        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3036        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3037          return {type => CHARACTER_TOKEN, data => '&'.$value,
3038                  line => $l, column => $c,
3039                 };
3040      }      }
3041    } else {    } else {
3042        !!!cp (1027);
3043      ## no characters are consumed      ## no characters are consumed
3044      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3045      return undef;      return undef;
3046    }    }
3047  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1977  sub _construct_tree ($) { Line 3079  sub _construct_tree ($) {
3079        
3080    !!!next-token;    !!!next-token;
3081    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3082    undef $self->{form_element};    undef $self->{form_element};
3083    undef $self->{head_element};    undef $self->{head_element};
3084    $self->{open_elements} = [];    $self->{open_elements} = [];
3085    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3086    
3087      ## NOTE: The "initial" insertion mode.
3088    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3089    
3090      ## NOTE: The "before html" insertion mode.
3091    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3092      $self->{insertion_mode} = BEFORE_HEAD_IM;
3093    
3094      ## NOTE: The "before head" insertion mode and so on.
3095    $self->_tree_construction_main;    $self->_tree_construction_main;
3096  } # _construct_tree  } # _construct_tree
3097    
3098  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3099    my $self = shift;    my $self = shift;
3100    
3101      ## NOTE: "initial" insertion mode
3102    
3103    INITIAL: {    INITIAL: {
3104      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3105        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2001  sub _tree_construction_initial ($) { Line 3111  sub _tree_construction_initial ($) {
3111        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3112            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3113            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3114          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3115            !!!parse-error (type => 'not HTML5', token => $token);
3116        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3117            !!!cp ('t2');
3118          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3119          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3120          } else {
3121            !!!cp ('t3');
3122        }        }
3123                
3124        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3125          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3126          ## NOTE: Default value for both |public_id| and |system_id| attributes
3127          ## are empty strings, so that we don't set any value in missing cases.
3128        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3129            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3130        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2017  sub _tree_construction_initial ($) { Line 3133  sub _tree_construction_initial ($) {
3133        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3134        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3135                
3136        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3137            !!!cp ('t4');
3138          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3139        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3140          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3141          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3142          if ({          my $prefix = [
3143            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3144            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3145            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3146            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3147            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3148            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3149            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3150            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3151            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3152            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3153            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3154            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3155            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3156            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3157            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3158            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3159            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3160            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3161            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3162            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3165            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3168            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3171            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3175            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3176            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3177            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3178            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3179            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3180            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3181            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3182            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3183            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3187            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3188            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3189            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3190            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3191            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3192            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3193            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3194            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3195            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3196            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3197            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3198            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3199            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3200            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3201            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3202            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3203            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3204            "-//W3C//DTD W3 HTML//EN" => 1,            }
3205            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3206            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3207            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3208            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3209            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3210            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3211            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3212          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3213                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3214            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3215                !!!cp ('t6');
3216              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3217            } else {            } else {
3218                !!!cp ('t7');
3219              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3220            }            }
3221          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3222                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3223              !!!cp ('t8');
3224            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3225            } else {
3226              !!!cp ('t9');
3227          }          }
3228          } else {
3229            !!!cp ('t10');
3230        }        }
3231        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3232          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3233          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3234          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") {
3235              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3236              ## marked as quirks.
3237            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3238              !!!cp ('t11');
3239            } else {
3240              !!!cp ('t12');
3241          }          }
3242          } else {
3243            !!!cp ('t13');
3244        }        }
3245                
3246        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3247        !!!next-token;        !!!next-token;
3248        return;        return;
3249      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 3251  sub _tree_construction_initial ($) {
3251                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3252                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3253               }->{$token->{type}}) {               }->{$token->{type}}) {
3254        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3255          !!!parse-error (type => 'no DOCTYPE', token => $token);
3256        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3257        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3258        ## reprocess        ## reprocess
3259          !!!ack-later;
3260        return;        return;
3261      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3262        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3263          ## Ignore the token          ## Ignore the token
3264    
3265          unless (length $token->{data}) {          unless (length $token->{data}) {
3266            ## Stay in the phase            !!!cp ('t15');
3267              ## Stay in the insertion mode.
3268            !!!next-token;            !!!next-token;
3269            redo INITIAL;            redo INITIAL;
3270            } else {
3271              !!!cp ('t16');
3272          }          }
3273          } else {
3274            !!!cp ('t17');
3275        }        }
3276    
3277        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3278        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3279        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3280        ## reprocess        ## reprocess
3281        return;        return;
3282      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3283          !!!cp ('t18');
3284        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3285        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3286                
3287        ## Stay in the phase.        ## Stay in the insertion mode.
3288        !!!next-token;        !!!next-token;
3289        redo INITIAL;        redo INITIAL;
3290      } else {      } else {
3291        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3292      }      }
3293    } # INITIAL    } # INITIAL
3294    
3295      die "$0: _tree_construction_initial: This should be never reached";
3296  } # _tree_construction_initial  } # _tree_construction_initial
3297    
3298  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3299    my $self = shift;    my $self = shift;
3300    
3301      ## NOTE: "before html" insertion mode.
3302        
3303    B: {    B: {
3304        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3305          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3306            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3307          ## Ignore the token          ## Ignore the token
3308          ## Stay in the phase          ## Stay in the insertion mode.
3309          !!!next-token;          !!!next-token;
3310          redo B;          redo B;
3311        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3312            !!!cp ('t20');
3313          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3314          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3315          ## Stay in the phase          ## Stay in the insertion mode.
3316          !!!next-token;          !!!next-token;
3317          redo B;          redo B;
3318        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2177  sub _tree_construction_root_element ($) Line 3320  sub _tree_construction_root_element ($)
3320            ## Ignore the token.            ## Ignore the token.
3321    
3322            unless (length $token->{data}) {            unless (length $token->{data}) {
3323              ## Stay in the phase              !!!cp ('t21');
3324                ## Stay in the insertion mode.
3325              !!!next-token;              !!!next-token;
3326              redo B;              redo B;
3327              } else {
3328                !!!cp ('t22');
3329            }            }
3330            } else {
3331              !!!cp ('t23');
3332          }          }
3333    
3334          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3335    
3336          #          #
3337        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3338          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3339              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3340            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3341                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3342            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3343                  [$root_element, $el_category->{html}];
3344    
3345              if ($token->{attributes}->{manifest}) {
3346                !!!cp ('t24');
3347                $self->{application_cache_selection}
3348                    ->($token->{attributes}->{manifest}->{value});
3349                ## ISSUE: Spec is unclear on relative references.
3350                ## According to Hixie (#whatwg 2008-03-19), it should be
3351                ## resolved against the base URI of the document in HTML
3352                ## or xml:base of the element in XHTML.
3353              } else {
3354                !!!cp ('t25');
3355                $self->{application_cache_selection}->(undef);
3356              }
3357    
3358              !!!nack ('t25c');
3359    
3360              !!!next-token;
3361              return; ## Go to the "before head" insertion mode.
3362          } else {          } else {
3363            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3364              #
3365          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3366        } elsif ({        } elsif ({
3367                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3368                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3369                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3370          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3371          #          #
3372        } else {        } else {
3373          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3374        }        }
3375    
3376        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3377        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3378        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3379        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3380        #redo B;  
3381        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3382    
3383        ## NOTE: Reprocess the token.
3384        !!!ack-later;
3385        return; ## Go to the "before head" insertion mode.
3386    
3387        ## ISSUE: There is an issue in the spec
3388    } # B    } # B
3389    
3390      die "$0: _tree_construction_root_element: This should never be reached";
3391  } # _tree_construction_root_element  } # _tree_construction_root_element
3392    
3393  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 3402  sub _reset_insertion_mode ($) {
3402            
3403      ## Step 3      ## Step 3
3404      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"!?  
3405        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3406          $last = 1;          $last = 1;
3407          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3408            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3409                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3410              #          } else {
3411            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3412          }          }
3413        }        }
3414              
3415        ## Step 4..13        ## Step 4..14
3416        my $new_mode = {        my $new_mode;
3417          if ($node->[1] & FOREIGN_EL) {
3418            !!!cp ('t28.1');
3419            ## NOTE: Strictly spaking, the line below only applies to MathML and
3420            ## SVG elements.  Currently the HTML syntax supports only MathML and
3421            ## SVG elements as foreigners.
3422            $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3423            ## ISSUE: What is set as the secondary insertion mode?
3424          } elsif ($node->[1] & TABLE_CELL_EL) {
3425            if ($last) {
3426              !!!cp ('t28.2');
3427              #
3428            } else {
3429              !!!cp ('t28.3');
3430              $new_mode = IN_CELL_IM;
3431            }
3432          } else {
3433            !!!cp ('t28.4');
3434            $new_mode = {
3435                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3436                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3437                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3438                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3439                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3440                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2263  sub _reset_insertion_mode ($) { Line 3445  sub _reset_insertion_mode ($) {
3445                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3446                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3447                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3448                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3449          }
3450        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3451                
3452        ## Step 14        ## Step 15
3453        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3454          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3455              !!!cp ('t29');
3456            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3457          } else {          } else {
3458              ## ISSUE: Can this state be reached?
3459              !!!cp ('t30');
3460            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3461          }          }
3462          return;          return;
3463          } else {
3464            !!!cp ('t31');
3465        }        }
3466                
3467        ## Step 15        ## Step 16
3468        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3469                
3470        ## Step 16        ## Step 17
3471        $i--;        $i--;
3472        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3473                
3474        ## Step 17        ## Step 18
3475        redo S3;        redo S3;
3476      } # S3      } # S3
3477    
3478      die "$0: _reset_insertion_mode: This line should never be reached";
3479  } # _reset_insertion_mode  } # _reset_insertion_mode
3480    
3481  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 3497  sub _tree_construction_main ($) {
3497      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3498      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3499        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3500            !!!cp ('t32');
3501          return;          return;
3502        }        }
3503      }      }
# Line 2321  sub _tree_construction_main ($) { Line 3512  sub _tree_construction_main ($) {
3512    
3513        ## Step 6        ## Step 6
3514        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3515            !!!cp ('t33_1');
3516          #          #
3517        } else {        } else {
3518          my $in_open_elements;          my $in_open_elements;
3519          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3520            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3521                !!!cp ('t33');
3522              $in_open_elements = 1;              $in_open_elements = 1;
3523              last OE;              last OE;
3524            }            }
3525          }          }
3526          if ($in_open_elements) {          if ($in_open_elements) {
3527              !!!cp ('t34');
3528            #            #
3529          } else {          } else {
3530              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3531              !!!cp ('t35');
3532            redo S4;            redo S4;
3533          }          }
3534        }        }
# Line 2355  sub _tree_construction_main ($) { Line 3551  sub _tree_construction_main ($) {
3551    
3552        ## Step 11        ## Step 11
3553        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3554            !!!cp ('t36');
3555          ## Step 7'          ## Step 7'
3556          $i++;          $i++;
3557          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3558                    
3559          redo S7;          redo S7;
3560        }        }
3561    
3562          !!!cp ('t37');
3563      } # S7      } # S7
3564    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3565    
3566    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3567      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3568        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3569            !!!cp ('t38');
3570          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3571          return;          return;
3572        }        }
3573      }      }
3574    
3575        !!!cp ('t39');
3576    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3577    
3578    my $parse_rcdata = sub ($$) {    my $insert;
3579      my ($content_model_flag, $insert) = @_;  
3580      my $parse_rcdata = sub ($) {
3581        my ($content_model_flag) = @_;
3582    
3583      ## Step 1      ## Step 1
3584      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3585      my $el;      my $el;
3586      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3587    
3588      ## Step 2      ## Step 2
3589      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3590    
3591      ## Step 3      ## Step 3
3592      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2390  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595      ## Step 4      ## Step 4
3596      my $text = '';      my $text = '';
3597        !!!nack ('t40.1');
3598      !!!next-token;      !!!next-token;
3599      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3600          !!!cp ('t40');
3601        $text .= $token->{data};        $text .= $token->{data};
3602        !!!next-token;        !!!next-token;
3603      }      }
3604    
3605      ## Step 5      ## Step 5
3606      if (length $text) {      if (length $text) {
3607          !!!cp ('t41');
3608        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3609        $el->append_child ($text);        $el->append_child ($text);
3610      }      }
# Line 2406  sub _tree_construction_main ($) { Line 3613  sub _tree_construction_main ($) {
3613      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3614    
3615      ## Step 7      ## Step 7
3616      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3617            $token->{tag_name} eq $start_tag_name) {
3618          !!!cp ('t42');
3619        ## 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});  
3620      } else {      } else {
3621        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3622          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3623            !!!cp ('t43');
3624            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3625          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3626            !!!cp ('t44');
3627            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3628          } else {
3629            die "$0: $content_model_flag in parse_rcdata";
3630          }
3631      }      }
3632      !!!next-token;      !!!next-token;
3633    }; # $parse_rcdata    }; # $parse_rcdata
3634    
3635    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3636      my $script_el;      my $script_el;
3637      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3638      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3639    
3640      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3641      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3642            
3643      my $text = '';      my $text = '';
3644        !!!nack ('t45.1');
3645      !!!next-token;      !!!next-token;
3646      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3647          !!!cp ('t45');
3648        $text .= $token->{data};        $text .= $token->{data};
3649        !!!next-token;        !!!next-token;
3650      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3651      if (length $text) {      if (length $text) {
3652          !!!cp ('t46');
3653        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3654      }      }
3655                                
# Line 2441  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657    
3658      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3659          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3660          !!!cp ('t47');
3661        ## Ignore the token        ## Ignore the token
3662      } else {      } else {
3663        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3664          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3665        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3666        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3667      }      }
3668            
3669      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3670          !!!cp ('t49');
3671        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3672      } else {      } else {
3673          !!!cp ('t50');
3674        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3675        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3676    
# Line 2464  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684      !!!next-token;      !!!next-token;
3685    }; # $script_start_tag    }; # $script_start_tag
3686    
3687      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3688      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3689      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3690    
3691    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3692      my $tag_name = shift;      my $end_tag_token = shift;
3693        my $tag_name = $end_tag_token->{tag_name};
3694    
3695        ## NOTE: The adoption agency algorithm (AAA).
3696    
3697      FET: {      FET: {
3698        ## Step 1        ## Step 1
3699        my $formatting_element;        my $formatting_element;
3700        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3701        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3702          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3703              !!!cp ('t52');
3704              last AFE;
3705            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3706                         eq $tag_name) {
3707              !!!cp ('t51');
3708            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3709            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3710            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3711          }          }
3712        } # AFE        } # AFE
3713        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3714          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3715            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3716          ## Ignore the token          ## Ignore the token
3717          !!!next-token;          !!!next-token;
3718          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 3724  sub _tree_construction_main ($) {
3724          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3725          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3726            if ($in_scope) {            if ($in_scope) {
3727                !!!cp ('t54');
3728              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3729              last INSCOPE;              last INSCOPE;
3730            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3731              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3732                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3733                                token => $end_tag_token);
3734              ## Ignore the token              ## Ignore the token
3735              !!!next-token;              !!!next-token;
3736              return;              return;
3737            }            }
3738          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3739                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3740            $in_scope = 0;            $in_scope = 0;
3741          }          }
3742        } # INSCOPE        } # INSCOPE
3743        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3744          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3745            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3746                            token => $end_tag_token);
3747          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3748          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3749          return;          return;
3750        }        }
3751        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3752          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3753            !!!parse-error (type => 'not closed',
3754                            value => $self->{open_elements}->[-1]->[0]
3755                                ->manakai_local_name,
3756                            token => $end_tag_token);
3757        }        }
3758                
3759        ## Step 2        ## Step 2
# Line 2523  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3762        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3763          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3764          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3765              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3766              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3767               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3768              !!!cp ('t59');
3769            $furthest_block = $node;            $furthest_block = $node;
3770            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3771          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3772              !!!cp ('t60');
3773            last OE;            last OE;
3774          }          }
3775        } # OE        } # OE
3776                
3777        ## Step 3        ## Step 3
3778        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3779            !!!cp ('t61');
3780          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3781          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3782          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789        ## Step 5        ## Step 5
3790        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3791        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3792            !!!cp ('t62');
3793          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3794        }        }
3795                
# Line 2570  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812          S7S2: {          S7S2: {
3813            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3814              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3815                  !!!cp ('t63');
3816                $node_i_in_active = $_;                $node_i_in_active = $_;
3817                last S7S2;                last S7S2;
3818              }              }
# Line 2583  sub _tree_construction_main ($) { Line 3826  sub _tree_construction_main ($) {
3826                    
3827          ## Step 4          ## Step 4
3828          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3829              !!!cp ('t64');
3830            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3831          }          }
3832                    
3833          ## Step 5          ## Step 5
3834          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3835              !!!cp ('t65');
3836            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3837            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3838            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850        } # S7          } # S7  
3851                
3852        ## Step 8        ## Step 8
3853        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3854            my $foster_parent_element;
3855            my $next_sibling;
3856            OE: for (reverse 0..$#{$self->{open_elements}}) {
3857              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3858                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3859                                 if (defined $parent and $parent->node_type == 1) {
3860                                   !!!cp ('t65.1');
3861                                   $foster_parent_element = $parent;
3862                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3863                                 } else {
3864                                   !!!cp ('t65.2');
3865                                   $foster_parent_element
3866                                     = $self->{open_elements}->[$_ - 1]->[0];
3867                                 }
3868                                 last OE;
3869                               }
3870                             } # OE
3871                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3872                               unless defined $foster_parent_element;
3873            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3874            $open_tables->[-1]->[1] = 1; # tainted
3875          } else {
3876            !!!cp ('t65.3');
3877            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3878          }
3879                
3880        ## Step 9        ## Step 9
3881        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 3892  sub _tree_construction_main ($) {
3892        my $i;        my $i;
3893        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3894          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3895              !!!cp ('t66');
3896            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3897            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3898          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3899              !!!cp ('t67');
3900            $i = $_;            $i = $_;
3901          }          }
3902        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906        undef $i;        undef $i;
3907        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3908          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3909              !!!cp ('t68');
3910            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3911            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3912          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3913              !!!cp ('t69');
3914            $i = $_;            $i = $_;
3915          }          }
3916        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921      } # FET      } # FET
3922    }; # $formatting_end_tag    }; # $formatting_end_tag
3923    
3924    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3925      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3926    }; # $insert_to_current    }; # $insert_to_current
3927    
3928    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3929                         my $child = shift;      my $child = shift;
3930                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3931                              table => 1, tbody => 1, tfoot => 1,        # MUST
3932                              thead => 1, tr => 1,        my $foster_parent_element;
3933                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3934                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3935                           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') {  
3936                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3937                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3938                                   !!!cp ('t70');
3939                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3940                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3941                               } else {                               } else {
3942                                   !!!cp ('t71');
3943                                 $foster_parent_element                                 $foster_parent_element
3944                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3945                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 3950  sub _tree_construction_main ($) {
3950                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3951                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3952                             ($child, $next_sibling);                             ($child, $next_sibling);
3953                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3954                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3955                         }        !!!cp ('t72');
3956          $self->{open_elements}->[-1]->[0]->append_child ($child);
3957        }
3958    }; # $insert_to_foster    }; # $insert_to_foster
3959    
3960    my $insert;    B: while (1) {
   
   B: {  
3961      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3962        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3963          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3964        ## Ignore the token        ## Ignore the token
3965        ## Stay in the phase        ## Stay in the phase
3966        !!!next-token;        !!!next-token;
3967        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;  
3968      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3969               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3970        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3971          ## Turn into the main phase          !!!cp ('t79');
3972          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3973          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3974        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3975          ## Turn into the main phase          !!!cp ('t80');
3976          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3977          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3978          } else {
3979            !!!cp ('t81');
3980        }        }
3981    
3982  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3983  ## 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');  
       }  
3984        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3985        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3986          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3987              !!!cp ('t84');
3988            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3989              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3990               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3991          }          }
3992        }        }
3993          !!!nack ('t84.1');
3994        !!!next-token;        !!!next-token;
3995        redo B;        next B;
3996      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3997        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3998        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3999            !!!cp ('t85');
4000          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4001        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4002            !!!cp ('t86');
4003          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4004        } else {        } else {
4005            !!!cp ('t87');
4006          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4007        }        }
4008        !!!next-token;        !!!next-token;
4009        redo B;        next B;
4010      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4011          if ($token->{type} == CHARACTER_TOKEN) {
4012            !!!cp ('t87.1');
4013            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4014            !!!next-token;
4015            next B;
4016          } elsif ($token->{type} == START_TAG_TOKEN) {
4017            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4018                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4019                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4020                ($token->{tag_name} eq 'svg' and
4021                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4022              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4023              !!!cp ('t87.2');
4024              #
4025            } elsif ({
4026                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4027                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4028                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4029                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4030                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4031                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4032                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4033                      var => 1,
4034                     }->{$token->{tag_name}}) {
4035              !!!cp ('t87.2');
4036              !!!parse-error (type => 'not closed',
4037                              value => $self->{open_elements}->[-1]->[0]
4038                                  ->manakai_local_name,
4039                              token => $token);
4040    
4041              pop @{$self->{open_elements}}
4042                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4043    
4044              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4045              ## Reprocess.
4046              next B;
4047            } else {
4048              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4049              my $tag_name = $token->{tag_name};
4050              if ($nsuri eq $SVG_NS) {
4051                $tag_name = {
4052                   altglyph => 'altGlyph',
4053                   altglyphdef => 'altGlyphDef',
4054                   altglyphitem => 'altGlyphItem',
4055                   animatecolor => 'animateColor',
4056                   animatemotion => 'animateMotion',
4057                   animatetransform => 'animateTransform',
4058                   clippath => 'clipPath',
4059                   feblend => 'feBlend',
4060                   fecolormatrix => 'feColorMatrix',
4061                   fecomponenttransfer => 'feComponentTransfer',
4062                   fecomposite => 'feComposite',
4063                   feconvolvematrix => 'feConvolveMatrix',
4064                   fediffuselighting => 'feDiffuseLighting',
4065                   fedisplacementmap => 'feDisplacementMap',
4066                   fedistantlight => 'feDistantLight',
4067                   feflood => 'feFlood',
4068                   fefunca => 'feFuncA',
4069                   fefuncb => 'feFuncB',
4070                   fefuncg => 'feFuncG',
4071                   fefuncr => 'feFuncR',
4072                   fegaussianblur => 'feGaussianBlur',
4073                   feimage => 'feImage',
4074                   femerge => 'feMerge',
4075                   femergenode => 'feMergeNode',
4076                   femorphology => 'feMorphology',
4077                   feoffset => 'feOffset',
4078                   fepointlight => 'fePointLight',
4079                   fespecularlighting => 'feSpecularLighting',
4080                   fespotlight => 'feSpotLight',
4081                   fetile => 'feTile',
4082                   feturbulence => 'feTurbulence',
4083                   foreignobject => 'foreignObject',
4084                   glyphref => 'glyphRef',
4085                   lineargradient => 'linearGradient',
4086                   radialgradient => 'radialGradient',
4087                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4088                   textpath => 'textPath',  
4089                }->{$tag_name} || $tag_name;
4090              }
4091    
4092              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4093    
4094              ## "adjust foreign attributes" - done in insert-element-f
4095    
4096              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4097    
4098              if ($self->{self_closing}) {
4099                pop @{$self->{open_elements}};
4100                !!!ack ('t87.3');
4101              } else {
4102                !!!cp ('t87.4');
4103              }
4104    
4105              !!!next-token;
4106              next B;
4107            }
4108          } elsif ($token->{type} == END_TAG_TOKEN) {
4109            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4110            !!!cp ('t87.5');
4111            #
4112          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4113            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4114            !!!cp ('t87.6');
4115            #
4116            ## TODO: ...
4117          } else {
4118            die "$0: $token->{type}: Unknown token type";        
4119          }
4120        }
4121    
4122        if ($self->{insertion_mode} & HEAD_IMS) {
4123        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4124          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4125            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4126                !!!cp ('t88.2');
4127                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4128              } else {
4129                !!!cp ('t88.1');
4130                ## Ignore the token.
4131                !!!next-token;
4132                next B;
4133              }
4134            unless (length $token->{data}) {            unless (length $token->{data}) {
4135                !!!cp ('t88');
4136              !!!next-token;              !!!next-token;
4137              redo B;              next B;
4138            }            }
4139          }          }
4140    
4141          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4142              !!!cp ('t89');
4143            ## As if <head>            ## As if <head>
4144            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4145            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4146            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4147                  [$self->{head_element}, $el_category->{head}];
4148    
4149            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4150            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4151    
4152            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4153          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4154              !!!cp ('t90');
4155            ## As if </noscript>            ## As if </noscript>
4156            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4157            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4158                        
4159            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4160            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 4162  sub _tree_construction_main ($) {
4162    
4163            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4164          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4165              !!!cp ('t91');
4166            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4167    
4168            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4169            } else {
4170              !!!cp ('t92');
4171          }          }
4172    
4173              ## "after head" insertion mode          ## "after head" insertion mode
4174              ## As if <body>          ## As if <body>
4175              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4176              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4177              ## reprocess          ## reprocess
4178              redo B;          next B;
4179            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4180              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4181                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4183                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4184                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4185                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4186                  !!!next-token;              push @{$self->{open_elements}},
4187                  redo B;                  [$self->{head_element}, $el_category->{head}];
4188                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4189                  #              !!!nack ('t93.1');
4190                } else {              !!!next-token;
4191                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4192                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4193                  !!!next-token;              !!!cp ('t93.2');
4194                  redo B;              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4195                }              ## Ignore the token
4196              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t93.3');
4197                ## As if <head>              !!!next-token;
4198                !!!create-element ($self->{head_element}, 'head');              next B;
4199                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            } else {
4200                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!cp ('t95');
4201                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4202                ## Ignore the token
4203                !!!nack ('t95.1');
4204                !!!next-token;
4205                next B;
4206              }
4207            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4208              !!!cp ('t96');
4209              ## As if <head>
4210              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4211              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4212              push @{$self->{open_elements}},
4213                  [$self->{head_element}, $el_category->{head}];
4214    
4215                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4216                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4217              }          } else {
4218              !!!cp ('t97');
4219            }
4220    
4221              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4222                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4223                    !!!cp ('t98');
4224                  ## As if </noscript>                  ## As if </noscript>
4225                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4226                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4227                                
4228                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4229                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4230                  } else {
4231                    !!!cp ('t99');
4232                }                }
4233    
4234                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4235                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4236                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4237                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4238                    push @{$self->{open_elements}},
4239                        [$self->{head_element}, $el_category->{head}];
4240                  } else {
4241                    !!!cp ('t101');
4242                }                }
4243                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4244                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4245                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4246                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4247                  !!!nack ('t101.1');
4248                !!!next-token;                !!!next-token;
4249                redo B;                next B;
4250              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4251                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4252                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4253                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4254                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4255                    push @{$self->{open_elements}},
4256                        [$self->{head_element}, $el_category->{head}];
4257                  } else {
4258                    !!!cp ('t103');
4259                }                }
4260                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4261                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4262                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4263                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4264                  !!!ack ('t103.1');
4265                !!!next-token;                !!!next-token;
4266                redo B;                next B;
4267              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4268                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4269                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4270                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4271                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4272                    push @{$self->{open_elements}},
4273                        [$self->{head_element}, $el_category->{head}];
4274                  } else {
4275                    !!!cp ('t105');
4276                }                }
4277                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4278                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.
4279    
4280                unless ($self->{confident}) {                unless ($self->{confident}) {
4281                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4282                      !!!cp ('t106');
4283                      ## NOTE: Whether the encoding is supported or not is handled
4284                      ## in the {change_encoding} callback.
4285                    $self->{change_encoding}                    $self->{change_encoding}
4286                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4287                             $token);
4288                      
4289                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4290                          ->set_user_data (manakai_has_reference =>
4291                                               $token->{attributes}->{charset}
4292                                                   ->{has_reference});
4293                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4294                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4295                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4296                              [\x09-\x0D\x20]*=
4297                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4298                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4299                        !!!cp ('t107');
4300                        ## NOTE: Whether the encoding is supported or not is handled
4301                        ## in the {change_encoding} callback.
4302                      $self->{change_encoding}                      $self->{change_encoding}
4303                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4304                               $token);
4305                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4306                            ->set_user_data (manakai_has_reference =>
4307                                                 $token->{attributes}->{content}
4308                                                       ->{has_reference});
4309                      } else {
4310                        !!!cp ('t108');
4311                    }                    }
4312                  }                  }
4313                  } else {
4314                    if ($token->{attributes}->{charset}) {
4315                      !!!cp ('t109');
4316                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4317                          ->set_user_data (manakai_has_reference =>
4318                                               $token->{attributes}->{charset}
4319                                                   ->{has_reference});
4320                    }
4321                    if ($token->{attributes}->{content}) {
4322                      !!!cp ('t110');
4323                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4324                          ->set_user_data (manakai_has_reference =>
4325                                               $token->{attributes}->{content}
4326                                                   ->{has_reference});
4327                    }
4328                }                }
4329    
4330                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4331                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4332                  !!!ack ('t110.1');
4333                !!!next-token;                !!!next-token;
4334                redo B;                next B;
4335              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4336                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4337                    !!!cp ('t111');
4338                  ## As if </noscript>                  ## As if </noscript>
4339                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4340                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4341                                
4342                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4343                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4344                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4345                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4346                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4347                    push @{$self->{open_elements}},
4348                        [$self->{head_element}, $el_category->{head}];
4349                  } else {
4350                    !!!cp ('t113');
4351                }                }
4352    
4353                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4354                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4355                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4356                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4357                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4358                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4359                redo B;                next B;
4360              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4361                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4362                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4363                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4364                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4365                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4366                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4367                    push @{$self->{open_elements}},
4368                        [$self->{head_element}, $el_category->{head}];
4369                  } else {
4370                    !!!cp ('t115');
4371                }                }
4372                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4373                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4374                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4375                redo B;                next B;
4376              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4377                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4378                    !!!cp ('t116');
4379                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4380                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4381                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4382                    !!!nack ('t116.1');
4383                  !!!next-token;                  !!!next-token;
4384                  redo B;                  next B;
4385                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4387                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4388                  ## Ignore the token                  ## Ignore the token
4389                    !!!nack ('t117.1');
4390                  !!!next-token;                  !!!next-token;
4391                  redo B;                  next B;
4392                } else {                } else {
4393                    !!!cp ('t118');
4394                  #                  #
4395                }                }
4396              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4397                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4398                    !!!cp ('t119');
4399                  ## As if </noscript>                  ## As if </noscript>
4400                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4401                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4402                                
4403                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4404                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4405                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4406                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4407                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4408                    push @{$self->{open_elements}},
4409                        [$self->{head_element}, $el_category->{head}];
4410                  } else {
4411                    !!!cp ('t121');
4412                }                }
4413    
4414                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4415                $script_start_tag->($insert_to_current);                $script_start_tag->();
4416                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4417                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4418                redo B;                next B;
4419              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4420                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4421                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4422                    !!!cp ('t122');
4423                  ## As if </noscript>                  ## As if </noscript>
4424                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4425                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4426                                    
4427                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4428                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 4430  sub _tree_construction_main ($) {
4430                                    
4431                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4432                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4433                    !!!cp ('t124');
4434                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4435                                    
4436                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4437                  } else {
4438                    !!!cp ('t125');
4439                }                }
4440    
4441                ## "after head" insertion mode                ## "after head" insertion mode
4442                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4443                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4444                    !!!cp ('t126');
4445                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4446                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4447                    !!!cp ('t127');
4448                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4449                } else {                } else {
4450                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4451                }                }
4452                  !!!nack ('t127.1');
4453                !!!next-token;                !!!next-token;
4454                redo B;                next B;
4455              } else {              } else {
4456                  !!!cp ('t128');
4457                #                #
4458              }              }
4459    
4460              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4461                  !!!cp ('t129');
4462                ## As if </noscript>                ## As if </noscript>
4463                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4464                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4465                                
4466                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4467                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 4469  sub _tree_construction_main ($) {
4469    
4470                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4471              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4472                  !!!cp ('t130');
4473                ## As if </head>                ## As if </head>
4474                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4475    
4476                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4477                } else {
4478                  !!!cp ('t131');
4479              }              }
4480    
4481              ## "after head" insertion mode              ## "after head" insertion mode
4482              ## As if <body>              ## As if <body>
4483              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4484              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4485              ## reprocess              ## reprocess
4486              redo B;              !!!ack-later;
4487                next B;
4488            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4489              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4490                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4491                    !!!cp ('t132');
4492                  ## As if <head>                  ## As if <head>
4493                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4494                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4495                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4496                        [$self->{head_element}, $el_category->{head}];
4497    
4498                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4499                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4500                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4501                  !!!next-token;                  !!!next-token;
4502                  redo B;                  next B;
4503                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                    !!!cp ('t133');
4505                  ## As if </noscript>                  ## As if </noscript>
4506                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4507                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4508                                    
4509                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4510                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4511                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4512                  !!!next-token;                  !!!next-token;
4513                  redo B;                  next B;
4514                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4515                    !!!cp ('t134');
4516                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4517                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4518                  !!!next-token;                  !!!next-token;
4519                  redo B;                  next B;
4520                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4521                    !!!cp ('t134.1');
4522                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4523                    ## Ignore the token
4524                    !!!next-token;
4525                    next B;
4526                } else {                } else {
4527                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4528                }                }
4529              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4530                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4531                    !!!cp ('t136');
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4534                  !!!next-token;                  !!!next-token;
4535                  redo B;                  next B;
4536                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4537                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4538                    !!!cp ('t137');
4539                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4540                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                } else {                } else {
4544                    !!!cp ('t138');
4545                  #                  #
4546                }                }
4547              } elsif ({              } elsif ({
4548                        body => 1, html => 1,                        body => 1, html => 1,
4549                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4550                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4551                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4552                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4553                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4554                  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});  
4555                  ## Ignore the token                  ## Ignore the token
4556                  !!!next-token;                  !!!next-token;
4557                  redo B;                  next B;
4558                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4559                    !!!cp ('t140.1');
4560                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4561                    ## Ignore the token
4562                    !!!next-token;
4563                    next B;
4564                  } else {
4565                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4566                }                }
4567                              } elsif ($token->{tag_name} eq 'p') {
4568                #                !!!cp ('t142');
4569              } elsif ({                !!!parse-error (type => 'unmatched end tag:p', token => $token);
4570                        p => 1, br => 1,                ## Ignore the token
4571                       }->{$token->{tag_name}}) {                !!!next-token;
4572                  next B;
4573                } elsif ($token->{tag_name} eq 'br') {
4574                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575                  ## As if <head>                  !!!cp ('t142.2');
4576                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4577                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4579                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4580      
4581                    ## Reprocess in the "after head" insertion mode...
4582                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4583                    !!!cp ('t143.2');
4584                    ## As if </head>
4585                    pop @{$self->{open_elements}};
4586                    $self->{insertion_mode} = AFTER_HEAD_IM;
4587      
4588                    ## Reprocess in the "after head" insertion mode...
4589                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4590                    !!!cp ('t143.3');
4591                    ## ISSUE: Two parse errors for <head><noscript></br>
4592                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4593                    ## As if </noscript>
4594                    pop @{$self->{open_elements}};
4595                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4596    
4597                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4598                }                  ## As if </head>
4599                    pop @{$self->{open_elements}};
4600                    $self->{insertion_mode} = AFTER_HEAD_IM;
4601    
4602                #                  ## Reprocess in the "after head" insertion mode...
4603              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4604                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4605                  #                  #
4606                } else {                } else {
4607                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4608                }                }
4609    
4610                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4611                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4612                  ## Ignore the token
4613                  !!!next-token;
4614                  next B;
4615                } else {
4616                  !!!cp ('t145');
4617                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4618                  ## Ignore the token
4619                  !!!next-token;
4620                  next B;
4621              }              }
4622    
4623              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4624                  !!!cp ('t146');
4625                ## As if </noscript>                ## As if </noscript>
4626                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4627                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4628                                
4629                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4630                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 4632  sub _tree_construction_main ($) {
4632    
4633                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4634              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4635                  !!!cp ('t147');
4636                ## As if </head>                ## As if </head>
4637                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4638    
4639                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4640              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4641                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4642                  !!!cp ('t148');
4643                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4644                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4645                !!!next-token;                !!!next-token;
4646                redo B;                next B;
4647                } else {
4648                  !!!cp ('t149');
4649              }              }
4650    
4651              ## "after head" insertion mode              ## "after head" insertion mode
4652              ## As if <body>              ## As if <body>
4653              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4654              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4655              ## reprocess              ## reprocess
4656              redo B;              next B;
4657            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4658              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4659            }            !!!cp ('t149.1');
4660    
4661              ## NOTE: As if <head>
4662              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663              $self->{open_elements}->[-1]->[0]->append_child
4664                  ($self->{head_element});
4665              #push @{$self->{open_elements}},
4666              #    [$self->{head_element}, $el_category->{head}];
4667              #$self->{insertion_mode} = IN_HEAD_IM;
4668              ## NOTE: Reprocess.
4669    
4670              ## NOTE: As if </head>
4671              #pop @{$self->{open_elements}};
4672              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4673              ## NOTE: Reprocess.
4674              
4675              #
4676            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4677              !!!cp ('t149.2');
4678    
4679              ## NOTE: As if </head>
4680              pop @{$self->{open_elements}};
4681              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4682              ## NOTE: Reprocess.
4683    
4684              #
4685            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4686              !!!cp ('t149.3');
4687    
4688              !!!parse-error (type => 'in noscript:#eof', token => $token);
4689    
4690              ## As if </noscript>
4691              pop @{$self->{open_elements}};
4692              #$self->{insertion_mode} = IN_HEAD_IM;
4693              ## NOTE: Reprocess.
4694    
4695              ## NOTE: As if </head>
4696              pop @{$self->{open_elements}};
4697              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4698              ## NOTE: Reprocess.
4699    
4700              #
4701            } else {
4702              !!!cp ('t149.4');
4703              #
4704            }
4705    
4706            ## NOTE: As if <body>
4707            !!!insert-element ('body',, $token);
4708            $self->{insertion_mode} = IN_BODY_IM;
4709            ## NOTE: Reprocess.
4710            next B;
4711          } else {
4712            die "$0: $token->{type}: Unknown token type";
4713          }
4714    
4715            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4716      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4717            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4718                !!!cp ('t150');
4719              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4720              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4721                            
4722              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4723    
4724              !!!next-token;              !!!next-token;
4725              redo B;              next B;
4726            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4727              if ({              if ({
4728                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3148  sub _tree_construction_main ($) { Line 4730  sub _tree_construction_main ($) {
4730                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4731                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4732                  ## have an element in table scope                  ## have an element in table scope
4733                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4734                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4735                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4736                      $tn = $node->[1];                      !!!cp ('t151');
4737                      last INSCOPE;  
4738                    } elsif ({                      ## Close the cell
4739                              table => 1, html => 1,                      !!!back-token; # <x>
4740                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4741                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4742                    }                                line => $token->{line},
4743                  } # INSCOPE                                column => $token->{column}};
4744                    unless (defined $tn) {                      next B;
4745                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4746                      ## Ignore the token                      !!!cp ('t152');
4747                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4748                      redo B;                      last;
4749                    }                    }
4750                                    }
4751                  ## Close the cell  
4752                  !!!back-token; # <?>                  !!!cp ('t153');
4753                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4754                  redo B;                      value => $token->{tag_name}, token => $token);
4755                    ## Ignore the token
4756                    !!!nack ('t153.1');
4757                    !!!next-token;
4758                    next B;
4759                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4760                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4761                                    
4762                  ## As if </caption>                  ## NOTE: As if </caption>.
4763                  ## have a table element in table scope                  ## have a table element in table scope
4764                  my $i;                  my $i;
4765                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4766                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4767                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4768                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4769                      last INSCOPE;                        !!!cp ('t155');
4770                    } elsif ({                        $i = $_;
4771                              table => 1, html => 1,                        last INSCOPE;
4772                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4773                      last INSCOPE;                        !!!cp ('t156');
4774                          last;
4775                        }
4776                    }                    }
4777    
4778                      !!!cp ('t157');
4779                      !!!parse-error (type => 'start tag not allowed',
4780                                      value => $token->{tag_name}, token => $token);
4781                      ## Ignore the token
4782                      !!!nack ('t157.1');
4783                      !!!next-token;
4784                      next B;
4785                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4786                                    
4787                  ## generate implied end tags                  ## generate implied end tags
4788                  if ({                  while ($self->{open_elements}->[-1]->[1]
4789                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4790                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4791                       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;  
4792                  }                  }
4793    
4794                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4795                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4796                      !!!parse-error (type => 'not closed',
4797                                      value => $self->{open_elements}->[-1]->[0]
4798                                          ->manakai_local_name,
4799                                      token => $token);
4800                    } else {
4801                      !!!cp ('t160');
4802                  }                  }
4803                                    
4804                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3220  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4809                                    
4810                  ## reprocess                  ## reprocess
4811                  redo B;                  !!!ack-later;
4812                    next B;
4813                } else {                } else {
4814                    !!!cp ('t161');
4815                  #                  #
4816                }                }
4817              } else {              } else {
4818                  !!!cp ('t162');
4819                #                #
4820              }              }
4821            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3234  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825                  my $i;                  my $i;
4826                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4827                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4828                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                        !!!cp ('t163');
4830                      $i = $_;                      $i = $_;
4831                      last INSCOPE;                      last INSCOPE;
4832                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4833                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4834                      last INSCOPE;                      last INSCOPE;
4835                    }                    }
4836                  } # INSCOPE                  } # INSCOPE
4837                    unless (defined $i) {                    unless (defined $i) {
4838                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4839                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4840                      ## Ignore the token                      ## Ignore the token
4841                      !!!next-token;                      !!!next-token;
4842                      redo B;                      next B;
4843                    }                    }
4844                                    
4845                  ## generate implied end tags                  ## generate implied end tags
4846                  if ({                  while ($self->{open_elements}->[-1]->[1]
4847                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4848                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4849                       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;  
4850                  }                  }
4851                    
4852                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4853                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4854                      !!!cp ('t167');
4855                      !!!parse-error (type => 'not closed',
4856                                      value => $self->{open_elements}->[-1]->[0]
4857                                          ->manakai_local_name,
4858                                      token => $token);
4859                    } else {
4860                      !!!cp ('t168');
4861                  }                  }
4862                                    
4863                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3275  sub _tree_construction_main ($) { Line 4867  sub _tree_construction_main ($) {
4867                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4868                                    
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4872                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4873                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4874                  ## Ignore the token                  ## Ignore the token
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } else {                } else {
4878                    !!!cp ('t170');
4879                  #                  #
4880                }                }
4881              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4882                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4883                  ## have a table element in table scope                  ## have a table element in table scope
4884                  my $i;                  my $i;
4885                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4886                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4887                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4888                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4889                      last INSCOPE;                        !!!cp ('t171');
4890                    } elsif ({                        $i = $_;
4891                              table => 1, html => 1,                        last INSCOPE;
4892                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4893                      last INSCOPE;                        !!!cp ('t172');
4894                          last;
4895                        }
4896                    }                    }
4897    
4898                      !!!cp ('t173');
4899                      !!!parse-error (type => 'unmatched end tag',
4900                                      value => $token->{tag_name}, token => $token);
4901                      ## Ignore the token
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4909                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4910                       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;  
4911                  }                  }
4912                                    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4915                      !!!parse-error (type => 'not closed',
4916                                      value => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                    } else {
4920                      !!!cp ('t176');
4921                  }                  }
4922                                    
4923                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3329  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  !!!next-token;                  !!!next-token;
4930                  redo B;                  next B;
4931                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4932                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4933                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4934                  ## Ignore the token                  ## Ignore the token
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                    !!!cp ('t178');
4939                  #                  #
4940                }                }
4941              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946                ## have an element in table scope                ## have an element in table scope
4947                my $i;                my $i;
4948                my $tn;                my $tn;
4949                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4950                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4951                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4952                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4953                    last INSCOPE;                      !!!cp ('t179');
4954                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4955                    $tn = $node->[1];  
4956                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4957                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4958                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4959                            table => 1, html => 1,                                line => $token->{line},
4960                           }->{$node->[1]}) {                                column => $token->{column}};
4961                    last INSCOPE;                      next B;
4962                      } elsif ($node->[1] & TABLE_CELL_EL) {
4963                        !!!cp ('t180');
4964                        $tn = $node->[0]->manakai_local_name;
4965                        ## NOTE: There is exactly one |td| or |th| element
4966                        ## in scope in the stack of open elements by definition.
4967                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4968                        ## ISSUE: Can this be reached?
4969                        !!!cp ('t181');
4970                        last;
4971                      }
4972                  }                  }
4973                } # INSCOPE  
4974                unless (defined $i) {                  !!!cp ('t182');
4975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4976                        value => $token->{tag_name}, token => $token);
4977                  ## Ignore the token                  ## Ignore the token
4978                  !!!next-token;                  !!!next-token;
4979                  redo B;                  next B;
4980                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4981              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4982                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4983                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4984    
4985                ## As if </caption>                ## As if </caption>
4986                ## have a table element in table scope                ## have a table element in table scope
4987                my $i;                my $i;
4988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4990                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4991                      !!!cp ('t184');
4992                    $i = $_;                    $i = $_;
4993                    last INSCOPE;                    last INSCOPE;
4994                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4995                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4996                    last INSCOPE;                    last INSCOPE;
4997                  }                  }
4998                } # INSCOPE                } # INSCOPE
4999                unless (defined $i) {                unless (defined $i) {
5000                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5001                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5002                  ## Ignore the token                  ## Ignore the token
5003                  !!!next-token;                  !!!next-token;
5004                  redo B;                  next B;
5005                }                }
5006                                
5007                ## generate implied end tags                ## generate implied end tags
5008                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5009                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5010                     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;  
5011                }                }
5012    
5013                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5014                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5015                    !!!parse-error (type => 'not closed',
5016                                    value => $self->{open_elements}->[-1]->[0]
5017                                        ->manakai_local_name,
5018                                    token => $token);
5019                  } else {
5020                    !!!cp ('t189');
5021                }                }
5022    
5023                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3422  sub _tree_construction_main ($) { Line 5027  sub _tree_construction_main ($) {
5027                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5028    
5029                ## reprocess                ## reprocess
5030                redo B;                next B;
5031              } elsif ({              } elsif ({
5032                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5033                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5034                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5035                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5036                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5037                  ## Ignore the token                  ## Ignore the token
5038                  !!!next-token;                  !!!next-token;
5039                  redo B;                  next B;
5040                } else {                } else {
5041                    !!!cp ('t191');
5042                  #                  #
5043                }                }
5044              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 5046  sub _tree_construction_main ($) {
5046                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5047                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5048                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5049                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5050                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5051                ## Ignore the token                ## Ignore the token
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054              } else {              } else {
5055                  !!!cp ('t193');
5056                #                #
5057              }              }
5058          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5059            for my $entry (@{$self->{open_elements}}) {
5060              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5061                !!!cp ('t75');
5062                !!!parse-error (type => 'in body:#eof', token => $token);
5063                last;
5064              }
5065            }
5066    
5067            ## Stop parsing.
5068            last B;
5069        } else {        } else {
5070          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5071        }        }
# Line 3454  sub _tree_construction_main ($) { Line 5074  sub _tree_construction_main ($) {
5074        #        #
5075      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5076        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5077              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5078                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5079              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5080                                
5081                unless (length $token->{data}) {            unless (length $token->{data}) {
5082                  !!!next-token;              !!!cp ('t194');
5083                  redo B;              !!!next-token;
5084                }              next B;
5085              }            } else {
5086                !!!cp ('t195');
5087              }
5088            }
5089    
5090              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5091    
5092              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5093              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3471  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095              ## result in a new Text node.              ## result in a new Text node.
5096              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5097                            
5098              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]}) {  
5099                # MUST                # MUST
5100                my $foster_parent_element;                my $foster_parent_element;
5101                my $next_sibling;                my $next_sibling;
5102                my $prev_sibling;                my $prev_sibling;
5103                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5104                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5105                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5106                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5107                        !!!cp ('t196');
5108                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5109                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5110                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5111                    } else {                    } else {
5112                        !!!cp ('t197');
5113                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5114                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5115                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 5121  sub _tree_construction_main ($) {
5121                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5122                if (defined $prev_sibling and                if (defined $prev_sibling and
5123                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5124                    !!!cp ('t198');
5125                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5126                } else {                } else {
5127                    !!!cp ('t199');
5128                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5129                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5130                     $next_sibling);                     $next_sibling);
5131                }                }
5132              } else {            $open_tables->[-1]->[1] = 1; # tainted
5133                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5134              }            !!!cp ('t200');
5135              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5136            }
5137                            
5138              !!!next-token;          !!!next-token;
5139              redo B;          next B;
5140        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5141              if ({              if ({
5142                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3517  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5145                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5146                  ## Clear back to table context                  ## Clear back to table context
5147                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5148                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5149                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5150                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5151                  }                  }
5152                                    
5153                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5154                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5155                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5156                }                }
5157    
5158                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5159                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5160                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5161                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5162                  }                  }
5163                                    
5164                  ## Clear back to table body context                  ## Clear back to table body context
5165                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5166                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5167                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5168                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5169                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5170                  }                  }
5171                                    
5172                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5173                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5174                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5175                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5176                      !!!nack ('t204');
5177                    !!!next-token;                    !!!next-token;
5178                    redo B;                    next B;
5179                  } else {                  } else {
5180                    !!!insert-element ('tr');                    !!!cp ('t205');
5181                      !!!insert-element ('tr',, $token);
5182                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5183                  }                  }
5184                  } else {
5185                    !!!cp ('t206');
5186                }                }
5187    
5188                ## Clear back to table row context                ## Clear back to table row context
5189                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5190                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5191                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5192                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5193                }                }
5194                                
5195                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5196                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5197    
5198                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5199                                
5200                  !!!nack ('t207.1');
5201                !!!next-token;                !!!next-token;
5202                redo B;                next B;
5203              } elsif ({              } elsif ({
5204                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5205                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3578  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                  my $i;                  my $i;
5212                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5214                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5215                        !!!cp ('t208');
5216                      $i = $_;                      $i = $_;
5217                      last INSCOPE;                      last INSCOPE;
5218                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5219                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5220                      last INSCOPE;                      last INSCOPE;
5221                    }                    }
5222                  } # INSCOPE                  } # INSCOPE
5223                  unless (defined $i) {                  unless (defined $i) {
5224                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5225    ## TODO: This type is wrong.
5226                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5227                    ## Ignore the token                    ## Ignore the token
5228                      !!!nack ('t210.1');
5229                    !!!next-token;                    !!!next-token;
5230                    redo B;                    next B;
5231                  }                  }
5232                                    
5233                  ## Clear back to table row context                  ## Clear back to table row context
5234                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5235                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5236                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5237                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5238                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5239                  }                  }
5240                                    
5241                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5242                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5243                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5244                      !!!cp ('t212');
5245                    ## reprocess                    ## reprocess
5246                    redo B;                    !!!ack-later;
5247                      next B;
5248                  } else {                  } else {
5249                      !!!cp ('t213');
5250                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5251                  }                  }
5252                }                }
# Line 3617  sub _tree_construction_main ($) { Line 5256  sub _tree_construction_main ($) {
5256                  my $i;                  my $i;
5257                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5258                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5259                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5260                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5261                      $i = $_;                      $i = $_;
5262                      last INSCOPE;                      last INSCOPE;
5263                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5264                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5265                      last INSCOPE;                      last INSCOPE;
5266                    }                    }
5267                  } # INSCOPE                  } # INSCOPE
5268                  unless (defined $i) {                  unless (defined $i) {
5269                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5270    ## TODO: This erorr type ios wrong.
5271                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5272                    ## Ignore the token                    ## Ignore the token
5273                      !!!nack ('t216.1');
5274                    !!!next-token;                    !!!next-token;
5275                    redo B;                    next B;
5276                  }                  }
5277    
5278                  ## Clear back to table body context                  ## Clear back to table body context
5279                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5280                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5281                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5282                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5283                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5284                  }                  }
5285                                    
# Line 3653  sub _tree_construction_main ($) { Line 5293  sub _tree_construction_main ($) {
5293                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5294                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5295                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5296                  } else {
5297                    !!!cp ('t218');
5298                }                }
5299    
5300                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5301                  ## Clear back to table context                  ## Clear back to table context
5302                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5303                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5304                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5305                      ## ISSUE: Can this state be reached?
5306                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5307                  }                  }
5308                                    
5309                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5310                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5311                  ## reprocess                  ## reprocess
5312                  redo B;                  !!!ack-later;
5313                    next B;
5314                } elsif ({                } elsif ({
5315                          caption => 1,                          caption => 1,
5316                          colgroup => 1,                          colgroup => 1,
5317                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5318                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5319                  ## Clear back to table context                  ## Clear back to table context
5320                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5321                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5322                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5323                      ## ISSUE: Can this state be reached?
5324                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5325                  }                  }
5326                                    
5327                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5328                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5329                                    
5330                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5331                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5332                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5333                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3691  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5337                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5338                  !!!next-token;                  !!!next-token;
5339                  redo B;                  !!!nack ('t220.1');
5340                    next B;
5341                } else {                } else {
5342                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5343                }                }
5344              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5345                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5346                                  value => $self->{open_elements}->[-1]->[0]
5347                                      ->manakai_local_name,
5348                                  token => $token);
5349    
5350                ## As if </table>                ## As if </table>
5351                ## have a table element in table scope                ## have a table element in table scope
5352                my $i;                my $i;
5353                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5354                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5355                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5356                      !!!cp ('t221');
5357                    $i = $_;                    $i = $_;
5358                    last INSCOPE;                    last INSCOPE;
5359                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5360                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5361                    last INSCOPE;                    last INSCOPE;
5362                  }                  }
5363                } # INSCOPE                } # INSCOPE
5364                unless (defined $i) {                unless (defined $i) {
5365                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5366    ## TODO: The following is wrong, maybe.
5367                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5368                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5369                    !!!nack ('t223.1');
5370                  !!!next-token;                  !!!next-token;
5371                  redo B;                  next B;
5372                }                }
5373                                
5374    ## TODO: Followings are removed from the latest spec.
5375                ## generate implied end tags                ## generate implied end tags
5376                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5377                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5378                     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;  
5379                }                }
5380    
5381                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5382                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5383                    ## NOTE: |<table><tr><table>|
5384                    !!!parse-error (type => 'not closed',
5385                                    value => $self->{open_elements}->[-1]->[0]
5386                                        ->manakai_local_name,
5387                                    token => $token);
5388                  } else {
5389                    !!!cp ('t226');
5390                }                }
5391    
5392                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5393                  pop @{$open_tables};
5394    
5395                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5396    
5397                ## reprocess            ## reprocess
5398                redo B;            !!!ack-later;
5399          } else {            next B;
5400            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5401              if (not $open_tables->[-1]->[1]) { # tainted
5402                !!!cp ('t227.8');
5403                ## NOTE: This is a "as if in head" code clone.
5404                $parse_rcdata->(CDATA_CONTENT_MODEL);
5405                next B;
5406              } else {
5407                !!!cp ('t227.7');
5408                #
5409              }
5410            } elsif ($token->{tag_name} eq 'script') {
5411              if (not $open_tables->[-1]->[1]) { # tainted
5412                !!!cp ('t227.6');
5413                ## NOTE: This is a "as if in head" code clone.
5414                $script_start_tag->();
5415                next B;
5416              } else {
5417                !!!cp ('t227.5');
5418                #
5419              }
5420            } elsif ($token->{tag_name} eq 'input') {
5421              if (not $open_tables->[-1]->[1]) { # tainted
5422                if ($token->{attributes}->{type}) { ## TODO: case
5423                  my $type = lc $token->{attributes}->{type}->{value};
5424                  if ($type eq 'hidden') {
5425                    !!!cp ('t227.3');
5426                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5427    
5428            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5429    
5430                    ## TODO: form element pointer
5431    
5432                    pop @{$self->{open_elements}};
5433    
5434                    !!!next-token;
5435                    !!!ack ('t227.2.1');
5436                    next B;
5437                  } else {
5438                    !!!cp ('t227.2');
5439                    #
5440                  }
5441                } else {
5442                  !!!cp ('t227.1');
5443                  #
5444                }
5445              } else {
5446                !!!cp ('t227.4');
5447                #
5448              }
5449            } else {
5450              !!!cp ('t227');
5451            #            #
5452          }          }
5453    
5454            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5455    
5456            $insert = $insert_to_foster;
5457            #
5458        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5459              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5460                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3756  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                my $i;                my $i;
5463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5464                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5465                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5466                      !!!cp ('t228');
5467                    $i = $_;                    $i = $_;
5468                    last INSCOPE;                    last INSCOPE;
5469                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5470                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5471                    last INSCOPE;                    last INSCOPE;
5472                  }                  }
5473                } # INSCOPE                } # INSCOPE
5474                unless (defined $i) {                unless (defined $i) {
5475                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5476                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                  ## Ignore the token                  ## Ignore the token
5478                    !!!nack ('t230.1');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                  } else {
5482                    !!!cp ('t232');
5483                }                }
5484    
5485                ## Clear back to table row context                ## Clear back to table row context
5486                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5487                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5488                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5489                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5490                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5491                }                }
5492    
5493                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5494                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5495                !!!next-token;                !!!next-token;
5496                redo B;                !!!nack ('t231.1');
5497                  next B;
5498              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5499                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5500                  ## As if </tr>                  ## As if </tr>
# Line 3791  sub _tree_construction_main ($) { Line 5502  sub _tree_construction_main ($) {
5502                  my $i;                  my $i;
5503                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5504                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5505                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5506                        !!!cp ('t233');
5507                      $i = $_;                      $i = $_;
5508                      last INSCOPE;                      last INSCOPE;
5509                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5510                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5511                      last INSCOPE;                      last INSCOPE;
5512                    }                    }
5513                  } # INSCOPE                  } # INSCOPE
5514                  unless (defined $i) {                  unless (defined $i) {
5515                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5516    ## TODO: The following is wrong.
5517                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5518                    ## Ignore the token                    ## Ignore the token
5519                      !!!nack ('t236.1');
5520                    !!!next-token;                    !!!next-token;
5521                    redo B;                    next B;
5522                  }                  }
5523                                    
5524                  ## Clear back to table row context                  ## Clear back to table row context
5525                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5526                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5527                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5528                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5529                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5530                  }                  }
5531                                    
# Line 3825  sub _tree_construction_main ($) { Line 5539  sub _tree_construction_main ($) {
5539                  my $i;                  my $i;
5540                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5541                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5542                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5543                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5544                      $i = $_;                      $i = $_;
5545                      last INSCOPE;                      last INSCOPE;
5546                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5547                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5548                      last INSCOPE;                      last INSCOPE;
5549                    }                    }
5550                  } # INSCOPE                  } # INSCOPE
5551                  unless (defined $i) {                  unless (defined $i) {
5552                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5553                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5554                    ## Ignore the token                    ## Ignore the token
5555                      !!!nack ('t239.1');
5556                    !!!next-token;                    !!!next-token;
5557                    redo B;                    next B;
5558                  }                  }
5559                                    
5560                  ## Clear back to table body context                  ## Clear back to table body context
5561                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5562                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5563                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5564                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5565                  }                  }
5566                                    
# Line 3863  sub _tree_construction_main ($) { Line 5576  sub _tree_construction_main ($) {
5576                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5577                }                }
5578    
5579                  ## NOTE: </table> in the "in table" insertion mode.
5580                  ## When you edit the code fragment below, please ensure that
5581                  ## the code for <table> in the "in table" insertion mode
5582                  ## is synced with it.
5583    
5584                ## have a table element in table scope                ## have a table element in table scope
5585                my $i;                my $i;
5586                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5587                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5588                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5589                      !!!cp ('t241');
5590                    $i = $_;                    $i = $_;
5591                    last INSCOPE;                    last INSCOPE;
5592                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5593                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5594                    last INSCOPE;                    last INSCOPE;
5595                  }                  }
5596                } # INSCOPE                } # INSCOPE
5597                unless (defined $i) {                unless (defined $i) {
5598                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5599                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                    !!!nack ('t243.1');
5602                  !!!next-token;                  !!!next-token;
5603                  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]);  
5604                }                }
5605                                    
5606                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5607                  pop @{$open_tables};
5608                                
5609                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5610                                
5611                !!!next-token;                !!!next-token;
5612                redo B;                next B;
5613              } elsif ({              } elsif ({
5614                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5615                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3914  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619                  my $i;                  my $i;
5620                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5621                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5622                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5623                        !!!cp ('t247');
5624                      $i = $_;                      $i = $_;
5625                      last INSCOPE;                      last INSCOPE;
5626                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5627                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5628                      last INSCOPE;                      last INSCOPE;
5629                    }                    }
5630                  } # INSCOPE                  } # INSCOPE
5631                    unless (defined $i) {                    unless (defined $i) {
5632                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5633                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                      ## Ignore the token                      ## Ignore the token
5635                        !!!nack ('t249.1');
5636                      !!!next-token;                      !!!next-token;
5637                      redo B;                      next B;
5638                    }                    }
5639                                    
5640                  ## As if </tr>                  ## As if </tr>
# Line 3935  sub _tree_construction_main ($) { Line 5642  sub _tree_construction_main ($) {
5642                  my $i;                  my $i;
5643                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5644                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5645                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5646                        !!!cp ('t250');
5647                      $i = $_;                      $i = $_;
5648                      last INSCOPE;                      last INSCOPE;
5649                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5650                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5651                      last INSCOPE;                      last INSCOPE;
5652                    }                    }
5653                  } # INSCOPE                  } # INSCOPE
5654                    unless (defined $i) {                    unless (defined $i) {
5655                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5656                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5657                      ## Ignore the token                      ## Ignore the token
5658                        !!!nack ('t252.1');
5659                      !!!next-token;                      !!!next-token;
5660                      redo B;                      next B;
5661                    }                    }
5662                                    
5663                  ## Clear back to table row context                  ## Clear back to table row context
5664                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5665                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5666                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5667                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5668                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5669                  }                  }
5670                                    
# Line 3968  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677                my $i;                my $i;
5678                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5679                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5680                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5681                      !!!cp ('t254');
5682                    $i = $_;                    $i = $_;
5683                    last INSCOPE;                    last INSCOPE;
5684                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5685                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5686                    last INSCOPE;                    last INSCOPE;
5687                  }                  }
5688                } # INSCOPE                } # INSCOPE
5689                unless (defined $i) {                unless (defined $i) {
5690                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5691                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                    !!!nack ('t256.1');
5694                  !!!next-token;                  !!!next-token;
5695                  redo B;                  next B;
5696                }                }
5697    
5698                ## Clear back to table body context                ## Clear back to table body context
5699                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5700                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5701                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5702                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5704                }                }
5705    
5706                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5707                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5708                  !!!nack ('t257.1');
5709                !!!next-token;                !!!next-token;
5710                redo B;                next B;
5711              } elsif ({              } elsif ({
5712                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5713                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5714                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5715                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5716                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5717                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5718                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5719                !!!next-token;            ## Ignore the token
5720                redo B;            !!!nack ('t258.1');
5721               !!!next-token;
5722              next B;
5723          } else {          } else {
5724            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5725              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5726    
5727            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5728            #            #
5729          }          }
5730          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5731            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5732                    @{$self->{open_elements}} == 1) { # redundant, maybe
5733              !!!parse-error (type => 'in body:#eof', token => $token);
5734              !!!cp ('t259.1');
5735              #
5736            } else {
5737              !!!cp ('t259.2');
5738              #
5739            }
5740    
5741            ## Stop parsing
5742            last B;
5743        } else {        } else {
5744          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5745        }        }
# Line 4020  sub _tree_construction_main ($) { Line 5748  sub _tree_construction_main ($) {
5748              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5749                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5750                unless (length $token->{data}) {                unless (length $token->{data}) {
5751                    !!!cp ('t260');
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                  next B;
5754                }                }
5755              }              }
5756                            
5757                !!!cp ('t261');
5758              #              #
5759            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5760              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5761                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5762                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5764                  !!!ack ('t262.1');
5765                !!!next-token;                !!!next-token;
5766                redo B;                next B;
5767              } else {              } else {
5768                  !!!cp ('t263');
5769                #                #
5770              }              }
5771            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5772              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5773                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5774                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5775                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5776                  ## Ignore the token                  ## Ignore the token
5777                  !!!next-token;                  !!!next-token;
5778                  redo B;                  next B;
5779                } else {                } else {
5780                    !!!cp ('t265');
5781                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5782                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5783                  !!!next-token;                  !!!next-token;
5784                  redo B;                              next B;            
5785                }                }
5786              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5787                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5788                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5789                ## Ignore the token                ## Ignore the token
5790                !!!next-token;                !!!next-token;
5791                redo B;                next B;
5792              } else {              } else {
5793                  !!!cp ('t267');
5794                #                #
5795              }              }
5796            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5797              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5798            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5799              !!!cp ('t270.2');
5800              ## Stop parsing.
5801              last B;
5802            } else {
5803              ## NOTE: As if </colgroup>.
5804              !!!cp ('t270.1');
5805              pop @{$self->{open_elements}}; # colgroup
5806              $self->{insertion_mode} = IN_TABLE_IM;
5807              ## Reprocess.
5808              next B;
5809            }
5810          } else {
5811            die "$0: $token->{type}: Unknown token type";
5812          }
5813    
5814            ## As if </colgroup>            ## As if </colgroup>
5815            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5816              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5817    ## TODO: Wrong error type?
5818                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5819              ## Ignore the token              ## Ignore the token
5820                !!!nack ('t269.1');
5821              !!!next-token;              !!!next-token;
5822              redo B;              next B;
5823            } else {            } else {
5824                !!!cp ('t270');
5825              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5826              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5827                !!!ack-later;
5828              ## reprocess              ## reprocess
5829              redo B;              next B;
5830            }            }
5831      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5832        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5833            !!!cp ('t271');
5834          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5835          !!!next-token;          !!!next-token;
5836          redo B;          next B;
5837        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5838              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5839                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5840                  ## As if </option>              !!!cp ('t272');
5841                  pop @{$self->{open_elements}};              ## As if </option>
5842                }              pop @{$self->{open_elements}};
5843              } else {
5844                !!!cp ('t273');
5845              }
5846    
5847                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5848                !!!next-token;            !!!nack ('t273.1');
5849                redo B;            !!!next-token;
5850              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5851                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5852                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5853                  pop @{$self->{open_elements}};              !!!cp ('t274');
5854                }              ## As if </option>
5855                pop @{$self->{open_elements}};
5856              } else {
5857                !!!cp ('t275');
5858              }
5859    
5860                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5861                  ## As if </optgroup>              !!!cp ('t276');
5862                  pop @{$self->{open_elements}};              ## As if </optgroup>
5863                }              pop @{$self->{open_elements}};
5864              } else {
5865                !!!cp ('t277');
5866              }
5867    
5868                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5869                !!!next-token;            !!!nack ('t277.1');
5870                redo B;            !!!next-token;
5871              } elsif ($token->{tag_name} eq 'select') {            next B;
5872                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5873                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5874                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5875                my $i;                    {
5876                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5877                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5878                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5879                    $i = $_;                    }->{$token->{tag_name}})) {
5880                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5881                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5882                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5883                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5884                    last INSCOPE;            ## have an element in table scope
5885                  }            my $i;
5886                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5887                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5888                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5889                  ## Ignore the token                !!!cp ('t278');
5890                  !!!next-token;                $i = $_;
5891                  redo B;                last INSCOPE;
5892                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5893                  !!!cp ('t279');
5894                  last INSCOPE;
5895                }
5896              } # INSCOPE
5897              unless (defined $i) {
5898                !!!cp ('t280');
5899                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5900                ## Ignore the token
5901                !!!nack ('t280.1');
5902                !!!next-token;
5903                next B;
5904              }
5905                                
5906                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5907              splice @{$self->{open_elements}}, $i;
5908    
5909                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5910    
5911                !!!next-token;            if ($token->{tag_name} eq 'select') {
5912                redo B;              !!!nack ('t281.2');
5913                !!!next-token;
5914                next B;
5915              } else {
5916                !!!cp ('t281.1');
5917                !!!ack-later;
5918                ## Reprocess the token.
5919                next B;
5920              }
5921          } else {          } else {
5922            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5923              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5924            ## Ignore the token            ## Ignore the token
5925              !!!nack ('t282.1');
5926            !!!next-token;            !!!next-token;
5927            redo B;            next B;
5928          }          }
5929        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5930              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5931                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5932                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5933                  ## As if </option>              !!!cp ('t283');
5934                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5935                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5936                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5937                } else {              !!!cp ('t284');
5938                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5939                  ## Ignore the token            } else {
5940                }              !!!cp ('t285');
5941                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5942                redo B;              ## Ignore the token
5943              } elsif ($token->{tag_name} eq 'option') {            }
5944                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5945                  pop @{$self->{open_elements}};            !!!next-token;
5946                } else {            next B;
5947                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5948                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5949                }              !!!cp ('t286');
5950                !!!next-token;              pop @{$self->{open_elements}};
5951                redo B;            } else {
5952              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5953                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5954                my $i;              ## Ignore the token
5955                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5956                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5957                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5958                    $i = $_;            next B;
5959                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5960                  } elsif ({            ## have an element in table scope
5961                            table => 1, html => 1,            my $i;
5962                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5963                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5964                  }              if ($node->[1] & SELECT_EL) {
5965                } # INSCOPE                !!!cp ('t288');
5966                unless (defined $i) {                $i = $_;
5967                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5968                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5969                  !!!next-token;                !!!cp ('t289');
5970                  redo B;                last INSCOPE;
5971                }              }
5972              } # INSCOPE
5973              unless (defined $i) {
5974                !!!cp ('t290');
5975                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5976                ## Ignore the token
5977                !!!nack ('t290.1');
5978                !!!next-token;
5979                next B;
5980              }
5981                                
5982                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5983              splice @{$self->{open_elements}}, $i;
5984    
5985                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5986    
5987                !!!next-token;            !!!nack ('t291.1');
5988                redo B;            !!!next-token;
5989              } elsif ({            next B;
5990                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5991                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5992                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5993                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5994                     }->{$token->{tag_name}}) {
5995    ## TODO: The following is wrong?
5996              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5997                                
5998                ## have an element in table scope            ## have an element in table scope
5999                my $i;            my $i;
6000                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6001                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6002                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6003                    $i = $_;                !!!cp ('t292');
6004                    last INSCOPE;                $i = $_;
6005                  } elsif ({                last INSCOPE;
6006                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6007                           }->{$node->[1]}) {                !!!cp ('t293');
6008                    last INSCOPE;                last INSCOPE;
6009                  }              }
6010                } # INSCOPE            } # INSCOPE
6011                unless (defined $i) {            unless (defined $i) {
6012                  ## Ignore the token              !!!cp ('t294');
6013                  !!!next-token;              ## Ignore the token
6014                  redo B;              !!!nack ('t294.1');
6015                }              !!!next-token;
6016                next B;
6017              }
6018                                
6019                ## As if </select>            ## As if </select>
6020                ## have an element in table scope            ## have an element in table scope
6021                undef $i;            undef $i;
6022                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6023                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6024                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6025                    $i = $_;                !!!cp ('t295');
6026                    last INSCOPE;                $i = $_;
6027                  } elsif ({                last INSCOPE;
6028                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6029                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6030                    last INSCOPE;                !!!cp ('t296');
6031                  }                last INSCOPE;
6032                } # INSCOPE              }
6033                unless (defined $i) {            } # INSCOPE
6034                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6035                  ## Ignore the </select> token              !!!cp ('t297');
6036                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6037                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
6038                }              ## Ignore the </select> token
6039                !!!nack ('t297.1');
6040                !!!next-token; ## TODO: ok?
6041                next B;
6042              }
6043                                
6044                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6045              splice @{$self->{open_elements}}, $i;
6046    
6047                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6048    
6049                ## reprocess            !!!ack-later;
6050                redo B;            ## reprocess
6051              next B;
6052          } else {          } else {
6053            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6054              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6055            ## Ignore the token            ## Ignore the token
6056              !!!nack ('t299.3');
6057            !!!next-token;            !!!next-token;
6058            redo B;            next B;
6059            }
6060          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6061            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6062                    @{$self->{open_elements}} == 1) { # redundant, maybe
6063              !!!cp ('t299.1');
6064              !!!parse-error (type => 'in body:#eof', token => $token);
6065            } else {
6066              !!!cp ('t299.2');
6067          }          }
6068    
6069            ## Stop parsing.
6070            last B;
6071        } else {        } else {
6072          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6073        }        }
# Line 4257  sub _tree_construction_main ($) { Line 6081  sub _tree_construction_main ($) {
6081            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6082                        
6083            unless (length $token->{data}) {            unless (length $token->{data}) {
6084                !!!cp ('t300');
6085              !!!next-token;              !!!next-token;
6086              redo B;              next B;
6087            }            }
6088          }          }
6089                    
6090          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6091            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6092              !!!parse-error (type => 'after html:#character', token => $token);
6093    
6094            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6095            } else {
6096              !!!cp ('t302');
6097          }          }
6098                    
6099          ## "after body" insertion mode          ## "after body" insertion mode
6100          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6101    
6102          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6103          ## reprocess          ## reprocess
6104          redo B;          next B;
6105        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6106          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6107            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6108              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6109                        
6110            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6111            } else {
6112              !!!cp ('t304');
6113          }          }
6114    
6115          ## "after body" insertion mode          ## "after body" insertion mode
6116          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6117    
6118          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6119            !!!ack-later;
6120          ## reprocess          ## reprocess
6121          redo B;          next B;
6122        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6123          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6124            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6125              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126                        
6127            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6128            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6129            } else {
6130              !!!cp ('t306');
6131          }          }
6132    
6133          ## "after body" insertion mode          ## "after body" insertion mode
6134          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6135            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6136              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6137                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6138              ## Ignore the token              ## Ignore the token
6139              !!!next-token;              !!!next-token;
6140              redo B;              next B;
6141            } else {            } else {
6142                !!!cp ('t308');
6143              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6144              !!!next-token;              !!!next-token;
6145              redo B;              next B;
6146            }            }
6147          } else {          } else {
6148            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6149              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6150    
6151            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6152            ## reprocess            ## reprocess
6153            redo B;            next B;
6154          }          }
6155          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156            !!!cp ('t309.2');
6157            ## Stop parsing
6158            last B;
6159        } else {        } else {
6160          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6161        }        }
# Line 4323  sub _tree_construction_main ($) { Line 6165  sub _tree_construction_main ($) {
6165            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6166                        
6167            unless (length $token->{data}) {            unless (length $token->{data}) {
6168                !!!cp ('t310');
6169              !!!next-token;              !!!next-token;
6170              redo B;              next B;
6171            }            }
6172          }          }
6173                    
6174          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6175            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6176              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6177                !!!parse-error (type => 'in frameset:#character', token => $token);
6178            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6179              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6180                !!!parse-error (type => 'after frameset:#character', token => $token);
6181            } else { # "after html frameset"            } else { # "after html frameset"
6182              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6183                !!!parse-error (type => 'after html:#character', token => $token);
6184    
6185              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6186              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6187              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6188            }            }
6189                        
6190            ## Ignore the token.            ## Ignore the token.
6191            if (length $token->{data}) {            if (length $token->{data}) {
6192                !!!cp ('t314');
6193              ## reprocess the rest of characters              ## reprocess the rest of characters
6194            } else {            } else {
6195                !!!cp ('t315');
6196              !!!next-token;              !!!next-token;
6197            }            }
6198            redo B;            next B;
6199          }          }
6200                    
6201          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6202        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6203          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6204            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6205              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6206    
6207            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6208            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6209          }          } else {
6210              !!!cp ('t317');
6211            }
6212    
6213          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6214              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6215            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6216              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217              !!!nack ('t318.1');
6218            !!!next-token;            !!!next-token;
6219            redo B;            next B;
6220          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6221                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6222            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6223              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6224            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6225              !!!ack ('t319.1');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6229              !!!cp ('t320');
6230            ## NOTE: As if in body.            ## NOTE: As if in body.
6231            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6232            redo B;            next B;
6233          } else {          } else {
6234            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6235              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6236                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6237            } else {            } else {
6238              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6239                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6240            }            }
6241            ## Ignore the token            ## Ignore the token
6242              !!!nack ('t322.1');
6243            !!!next-token;            !!!next-token;
6244            redo B;            next B;
6245          }          }
6246        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6247          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6248            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6249              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6250    
6251            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6252            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6253            } else {
6254              !!!cp ('t324');
6255          }          }
6256    
6257          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6258              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6259            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6260                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6261              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6262                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6263              ## Ignore the token              ## Ignore the token
6264              !!!next-token;              !!!next-token;
6265            } else {            } else {
6266                !!!cp ('t326');
6267              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6268              !!!next-token;              !!!next-token;
6269            }            }
6270    
6271            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6272                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6273                !!!cp ('t327');
6274              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275              } else {
6276                !!!cp ('t328');
6277            }            }
6278            redo B;            next B;
6279          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6280                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6281              !!!cp ('t329');
6282            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6283            !!!next-token;            !!!next-token;
6284            redo B;            next B;
6285          } else {          } else {
6286            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6287              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6288                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6289            } else {            } else {
6290              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6291                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6292            }            }
6293            ## Ignore the token            ## Ignore the token
6294            !!!next-token;            !!!next-token;
6295            redo B;            next B;
6296            }
6297          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6298            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6299                    @{$self->{open_elements}} == 1) { # redundant, maybe
6300              !!!cp ('t331.1');
6301              !!!parse-error (type => 'in body:#eof', token => $token);
6302            } else {
6303              !!!cp ('t331.2');
6304          }          }
6305            
6306            ## Stop parsing
6307            last B;
6308        } else {        } else {
6309          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6310        }        }
# Line 4436  sub _tree_construction_main ($) { Line 6317  sub _tree_construction_main ($) {
6317      ## "in body" insertion mode      ## "in body" insertion mode
6318      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6319        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6320            !!!cp ('t332');
6321          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6322          $script_start_tag->($insert);          $script_start_tag->();
6323          redo B;          next B;
6324        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6325            !!!cp ('t333');
6326          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6327          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6328          redo B;          next B;
6329        } elsif ({        } elsif ({
6330                  base => 1, link => 1,                  base => 1, link => 1,
6331                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6332            !!!cp ('t334');
6333          ## 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
6334          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6336            !!!ack ('t334.1');
6337          !!!next-token;          !!!next-token;
6338          redo B;          next B;
6339        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6340          ## 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
6341          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6342          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.
6343    
6344          unless ($self->{confident}) {          unless ($self->{confident}) {
6345            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6346                !!!cp ('t335');
6347                ## NOTE: Whether the encoding is supported or not is handled
6348                ## in the {change_encoding} callback.
6349              $self->{change_encoding}              $self->{change_encoding}
6350                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6351                
6352                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6353                    ->set_user_data (manakai_has_reference =>
6354                                         $token->{attributes}->{charset}
6355                                             ->{has_reference});
6356            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6357              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6358                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6359                        [\x09-\x0D\x20]*=
6360                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6361                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6362                  !!!cp ('t336');
6363                  ## NOTE: Whether the encoding is supported or not is handled
6364                  ## in the {change_encoding} callback.
6365                $self->{change_encoding}                $self->{change_encoding}
6366                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6367                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6368                      ->set_user_data (manakai_has_reference =>
6369                                           $token->{attributes}->{content}
6370                                                 ->{has_reference});
6371              }              }
6372            }            }
6373            } else {
6374              if ($token->{attributes}->{charset}) {
6375                !!!cp ('t337');
6376                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6377                    ->set_user_data (manakai_has_reference =>
6378                                         $token->{attributes}->{charset}
6379                                             ->{has_reference});
6380              }
6381              if ($token->{attributes}->{content}) {
6382                !!!cp ('t338');
6383                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6384                    ->set_user_data (manakai_has_reference =>
6385                                         $token->{attributes}->{content}
6386                                             ->{has_reference});
6387              }
6388          }          }
6389    
6390            !!!ack ('t338.1');
6391          !!!next-token;          !!!next-token;
6392          redo B;          next B;
6393        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6394          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6395          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6396          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6397            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6398        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6399          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6400                                
6401          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6402              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6403              !!!cp ('t342');
6404            ## Ignore the token            ## Ignore the token
6405          } else {          } else {
6406            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6407            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6408              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6409                  !!!cp ('t343');
6410                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6411                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6412                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6413              }              }
6414            }            }
6415          }          }
6416            !!!nack ('t343.1');
6417          !!!next-token;          !!!next-token;
6418          redo B;          next B;
6419        } elsif ({        } elsif ({
6420                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6421                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6422                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6423                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6424                  pre => 1,                  pre => 1, listing => 1,
6425                    form => 1,
6426                    table => 1,
6427                    hr => 1,
6428                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6429            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6430              !!!cp ('t350');
6431              !!!parse-error (type => 'in form:form', token => $token);
6432              ## Ignore the token
6433              !!!nack ('t350.1');
6434              !!!next-token;
6435              next B;
6436            }
6437    
6438          ## has a p element in scope          ## has a p element in scope
6439          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6440            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6441              !!!back-token;              !!!cp ('t344');
6442              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6443              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6444            } elsif ({                        line => $token->{line}, column => $token->{column}};
6445                      table => 1, caption => 1, td => 1, th => 1,              next B;
6446                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6447                     }->{$_->[1]}) {              !!!cp ('t345');
6448              last INSCOPE;              last INSCOPE;
6449            }            }
6450          } # INSCOPE          } # INSCOPE
6451                        
6452          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6453          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6454              !!!nack ('t346.1');
6455            !!!next-token;            !!!next-token;
6456            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6457              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6458              unless (length $token->{data}) {              unless (length $token->{data}) {
6459                  !!!cp ('t346');
6460                !!!next-token;                !!!next-token;
6461                } else {
6462                  !!!cp ('t349');
6463              }              }
6464              } else {
6465                !!!cp ('t348');
6466            }            }
6467          } else {          } elsif ($token->{tag_name} eq 'form') {
6468              !!!cp ('t347.1');
6469              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6470    
6471              !!!nack ('t347.2');
6472            !!!next-token;            !!!next-token;
6473          }          } elsif ($token->{tag_name} eq 'table') {
6474          redo B;            !!!cp ('t382');
6475        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6476          if (defined $self->{form_element}) {            
6477            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6478            ## Ignore the token  
6479              !!!nack ('t382.1');
6480              !!!next-token;
6481            } elsif ($token->{tag_name} eq 'hr') {
6482              !!!cp ('t386');
6483              pop @{$self->{open_elements}};
6484            
6485              !!!nack ('t386.1');
6486            !!!next-token;            !!!next-token;
           redo B;  
6487          } else {          } else {
6488            ## 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];  
6489            !!!next-token;            !!!next-token;
           redo B;  
6490          }          }
6491        } elsif ($token->{tag_name} eq 'li') {          next B;
6492          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6493          ## has a p element in scope          ## has a p element in scope
6494          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6495            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6496              !!!back-token;              !!!cp ('t353');
6497              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6498              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6499            } elsif ({                        line => $token->{line}, column => $token->{column}};
6500                      table => 1, caption => 1, td => 1, th => 1,              next B;
6501                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6502                     }->{$_->[1]}) {              !!!cp ('t354');
6503              last INSCOPE;              last INSCOPE;
6504            }            }
6505          } # INSCOPE          } # INSCOPE
# Line 4580  sub _tree_construction_main ($) { Line 6507  sub _tree_construction_main ($) {
6507          ## Step 1          ## Step 1
6508          my $i = -1;          my $i = -1;
6509          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6510            my $li_or_dtdd = {li => {li => 1},
6511                              dt => {dt => 1, dd => 1},
6512                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6513          LI: {          LI: {
6514            ## Step 2            ## Step 2
6515            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6516              if ($i != -1) {              if ($i != -1) {
6517                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6518                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6519              }                                value => $self->{open_elements}->[-1]->[0]
6520              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6521              last LI;                                token => $token);
6522            }              } else {
6523                            !!!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]);  
6524              }              }
6525              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6526              last LI;              last LI;
6527              } else {
6528                !!!cp ('t357');
6529            }            }
6530                        
6531            ## Step 3            ## Step 3
6532            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6533                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6534                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6535                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6536                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6537                  not ($node->[1] & DIV_EL)) {
6538                !!!cp ('t358');
6539              last LI;              last LI;
6540            }            }
6541                        
6542              !!!cp ('t359');
6543            ## Step 4            ## Step 4
6544            $i--;            $i--;
6545            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6546            redo LI;            redo LI;
6547          } # LI          } # LI
6548                        
6549          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6550            !!!nack ('t359.1');
6551          !!!next-token;          !!!next-token;
6552          redo B;          next B;
6553        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6554          ## has a p element in scope          ## has a p element in scope
6555          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6556            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6557              !!!back-token;              !!!cp ('t367');
6558              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6559              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6560            } elsif ({                        line => $token->{line}, column => $token->{column}};
6561                      table => 1, caption => 1, td => 1, th => 1,              next B;
6562                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6563                     }->{$_->[1]}) {              !!!cp ('t368');
6564              last INSCOPE;              last INSCOPE;
6565            }            }
6566          } # INSCOPE          } # INSCOPE
6567                        
6568          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6569                        
6570          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6571                        
6572            !!!nack ('t368.1');
6573          !!!next-token;          !!!next-token;
6574          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;  
6575        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6576          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6577            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6578            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6579              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6580                !!!parse-error (type => 'in a:a', token => $token);
6581                            
6582              !!!back-token;              !!!back-token; # <a>
6583              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6584              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6585                $formatting_end_tag->($token);
6586                            
6587              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6588                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6589                    !!!cp ('t372');
6590                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6591                  last AFE2;                  last AFE2;
6592                }                }
6593              } # AFE2              } # AFE2
6594              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6595                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6596                    !!!cp ('t373');
6597                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6598                  last OE;                  last OE;
6599                }                }
6600              } # OE              } # OE
6601              last AFE;              last AFE;
6602            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6603                !!!cp ('t374');
6604              last AFE;              last AFE;
6605            }            }
6606          } # AFE          } # AFE
6607                        
6608          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6609    
6610          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6611          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6612    
6613            !!!nack ('t374.1');
6614          !!!next-token;          !!!next-token;
6615          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;  
6616        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6617          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6618    
6619          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6620          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6621            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6622            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6623              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6624              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6625              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6626              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6627            } elsif ({                        line => $token->{line}, column => $token->{column}};
6628                      table => 1, caption => 1, td => 1, th => 1,              next B;
6629                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6630                     }->{$node->[1]}) {              !!!cp ('t377');
6631              last INSCOPE;              last INSCOPE;
6632            }            }
6633          } # INSCOPE          } # INSCOPE
6634                    
6635          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6636          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6637                    
6638            !!!nack ('t377.1');
6639          !!!next-token;          !!!next-token;
6640          redo B;          next B;
6641        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6642          ## has a button element in scope          ## has a button element in scope
6643          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6645            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6646              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6647              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6648              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6649              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6650            } elsif ({                        line => $token->{line}, column => $token->{column}};
6651                      table => 1, caption => 1, td => 1, th => 1,              next B;
6652                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6653                     }->{$node->[1]}) {              !!!cp ('t379');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
6656          } # INSCOPE          } # INSCOPE
6657                        
6658          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6659                        
6660          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661          push @$active_formatting_elements, ['#marker', ''];  
6662            ## TODO: associate with $self->{form_element} if defined
6663    
         !!!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});  
6664          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6665            
6666          !!!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;  
             
6667          !!!next-token;          !!!next-token;
6668          redo B;          next B;
6669        } elsif ({        } elsif ({
6670                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6671                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6672                  image => 1,                  noembed => 1,
6673                    noframes => 1,
6674                    noscript => 0, ## TODO: 1 if scripting is enabled
6675                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6676          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6677            !!!parse-error (type => 'image');            !!!cp ('t381');
6678            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6679            } else {
6680              !!!cp ('t399');
6681          }          }
6682            ## NOTE: There is an "as if in body" code clone.
6683          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6684          $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;  
6685        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6686          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6687                    
6688          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6689              !!!cp ('t389');
6690            ## Ignore the token            ## Ignore the token
6691              !!!nack ('t389'); ## NOTE: Not acknowledged.
6692            !!!next-token;            !!!next-token;
6693            redo B;            next B;
6694          } else {          } else {
6695            my $at = $token->{attributes};            my $at = $token->{attributes};
6696            my $form_attrs;            my $form_attrs;
# Line 4915  sub _tree_construction_main ($) { Line 6701  sub _tree_construction_main ($) {
6701            delete $at->{prompt};            delete $at->{prompt};
6702            my @tokens = (            my @tokens = (
6703                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6704                           attributes => $form_attrs},                           attributes => $form_attrs,
6705                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6706                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6707                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6708                            {type => START_TAG_TOKEN, tag_name => 'p',
6709                             line => $token->{line}, column => $token->{column}},
6710                            {type => START_TAG_TOKEN, tag_name => 'label',
6711                             line => $token->{line}, column => $token->{column}},
6712                         );                         );
6713            if ($prompt_attr) {            if ($prompt_attr) {
6714              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6715                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6716                               #line => $token->{line}, column => $token->{column},
6717                              };
6718            } else {            } else {
6719                !!!cp ('t391');
6720              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6721                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6722                               #line => $token->{line}, column => $token->{column},
6723                              }; # SHOULD
6724              ## TODO: make this configurable              ## TODO: make this configurable
6725            }            }
6726            push @tokens,            push @tokens,
6727                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6728                             line => $token->{line}, column => $token->{column}},
6729                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6730                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6731                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6732                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6733                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6734            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6735                             line => $token->{line}, column => $token->{column}},
6736                            {type => END_TAG_TOKEN, tag_name => 'form',
6737                             line => $token->{line}, column => $token->{column}};
6738              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6739            !!!back-token (@tokens);            !!!back-token (@tokens);
6740            redo B;            !!!next-token;
6741              next B;
6742          }          }
6743        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6744          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6745          my $el;          my $el;
6746          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6747                    
6748          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6749          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 6752  sub _tree_construction_main ($) {
6752          $insert->($el);          $insert->($el);
6753                    
6754          my $text = '';          my $text = '';
6755            !!!nack ('t392.1');
6756          !!!next-token;          !!!next-token;
6757          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6758            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6759            unless (length $token->{data}) {            unless (length $token->{data}) {
6760                !!!cp ('t392');
6761              !!!next-token;              !!!next-token;
6762              } else {
6763                !!!cp ('t393');
6764            }            }
6765            } else {
6766              !!!cp ('t394');
6767          }          }
6768          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6769              !!!cp ('t395');
6770            $text .= $token->{data};            $text .= $token->{data};
6771            !!!next-token;            !!!next-token;
6772          }          }
6773          if (length $text) {          if (length $text) {
6774              !!!cp ('t396');
6775            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6776          }          }
6777                    
# Line 4969  sub _tree_construction_main ($) { Line 6779  sub _tree_construction_main ($) {
6779                    
6780          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6781              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6782              !!!cp ('t397');
6783            ## Ignore the token            ## Ignore the token
6784          } else {          } else {
6785            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6786              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6787          }          }
6788          !!!next-token;          !!!next-token;
6789          redo B;          next B;
6790        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6791                  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') {  
6792          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6793    
6794            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6795    
6796            ## "adjust foreign attributes" - done in insert-element-f
6797                    
6798          !!!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);
6799                    
6800          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6801              pop @{$self->{open_elements}};
6802              !!!ack ('t398.1');
6803            } else {
6804              !!!cp ('t398.2');
6805              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6806              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6807              ## mode, "in body" (not "in foreign content") secondary insertion
6808              ## mode, maybe.
6809            }
6810    
6811          !!!next-token;          !!!next-token;
6812          redo B;          next B;
6813        } elsif ({        } elsif ({
6814                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6815                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6816                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6817                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6818                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6819          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6820            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6821          ## Ignore the token          ## Ignore the token
6822            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6823          !!!next-token;          !!!next-token;
6824          redo B;          next B;
6825                    
6826          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6827        } else {        } else {
6828            if ($token->{tag_name} eq 'image') {
6829              !!!cp ('t384');
6830              !!!parse-error (type => 'image', token => $token);
6831              $token->{tag_name} = 'img';
6832            } else {
6833              !!!cp ('t385');
6834            }
6835    
6836            ## NOTE: There is an "as if <br>" code clone.
6837          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6838                    
6839          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6840    
6841            if ({
6842                 applet => 1, marquee => 1, object => 1,
6843                }->{$token->{tag_name}}) {
6844              !!!cp ('t380');
6845              push @$active_formatting_elements, ['#marker', ''];
6846              !!!nack ('t380.1');
6847            } elsif ({
6848                      b => 1, big => 1, em => 1, font => 1, i => 1,
6849                      s => 1, small => 1, strile => 1,
6850                      strong => 1, tt => 1, u => 1,
6851                     }->{$token->{tag_name}}) {
6852              !!!cp ('t375');
6853              push @$active_formatting_elements, $self->{open_elements}->[-1];
6854              !!!nack ('t375.1');
6855            } elsif ($token->{tag_name} eq 'input') {
6856              !!!cp ('t388');
6857              ## TODO: associate with $self->{form_element} if defined
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t388.2');
6860            } elsif ({
6861                      area => 1, basefont => 1, bgsound => 1, br => 1,
6862                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6863                      #image => 1,
6864                     }->{$token->{tag_name}}) {
6865              !!!cp ('t388.1');
6866              pop @{$self->{open_elements}};
6867              !!!ack ('t388.3');
6868            } elsif ($token->{tag_name} eq 'select') {
6869              ## TODO: associate with $self->{form_element} if defined
6870            
6871              if ($self->{insertion_mode} & TABLE_IMS or
6872                  $self->{insertion_mode} & BODY_TABLE_IMS or
6873                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6874                !!!cp ('t400.1');
6875                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6876              } else {
6877                !!!cp ('t400.2');
6878                $self->{insertion_mode} = IN_SELECT_IM;
6879              }
6880              !!!nack ('t400.3');
6881            } else {
6882              !!!nack ('t402');
6883            }
6884                    
6885          !!!next-token;          !!!next-token;
6886          redo B;          next B;
6887        }        }
6888      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6889        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6890          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6891              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6892            for (@{$self->{open_elements}}) {          INSCOPE: {
6893              unless ({            for (reverse @{$self->{open_elements}}) {
6894                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6895                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6896                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6897                      }->{$_->[1]}) {                last INSCOPE;
6898                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6899                  !!!cp ('t405.1');
6900                  last;
6901              }              }
6902            }            }
6903    
6904            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6905                              value => $token->{tag_name}, token => $token);
6906              ## NOTE: Ignore the token.
6907            !!!next-token;            !!!next-token;
6908            redo B;            next B;
6909          } else {          } # INSCOPE
6910            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6911            ## Ignore the token          for (@{$self->{open_elements}}) {
6912            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6913            redo B;              !!!cp ('t403');
6914                !!!parse-error (type => 'not closed',
6915                                value => $_->[0]->manakai_local_name,
6916                                token => $token);
6917                last;
6918              } else {
6919                !!!cp ('t404');
6920              }
6921          }          }
6922    
6923            $self->{insertion_mode} = AFTER_BODY_IM;
6924            !!!next-token;
6925            next B;
6926        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6927          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
6928            ## up-to-date, though it has same effect as speced.
6929            if (@{$self->{open_elements}} > 1 and
6930                $self->{open_elements}->[1]->[1] & BODY_EL) {
6931            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6932            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6933              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6934                !!!parse-error (type => 'not closed',
6935                                value => $self->{open_elements}->[1]->[0]
6936                                    ->manakai_local_name,
6937                                token => $token);
6938              } else {
6939                !!!cp ('t407');
6940            }            }
6941            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6942            ## reprocess            ## reprocess
6943            redo B;            next B;
6944          } else {          } else {
6945            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6946              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6947            ## Ignore the token            ## Ignore the token
6948            !!!next-token;            !!!next-token;
6949            redo B;            next B;
6950          }          }
6951        } elsif ({        } elsif ({
6952                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6953                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6954                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6955                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6956                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6957                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6958          ## has an element in scope          ## has an element in scope
6959          my $i;          my $i;
6960          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6962            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6963              ## 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;  
             }  
6964              $i = $_;              $i = $_;
6965              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6966            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6967                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6968              last INSCOPE;              last INSCOPE;
6969            }            }
6970          } # INSCOPE          } # INSCOPE
6971            
6972          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6973            if (defined $i) {            !!!cp ('t413');
6974              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6975            } else {
6976              ## Step 1. generate implied end tags
6977              while ({
6978                      dd => ($token->{tag_name} ne 'dd'),
6979                      dt => ($token->{tag_name} ne 'dt'),
6980                      li => ($token->{tag_name} ne 'li'),
6981                      p => 1,
6982                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6983                !!!cp ('t409');
6984                pop @{$self->{open_elements}};
6985              }
6986    
6987              ## Step 2.
6988              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6989                      ne $token->{tag_name}) {
6990                !!!cp ('t412');
6991                !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[-1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995            } else {            } else {
6996              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6997            }            }
6998          }  
6999                      ## Step 3.
         if (defined $i) {  
7000            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7001          } elsif ($token->{tag_name} eq 'p') {  
7002            ## As if <p>, then reprocess the current token            ## Step 4.
7003            my $el;            $clear_up_to_marker->()
7004            !!!create-element ($el, 'p');                if {
7005            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7006                  }->{$token->{tag_name}};
7007          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7011            undef $self->{form_element};
7012    
7013          ## has an element in scope          ## has an element in scope
7014            my $i;
7015          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7017            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7018              ## generate implied end tags              !!!cp ('t418');
7019              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;  
             }  
7020              last INSCOPE;              last INSCOPE;
7021            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7022                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7023              last INSCOPE;              last INSCOPE;
7024            }            }
7025          } # INSCOPE          } # INSCOPE
7026            
7027          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7028            pop @{$self->{open_elements}};            !!!cp ('t421');
7029          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7030            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
7031              ## Step 1. generate implied end tags
7032              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7033                !!!cp ('t417');
7034                pop @{$self->{open_elements}};
7035              }
7036              
7037              ## Step 2.
7038              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7039                      ne $token->{tag_name}) {
7040                !!!cp ('t417.1');
7041                !!!parse-error (type => 'not closed',
7042                                value => $self->{open_elements}->[-1]->[0]
7043                                    ->manakai_local_name,
7044                                token => $token);
7045              } else {
7046                !!!cp ('t420');
7047              }  
7048              
7049              ## Step 3.
7050              splice @{$self->{open_elements}}, $i;
7051          }          }
7052    
         undef $self->{form_element};  
7053          !!!next-token;          !!!next-token;
7054          redo B;          next B;
7055        } elsif ({        } elsif ({
7056                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7057                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5150  sub _tree_construction_main ($) { Line 7059  sub _tree_construction_main ($) {
7059          my $i;          my $i;
7060          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7062            if ({            if ($node->[1] & HEADING_EL) {
7063                 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;  
             }  
7064              $i = $_;              $i = $_;
7065              last INSCOPE;              last INSCOPE;
7066            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7067                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7068              last INSCOPE;              last INSCOPE;
7069            }            }
7070          } # INSCOPE          } # INSCOPE
7071            
7072          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7073            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7074              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7075            } else {
7076              ## Step 1. generate implied end tags
7077              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7078                !!!cp ('t422');
7079                pop @{$self->{open_elements}};
7080              }
7081              
7082              ## Step 2.
7083              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7084                      ne $token->{tag_name}) {
7085                !!!cp ('t425');
7086                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7087              } else {
7088                !!!cp ('t426');
7089              }
7090    
7091              ## Step 3.
7092              splice @{$self->{open_elements}}, $i;
7093          }          }
7094                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7095          !!!next-token;          !!!next-token;
7096          redo B;          next B;
7097          } elsif ($token->{tag_name} eq 'p') {
7098            ## has an element in scope
7099            my $i;
7100            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7101              my $node = $self->{open_elements}->[$_];
7102              if ($node->[1] & P_EL) {
7103                !!!cp ('t410.1');
7104                $i = $_;
7105                last INSCOPE;
7106              } elsif ($node->[1] & SCOPING_EL) {
7107                !!!cp ('t411.1');
7108                last INSCOPE;
7109              }
7110            } # INSCOPE
7111    
7112            if (defined $i) {
7113              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7114                      ne $token->{tag_name}) {
7115                !!!cp ('t412.1');
7116                !!!parse-error (type => 'not closed',
7117                                value => $self->{open_elements}->[-1]->[0]
7118                                    ->manakai_local_name,
7119                                token => $token);
7120              } else {
7121                !!!cp ('t414.1');
7122              }
7123    
7124              splice @{$self->{open_elements}}, $i;
7125            } else {
7126              !!!cp ('t413.1');
7127              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7128    
7129              !!!cp ('t415.1');
7130              ## As if <p>, then reprocess the current token
7131              my $el;
7132              !!!create-element ($el, $HTML_NS, 'p',, $token);
7133              $insert->($el);
7134              ## NOTE: Not inserted into |$self->{open_elements}|.
7135            }
7136    
7137            !!!next-token;
7138            next B;
7139        } elsif ({        } elsif ({
7140                  a => 1,                  a => 1,
7141                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7142                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7143                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7145          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7146          redo B;          $formatting_end_tag->($token);
7147            next B;
7148        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7149          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7150            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7151    
7152          ## As if <br>          ## As if <br>
7153          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7154                    
7155          my $el;          my $el;
7156          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7157          $insert->($el);          $insert->($el);
7158                    
7159          ## Ignore the token.          ## Ignore the token.
7160          !!!next-token;          !!!next-token;
7161          redo B;          next B;
7162        } elsif ({        } elsif ({
7163                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7164                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5214  sub _tree_construction_main ($) { Line 7171  sub _tree_construction_main ($) {
7171                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7172                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7173                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7174          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7175            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7176          ## Ignore the token          ## Ignore the token
7177          !!!next-token;          !!!next-token;
7178          redo B;          next B;
7179                    
7180          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7181                    
# Line 5228  sub _tree_construction_main ($) { Line 7186  sub _tree_construction_main ($) {
7186    
7187          ## Step 2          ## Step 2
7188          S2: {          S2: {
7189            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7190              ## Step 1              ## Step 1
7191              ## generate implied end tags              ## generate implied end tags
7192              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7193                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7194                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7195                   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;  
7196              }              }
7197                    
7198              ## Step 2              ## Step 2
7199              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7200                        ne $token->{tag_name}) {
7201                  !!!cp ('t431');
7202                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7203                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7204                                  value => $self->{open_elements}->[-1]->[0]
7205                                      ->manakai_local_name,
7206                                  token => $token);
7207                } else {
7208                  !!!cp ('t432');
7209              }              }
7210                            
7211              ## Step 3              ## Step 3
# Line 5255  sub _tree_construction_main ($) { Line 7215  sub _tree_construction_main ($) {
7215              last S2;              last S2;
7216            } else {            } else {
7217              ## Step 3              ## Step 3
7218              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7219                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7220                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7221                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7222                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7223                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7224                ## Ignore the token                ## Ignore the token
7225                !!!next-token;                !!!next-token;
7226                last S2;                last S2;
7227              }              }
7228    
7229                !!!cp ('t434');
7230            }            }
7231                        
7232            ## Step 4            ## Step 4
# Line 5273  sub _tree_construction_main ($) { Line 7236  sub _tree_construction_main ($) {
7236            ## Step 5;            ## Step 5;
7237            redo S2;            redo S2;
7238          } # S2          } # S2
7239          redo B;          next B;
7240        }        }
7241      }      }
7242      redo B;      next B;
7243      } continue { # B
7244        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7245          ## NOTE: The code below is executed in cases where it does not have
7246          ## to be, but it it is harmless even in those cases.
7247          ## has an element in scope
7248          INSCOPE: {
7249            for (reverse 0..$#{$self->{open_elements}}) {
7250              my $node = $self->{open_elements}->[$_];
7251              if ($node->[1] & FOREIGN_EL) {
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                last;
7255              }
7256            }
7257            
7258            ## NOTE: No foreign element in scope.
7259            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7260          } # INSCOPE
7261        }
7262    } # B    } # B
7263    
   ## 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  
   
7264    ## Stop parsing # MUST    ## Stop parsing # MUST
7265        
7266    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5325  sub set_inner_html ($$$) { Line 7302  sub set_inner_html ($$$) {
7302      my $p = $class->new;      my $p = $class->new;
7303      $p->{document} = $doc;      $p->{document} = $doc;
7304    
7305      ## Step 9 # MUST      ## Step 8 # MUST
7306      my $i = 0;      my $i = 0;
7307      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7308      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7309      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7310        my $self = shift;        my $self = shift;
7311    
7312        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7313        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7314    
7315        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7316        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7317        $column++;  
7318          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7319        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
7320          $line++;  
7321          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
7322        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
7323            $p->{column} = 0;
7324            !!!cp ('i1');
7325          } elsif ($self->{next_char} == 0x000D) { # CR
7326          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7327          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7328          $line++;          $p->{line}++;
7329          $column = 0;          $p->{column} = 0;
7330        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7331          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7332        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7333            !!!cp ('i3');
7334          } elsif ($self->{next_char} == 0x0000) { # NULL
7335            !!!cp ('i4');
7336          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7337          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7338          } elsif ($self->{next_char} <= 0x0008 or
7339                   (0x000E <= $self->{next_char} and
7340                    $self->{next_char} <= 0x001F) or
7341                   (0x007F <= $self->{next_char} and
7342                    $self->{next_char} <= 0x009F) or
7343                   (0xD800 <= $self->{next_char} and
7344                    $self->{next_char} <= 0xDFFF) or
7345                   (0xFDD0 <= $self->{next_char} and
7346                    $self->{next_char} <= 0xFDDF) or
7347                   {
7348                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7349                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7350                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7351                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7352                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7353                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7354                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7355                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7356                    0x10FFFE => 1, 0x10FFFF => 1,
7357                   }->{$self->{next_char}}) {
7358            !!!cp ('i4.1');
7359            !!!parse-error (type => 'control char', level => $self->{must_level});
7360    ## TODO: error type documentation
7361        }        }
7362      };      };
7363      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7364      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7365            
7366      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7367        my (%opt) = @_;        my (%opt) = @_;
7368        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7369          my $column = $opt{column};
7370          if (defined $opt{token} and defined $opt{token}->{line}) {
7371            $line = $opt{token}->{line};
7372            $column = $opt{token}->{column};
7373          }
7374          warn "Parse error ($opt{type}) at line $line column $column\n";
7375      };      };
7376      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7377        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7378      };      };
7379            
7380      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7381      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7382    
7383      ## Step 2      ## Step 2
7384      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7385      $p->{content_model} = {      $p->{content_model} = {
7386        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7387        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5386  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          unless defined $p->{content_model};          unless defined $p->{content_model};
7399          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7400    
7401      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7402          ## TODO: Foreign element OK?
7403    
7404      ## Step 4      ## Step 3
7405      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7406        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7407    
7408      ## Step 5 # MUST      ## Step 4 # MUST
7409      $doc->append_child ($root);      $doc->append_child ($root);
7410    
7411      ## Step 6 # MUST      ## Step 5 # MUST
7412      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7413    
7414      undef $p->{head_element};      undef $p->{head_element};
7415    
7416      ## Step 7 # MUST      ## Step 6 # MUST
7417      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7418    
7419      ## Step 8 # MUST      ## Step 7 # MUST
7420      my $anode = $node;      my $anode = $node;
7421      AN: while (defined $anode) {      AN: while (defined $anode) {
7422        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7423          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7424          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7425            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7426                !!!cp ('i5');
7427              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7428              last AN;              last AN;
7429            }            }
# Line 5418  sub set_inner_html ($$$) { Line 7432  sub set_inner_html ($$$) {
7432        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7433      } # AN      } # AN
7434            
7435      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7436      {      {
7437        my $self = $p;        my $self = $p;
7438        !!!next-token;        !!!next-token;
7439      }      }
7440      $p->_tree_construction_main;      $p->_tree_construction_main;
7441    
7442      ## Step 11 # MUST      ## Step 10 # MUST
7443      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7444      for (@cn) {      for (@cn) {
7445        $node->remove_child ($_);        $node->remove_child ($_);
7446      }      }
7447      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7448    
7449      ## Step 12 # MUST      ## Step 11 # MUST
7450      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7451      for (@cn) {      for (@cn) {
7452        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 7455  sub set_inner_html ($$$) {
7455      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7456    
7457      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7458    
7459        delete $p->{parse_error}; # delete loop
7460    } else {    } else {
7461      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";
7462    }    }

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.145

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24