/[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.43 by wakaba, Sat Jul 21 07:21:44 2007 UTC revision 1.152 by wakaba, Sun Jun 29 11:15:53 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
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    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $byte_stream = $_[0];
 };  
 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  
361    
362  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
363    my $self = shift->new;      my (%opt) = @_;
364    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
365      };
366      $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390        }
391    
392        ## Step 2
393        my $byte_buffer = '';
394        for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422        }
423    
424        ## Step 4
425        ## TODO: <meta charset>
426    
427        ## Step 5
428        ## TODO: from history
429    
430        ## Step 6
431        require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
447                            value => $charset_name,
448                            level => $self->{info_level},
449                            line => 1, column => 1);
450            $self->{confident} = 0;
451            last SNIFFING;
452          }
453        }
454    
455        ## Step 7: default
456        ## TODO: Make this configurable.
457        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
458            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
459            ## detectable in the step 6.
460        require Whatpm::Charset::DecodeHandle;
461        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
462            ($byte_stream);
463        ($char_stream, $e_status)
464            = $charset->get_decode_handle ($buffer,
465                                           allow_error_reporting => 1,
466                                           allow_fallback => 1,
467                                           byte_buffer => \$byte_buffer);
468        $buffer->{buffer} = $byte_buffer;
469        !!!parse-error (type => 'sniffing:default', ## TODO: type name
470                        value => 'windows-1252',
471                        level => $self->{info_level},
472                        line => 1, column => 1);
473        $self->{confident} = 0;
474      } # SNIFFING
475    
476      $self->{input_encoding} = $charset->get_iana_name;
477      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
478        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
479                        value => $self->{input_encoding},
480                        level => $self->{unsupported_level},
481                        line => 1, column => 1);
482      } elsif (not ($e_status &
483                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
484        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
485                        value => $self->{input_encoding},
486                        level => $self->{unsupported_level},
487                        line => 1, column => 1);
488      }
489    
490      $self->{change_encoding} = sub {
491        my $self = shift;
492        $charset_name = shift;
493        my $token = shift;
494    
495        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
496        ($char_stream, $e_status) = $charset->get_decode_handle
497            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
498             byte_buffer => \ $buffer->{buffer});
499        
500        if ($char_stream) { # if supported
501          ## "Change the encoding" algorithm:
502    
503          ## Step 1    
504          if ($charset->{category} &
505              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
506            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
507            ($char_stream, $e_status) = $charset->get_decode_handle
508                ($byte_stream,
509                 byte_buffer => \ $buffer->{buffer});
510          }
511          $charset_name = $charset->get_iana_name;
512          
513          ## Step 2
514          if (defined $self->{input_encoding} and
515              $self->{input_encoding} eq $charset_name) {
516            !!!parse-error (type => 'charset label:matching', ## TODO: type
517                            value => $charset_name,
518                            level => $self->{info_level});
519            $self->{confident} = 1;
520            return;
521          }
522    
523          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
524              ':'.$charset_name, level => 'w', token => $token);
525          
526          ## Step 3
527          # if (can) {
528            ## change the encoding on the fly.
529            #$self->{confident} = 1;
530            #return;
531          # }
532          
533          ## Step 4
534          throw Whatpm::HTML::RestartParser ();
535        }
536      }; # $self->{change_encoding}
537    
538      my $char_onerror = sub {
539        my (undef, $type, %opt) = @_;
540        !!!parse-error (%opt, type => $type,
541                        line => $self->{line}, column => $self->{column} + 1);
542        if ($opt{octets}) {
543          ${$opt{octets}} = "\x{FFFD}"; # relacement character
544        }
545      };
546      $char_stream->onerror ($char_onerror);
547    
548      my @args = @_; shift @args; # $s
549      my $return;
550      try {
551        $return = $self->parse_char_stream ($char_stream, @args);  
552      } catch Whatpm::HTML::RestartParser with {
553        ## NOTE: Invoked after {change_encoding}.
554    
555        $self->{input_encoding} = $charset->get_iana_name;
556        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
557          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
558                          value => $self->{input_encoding},
559                          level => $self->{unsupported_level},
560                          line => 1, column => 1);
561        } elsif (not ($e_status &
562                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
563          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
564                          value => $self->{input_encoding},
565                          level => $self->{unsupported_level},
566                          line => 1, column => 1);
567        }
568        $self->{confident} = 1;
569        $char_stream->onerror ($char_onerror);
570        $return = $self->parse_char_stream ($char_stream, @args);
571      };
572      return $return;
573    } # parse_byte_stream
574    
575    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
576    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
577    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
578    ## because the core part of our HTML parser expects a string of character,
579    ## not a string of bytes or code units or anything which might contain a BOM.
580    ## Therefore, any parser interface that accepts a string of bytes,
581    ## such as |parse_byte_string| in this module, must ensure that it does
582    ## strip the BOM and never strip any ZWNBSP.
583    
584    sub parse_char_string ($$$;$) {
585      my $self = shift;
586      require utf8;
587      my $s = ref $_[0] ? $_[0] : \($_[0]);
588      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
589      return $self->parse_char_stream ($input, @_[1..$#_]);
590    } # parse_char_string
591    *parse_string = \&parse_char_string;
592    
593    sub parse_char_stream ($$$;$) {
594      my $self = ref $_[0] ? shift : shift->new;
595      my $input = $_[0];
596    $self->{document} = $_[1];    $self->{document} = $_[1];
597      @{$self->{document}->child_nodes} = ();
598    
599    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
600    
601      $self->{confident} = 1 unless exists $self->{confident};
602      $self->{document}->input_encoding ($self->{input_encoding})
603          if defined $self->{input_encoding};
604    
605    my $i = 0;    my $i = 0;
606    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
607    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
608    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
609      my $self = shift;      my $self = shift;
610    
611      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
612      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
613    
614      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
615      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
616      $column++;        $char = $self->{next_next_char};
617          delete $self->{next_next_char};
618        } else {
619          $char = $input->getc;
620        }
621        $self->{next_char} = -1 and return unless defined $char;
622        $self->{next_char} = ord $char;
623    
624        ($self->{line_prev}, $self->{column_prev})
625            = ($self->{line}, $self->{column});
626        $self->{column}++;
627            
628      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
629        $line++;        !!!cp ('j1');
630        $column = 0;        $self->{line}++;
631      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
632        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
633        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
634        $line++;        my $next = $input->getc;
635        $column = 0;        if (defined $next and $next ne "\x0A") {
636      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
637        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
638      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
639          $self->{line}++;
640          $self->{column} = 0;
641        } elsif ($self->{next_char} > 0x10FFFF) {
642          !!!cp ('j3');
643          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
644        } elsif ($self->{next_char} == 0x0000) { # NULL
645          !!!cp ('j4');
646        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
647        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
648        } elsif ($self->{next_char} <= 0x0008 or
649                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
650                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
651                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
652                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
653                 {
654                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
655                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
656                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
657                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
658                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
659                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
660                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
661                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
662                  0x10FFFE => 1, 0x10FFFF => 1,
663                 }->{$self->{next_char}}) {
664          !!!cp ('j5');
665          !!!parse-error (type => 'control char', level => $self->{must_level});
666    ## TODO: error type documentation
667      }      }
668    };    };
669    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
670    $self->{next_input_character} = -1;    $self->{next_char} = -1;
671    
672    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
673      my (%opt) = @_;      my (%opt) = @_;
674      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
675        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
676        warn "Parse error ($opt{type}) at line $line column $column\n";
677    };    };
678    $self->{parse_error} = sub {    $self->{parse_error} = sub {
679      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
680    };    };
681    
682    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 684  sub parse_string ($$$;$) {
684    $self->_construct_tree;    $self->_construct_tree;
685    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
686    
687      delete $self->{parse_error}; # remove loop
688    
689    return $self->{document};    return $self->{document};
690  } # parse_string  } # parse_char_stream
691    
692  sub new ($) {  sub new ($) {
693    my $class = shift;    my $class = shift;
694    my $self = bless {}, $class;    my $self = bless {
695    $self->{set_next_input_character} = sub {      must_level => 'm',
696      $self->{next_input_character} = -1;      should_level => 's',
697        good_level => 'w',
698        warn_level => 'w',
699        info_level => 'i',
700        unsupported_level => 'u',
701      }, $class;
702      $self->{set_next_char} = sub {
703        $self->{next_char} = -1;
704    };    };
705    $self->{parse_error} = sub {    $self->{parse_error} = sub {
706      #      #
707    };    };
708      $self->{change_encoding} = sub {
709        # if ($_[0] is a supported encoding) {
710        #   run "change the encoding" algorithm;
711        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
712        # }
713      };
714      $self->{application_cache_selection} = sub {
715        #
716      };
717    return $self;    return $self;
718  } # new  } # new
719    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 726  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
726  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
727  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
728    
729    sub DATA_STATE () { 0 }
730    sub ENTITY_DATA_STATE () { 1 }
731    sub TAG_OPEN_STATE () { 2 }
732    sub CLOSE_TAG_OPEN_STATE () { 3 }
733    sub TAG_NAME_STATE () { 4 }
734    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
735    sub ATTRIBUTE_NAME_STATE () { 6 }
736    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
737    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
738    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
739    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
740    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
741    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
742    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
743    sub COMMENT_START_STATE () { 14 }
744    sub COMMENT_START_DASH_STATE () { 15 }
745    sub COMMENT_STATE () { 16 }
746    sub COMMENT_END_STATE () { 17 }
747    sub COMMENT_END_DASH_STATE () { 18 }
748    sub BOGUS_COMMENT_STATE () { 19 }
749    sub DOCTYPE_STATE () { 20 }
750    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
751    sub DOCTYPE_NAME_STATE () { 22 }
752    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
753    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
754    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
755    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
756    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
757    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
758    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
759    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
760    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
761    sub BOGUS_DOCTYPE_STATE () { 32 }
762    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
763    sub SELF_CLOSING_START_TAG_STATE () { 34 }
764    sub CDATA_BLOCK_STATE () { 35 }
765    
766    sub DOCTYPE_TOKEN () { 1 }
767    sub COMMENT_TOKEN () { 2 }
768    sub START_TAG_TOKEN () { 3 }
769    sub END_TAG_TOKEN () { 4 }
770    sub END_OF_FILE_TOKEN () { 5 }
771    sub CHARACTER_TOKEN () { 6 }
772    
773    sub AFTER_HTML_IMS () { 0b100 }
774    sub HEAD_IMS ()       { 0b1000 }
775    sub BODY_IMS ()       { 0b10000 }
776    sub BODY_TABLE_IMS () { 0b100000 }
777    sub TABLE_IMS ()      { 0b1000000 }
778    sub ROW_IMS ()        { 0b10000000 }
779    sub BODY_AFTER_IMS () { 0b100000000 }
780    sub FRAME_IMS ()      { 0b1000000000 }
781    sub SELECT_IMS ()     { 0b10000000000 }
782    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
783        ## NOTE: "in foreign content" insertion mode is special; it is combined
784        ## with the secondary insertion mode.  In this parser, they are stored
785        ## together in the bit-or'ed form.
786    
787    ## NOTE: "initial" and "before html" insertion modes have no constants.
788    
789    ## NOTE: "after after body" insertion mode.
790    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
791    
792    ## NOTE: "after after frameset" insertion mode.
793    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
794    
795    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
796    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
797    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
798    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
799    sub IN_BODY_IM () { BODY_IMS }
800    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
801    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
802    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
803    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
804    sub IN_TABLE_IM () { TABLE_IMS }
805    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
806    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
807    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
808    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
809    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
810    sub IN_COLUMN_GROUP_IM () { 0b10 }
811    
812  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
813    
814  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
815    my $self = shift;    my $self = shift;
816    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
817    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
818    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
819    undef $self->{current_attribute};    undef $self->{current_attribute};
820    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
821    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
822      delete $self->{self_closing};
823    $self->{char} = [];    $self->{char} = [];
824    # $self->{next_input_character}    # $self->{next_char}
825    !!!next-input-character;    !!!next-input-character;
826    $self->{token} = [];    $self->{token} = [];
827    # $self->{escape}    # $self->{escape}
828  } # _initialize_tokenizer  } # _initialize_tokenizer
829    
830  ## A token has:  ## A token has:
831  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
832  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
833  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
834  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
835  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
836  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
837  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
838  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
839    ##        ->{name}
840    ##        ->{value}
841    ##        ->{has_reference} == 1 or 0
842    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
843    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
844    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
845    ##     while the token is pushed back to the stack.
846    
847  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
848    
# Line 194  sub _initialize_tokenizer ($) { Line 852  sub _initialize_tokenizer ($) {
852  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
853  ## and removed from the list.  ## and removed from the list.
854    
855    ## NOTE: HTML5 "Writing HTML documents" section, applied to
856    ## documents and not to user agents and conformance checkers,
857    ## contains some requirements that are not detected by the
858    ## parsing algorithm:
859    ## - Some requirements on character encoding declarations. ## TODO
860    ## - "Elements MUST NOT contain content that their content model disallows."
861    ##   ... Some are parse error, some are not (will be reported by c.c.).
862    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
863    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
864    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
865    
866    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
867    ## be detected by the HTML5 parsing algorithm:
868    ## - Text,
869    
870  sub _get_next_token ($) {  sub _get_next_token ($) {
871    my $self = shift;    my $self = shift;
872    
873      if ($self->{self_closing}) {
874        !!!parse-error (type => 'nestc', token => $self->{current_token});
875        ## NOTE: The |self_closing| flag is only set by start tag token.
876        ## In addition, when a start tag token is emitted, it is always set to
877        ## |current_token|.
878        delete $self->{self_closing};
879      }
880    
881    if (@{$self->{token}}) {    if (@{$self->{token}}) {
882        $self->{self_closing} = $self->{token}->[0]->{self_closing};
883      return shift @{$self->{token}};      return shift @{$self->{token}};
884    }    }
885    
886    A: {    A: {
887      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
888        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
889          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
890            $self->{state} = 'entity data';              not $self->{escape}) {
891              !!!cp (1);
892              $self->{state} = ENTITY_DATA_STATE;
893            !!!next-input-character;            !!!next-input-character;
894            redo A;            redo A;
895          } else {          } else {
896              !!!cp (2);
897            #            #
898          }          }
899        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
900          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
901            unless ($self->{escape}) {            unless ($self->{escape}) {
902              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
903                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
904                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
905                  !!!cp (3);
906                $self->{escape} = 1;                $self->{escape} = 1;
907                } else {
908                  !!!cp (4);
909              }              }
910              } else {
911                !!!cp (5);
912            }            }
913          }          }
914                    
915          #          #
916        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
917          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
918              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
919               not $self->{escape})) {               not $self->{escape})) {
920            $self->{state} = 'tag open';            !!!cp (6);
921              $self->{state} = TAG_OPEN_STATE;
922            !!!next-input-character;            !!!next-input-character;
923            redo A;            redo A;
924          } else {          } else {
925              !!!cp (7);
926            #            #
927          }          }
928        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
929          if ($self->{escape} and          if ($self->{escape} and
930              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
931            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
932                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
933                !!!cp (8);
934              delete $self->{escape};              delete $self->{escape};
935              } else {
936                !!!cp (9);
937            }            }
938            } else {
939              !!!cp (10);
940          }          }
941                    
942          #          #
943        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
944          !!!emit ({type => 'end-of-file'});          !!!cp (11);
945            !!!emit ({type => END_OF_FILE_TOKEN,
946                      line => $self->{line}, column => $self->{column}});
947          last A; ## TODO: ok?          last A; ## TODO: ok?
948          } else {
949            !!!cp (12);
950        }        }
951        # Anything else        # Anything else
952        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
953                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
954                       line => $self->{line}, column => $self->{column},
955                      };
956        ## Stay in the data state        ## Stay in the data state
957        !!!next-input-character;        !!!next-input-character;
958    
959        !!!emit ($token);        !!!emit ($token);
960    
961        redo A;        redo A;
962      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
963        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
964    
965          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
966                
967        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
968    
969        $self->{state} = 'data';        $self->{state} = DATA_STATE;
970        # next-input-character is already done        # next-input-character is already done
971    
972        unless (defined $token) {        unless (defined $token) {
973          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
974            !!!emit ({type => CHARACTER_TOKEN, data => '&',
975                      line => $l, column => $c,
976                     });
977        } else {        } else {
978            !!!cp (14);
979          !!!emit ($token);          !!!emit ($token);
980        }        }
981    
982        redo A;        redo A;
983      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
984        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
985          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
986              !!!cp (15);
987            !!!next-input-character;            !!!next-input-character;
988            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
989            redo A;            redo A;
990          } else {          } else {
991              !!!cp (16);
992            ## reconsume            ## reconsume
993            $self->{state} = 'data';            $self->{state} = DATA_STATE;
994    
995            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
996                        line => $self->{line_prev},
997                        column => $self->{column_prev},
998                       });
999    
1000            redo A;            redo A;
1001          }          }
1002        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1003          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1004            $self->{state} = 'markup declaration open';            !!!cp (17);
1005              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1006            !!!next-input-character;            !!!next-input-character;
1007            redo A;            redo A;
1008          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1009            $self->{state} = 'close tag open';            !!!cp (18);
1010              $self->{state} = CLOSE_TAG_OPEN_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1014                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1015              !!!cp (19);
1016            $self->{current_token}            $self->{current_token}
1017              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1018                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1019            $self->{state} = 'tag name';                 line => $self->{line_prev},
1020                   column => $self->{column_prev}};
1021              $self->{state} = TAG_NAME_STATE;
1022            !!!next-input-character;            !!!next-input-character;
1023            redo A;            redo A;
1024          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1025                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1026            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1027                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1028            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1029                                        line => $self->{line_prev},
1030                                        column => $self->{column_prev}};
1031              $self->{state} = TAG_NAME_STATE;
1032            !!!next-input-character;            !!!next-input-character;
1033            redo A;            redo A;
1034          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1035            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1036            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1037                              line => $self->{line_prev},
1038                              column => $self->{column_prev});
1039              $self->{state} = DATA_STATE;
1040            !!!next-input-character;            !!!next-input-character;
1041    
1042            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1043                        line => $self->{line_prev},
1044                        column => $self->{column_prev},
1045                       });
1046    
1047            redo A;            redo A;
1048          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1049            !!!parse-error (type => 'pio');            !!!cp (22);
1050            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1051            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1052                              column => $self->{column_prev});
1053              $self->{state} = BOGUS_COMMENT_STATE;
1054              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1055                                        line => $self->{line_prev},
1056                                        column => $self->{column_prev},
1057                                       };
1058              ## $self->{next_char} is intentionally left as is
1059            redo A;            redo A;
1060          } else {          } else {
1061            !!!parse-error (type => 'bare stago');            !!!cp (23);
1062            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1063                              line => $self->{line_prev},
1064                              column => $self->{column_prev});
1065              $self->{state} = DATA_STATE;
1066            ## reconsume            ## reconsume
1067    
1068            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1069                        line => $self->{line_prev},
1070                        column => $self->{column_prev},
1071                       });
1072    
1073            redo A;            redo A;
1074          }          }
1075        } else {        } else {
1076          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1077        }        }
1078      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1079          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1080        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1081          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1082    
1083            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1084            my @next_char;            my @next_char;
1085            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++) {
1086              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1087              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1088              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1089              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1090                  !!!cp (24);
1091                !!!next-input-character;                !!!next-input-character;
1092                next TAGNAME;                next TAGNAME;
1093              } else {              } else {
1094                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1095                  $self->{next_char} = shift @next_char; # reconsume
1096                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1097                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1098    
1099                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1100                            line => $l, column => $c,
1101                           });
1102        
1103                redo A;                redo A;
1104              }              }
1105            }            }
1106            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1107                
1108            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1109                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1110                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1111                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1112                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1113                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1114                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1115                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1116              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1117                $self->{next_char} = shift @next_char; # reconsume
1118              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1119              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1120              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1121                          line => $l, column => $c,
1122                         });
1123              redo A;              redo A;
1124            } else {            } else {
1125              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1126                $self->{next_char} = shift @next_char;
1127              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1128              # and consume...              # and consume...
1129            }            }
1130          } else {          } else {
1131            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1132              !!!cp (28);
1133            # next-input-character is already done            # next-input-character is already done
1134            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1135            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1136                        line => $l, column => $c,
1137                       });
1138            redo A;            redo A;
1139          }          }
1140        }        }
1141                
1142        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1143            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1144          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1145                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1146          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1147          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1148          redo A;                 line => $l, column => $c};
1149        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1150                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1151          $self->{current_token} = {type => 'end tag',          redo A;
1152                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1153          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1154          !!!next-input-character;          !!!cp (30);
1155          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1156        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1157          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1158          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1159            !!!next-input-character;
1160            redo A;
1161          } elsif ($self->{next_char} == 0x003E) { # >
1162            !!!cp (31);
1163            !!!parse-error (type => 'empty end tag',
1164                            line => $self->{line_prev}, ## "<" in "</>"
1165                            column => $self->{column_prev} - 1);
1166            $self->{state} = DATA_STATE;
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
1169        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1170            !!!cp (32);
1171          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1172          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1173          # reconsume          # reconsume
1174    
1175          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1176                      line => $l, column => $c,
1177                     });
1178    
1179          redo A;          redo A;
1180        } else {        } else {
1181            !!!cp (33);
1182          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1183          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1184          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1185          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1186        }                                    column => $self->{column_prev} - 1,
1187      } elsif ($self->{state} eq 'tag name') {                                   };
1188        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1189            $self->{next_input_character} == 0x000A or # LF          redo A;
1190            $self->{next_input_character} == 0x000B or # VT        }
1191            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1192            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1193          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1194          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1195          redo A;            $self->{next_char} == 0x000C or # FF
1196        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1197          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1198            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1199                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1200            redo A;
1201          } elsif ($self->{next_char} == 0x003E) { # >
1202            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1203              !!!cp (35);
1204            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1205          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1206            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1207            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1208              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1209            }            #  !!! cp (36);
1210              #  !!! parse-error (type => 'end tag attribute');
1211              #} else {
1212                !!!cp (37);
1213              #}
1214          } else {          } else {
1215            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1216          }          }
1217          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219    
1220          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1221    
1222          redo A;          redo A;
1223        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1224                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1225          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1226            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1227            # start tag or end tag            # start tag or end tag
1228          ## Stay in this state          ## Stay in this state
1229          !!!next-input-character;          !!!next-input-character;
1230          redo A;          redo A;
1231        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1232          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1233          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1234            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1235            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1236          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1237            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1238            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1239              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1240            }            #  !!! cp (40);
1241              #  !!! parse-error (type => 'end tag attribute');
1242              #} else {
1243                !!!cp (41);
1244              #}
1245          } else {          } else {
1246            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1247          }          }
1248          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1249          # reconsume          # reconsume
1250    
1251          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1252    
1253          redo A;          redo A;
1254        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1255            !!!cp (42);
1256            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1257          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1258          redo A;          redo A;
1259        } else {        } else {
1260          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1261            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1262            # start tag or end tag            # start tag or end tag
1263          ## Stay in the state          ## Stay in the state
1264          !!!next-input-character;          !!!next-input-character;
1265          redo A;          redo A;
1266        }        }
1267      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1268        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1269            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1270            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1271            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1272            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1273            !!!cp (45);
1274          ## Stay in the state          ## Stay in the state
1275          !!!next-input-character;          !!!next-input-character;
1276          redo A;          redo A;
1277        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1278          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1279            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1280            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1281          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1282            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1283            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1284                !!!cp (47);
1285              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1286              } else {
1287                !!!cp (48);
1288            }            }
1289          } else {          } else {
1290            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1291          }          }
1292          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294    
1295          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1296    
1297          redo A;          redo A;
1298        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1299                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1300          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1301                                value => ''};          $self->{current_attribute}
1302          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1303                   value => '',
1304                   line => $self->{line}, column => $self->{column}};
1305            $self->{state} = ATTRIBUTE_NAME_STATE;
1306            !!!next-input-character;
1307            redo A;
1308          } elsif ($self->{next_char} == 0x002F) { # /
1309            !!!cp (50);
1310            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1311          !!!next-input-character;          !!!next-input-character;
1312          redo A;          redo A;
1313        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' 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  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1315          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1318          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1319            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1320            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1321                !!!cp (53);
1322              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1323              } else {
1324                !!!cp (54);
1325            }            }
1326          } else {          } else {
1327            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1328          }          }
1329          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1330          # reconsume          # reconsume
1331    
1332          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1333    
1334          redo A;          redo A;
1335        } else {        } else {
1336          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1337                                value => ''};               0x0022 => 1, # "
1338          $self->{state} = 'attribute name';               0x0027 => 1, # '
1339                 0x003D => 1, # =
1340                }->{$self->{next_char}}) {
1341              !!!cp (55);
1342              !!!parse-error (type => 'bad attribute name');
1343            } else {
1344              !!!cp (56);
1345            }
1346            $self->{current_attribute}
1347                = {name => chr ($self->{next_char}),
1348                   value => '',
1349                   line => $self->{line}, column => $self->{column}};
1350            $self->{state} = ATTRIBUTE_NAME_STATE;
1351          !!!next-input-character;          !!!next-input-character;
1352          redo A;          redo A;
1353        }        }
1354      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1355        my $before_leave = sub {        my $before_leave = sub {
1356          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1357              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1358            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1359              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1360            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1361          } else {          } else {
1362              !!!cp (58);
1363            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1364              = $self->{current_attribute};              = $self->{current_attribute};
1365          }          }
1366        }; # $before_leave        }; # $before_leave
1367    
1368        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1369            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1370            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1371            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1372            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1373            !!!cp (59);
1374          $before_leave->();          $before_leave->();
1375          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1376          !!!next-input-character;          !!!next-input-character;
1377          redo A;          redo A;
1378        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1379            !!!cp (60);
1380          $before_leave->();          $before_leave->();
1381          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1385          $before_leave->();          $before_leave->();
1386          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1387            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1388            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1389          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1390              !!!cp (62);
1391            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1392            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1393              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395          } else {          } else {
1396            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1397          }          }
1398          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1399          !!!next-input-character;          !!!next-input-character;
1400    
1401          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1402    
1403          redo A;          redo A;
1404        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1405                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1406          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1407            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1408          ## Stay in the state          ## Stay in the state
1409          !!!next-input-character;          !!!next-input-character;
1410          redo A;          redo A;
1411        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1412            !!!cp (64);
1413          $before_leave->();          $before_leave->();
1414            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1415          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1416          redo A;          redo A;
1417        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1418          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1419          $before_leave->();          $before_leave->();
1420          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1421            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1422            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1423          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1424            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1425            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1426                !!!cp (67);
1427              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1428              } else {
1429                ## NOTE: This state should never be reached.
1430                !!!cp (68);
1431            }            }
1432          } else {          } else {
1433            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1434          }          }
1435          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1436          # reconsume          # reconsume
1437    
1438          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1439    
1440          redo A;          redo A;
1441        } else {        } else {
1442          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1443                $self->{next_char} == 0x0027) { # '
1444              !!!cp (69);
1445              !!!parse-error (type => 'bad attribute name');
1446            } else {
1447              !!!cp (70);
1448            }
1449            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        }        }
1454      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1455        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1456            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1457            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1458            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1459            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1460            !!!cp (71);
1461          ## Stay in the state          ## Stay in the state
1462          !!!next-input-character;          !!!next-input-character;
1463          redo A;          redo A;
1464        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1465          $self->{state} = 'before attribute value';          !!!cp (72);
1466            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1467          !!!next-input-character;          !!!next-input-character;
1468          redo A;          redo A;
1469        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1470          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1471            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1472            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1473          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1474            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1475            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1476                !!!cp (74);
1477              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1478              } else {
1479                ## NOTE: This state should never be reached.
1480                !!!cp (75);
1481            }            }
1482          } else {          } else {
1483            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1484          }          }
1485          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1486          !!!next-input-character;          !!!next-input-character;
1487    
1488          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1489    
1490          redo A;          redo A;
1491        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1492                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1493          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1494                                value => ''};          $self->{current_attribute}
1495          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1496                   value => '',
1497                   line => $self->{line}, column => $self->{column}};
1498            $self->{state} = ATTRIBUTE_NAME_STATE;
1499            !!!next-input-character;
1500            redo A;
1501          } elsif ($self->{next_char} == 0x002F) { # /
1502            !!!cp (77);
1503            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1504          !!!next-input-character;          !!!next-input-character;
1505          redo A;          redo A;
1506        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' 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';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1507          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1508          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1509            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1510            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1511          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1512            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1513            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1514                !!!cp (80);
1515              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1516              } else {
1517                ## NOTE: This state should never be reached.
1518                !!!cp (81);
1519            }            }
1520          } else {          } else {
1521            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1522          }          }
1523          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1524          # reconsume          # reconsume
1525    
1526          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1527    
1528          redo A;          redo A;
1529        } else {        } else {
1530          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1531                                value => ''};          $self->{current_attribute}
1532          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1533                   value => '',
1534                   line => $self->{line}, column => $self->{column}};
1535            $self->{state} = ATTRIBUTE_NAME_STATE;
1536          !!!next-input-character;          !!!next-input-character;
1537          redo A;                  redo A;        
1538        }        }
1539      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1540        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1541            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1542            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1543            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1544            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1545            !!!cp (83);
1546          ## Stay in the state          ## Stay in the state
1547          !!!next-input-character;          !!!next-input-character;
1548          redo A;          redo A;
1549        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1550          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1551            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1552          !!!next-input-character;          !!!next-input-character;
1553          redo A;          redo A;
1554        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1555          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1556            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1557          ## reconsume          ## reconsume
1558          redo A;          redo A;
1559        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1560          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1561            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1562          !!!next-input-character;          !!!next-input-character;
1563          redo A;          redo A;
1564        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1565          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1566            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1567            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1568          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1569            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1570            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1571                !!!cp (88);
1572              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1573              } else {
1574                ## NOTE: This state should never be reached.
1575                !!!cp (89);
1576            }            }
1577          } else {          } else {
1578            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1579          }          }
1580          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1581          !!!next-input-character;          !!!next-input-character;
1582    
1583          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1584    
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1587          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1588          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1589            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1590            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1591          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1592            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1593            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1594                !!!cp (91);
1595              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1596              } else {
1597                ## NOTE: This state should never be reached.
1598                !!!cp (92);
1599            }            }
1600          } else {          } else {
1601            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1602          }          }
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          ## reconsume          ## reconsume
1605    
1606          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1607    
1608          redo A;          redo A;
1609        } else {        } else {
1610          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1611          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1612              !!!parse-error (type => 'bad attribute value');
1613            } else {
1614              !!!cp (94);
1615            }
1616            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1617            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1618          !!!next-input-character;          !!!next-input-character;
1619          redo A;          redo A;
1620        }        }
1621      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1622        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1623          $self->{state} = 'before attribute name';          !!!cp (95);
1624            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1628          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1629          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1630            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1631          !!!next-input-character;          !!!next-input-character;
1632          redo A;          redo A;
1633        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1634          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1635          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1636            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1637            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1638          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1639            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1640            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1641                !!!cp (98);
1642              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1643              } else {
1644                ## NOTE: This state should never be reached.
1645                !!!cp (99);
1646            }            }
1647          } else {          } else {
1648            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1649          }          }
1650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1651          ## reconsume          ## reconsume
1652    
1653          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1654    
1655          redo A;          redo A;
1656        } else {        } else {
1657          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1658            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1659          ## Stay in the state          ## Stay in the state
1660          !!!next-input-character;          !!!next-input-character;
1661          redo A;          redo A;
1662        }        }
1663      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1664        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1665          $self->{state} = 'before attribute name';          !!!cp (101);
1666            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1670          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1671          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1672            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1673          !!!next-input-character;          !!!next-input-character;
1674          redo A;          redo A;
1675        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1676          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1678            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1680          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1681            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1682            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1683                !!!cp (104);
1684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1685              } else {
1686                ## NOTE: This state should never be reached.
1687                !!!cp (105);
1688            }            }
1689          } else {          } else {
1690            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1691          }          }
1692          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1693          ## reconsume          ## reconsume
1694    
1695          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1696    
1697          redo A;          redo A;
1698        } else {        } else {
1699          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1700            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1701          ## Stay in the state          ## Stay in the state
1702          !!!next-input-character;          !!!next-input-character;
1703          redo A;          redo A;
1704        }        }
1705      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1706        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1707            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1708            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1709            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1710            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1711          $self->{state} = 'before attribute name';          !!!cp (107);
1712          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1713          redo A;          !!!next-input-character;
1714        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1715          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1716          $self->{state} = 'entity in attribute value';          !!!cp (108);
1717          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1718          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1719        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1720          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1721            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1722                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723              !!!cp (109);
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1727            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1728                !!!cp (110);
1729              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1730              } else {
1731                ## NOTE: This state should never be reached.
1732                !!!cp (111);
1733            }            }
1734          } else {          } else {
1735            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1736          }          }
1737          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1738          !!!next-input-character;          !!!next-input-character;
1739    
1740          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1741    
1742          redo A;          redo A;
1743        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1744          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1745          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1746            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1747            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1748          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1749            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1750            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1751                !!!cp (113);
1752              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1753              } else {
1754                ## NOTE: This state should never be reached.
1755                !!!cp (114);
1756            }            }
1757          } else {          } else {
1758            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1759          }          }
1760          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1761          ## reconsume          ## reconsume
1762    
1763          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1764    
1765          redo A;          redo A;
1766        } else {        } else {
1767          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1768                 0x0022 => 1, # "
1769                 0x0027 => 1, # '
1770                 0x003D => 1, # =
1771                }->{$self->{next_char}}) {
1772              !!!cp (115);
1773              !!!parse-error (type => 'bad attribute value');
1774            } else {
1775              !!!cp (116);
1776            }
1777            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1778          ## Stay in the state          ## Stay in the state
1779          !!!next-input-character;          !!!next-input-character;
1780          redo A;          redo A;
1781        }        }
1782      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1783        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1784              (1,
1785               $self->{last_attribute_value_state}
1786                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1787               $self->{last_attribute_value_state}
1788                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1789               -1);
1790    
1791        unless (defined $token) {        unless (defined $token) {
1792            !!!cp (117);
1793          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1794        } else {        } else {
1795            !!!cp (118);
1796          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1797            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1798          ## 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"
1799        }        }
1800    
1801        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1802        # next-input-character is already done        # next-input-character is already done
1803        redo A;        redo A;
1804      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1805          if ($self->{next_char} == 0x0009 or # HT
1806              $self->{next_char} == 0x000A or # LF
1807              $self->{next_char} == 0x000B or # VT
1808              $self->{next_char} == 0x000C or # FF
1809              $self->{next_char} == 0x0020) { # SP
1810            !!!cp (118);
1811            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1812            !!!next-input-character;
1813            redo A;
1814          } elsif ($self->{next_char} == 0x003E) { # >
1815            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1816              !!!cp (119);
1817              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1818            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1819              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1820              if ($self->{current_token}->{attributes}) {
1821                !!!cp (120);
1822                !!!parse-error (type => 'end tag attribute');
1823              } else {
1824                ## NOTE: This state should never be reached.
1825                !!!cp (121);
1826              }
1827            } else {
1828              die "$0: $self->{current_token}->{type}: Unknown token type";
1829            }
1830            $self->{state} = DATA_STATE;
1831            !!!next-input-character;
1832    
1833            !!!emit ($self->{current_token}); # start tag or end tag
1834    
1835            redo A;
1836          } elsif ($self->{next_char} == 0x002F) { # /
1837            !!!cp (122);
1838            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1839            !!!next-input-character;
1840            redo A;
1841          } elsif ($self->{next_char} == -1) {
1842            !!!parse-error (type => 'unclosed tag');
1843            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1844              !!!cp (122.3);
1845              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1846            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1847              if ($self->{current_token}->{attributes}) {
1848                !!!cp (122.1);
1849                !!!parse-error (type => 'end tag attribute');
1850              } else {
1851                ## NOTE: This state should never be reached.
1852                !!!cp (122.2);
1853              }
1854            } else {
1855              die "$0: $self->{current_token}->{type}: Unknown token type";
1856            }
1857            $self->{state} = DATA_STATE;
1858            ## Reconsume.
1859            !!!emit ($self->{current_token}); # start tag or end tag
1860            redo A;
1861          } else {
1862            !!!cp ('124.1');
1863            !!!parse-error (type => 'no space between attributes');
1864            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1865            ## reconsume
1866            redo A;
1867          }
1868        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1869          if ($self->{next_char} == 0x003E) { # >
1870            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1871              !!!cp ('124.2');
1872              !!!parse-error (type => 'nestc', token => $self->{current_token});
1873              ## TODO: Different type than slash in start tag
1874              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1875              if ($self->{current_token}->{attributes}) {
1876                !!!cp ('124.4');
1877                !!!parse-error (type => 'end tag attribute');
1878              } else {
1879                !!!cp ('124.5');
1880              }
1881              ## TODO: Test |<title></title/>|
1882            } else {
1883              !!!cp ('124.3');
1884              $self->{self_closing} = 1;
1885            }
1886    
1887            $self->{state} = DATA_STATE;
1888            !!!next-input-character;
1889    
1890            !!!emit ($self->{current_token}); # start tag or end tag
1891    
1892            redo A;
1893          } elsif ($self->{next_char} == -1) {
1894            !!!parse-error (type => 'unclosed tag');
1895            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1896              !!!cp (124.7);
1897              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1898            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1899              if ($self->{current_token}->{attributes}) {
1900                !!!cp (124.5);
1901                !!!parse-error (type => 'end tag attribute');
1902              } else {
1903                ## NOTE: This state should never be reached.
1904                !!!cp (124.6);
1905              }
1906            } else {
1907              die "$0: $self->{current_token}->{type}: Unknown token type";
1908            }
1909            $self->{state} = DATA_STATE;
1910            ## Reconsume.
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912            redo A;
1913          } else {
1914            !!!cp ('124.4');
1915            !!!parse-error (type => 'nestc');
1916            ## TODO: This error type is wrong.
1917            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1918            ## Reconsume.
1919            redo A;
1920          }
1921        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1922        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1923                
1924        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1925          #my $token = {type => COMMENT_TOKEN, data => ''};
1926    
1927        BC: {        BC: {
1928          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1929            $self->{state} = 'data';            !!!cp (124);
1930              $self->{state} = DATA_STATE;
1931            !!!next-input-character;            !!!next-input-character;
1932    
1933            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1934    
1935            redo A;            redo A;
1936          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1937            $self->{state} = 'data';            !!!cp (125);
1938              $self->{state} = DATA_STATE;
1939            ## reconsume            ## reconsume
1940    
1941            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1942    
1943            redo A;            redo A;
1944          } else {          } else {
1945            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1946              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1947            !!!next-input-character;            !!!next-input-character;
1948            redo BC;            redo BC;
1949          }          }
1950        } # BC        } # BC
1951      } elsif ($self->{state} eq 'markup declaration open') {  
1952          die "$0: _get_next_token: unexpected case [BC]";
1953        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1954        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1955    
1956          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1957    
1958        my @next_char;        my @next_char;
1959        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1960                
1961        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1962          !!!next-input-character;          !!!next-input-character;
1963          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1964          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1965            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1966            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1967                                        line => $l, column => $c,
1968                                       };
1969              $self->{state} = COMMENT_START_STATE;
1970            !!!next-input-character;            !!!next-input-character;
1971            redo A;            redo A;
1972            } else {
1973              !!!cp (128);
1974          }          }
1975        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1976                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1977          !!!next-input-character;          !!!next-input-character;
1978          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1979          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1980              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1981            !!!next-input-character;            !!!next-input-character;
1982            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1983            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1984                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1985              !!!next-input-character;              !!!next-input-character;
1986              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1987              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1988                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1989                !!!next-input-character;                !!!next-input-character;
1990                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1991                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1992                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1993                  !!!next-input-character;                  !!!next-input-character;
1994                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1995                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1996                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1997                    !!!next-input-character;                    !!!next-input-character;
1998                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1999                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2000                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2001                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2002                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2003                        $self->{state} = DOCTYPE_STATE;
2004                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2005                                                  quirks => 1,
2006                                                  line => $l, column => $c,
2007                                                 };
2008                      !!!next-input-character;                      !!!next-input-character;
2009                      redo A;                      redo A;
2010                      } else {
2011                        !!!cp (130);
2012                    }                    }
2013                    } else {
2014                      !!!cp (131);
2015                  }                  }
2016                  } else {
2017                    !!!cp (132);
2018                }                }
2019                } else {
2020                  !!!cp (133);
2021              }              }
2022              } else {
2023                !!!cp (134);
2024            }            }
2025            } else {
2026              !!!cp (135);
2027            }
2028          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2029                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2030                   $self->{next_char} == 0x005B) { # [
2031            !!!next-input-character;
2032            push @next_char, $self->{next_char};
2033            if ($self->{next_char} == 0x0043) { # C
2034              !!!next-input-character;
2035              push @next_char, $self->{next_char};
2036              if ($self->{next_char} == 0x0044) { # D
2037                !!!next-input-character;
2038                push @next_char, $self->{next_char};
2039                if ($self->{next_char} == 0x0041) { # A
2040                  !!!next-input-character;
2041                  push @next_char, $self->{next_char};
2042                  if ($self->{next_char} == 0x0054) { # T
2043                    !!!next-input-character;
2044                    push @next_char, $self->{next_char};
2045                    if ($self->{next_char} == 0x0041) { # A
2046                      !!!next-input-character;
2047                      push @next_char, $self->{next_char};
2048                      if ($self->{next_char} == 0x005B) { # [
2049                        !!!cp (135.1);
2050                        $self->{state} = CDATA_BLOCK_STATE;
2051                        !!!next-input-character;
2052                        redo A;
2053                      } else {
2054                        !!!cp (135.2);
2055                      }
2056                    } else {
2057                      !!!cp (135.3);
2058                    }
2059                  } else {
2060                    !!!cp (135.4);                
2061                  }
2062                } else {
2063                  !!!cp (135.5);
2064                }
2065              } else {
2066                !!!cp (135.6);
2067              }
2068            } else {
2069              !!!cp (135.7);
2070          }          }
2071          } else {
2072            !!!cp (136);
2073        }        }
2074    
2075        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2076        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2077        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2078        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2079          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2080                                    line => $l, column => $c,
2081                                   };
2082        redo A;        redo A;
2083                
2084        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2085        ## 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?
2086      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2087        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2088          $self->{state} = 'comment start dash';          !!!cp (137);
2089            $self->{state} = COMMENT_START_DASH_STATE;
2090          !!!next-input-character;          !!!next-input-character;
2091          redo A;          redo A;
2092        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2093            !!!cp (138);
2094          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2095          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2096          !!!next-input-character;          !!!next-input-character;
2097    
2098          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2099    
2100          redo A;          redo A;
2101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2102            !!!cp (139);
2103          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2105          ## reconsume          ## reconsume
2106    
2107          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2108    
2109          redo A;          redo A;
2110        } else {        } else {
2111            !!!cp (140);
2112          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2113              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2114          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2115          !!!next-input-character;          !!!next-input-character;
2116          redo A;          redo A;
2117        }        }
2118      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2119        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2120          $self->{state} = 'comment end';          !!!cp (141);
2121            $self->{state} = COMMENT_END_STATE;
2122          !!!next-input-character;          !!!next-input-character;
2123          redo A;          redo A;
2124        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2125            !!!cp (142);
2126          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2127          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2128          !!!next-input-character;          !!!next-input-character;
2129    
2130          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2131    
2132          redo A;          redo A;
2133        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2134            !!!cp (143);
2135          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2136          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2137          ## reconsume          ## reconsume
2138    
2139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2140    
2141          redo A;          redo A;
2142        } else {        } else {
2143            !!!cp (144);
2144          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2145              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2146          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2147          !!!next-input-character;          !!!next-input-character;
2148          redo A;          redo A;
2149        }        }
2150      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2151        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2152          $self->{state} = 'comment end dash';          !!!cp (145);
2153            $self->{state} = COMMENT_END_DASH_STATE;
2154          !!!next-input-character;          !!!next-input-character;
2155          redo A;          redo A;
2156        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2157            !!!cp (146);
2158          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2159          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2160          ## reconsume          ## reconsume
2161    
2162          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2163    
2164          redo A;          redo A;
2165        } else {        } else {
2166          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2167            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2168          ## Stay in the state          ## Stay in the state
2169          !!!next-input-character;          !!!next-input-character;
2170          redo A;          redo A;
2171        }        }
2172      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2173        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2174          $self->{state} = 'comment end';          !!!cp (148);
2175            $self->{state} = COMMENT_END_STATE;
2176          !!!next-input-character;          !!!next-input-character;
2177          redo A;          redo A;
2178        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2179            !!!cp (149);
2180          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2181          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2182          ## reconsume          ## reconsume
2183    
2184          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2185    
2186          redo A;          redo A;
2187        } else {        } else {
2188          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2189          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2190            $self->{state} = COMMENT_STATE;
2191          !!!next-input-character;          !!!next-input-character;
2192          redo A;          redo A;
2193        }        }
2194      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2195        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2196          $self->{state} = 'data';          !!!cp (151);
2197            $self->{state} = DATA_STATE;
2198          !!!next-input-character;          !!!next-input-character;
2199    
2200          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2201    
2202          redo A;          redo A;
2203        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2204          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2205            !!!parse-error (type => 'dash in comment',
2206                            line => $self->{line_prev},
2207                            column => $self->{column_prev});
2208          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2209          ## Stay in the state          ## Stay in the state
2210          !!!next-input-character;          !!!next-input-character;
2211          redo A;          redo A;
2212        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2213            !!!cp (153);
2214          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2215          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2216          ## reconsume          ## reconsume
2217    
2218          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2219    
2220          redo A;          redo A;
2221        } else {        } else {
2222          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2223          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2224          $self->{state} = 'comment';                          line => $self->{line_prev},
2225                            column => $self->{column_prev});
2226            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2227            $self->{state} = COMMENT_STATE;
2228          !!!next-input-character;          !!!next-input-character;
2229          redo A;          redo A;
2230        }        }
2231      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2232        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2233            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2234            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2235            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2236            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2237          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2238            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2239          !!!next-input-character;          !!!next-input-character;
2240          redo A;          redo A;
2241        } else {        } else {
2242            !!!cp (156);
2243          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2244          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2245          ## reconsume          ## reconsume
2246          redo A;          redo A;
2247        }        }
2248      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2249        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2250            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2251            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2252            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2253            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2254            !!!cp (157);
2255          ## Stay in the state          ## Stay in the state
2256          !!!next-input-character;          !!!next-input-character;
2257          redo A;          redo A;
2258        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2259            !!!cp (158);
2260          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2261          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2268            !!!cp (159);
2269          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2270          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2271          ## reconsume          ## reconsume
2272    
2273          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277          $self->{current_token}          !!!cp (160);
2278              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2279                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2280  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2281          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2282          !!!next-input-character;          !!!next-input-character;
2283          redo A;          redo A;
2284        }        }
2285      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2286  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2287        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2288            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2289            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2290            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2291            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2292          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2293            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2294          !!!next-input-character;          !!!next-input-character;
2295          redo A;          redo A;
2296        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2297          $self->{state} = 'data';          !!!cp (162);
2298            $self->{state} = DATA_STATE;
2299          !!!next-input-character;          !!!next-input-character;
2300    
2301          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2302    
2303          redo A;          redo A;
2304        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2305            !!!cp (163);
2306          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2307          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2308          ## reconsume          ## reconsume
2309    
2310          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2311          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2312    
2313          redo A;          redo A;
2314        } else {        } else {
2315            !!!cp (164);
2316          $self->{current_token}->{name}          $self->{current_token}->{name}
2317            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2318          ## Stay in the state          ## Stay in the state
2319          !!!next-input-character;          !!!next-input-character;
2320          redo A;          redo A;
2321        }        }
2322      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2323        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2324            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2325            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2326            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2327            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2328            !!!cp (165);
2329          ## Stay in the state          ## Stay in the state
2330          !!!next-input-character;          !!!next-input-character;
2331          redo A;          redo A;
2332        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2333          $self->{state} = 'data';          !!!cp (166);
2334            $self->{state} = DATA_STATE;
2335          !!!next-input-character;          !!!next-input-character;
2336    
2337          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2338    
2339          redo A;          redo A;
2340        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2341            !!!cp (167);
2342          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2343          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2344          ## reconsume          ## reconsume
2345    
2346          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2347          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2348    
2349          redo A;          redo A;
2350        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2351                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2352          !!!next-input-character;          !!!next-input-character;
2353          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2354              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2355            !!!next-input-character;            !!!next-input-character;
2356            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2357                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2358              !!!next-input-character;              !!!next-input-character;
2359              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2360                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2361                !!!next-input-character;                !!!next-input-character;
2362                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2363                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2364                  !!!next-input-character;                  !!!next-input-character;
2365                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2366                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2367                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2368                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2369                    !!!next-input-character;                    !!!next-input-character;
2370                    redo A;                    redo A;
2371                    } else {
2372                      !!!cp (169);
2373                  }                  }
2374                  } else {
2375                    !!!cp (170);
2376                }                }
2377                } else {
2378                  !!!cp (171);
2379              }              }
2380              } else {
2381                !!!cp (172);
2382            }            }
2383            } else {
2384              !!!cp (173);
2385          }          }
2386    
2387          #          #
2388        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2389                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2390          !!!next-input-character;          !!!next-input-character;
2391          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2392              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2393            !!!next-input-character;            !!!next-input-character;
2394            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2395                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2396              !!!next-input-character;              !!!next-input-character;
2397              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2398                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2399                !!!next-input-character;                !!!next-input-character;
2400                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2401                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2402                  !!!next-input-character;                  !!!next-input-character;
2403                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2404                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2405                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2406                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2407                    !!!next-input-character;                    !!!next-input-character;
2408                    redo A;                    redo A;
2409                    } else {
2410                      !!!cp (175);
2411                  }                  }
2412                  } else {
2413                    !!!cp (176);
2414                }                }
2415                } else {
2416                  !!!cp (177);
2417              }              }
2418              } else {
2419                !!!cp (178);
2420            }            }
2421            } else {
2422              !!!cp (179);
2423          }          }
2424    
2425          #          #
2426        } else {        } else {
2427            !!!cp (180);
2428          !!!next-input-character;          !!!next-input-character;
2429          #          #
2430        }        }
2431    
2432        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2433        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2434    
2435          $self->{state} = BOGUS_DOCTYPE_STATE;
2436        # next-input-character is already done        # next-input-character is already done
2437        redo A;        redo A;
2438      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2439        if ({        if ({
2440              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2441              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2442            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2443            !!!cp (181);
2444          ## Stay in the state          ## Stay in the state
2445          !!!next-input-character;          !!!next-input-character;
2446          redo A;          redo A;
2447        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2448            !!!cp (182);
2449          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2450          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2451          !!!next-input-character;          !!!next-input-character;
2452          redo A;          redo A;
2453        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2454            !!!cp (183);
2455          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2456          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2457          !!!next-input-character;          !!!next-input-character;
2458          redo A;          redo A;
2459        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2460            !!!cp (184);
2461          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2462    
2463          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2464          !!!next-input-character;          !!!next-input-character;
2465    
2466          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2467          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2468    
2469          redo A;          redo A;
2470        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2471            !!!cp (185);
2472          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2473    
2474          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2475          ## reconsume          ## reconsume
2476    
2477          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2478          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2479    
2480          redo A;          redo A;
2481        } else {        } else {
2482            !!!cp (186);
2483          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2484          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2485    
2486            $self->{state} = BOGUS_DOCTYPE_STATE;
2487          !!!next-input-character;          !!!next-input-character;
2488          redo A;          redo A;
2489        }        }
2490      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2491        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2492          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2493            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2494          !!!next-input-character;          !!!next-input-character;
2495          redo A;          redo A;
2496        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2497            !!!cp (188);
2498            !!!parse-error (type => 'unclosed PUBLIC literal');
2499    
2500            $self->{state} = DATA_STATE;
2501            !!!next-input-character;
2502    
2503            $self->{current_token}->{quirks} = 1;
2504            !!!emit ($self->{current_token}); # DOCTYPE
2505    
2506            redo A;
2507          } elsif ($self->{next_char} == -1) {
2508            !!!cp (189);
2509          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2510    
2511          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2512          ## reconsume          ## reconsume
2513    
2514          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2515          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2516    
2517          redo A;          redo A;
2518        } else {        } else {
2519            !!!cp (190);
2520          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2521              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2522          ## Stay in the state          ## Stay in the state
2523          !!!next-input-character;          !!!next-input-character;
2524          redo A;          redo A;
2525        }        }
2526      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2527        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2528          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2529            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2530          !!!next-input-character;          !!!next-input-character;
2531          redo A;          redo A;
2532        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2533            !!!cp (192);
2534          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2535    
2536          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2537            !!!next-input-character;
2538    
2539            $self->{current_token}->{quirks} = 1;
2540            !!!emit ($self->{current_token}); # DOCTYPE
2541    
2542            redo A;
2543          } elsif ($self->{next_char} == -1) {
2544            !!!cp (193);
2545            !!!parse-error (type => 'unclosed PUBLIC literal');
2546    
2547            $self->{state} = DATA_STATE;
2548          ## reconsume          ## reconsume
2549    
2550          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2551          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2552    
2553          redo A;          redo A;
2554        } else {        } else {
2555            !!!cp (194);
2556          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2557              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2558          ## Stay in the state          ## Stay in the state
2559          !!!next-input-character;          !!!next-input-character;
2560          redo A;          redo A;
2561        }        }
2562      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2563        if ({        if ({
2564              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2565              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2566            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2567            !!!cp (195);
2568          ## Stay in the state          ## Stay in the state
2569          !!!next-input-character;          !!!next-input-character;
2570          redo A;          redo A;
2571        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2572            !!!cp (196);
2573          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2574          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2575          !!!next-input-character;          !!!next-input-character;
2576          redo A;          redo A;
2577        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2578            !!!cp (197);
2579          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2580          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2581          !!!next-input-character;          !!!next-input-character;
2582          redo A;          redo A;
2583        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2584          $self->{state} = 'data';          !!!cp (198);
2585            $self->{state} = DATA_STATE;
2586          !!!next-input-character;          !!!next-input-character;
2587    
2588          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2589    
2590          redo A;          redo A;
2591        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2592            !!!cp (199);
2593          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2594    
2595          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2596          ## reconsume          ## reconsume
2597    
2598          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2599          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2600    
2601          redo A;          redo A;
2602        } else {        } else {
2603            !!!cp (200);
2604          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2605          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2606    
2607            $self->{state} = BOGUS_DOCTYPE_STATE;
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        }        }
2611      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2612        if ({        if ({
2613              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2614              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2615            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2616            !!!cp (201);
2617          ## Stay in the state          ## Stay in the state
2618          !!!next-input-character;          !!!next-input-character;
2619          redo A;          redo A;
2620        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2621            !!!cp (202);
2622          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2623          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2624          !!!next-input-character;          !!!next-input-character;
2625          redo A;          redo A;
2626        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2627            !!!cp (203);
2628          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2629          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2630          !!!next-input-character;          !!!next-input-character;
2631          redo A;          redo A;
2632        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2633            !!!cp (204);
2634          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2635          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2636          !!!next-input-character;          !!!next-input-character;
2637    
2638          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2639          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2640    
2641          redo A;          redo A;
2642        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2643            !!!cp (205);
2644          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2645    
2646          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2647          ## reconsume          ## reconsume
2648    
2649          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2650          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2651    
2652          redo A;          redo A;
2653        } else {        } else {
2654            !!!cp (206);
2655          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2656          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2657    
2658            $self->{state} = BOGUS_DOCTYPE_STATE;
2659          !!!next-input-character;          !!!next-input-character;
2660          redo A;          redo A;
2661        }        }
2662      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2663        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2664          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2665            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2666            !!!next-input-character;
2667            redo A;
2668          } elsif ($self->{next_char} == 0x003E) { # >
2669            !!!cp (208);
2670            !!!parse-error (type => 'unclosed PUBLIC literal');
2671    
2672            $self->{state} = DATA_STATE;
2673          !!!next-input-character;          !!!next-input-character;
2674    
2675            $self->{current_token}->{quirks} = 1;
2676            !!!emit ($self->{current_token}); # DOCTYPE
2677    
2678          redo A;          redo A;
2679        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2680            !!!cp (209);
2681          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2682    
2683          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2684          ## reconsume          ## reconsume
2685    
2686          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2687          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2688    
2689          redo A;          redo A;
2690        } else {        } else {
2691            !!!cp (210);
2692          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2693              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2694          ## Stay in the state          ## Stay in the state
2695          !!!next-input-character;          !!!next-input-character;
2696          redo A;          redo A;
2697        }        }
2698      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2699        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2700          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2701            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2702            !!!next-input-character;
2703            redo A;
2704          } elsif ($self->{next_char} == 0x003E) { # >
2705            !!!cp (212);
2706            !!!parse-error (type => 'unclosed PUBLIC literal');
2707    
2708            $self->{state} = DATA_STATE;
2709          !!!next-input-character;          !!!next-input-character;
2710    
2711            $self->{current_token}->{quirks} = 1;
2712            !!!emit ($self->{current_token}); # DOCTYPE
2713    
2714          redo A;          redo A;
2715        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2716            !!!cp (213);
2717          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2718    
2719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2720          ## reconsume          ## reconsume
2721    
2722          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2723          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2724    
2725          redo A;          redo A;
2726        } else {        } else {
2727            !!!cp (214);
2728          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2729              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2730          ## Stay in the state          ## Stay in the state
2731          !!!next-input-character;          !!!next-input-character;
2732          redo A;          redo A;
2733        }        }
2734      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2735        if ({        if ({
2736              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2737              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2738            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2739            !!!cp (215);
2740          ## Stay in the state          ## Stay in the state
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2744          $self->{state} = 'data';          !!!cp (216);
2745            $self->{state} = DATA_STATE;
2746          !!!next-input-character;          !!!next-input-character;
2747    
2748          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2749    
2750          redo A;          redo A;
2751        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2752            !!!cp (217);
2753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2754            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2755          ## reconsume          ## reconsume
2756    
2757          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2758          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2759    
2760          redo A;          redo A;
2761        } else {        } else {
2762            !!!cp (218);
2763          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2764          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2765    
2766            $self->{state} = BOGUS_DOCTYPE_STATE;
2767          !!!next-input-character;          !!!next-input-character;
2768          redo A;          redo A;
2769        }        }
2770      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2771        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2772          $self->{state} = 'data';          !!!cp (219);
2773            $self->{state} = DATA_STATE;
2774          !!!next-input-character;          !!!next-input-character;
2775    
         delete $self->{current_token}->{correct};  
2776          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2777    
2778          redo A;          redo A;
2779        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2780            !!!cp (220);
2781          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2782          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2783          ## reconsume          ## reconsume
2784    
         delete $self->{current_token}->{correct};  
2785          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2786    
2787          redo A;          redo A;
2788        } else {        } else {
2789            !!!cp (221);
2790          ## Stay in the state          ## Stay in the state
2791          !!!next-input-character;          !!!next-input-character;
2792          redo A;          redo A;
2793        }        }
2794        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2795          my $s = '';
2796          
2797          my ($l, $c) = ($self->{line}, $self->{column});
2798    
2799          CS: while ($self->{next_char} != -1) {
2800            if ($self->{next_char} == 0x005D) { # ]
2801              !!!next-input-character;
2802              if ($self->{next_char} == 0x005D) { # ]
2803                !!!next-input-character;
2804                MDC: {
2805                  if ($self->{next_char} == 0x003E) { # >
2806                    !!!cp (221.1);
2807                    !!!next-input-character;
2808                    last CS;
2809                  } elsif ($self->{next_char} == 0x005D) { # ]
2810                    !!!cp (221.2);
2811                    $s .= ']';
2812                    !!!next-input-character;
2813                    redo MDC;
2814                  } else {
2815                    !!!cp (221.3);
2816                    $s .= ']]';
2817                    #
2818                  }
2819                } # MDC
2820              } else {
2821                !!!cp (221.4);
2822                $s .= ']';
2823                #
2824              }
2825            } else {
2826              !!!cp (221.5);
2827              #
2828            }
2829            $s .= chr $self->{next_char};
2830            !!!next-input-character;
2831          } # CS
2832    
2833          $self->{state} = DATA_STATE;
2834          ## next-input-character done or EOF, which is reconsumed.
2835    
2836          if (length $s) {
2837            !!!cp (221.6);
2838            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2839                      line => $l, column => $c});
2840          } else {
2841            !!!cp (221.7);
2842          }
2843    
2844          redo A;
2845    
2846          ## ISSUE: "text tokens" in spec.
2847          ## TODO: Streaming support
2848      } else {      } else {
2849        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2850      }      }
# Line 1609  sub _get_next_token ($) { Line 2853  sub _get_next_token ($) {
2853    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2854  } # _get_next_token  } # _get_next_token
2855    
2856  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2857    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2858    
2859      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2860    
2861    if ({    if ({
2862         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2863         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2864        }->{$self->{next_input_character}}) {         $additional => 1,
2865          }->{$self->{next_char}}) {
2866        !!!cp (1001);
2867      ## Don't consume      ## Don't consume
2868      ## No error      ## No error
2869      return undef;      return undef;
2870    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2871      !!!next-input-character;      !!!next-input-character;
2872      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2873          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2874        my $code;        my $code;
2875        X: {        X: {
2876          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2877          !!!next-input-character;          !!!next-input-character;
2878          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2879              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2880              !!!cp (1002);
2881            $code ||= 0;            $code ||= 0;
2882            $code *= 0x10;            $code *= 0x10;
2883            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2884            redo X;            redo X;
2885          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2886                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2887              !!!cp (1003);
2888            $code ||= 0;            $code ||= 0;
2889            $code *= 0x10;            $code *= 0x10;
2890            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2891            redo X;            redo X;
2892          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2893                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2894              !!!cp (1004);
2895            $code ||= 0;            $code ||= 0;
2896            $code *= 0x10;            $code *= 0x10;
2897            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2898            redo X;            redo X;
2899          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2900            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2901            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2902            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2903              $self->{next_char} = 0x0023; # #
2904            return undef;            return undef;
2905          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2906              !!!cp (1006);
2907            !!!next-input-character;            !!!next-input-character;
2908          } else {          } else {
2909            !!!parse-error (type => 'no refc');            !!!cp (1007);
2910              !!!parse-error (type => 'no refc', line => $l, column => $c);
2911          }          }
2912    
2913          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2914            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2915              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2916            $code = 0xFFFD;            $code = 0xFFFD;
2917          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2918            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2919              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2920            $code = 0xFFFD;            $code = 0xFFFD;
2921          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2922            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2923              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2924            $code = 0x000A;            $code = 0x000A;
2925          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2926            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2927              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2928            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2929          }          }
2930    
2931          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2932                    has_reference => 1,
2933                    line => $l, column => $c,
2934                   };
2935        } # X        } # X
2936      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2937               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2938        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2939        !!!next-input-character;        !!!next-input-character;
2940                
2941        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2942                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2943            !!!cp (1012);
2944          $code *= 10;          $code *= 10;
2945          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2946                    
2947          !!!next-input-character;          !!!next-input-character;
2948        }        }
2949    
2950        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2951            !!!cp (1013);
2952          !!!next-input-character;          !!!next-input-character;
2953        } else {        } else {
2954          !!!parse-error (type => 'no refc');          !!!cp (1014);
2955            !!!parse-error (type => 'no refc', line => $l, column => $c);
2956        }        }
2957    
2958        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2959          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2960            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2961          $code = 0xFFFD;          $code = 0xFFFD;
2962        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2963          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2964            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2965          $code = 0xFFFD;          $code = 0xFFFD;
2966        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2967          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2968            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2969          $code = 0x000A;          $code = 0x000A;
2970        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2971          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2972            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2973          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2974        }        }
2975                
2976        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2977                  line => $l, column => $c,
2978                 };
2979      } else {      } else {
2980        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2981        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2982        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2983          $self->{next_char} = 0x0023; # #
2984        return undef;        return undef;
2985      }      }
2986    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2987              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2988             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2989              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2990      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2991      !!!next-input-character;      !!!next-input-character;
2992    
2993      my $value = $entity_name;      my $value = $entity_name;
# Line 1724  sub _tokenize_attempt_to_consume_an_enti Line 2995  sub _tokenize_attempt_to_consume_an_enti
2995      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2996      our $EntityChar;      our $EntityChar;
2997    
2998      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2999             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3000             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3001               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3002              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3003               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3004              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3005               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3006              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3007        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3008        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3009          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3010              !!!cp (1020);
3011            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3012            $match = 1;            $match = 1;
3013            !!!next-input-character;            !!!next-input-character;
3014            last;            last;
3015          } else {          } else {
3016              !!!cp (1021);
3017            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3018            $match = -1;            $match = -1;
3019            !!!next-input-character;            !!!next-input-character;
3020          }          }
3021        } else {        } else {
3022          $value .= chr $self->{next_input_character};          !!!cp (1022);
3023            $value .= chr $self->{next_char};
3024          $match *= 2;          $match *= 2;
3025          !!!next-input-character;          !!!next-input-character;
3026        }        }
3027      }      }
3028            
3029      if ($match > 0) {      if ($match > 0) {
3030        return {type => 'character', data => $value};        !!!cp (1023);
3031          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3032                  line => $l, column => $c,
3033                 };
3034      } elsif ($match < 0) {      } elsif ($match < 0) {
3035        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3036        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3037          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
3038        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3039          return {type => 'character', data => $value};                  line => $l, column => $c,
3040                   };
3041          } else {
3042            !!!cp (1025);
3043            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3044                    line => $l, column => $c,
3045                   };
3046        }        }
3047      } else {      } else {
3048        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3049        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3050        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3051          return {type => CHARACTER_TOKEN, data => '&'.$value,
3052                  line => $l, column => $c,
3053                 };
3054      }      }
3055    } else {    } else {
3056        !!!cp (1027);
3057      ## no characters are consumed      ## no characters are consumed
3058      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3059      return undef;      return undef;
3060    }    }
3061  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1806  sub _construct_tree ($) { Line 3093  sub _construct_tree ($) {
3093        
3094    !!!next-token;    !!!next-token;
3095    
   $self->{insertion_mode} = 'before head';  
3096    undef $self->{form_element};    undef $self->{form_element};
3097    undef $self->{head_element};    undef $self->{head_element};
3098    $self->{open_elements} = [];    $self->{open_elements} = [];
3099    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3100    
3101      ## NOTE: The "initial" insertion mode.
3102    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3103    
3104      ## NOTE: The "before html" insertion mode.
3105    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3106      $self->{insertion_mode} = BEFORE_HEAD_IM;
3107    
3108      ## NOTE: The "before head" insertion mode and so on.
3109    $self->_tree_construction_main;    $self->_tree_construction_main;
3110  } # _construct_tree  } # _construct_tree
3111    
3112  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3113    my $self = shift;    my $self = shift;
3114    
3115      ## NOTE: "initial" insertion mode
3116    
3117    INITIAL: {    INITIAL: {
3118      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3119        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3120        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3121        ## language.        ## language.
# Line 1830  sub _tree_construction_initial ($) { Line 3125  sub _tree_construction_initial ($) {
3125        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3126            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3127            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3128          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3129            !!!parse-error (type => 'not HTML5', token => $token);
3130        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3131            !!!cp ('t2');
3132          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3133          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3134          } else {
3135            !!!cp ('t3');
3136        }        }
3137                
3138        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3139          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3140          ## NOTE: Default value for both |public_id| and |system_id| attributes
3141          ## are empty strings, so that we don't set any value in missing cases.
3142        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3143            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3144        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 3147  sub _tree_construction_initial ($) {
3147        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3148        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3149                
3150        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3151            !!!cp ('t4');
3152          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3153        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3154          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3155          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3156          if ({          my $prefix = [
3157            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3158            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3159            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3160            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3161            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3162            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3165            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3166            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3167            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3168            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3169            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3170            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3171            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3172            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3173            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3174            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3175            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3176            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3177            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3178            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3179            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3180            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3181            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3182            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3183            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3184            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3185            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3186            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3187            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3188            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3189            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3190            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3191            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3192            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3193            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3194            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3195            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3196            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3197            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3198            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3199            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3200            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3201            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3202            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3203            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3204            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3205            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3206            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3207            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3208            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3209            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3210            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3211            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3212            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3213            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3214            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3215            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3216            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3217            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3218            "-//W3C//DTD W3 HTML//EN" => 1,            }
3219            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3220            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3221            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3222            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3223            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3224            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3225            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3226          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3227                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3228            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3229                !!!cp ('t6');
3230              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3231            } else {            } else {
3232                !!!cp ('t7');
3233              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3234            }            }
3235          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3236                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3237              !!!cp ('t8');
3238            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3239            } else {
3240              !!!cp ('t9');
3241          }          }
3242          } else {
3243            !!!cp ('t10');
3244        }        }
3245        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3246          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3247          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3248          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") {
3249              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3250              ## marked as quirks.
3251            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3252              !!!cp ('t11');
3253            } else {
3254              !!!cp ('t12');
3255          }          }
3256          } else {
3257            !!!cp ('t13');
3258        }        }
3259                
3260        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3261        !!!next-token;        !!!next-token;
3262        return;        return;
3263      } elsif ({      } elsif ({
3264                'start tag' => 1,                START_TAG_TOKEN, 1,
3265                'end tag' => 1,                END_TAG_TOKEN, 1,
3266                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3267               }->{$token->{type}}) {               }->{$token->{type}}) {
3268        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3269          !!!parse-error (type => 'no DOCTYPE', token => $token);
3270        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3271        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3272        ## reprocess        ## reprocess
3273          !!!ack-later;
3274        return;        return;
3275      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3276        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3277          ## Ignore the token          ## Ignore the token
3278    
3279          unless (length $token->{data}) {          unless (length $token->{data}) {
3280            ## Stay in the phase            !!!cp ('t15');
3281              ## Stay in the insertion mode.
3282            !!!next-token;            !!!next-token;
3283            redo INITIAL;            redo INITIAL;
3284            } else {
3285              !!!cp ('t16');
3286          }          }
3287          } else {
3288            !!!cp ('t17');
3289        }        }
3290    
3291        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3292        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3293        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3294        ## reprocess        ## reprocess
3295        return;        return;
3296      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3297          !!!cp ('t18');
3298        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3299        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3300                
3301        ## Stay in the phase.        ## Stay in the insertion mode.
3302        !!!next-token;        !!!next-token;
3303        redo INITIAL;        redo INITIAL;
3304      } else {      } else {
3305        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3306      }      }
3307    } # INITIAL    } # INITIAL
3308    
3309      die "$0: _tree_construction_initial: This should be never reached";
3310  } # _tree_construction_initial  } # _tree_construction_initial
3311    
3312  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3313    my $self = shift;    my $self = shift;
3314    
3315      ## NOTE: "before html" insertion mode.
3316        
3317    B: {    B: {
3318        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3319          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3320            !!!parse-error (type => 'in html:#DOCTYPE', token => $token); ## TODO: type
3321          ## Ignore the token          ## Ignore the token
3322          ## Stay in the phase          ## Stay in the insertion mode.
3323          !!!next-token;          !!!next-token;
3324          redo B;          redo B;
3325        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3326            !!!cp ('t20');
3327          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3328          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3329          ## Stay in the phase          ## Stay in the insertion mode.
3330          !!!next-token;          !!!next-token;
3331          redo B;          redo B;
3332        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3333          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3334            ## Ignore the token.            ## Ignore the token.
3335    
3336            unless (length $token->{data}) {            unless (length $token->{data}) {
3337              ## Stay in the phase              !!!cp ('t21');
3338                ## Stay in the insertion mode.
3339              !!!next-token;              !!!next-token;
3340              redo B;              redo B;
3341              } else {
3342                !!!cp ('t22');
3343            }            }
3344            } else {
3345              !!!cp ('t23');
3346          }          }
3347    
3348            $self->{application_cache_selection}->(undef);
3349    
3350          #          #
3351          } elsif ($token->{type} == START_TAG_TOKEN) {
3352            if ($token->{tag_name} eq 'html') {
3353              my $root_element;
3354              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3355              $self->{document}->append_child ($root_element);
3356              push @{$self->{open_elements}},
3357                  [$root_element, $el_category->{html}];
3358    
3359              if ($token->{attributes}->{manifest}) {
3360                !!!cp ('t24');
3361                $self->{application_cache_selection}
3362                    ->($token->{attributes}->{manifest}->{value});
3363                ## ISSUE: Spec is unclear on relative references.
3364                ## According to Hixie (#whatwg 2008-03-19), it should be
3365                ## resolved against the base URI of the document in HTML
3366                ## or xml:base of the element in XHTML.
3367              } else {
3368                !!!cp ('t25');
3369                $self->{application_cache_selection}->(undef);
3370              }
3371    
3372              !!!nack ('t25c');
3373    
3374              !!!next-token;
3375              return; ## Go to the "before head" insertion mode.
3376            } else {
3377              !!!cp ('t25.1');
3378              #
3379            }
3380        } elsif ({        } elsif ({
3381                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3382                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3383                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3384          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3385          #          #
3386        } else {        } else {
3387          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3388        }        }
3389        my $root_element; !!!create-element ($root_element, 'html');  
3390        $self->{document}->append_child ($root_element);      my $root_element;
3391        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3392        ## reprocess      $self->{document}->append_child ($root_element);
3393        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3394        return; ## Go to the main phase.  
3395        $self->{application_cache_selection}->(undef);
3396    
3397        ## NOTE: Reprocess the token.
3398        !!!ack-later;
3399        return; ## Go to the "before head" insertion mode.
3400    
3401        ## ISSUE: There is an issue in the spec
3402    } # B    } # B
3403    
3404      die "$0: _tree_construction_root_element: This should never be reached";
3405  } # _tree_construction_root_element  } # _tree_construction_root_element
3406    
3407  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3416  sub _reset_insertion_mode ($) {
3416            
3417      ## Step 3      ## Step 3
3418      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"!?  
3419        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3420          $last = 1;          $last = 1;
3421          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3422            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3423                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3424              #          } else {
3425            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3426          }          }
3427        }        }
3428              
3429        ## Step 4..13        ## Step 4..14
3430        my $new_mode = {        my $new_mode;
3431                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3432                        td => 'in cell',          !!!cp ('t28.1');
3433                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3434                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3435                        tbody => 'in table body',          ## SVG elements as foreigners.
3436                        thead => 'in table head',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3437                        tfoot => 'in table foot',        } elsif ($node->[1] & TABLE_CELL_EL) {
3438                        caption => 'in caption',          if ($last) {
3439                        colgroup => 'in column group',            !!!cp ('t28.2');
3440                        table => 'in table',            #
3441                        head => 'in body', # not in head!          } else {
3442                        body => 'in body',            !!!cp ('t28.3');
3443                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3444                       }->{$node->[1]};          }
3445          } else {
3446            !!!cp ('t28.4');
3447            $new_mode = {
3448                          select => IN_SELECT_IM,
3449                          ## NOTE: |option| and |optgroup| do not set
3450                          ## insertion mode to "in select" by themselves.
3451                          tr => IN_ROW_IM,
3452                          tbody => IN_TABLE_BODY_IM,
3453                          thead => IN_TABLE_BODY_IM,
3454                          tfoot => IN_TABLE_BODY_IM,
3455                          caption => IN_CAPTION_IM,
3456                          colgroup => IN_COLUMN_GROUP_IM,
3457                          table => IN_TABLE_IM,
3458                          head => IN_BODY_IM, # not in head!
3459                          body => IN_BODY_IM,
3460                          frameset => IN_FRAMESET_IM,
3461                         }->{$node->[0]->manakai_local_name};
3462          }
3463        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3464                
3465        ## Step 14        ## Step 15
3466        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3467          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3468            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3469              $self->{insertion_mode} = BEFORE_HEAD_IM;
3470          } else {          } else {
3471            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3472              !!!cp ('t30');
3473              $self->{insertion_mode} = AFTER_HEAD_IM;
3474          }          }
3475          return;          return;
3476          } else {
3477            !!!cp ('t31');
3478        }        }
3479                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3480        ## Step 16        ## Step 16
3481          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3482          
3483          ## Step 17
3484        $i--;        $i--;
3485        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3486                
3487        ## Step 17        ## Step 18
3488        redo S3;        redo S3;
3489      } # S3      } # S3
3490    
3491      die "$0: _reset_insertion_mode: This line should never be reached";
3492  } # _reset_insertion_mode  } # _reset_insertion_mode
3493    
3494  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3495    my $self = shift;    my $self = shift;
3496    
   my $previous_insertion_mode;  
   
3497    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3498    
3499    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3510  sub _tree_construction_main ($) {
3510      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3511      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3512        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3513            !!!cp ('t32');
3514          return;          return;
3515        }        }
3516      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3525  sub _tree_construction_main ($) {
3525    
3526        ## Step 6        ## Step 6
3527        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3528            !!!cp ('t33_1');
3529          #          #
3530        } else {        } else {
3531          my $in_open_elements;          my $in_open_elements;
3532          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3533            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3534                !!!cp ('t33');
3535              $in_open_elements = 1;              $in_open_elements = 1;
3536              last OE;              last OE;
3537            }            }
3538          }          }
3539          if ($in_open_elements) {          if ($in_open_elements) {
3540              !!!cp ('t34');
3541            #            #
3542          } else {          } else {
3543              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3544              !!!cp ('t35');
3545            redo S4;            redo S4;
3546          }          }
3547        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3564  sub _tree_construction_main ($) {
3564    
3565        ## Step 11        ## Step 11
3566        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3567            !!!cp ('t36');
3568          ## Step 7'          ## Step 7'
3569          $i++;          $i++;
3570          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3571                    
3572          redo S7;          redo S7;
3573        }        }
3574    
3575          !!!cp ('t37');
3576      } # S7      } # S7
3577    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3578    
3579    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3580      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3581        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3582            !!!cp ('t38');
3583          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3584          return;          return;
3585        }        }
3586      }      }
3587    
3588        !!!cp ('t39');
3589    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3590    
3591    my $parse_rcdata = sub ($$) {    my $insert;
3592      my ($content_model_flag, $insert) = @_;  
3593      my $parse_rcdata = sub ($) {
3594        my ($content_model_flag) = @_;
3595    
3596      ## Step 1      ## Step 1
3597      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3598      my $el;      my $el;
3599      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3600    
3601      ## Step 2      ## Step 2
3602      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3603    
3604      ## Step 3      ## Step 3
3605      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2204  sub _tree_construction_main ($) { Line 3607  sub _tree_construction_main ($) {
3607    
3608      ## Step 4      ## Step 4
3609      my $text = '';      my $text = '';
3610        !!!nack ('t40.1');
3611      !!!next-token;      !!!next-token;
3612      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3613          !!!cp ('t40');
3614        $text .= $token->{data};        $text .= $token->{data};
3615        !!!next-token;        !!!next-token;
3616      }      }
3617    
3618      ## Step 5      ## Step 5
3619      if (length $text) {      if (length $text) {
3620          !!!cp ('t41');
3621        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3622        $el->append_child ($text);        $el->append_child ($text);
3623      }      }
# Line 2220  sub _tree_construction_main ($) { Line 3626  sub _tree_construction_main ($) {
3626      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3627    
3628      ## Step 7      ## Step 7
3629      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3630            $token->{tag_name} eq $start_tag_name) {
3631          !!!cp ('t42');
3632        ## 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});  
3633      } else {      } else {
3634        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3635          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3636            !!!cp ('t43');
3637            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3638          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3639            !!!cp ('t44');
3640            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3641          } else {
3642            die "$0: $content_model_flag in parse_rcdata";
3643          }
3644      }      }
3645      !!!next-token;      !!!next-token;
3646    }; # $parse_rcdata    }; # $parse_rcdata
3647    
3648    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3649      my $script_el;      my $script_el;
3650      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3651      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3652    
3653      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3654      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3655            
3656      my $text = '';      my $text = '';
3657        !!!nack ('t45.1');
3658      !!!next-token;      !!!next-token;
3659      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3660          !!!cp ('t45');
3661        $text .= $token->{data};        $text .= $token->{data};
3662        !!!next-token;        !!!next-token;
3663      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3664      if (length $text) {      if (length $text) {
3665          !!!cp ('t46');
3666        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3667      }      }
3668                                
3669      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3670    
3671      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3672          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3673          !!!cp ('t47');
3674        ## Ignore the token        ## Ignore the token
3675      } else {      } else {
3676        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3677          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3678        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3679        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3680      }      }
3681            
3682      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3683          !!!cp ('t49');
3684        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3685      } else {      } else {
3686          !!!cp ('t50');
3687        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3688        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3689    
# Line 2278  sub _tree_construction_main ($) { Line 3697  sub _tree_construction_main ($) {
3697      !!!next-token;      !!!next-token;
3698    }; # $script_start_tag    }; # $script_start_tag
3699    
3700      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3701      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3702      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3703    
3704    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3705      my $tag_name = shift;      my $end_tag_token = shift;
3706        my $tag_name = $end_tag_token->{tag_name};
3707    
3708        ## NOTE: The adoption agency algorithm (AAA).
3709    
3710      FET: {      FET: {
3711        ## Step 1        ## Step 1
3712        my $formatting_element;        my $formatting_element;
3713        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3714        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3715          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3716              !!!cp ('t52');
3717              last AFE;
3718            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3719                         eq $tag_name) {
3720              !!!cp ('t51');
3721            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3722            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3723            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3724          }          }
3725        } # AFE        } # AFE
3726        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3727          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3728            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3729          ## Ignore the token          ## Ignore the token
3730          !!!next-token;          !!!next-token;
3731          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3737  sub _tree_construction_main ($) {
3737          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3738          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3739            if ($in_scope) {            if ($in_scope) {
3740                !!!cp ('t54');
3741              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3742              last INSCOPE;              last INSCOPE;
3743            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3744              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3745                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3746                                token => $end_tag_token);
3747              ## Ignore the token              ## Ignore the token
3748              !!!next-token;              !!!next-token;
3749              return;              return;
3750            }            }
3751          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3752                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3753            $in_scope = 0;            $in_scope = 0;
3754          }          }
3755        } # INSCOPE        } # INSCOPE
3756        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3757          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3758            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3759                            token => $end_tag_token);
3760          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3761          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3762          return;          return;
3763        }        }
3764        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3765          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3766            !!!parse-error (type => 'not closed',
3767                            value => $self->{open_elements}->[-1]->[0]
3768                                ->manakai_local_name,
3769                            token => $end_tag_token);
3770        }        }
3771                
3772        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 3774  sub _tree_construction_main ($) {
3774        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3775        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3776          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3777          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3778              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3779              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3780               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3781              !!!cp ('t59');
3782            $furthest_block = $node;            $furthest_block = $node;
3783            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3784          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3785              !!!cp ('t60');
3786            last OE;            last OE;
3787          }          }
3788        } # OE        } # OE
3789                
3790        ## Step 3        ## Step 3
3791        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3792            !!!cp ('t61');
3793          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3794          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3795          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3802  sub _tree_construction_main ($) {
3802        ## Step 5        ## Step 5
3803        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3804        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3805            !!!cp ('t62');
3806          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3807        }        }
3808                
# Line 2384  sub _tree_construction_main ($) { Line 3825  sub _tree_construction_main ($) {
3825          S7S2: {          S7S2: {
3826            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3827              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3828                  !!!cp ('t63');
3829                $node_i_in_active = $_;                $node_i_in_active = $_;
3830                last S7S2;                last S7S2;
3831              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839                    
3840          ## Step 4          ## Step 4
3841          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3842              !!!cp ('t64');
3843            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3844          }          }
3845                    
3846          ## Step 5          ## Step 5
3847          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3848              !!!cp ('t65');
3849            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3850            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3851            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863        } # S7          } # S7  
3864                
3865        ## Step 8        ## Step 8
3866        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3867            my $foster_parent_element;
3868            my $next_sibling;
3869            OE: for (reverse 0..$#{$self->{open_elements}}) {
3870              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3871                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3872                                 if (defined $parent and $parent->node_type == 1) {
3873                                   !!!cp ('t65.1');
3874                                   $foster_parent_element = $parent;
3875                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3876                                 } else {
3877                                   !!!cp ('t65.2');
3878                                   $foster_parent_element
3879                                     = $self->{open_elements}->[$_ - 1]->[0];
3880                                 }
3881                                 last OE;
3882                               }
3883                             } # OE
3884                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3885                               unless defined $foster_parent_element;
3886            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3887            $open_tables->[-1]->[1] = 1; # tainted
3888          } else {
3889            !!!cp ('t65.3');
3890            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3891          }
3892                
3893        ## Step 9        ## Step 9
3894        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3905  sub _tree_construction_main ($) {
3905        my $i;        my $i;
3906        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3907          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3908              !!!cp ('t66');
3909            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3910            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3911          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3912              !!!cp ('t67');
3913            $i = $_;            $i = $_;
3914          }          }
3915        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3919  sub _tree_construction_main ($) {
3919        undef $i;        undef $i;
3920        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3921          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3922              !!!cp ('t68');
3923            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3924            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3925          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3926              !!!cp ('t69');
3927            $i = $_;            $i = $_;
3928          }          }
3929        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934      } # FET      } # FET
3935    }; # $formatting_end_tag    }; # $formatting_end_tag
3936    
3937    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3938      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3939    }; # $insert_to_current    }; # $insert_to_current
3940    
3941    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3942                         my $child = shift;      my $child = shift;
3943                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3944                              table => 1, tbody => 1, tfoot => 1,        # MUST
3945                              thead => 1, tr => 1,        my $foster_parent_element;
3946                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3947                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3948                           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') {  
3949                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3950                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3951                                   !!!cp ('t70');
3952                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3953                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3954                               } else {                               } else {
3955                                   !!!cp ('t71');
3956                                 $foster_parent_element                                 $foster_parent_element
3957                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3958                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 3963  sub _tree_construction_main ($) {
3963                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3964                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3965                             ($child, $next_sibling);                             ($child, $next_sibling);
3966                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3967                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3968                         }        !!!cp ('t72');
3969          $self->{open_elements}->[-1]->[0]->append_child ($child);
3970        }
3971    }; # $insert_to_foster    }; # $insert_to_foster
3972    
3973    my $in_body = sub {    B: while (1) {
3974      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3975      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3976        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3977          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
3978          $script_start_tag->($insert);        ## Stay in the phase
3979          return;        !!!next-token;
3980        } elsif ($token->{tag_name} eq 'style') {        next B;
3981          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
3982          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);               $token->{tag_name} eq 'html') {
3983          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3984        } elsif ({          !!!cp ('t79');
3985                  base => 1, link => 1,          !!!parse-error (type => 'after html:html', token => $token);
3986                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
3987          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3988          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3989          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html:html', token => $token);
3990          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3991          return;        } else {
3992        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
3993          ## NOTE: This is an "as if in head" code clone, only "-t" differs        }
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
3994    
3995          !!!next-token;        !!!cp ('t82');
3996          return;        !!!parse-error (type => 'not first start tag', token => $token);
3997        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3998          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3999          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4000          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {            !!!cp ('t84');
4001            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
4002              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
4003            } else {               $token->{attributes}->{$attr_name}->{value});
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } 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];  
           !!!next-token;  
           return;  
4004          }          }
4005        } elsif ($token->{tag_name} eq 'li') {        }
4006          ## has a p element in scope        !!!nack ('t84.1');
4007          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
4008            if ($_->[1] eq 'p') {        next B;
4009              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
4010              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
4011              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4012            } elsif ({          !!!cp ('t85');
4013                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
4014                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4015                     }->{$_->[1]}) {          !!!cp ('t86');
4016              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
4017            }        } else {
4018          } # INSCOPE          !!!cp ('t87');
4019                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
4020          ## Step 1        }
4021          my $i = -1;        !!!next-token;
4022          my $node = $self->{open_elements}->[$i];        next B;
4023          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4024            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
4025            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
4026              if ($i != -1) {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## 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;  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## 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;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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});  
             
4027          !!!next-token;          !!!next-token;
4028          return;          next B;
4029        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4030          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4031            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4032            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4033              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4034                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4035              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4036              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4037              $formatting_end_tag->($token->{tag_name});            #
4038                        } elsif ({
4039              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4040                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4041                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4042                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4043                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4044              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4045              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4046                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4047                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4048                  last OE;            !!!cp ('t87.2');
4049                }            !!!parse-error (type => 'not closed',
4050              } # OE                            value => $self->{open_elements}->[-1]->[0]
4051              last AFE;                                ->manakai_local_name,
4052            } elsif ($node->[0] eq '#marker') {                            token => $token);
4053              last AFE;  
4054              pop @{$self->{open_elements}}
4055                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4056    
4057              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4058              ## Reprocess.
4059              next B;
4060            } else {
4061              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4062              my $tag_name = $token->{tag_name};
4063              if ($nsuri eq $SVG_NS) {
4064                $tag_name = {
4065                   altglyph => 'altGlyph',
4066                   altglyphdef => 'altGlyphDef',
4067                   altglyphitem => 'altGlyphItem',
4068                   animatecolor => 'animateColor',
4069                   animatemotion => 'animateMotion',
4070                   animatetransform => 'animateTransform',
4071                   clippath => 'clipPath',
4072                   feblend => 'feBlend',
4073                   fecolormatrix => 'feColorMatrix',
4074                   fecomponenttransfer => 'feComponentTransfer',
4075                   fecomposite => 'feComposite',
4076                   feconvolvematrix => 'feConvolveMatrix',
4077                   fediffuselighting => 'feDiffuseLighting',
4078                   fedisplacementmap => 'feDisplacementMap',
4079                   fedistantlight => 'feDistantLight',
4080                   feflood => 'feFlood',
4081                   fefunca => 'feFuncA',
4082                   fefuncb => 'feFuncB',
4083                   fefuncg => 'feFuncG',
4084                   fefuncr => 'feFuncR',
4085                   fegaussianblur => 'feGaussianBlur',
4086                   feimage => 'feImage',
4087                   femerge => 'feMerge',
4088                   femergenode => 'feMergeNode',
4089                   femorphology => 'feMorphology',
4090                   feoffset => 'feOffset',
4091                   fepointlight => 'fePointLight',
4092                   fespecularlighting => 'feSpecularLighting',
4093                   fespotlight => 'feSpotLight',
4094                   fetile => 'feTile',
4095                   feturbulence => 'feTurbulence',
4096                   foreignobject => 'foreignObject',
4097                   glyphref => 'glyphRef',
4098                   lineargradient => 'linearGradient',
4099                   radialgradient => 'radialGradient',
4100                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4101                   textpath => 'textPath',  
4102                }->{$tag_name} || $tag_name;
4103            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4104    
4105          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            ## "adjust SVG attributes" (SVG only) - done in insert-element-f
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
4106    
4107          !!!next-token;            ## "adjust foreign attributes" - done in insert-element-f
         return;  
       } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
4108    
4109          ## has a |nobr| element in scope            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } 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});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
4110    
4111          ## NOTE: There is an "as if <br>" code clone.            if ($self->{self_closing}) {
4112          $reconstruct_active_formatting_elements->($insert_to_current);              pop @{$self->{open_elements}};
4113                        !!!ack ('t87.3');
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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;  
         return;  
       } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4114            } else {            } else {
4115              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
4116            }            }
4117            push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4118            !!!next-token;            !!!next-token;
4119              next B;
4120          }          }
4121          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4122            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4123          }          !!!cp ('t87.5');
4124                    #
4125          $self->{content_model} = PCDATA_CONTENT_MODEL;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4126                    !!!cp ('t87.6');
4127          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
4128              $token->{tag_name} eq $tag_name) {                          value => $self->{open_elements}->[-1]->[0]
4129            ## Ignore the token                              ->manakai_local_name,
4130          } else {                          token => $token);
4131            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4132          }          pop @{$self->{open_elements}}
4133          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4134          return;  
4135        } elsif ({          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4136                  iframe => 1,          ## Reprocess.
4137                  noembed => 1,          next B;
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4138        } else {        } else {
4139          $reconstruct_active_formatting_elements->($insert_to_current);          die "$0: $token->{type}: Unknown token type";        
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
4140        }        }
4141      } elsif ($token->{type} eq 'end tag') {      }
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
4142    
4143            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4144            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4145            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4146          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4147            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4148            ## Ignore the token              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4149            } else {            } else {
4150              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4151            }              ## Ignore the token.
4152          }              !!!next-token;
4153                        next B;
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4154            }            }
4155          } # INSCOPE            unless (length $token->{data}) {
4156                        !!!cp ('t88');
4157          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {              !!!next-token;
4158            pop @{$self->{open_elements}};              next B;
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## 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]}) {  
             ## 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4159            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4160          }          }
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 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,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
4161    
4162          ## As if <br>          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4163          $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t89');
4164                      ## As if <head>
4165          my $el;            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4166          !!!create-element ($el, 'br');            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4167          $insert->($el);            push @{$self->{open_elements}},
4168                          [$self->{head_element}, $el_category->{head}];
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4169    
4170          ## Step 2            ## Reprocess in the "in head" insertion mode...
4171          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4172    
4173              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4174              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4175            } else {            !!!cp ('t90');
4176              ## Step 3            ## As if </noscript>
4177              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4178                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#character', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4179                        
4180            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4181            redo S2;            ## As if </head>
4182          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($token->{type} eq 'DOCTYPE') {  
       !!!parse-error (type => 'DOCTYPE in the middle');  
       ## Ignore the token  
       ## Stay in the phase  
       !!!next-token;  
       redo B;  
     } elsif ($token->{type} eq 'end-of-file') {  
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## 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', 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.  
       }  
4183    
4184        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4185        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4186      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4187               $token->{tag_name} eq 'html') {            pop @{$self->{open_elements}};
       if ($self->{insertion_mode} eq 'trailing end') {  
         ## Turn into the main phase  
         !!!parse-error (type => 'after html:html');  
         $self->{insertion_mode} = $previous_insertion_mode;  
       }  
4188    
4189  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4190  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4191        unless ($token->{first_start_tag}) {            !!!cp ('t92');
         !!!parse-error (type => 'not first start tag');  
       }  
       my $top_el = $self->{open_elements}->[0]->[0];  
       for my $attr_name (keys %{$token->{attributes}}) {  
         unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
           $top_el->set_attribute_ns  
             (undef, [undef, $attr_name],  
              $token->{attributes}->{$attr_name}->{value});  
4192          }          }
4193        }  
4194        !!!next-token;          ## "after head" insertion mode
4195        redo B;          ## As if <body>
4196      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4197        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4198        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4199          $self->{document}->append_child ($comment);          next B;
4200        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4201          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4202        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4203          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4204        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4205        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4206        redo B;                  ($self->{head_element});
4207      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4208            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4209              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4210                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4211                unless (length $token->{data}) {              !!!next-token;
4212                  !!!next-token;              next B;
4213                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4214                }              !!!cp ('t93.2');
4215              }              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4216              ## As if <head>              ## Ignore the token
4217              !!!create-element ($self->{head_element}, 'head');              !!!nack ('t93.3');
4218              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!next-token;
4219              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              next B;
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
4220            } else {            } else {
4221              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4222                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4223                ## Ignore the token
4224                !!!nack ('t95.1');
4225                !!!next-token;
4226                next B;
4227            }            }
4228          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4229                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4230                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4231            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4232              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4233                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4234                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4235                  !!!next-token;  
4236                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4237              ## Reprocess in the "in head" insertion mode...
4238            } else {
4239              !!!cp ('t97');
4240            }
4241    
4242                if ($token->{tag_name} eq 'base') {
4243                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4244                    !!!cp ('t98');
4245                    ## As if </noscript>
4246                    pop @{$self->{open_elements}};
4247                    !!!parse-error (type => 'in noscript:base', token => $token);
4248                  
4249                    $self->{insertion_mode} = IN_HEAD_IM;
4250                    ## Reprocess in the "in head" insertion mode...
4251                  } else {
4252                    !!!cp ('t99');
4253                }                }
4254              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
4255                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4256                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4258                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4259                    push @{$self->{open_elements}},
4260                        [$self->{head_element}, $el_category->{head}];
4261                  } else {
4262                    !!!cp ('t101');
4263                }                }
4264                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4265                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4266                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4267                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4268                  !!!nack ('t101.1');
4269                !!!next-token;                !!!next-token;
4270                redo B;                next B;
4271              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4272                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4273                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4274                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4275                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4276                    push @{$self->{open_elements}},
4277                        [$self->{head_element}, $el_category->{head}];
4278                  } else {
4279                    !!!cp ('t103');
4280                }                }
4281                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4282                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4283                  pop @{$self->{open_elements}} # <head>
4284                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4285                  !!!ack ('t103.1');
4286                  !!!next-token;
4287                  next B;
4288                } elsif ($token->{tag_name} eq 'meta') {
4289                  ## NOTE: There is a "as if in head" code clone.
4290                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4291                    !!!cp ('t104');
4292                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4293                    push @{$self->{open_elements}},
4294                        [$self->{head_element}, $el_category->{head}];
4295                  } else {
4296                    !!!cp ('t105');
4297                  }
4298                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4299                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4300    
4301                unless ($self->{confident}) {                unless ($self->{confident}) {
4302                  my $charset;                  if ($token->{attributes}->{charset}) {
4303                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4304                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4305                  }                    ## in the {change_encoding} callback.
4306                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4307                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4308                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4309                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4310                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4311                          ->set_user_data (manakai_has_reference =>
4312                                               $token->{attributes}->{charset}
4313                                                   ->{has_reference});
4314                    } elsif ($token->{attributes}->{content}) {
4315                      if ($token->{attributes}->{content}->{value}
4316                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4317                              [\x09-\x0D\x20]*=
4318                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4319                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4320                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4321                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4322                        ## in the {change_encoding} callback.
4323                        $self->{change_encoding}
4324                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4325                               $token);
4326                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4327                            ->set_user_data (manakai_has_reference =>
4328                                                 $token->{attributes}->{content}
4329                                                       ->{has_reference});
4330                      } else {
4331                        !!!cp ('t108');
4332                      }
4333                    }
4334                  } else {
4335                    if ($token->{attributes}->{charset}) {
4336                      !!!cp ('t109');
4337                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4338                          ->set_user_data (manakai_has_reference =>
4339                                               $token->{attributes}->{charset}
4340                                                   ->{has_reference});
4341                    }
4342                    if ($token->{attributes}->{content}) {
4343                      !!!cp ('t110');
4344                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4345                          ->set_user_data (manakai_has_reference =>
4346                                               $token->{attributes}->{content}
4347                                                   ->{has_reference});
4348                  }                  }
                 ## TODO: Change the encoding  
4349                }                }
4350    
4351                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4352                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4353                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4354                !!!next-token;                !!!next-token;
4355                redo B;                next B;
4356              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4357                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4358                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4359                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4360                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4361                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
4362                  
4363                    $self->{insertion_mode} = IN_HEAD_IM;
4364                    ## Reprocess in the "in head" insertion mode...
4365                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4366                    !!!cp ('t112');
4367                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4368                    push @{$self->{open_elements}},
4369                        [$self->{head_element}, $el_category->{head}];
4370                  } else {
4371                    !!!cp ('t113');
4372                }                }
4373    
4374                  ## NOTE: There is a "as if in head" code clone.
4375                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4376                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4377                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4378                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4379                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4380                    if $self->{insertion_mode} eq 'after head';                next B;
4381                redo B;              } elsif ($token->{tag_name} eq 'style' or
4382              } elsif ($token->{tag_name} eq 'style') {                       $token->{tag_name} eq 'noframes') {
4383                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4384                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4385                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4386                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4387                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4388                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4389                    push @{$self->{open_elements}},
4390                        [$self->{head_element}, $el_category->{head}];
4391                  } else {
4392                    !!!cp ('t115');
4393                }                }
4394                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4395                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4396                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4397                redo B;                next B;
4398              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4399                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4400                    !!!cp ('t116');
4401                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4402                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4403                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4404                    !!!nack ('t116.1');
4405                  !!!next-token;                  !!!next-token;
4406                  redo B;                  next B;
4407                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4408                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4409                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                    !!!nack ('t117.1');
4412                  !!!next-token;                  !!!next-token;
4413                  redo B;                  next B;
4414                } else {                } else {
4415                    !!!cp ('t118');
4416                  #                  #
4417                }                }
4418              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4419                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4420                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4421                ## Ignore the token                  ## As if </noscript>
4422                !!!next-token;                  pop @{$self->{open_elements}};
4423                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
4424              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
4425                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
4426                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
4427                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4428                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
4429                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4430                    push @{$self->{open_elements}},
4431                        [$self->{head_element}, $el_category->{head}];
4432                  } else {
4433                    !!!cp ('t121');
4434                }                }
4435    
4436                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4437                $script_start_tag->($insert_to_current);                $script_start_tag->();
4438                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4439                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4440                redo B;                next B;
4441              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
4442                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4443                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4444                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4445                    ## As if </noscript>
4446                    pop @{$self->{open_elements}};
4447                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4448                    
4449                    ## Reprocess in the "in head" insertion mode...
4450                    ## As if </head>
4451                    pop @{$self->{open_elements}};
4452                    
4453                    ## Reprocess in the "after head" insertion mode...
4454                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4455                    !!!cp ('t124');
4456                    pop @{$self->{open_elements}};
4457                    
4458                    ## Reprocess in the "after head" insertion mode...
4459                  } else {
4460                    !!!cp ('t125');
4461                  }
4462    
4463                  ## "after head" insertion mode
4464                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4465                  if ($token->{tag_name} eq 'body') {
4466                    !!!cp ('t126');
4467                    $self->{insertion_mode} = IN_BODY_IM;
4468                  } elsif ($token->{tag_name} eq 'frameset') {
4469                    !!!cp ('t127');
4470                    $self->{insertion_mode} = IN_FRAMESET_IM;
4471                  } else {
4472                    die "$0: tag name: $self->{tag_name}";
4473                  }
4474                  !!!nack ('t127.1');
4475                !!!next-token;                !!!next-token;
4476                redo B;                next B;
4477              } else {              } else {
4478                  !!!cp ('t128');
4479                #                #
4480              }              }
4481            } elsif ($token->{type} eq 'end tag') {  
4482              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4483                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4484                  ## As if </noscript>
4485                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4486                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4487                !!!next-token;                
4488                redo B;                ## Reprocess in the "in head" insertion mode...
4489              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
4490                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4491                $self->{insertion_mode} = 'in head';  
4492                !!!next-token;                ## Reprocess in the "after head" insertion mode...
4493                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4494              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
4495                       {                ## As if </head>
4496                  pop @{$self->{open_elements}};
4497    
4498                  ## Reprocess in the "after head" insertion mode...
4499                } else {
4500                  !!!cp ('t131');
4501                }
4502    
4503                ## "after head" insertion mode
4504                ## As if <body>
4505                !!!insert-element ('body',, $token);
4506                $self->{insertion_mode} = IN_BODY_IM;
4507                ## reprocess
4508                !!!ack-later;
4509                next B;
4510              } elsif ($token->{type} == END_TAG_TOKEN) {
4511                if ($token->{tag_name} eq 'head') {
4512                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4513                    !!!cp ('t132');
4514                    ## As if <head>
4515                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4516                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4517                    push @{$self->{open_elements}},
4518                        [$self->{head_element}, $el_category->{head}];
4519    
4520                    ## Reprocess in the "in head" insertion mode...
4521                    pop @{$self->{open_elements}};
4522                    $self->{insertion_mode} = AFTER_HEAD_IM;
4523                    !!!next-token;
4524                    next B;
4525                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4526                    !!!cp ('t133');
4527                    ## As if </noscript>
4528                    pop @{$self->{open_elements}};
4529                    !!!parse-error (type => 'in noscript:/head', token => $token);
4530                    
4531                    ## Reprocess in the "in head" insertion mode...
4532                    pop @{$self->{open_elements}};
4533                    $self->{insertion_mode} = AFTER_HEAD_IM;
4534                    !!!next-token;
4535                    next B;
4536                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4537                    !!!cp ('t134');
4538                    pop @{$self->{open_elements}};
4539                    $self->{insertion_mode} = AFTER_HEAD_IM;
4540                    !!!next-token;
4541                    next B;
4542                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4543                    !!!cp ('t134.1');
4544                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4545                    ## Ignore the token
4546                    !!!next-token;
4547                    next B;
4548                  } else {
4549                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4550                  }
4551                } elsif ($token->{tag_name} eq 'noscript') {
4552                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4553                    !!!cp ('t136');
4554                    pop @{$self->{open_elements}};
4555                    $self->{insertion_mode} = IN_HEAD_IM;
4556                    !!!next-token;
4557                    next B;
4558                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4559                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4560                    !!!cp ('t137');
4561                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4562                    ## Ignore the token ## ISSUE: An issue in the spec.
4563                    !!!next-token;
4564                    next B;
4565                  } else {
4566                    !!!cp ('t138');
4567                    #
4568                  }
4569                } elsif ({
4570                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
4571                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4572                #                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4573              } elsif ($self->{insertion_mode} eq 'in head noscript' and                    $self->{insertion_mode} == IN_HEAD_IM or
4574                       {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4575                        p => 1, br => 1,                  !!!cp ('t140');
4576                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4577                #                  ## Ignore the token
4578              } elsif ($self->{insertion_mode} ne 'after head') {                  !!!next-token;
4579                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  next B;
4580                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4581                    !!!cp ('t140.1');
4582                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4583                    ## Ignore the token
4584                    !!!next-token;
4585                    next B;
4586                  } else {
4587                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4588                  }
4589                } elsif ($token->{tag_name} eq 'p') {
4590                  !!!cp ('t142');
4591                  !!!parse-error (type => 'unmatched end tag:p', token => $token);
4592                ## Ignore the token                ## Ignore the token
4593                !!!next-token;                !!!next-token;
4594                redo B;                next B;
4595                } elsif ($token->{tag_name} eq 'br') {
4596                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4597                    !!!cp ('t142.2');
4598                    ## (before head) as if <head>, (in head) as if </head>
4599                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4600                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4601                    $self->{insertion_mode} = AFTER_HEAD_IM;
4602      
4603                    ## Reprocess in the "after head" insertion mode...
4604                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4605                    !!!cp ('t143.2');
4606                    ## As if </head>
4607                    pop @{$self->{open_elements}};
4608                    $self->{insertion_mode} = AFTER_HEAD_IM;
4609      
4610                    ## Reprocess in the "after head" insertion mode...
4611                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4612                    !!!cp ('t143.3');
4613                    ## ISSUE: Two parse errors for <head><noscript></br>
4614                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4615                    ## As if </noscript>
4616                    pop @{$self->{open_elements}};
4617                    $self->{insertion_mode} = IN_HEAD_IM;
4618    
4619                    ## Reprocess in the "in head" insertion mode...
4620                    ## As if </head>
4621                    pop @{$self->{open_elements}};
4622                    $self->{insertion_mode} = AFTER_HEAD_IM;
4623    
4624                    ## Reprocess in the "after head" insertion mode...
4625                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4626                    !!!cp ('t143.4');
4627                    #
4628                  } else {
4629                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4630                  }
4631    
4632                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4633                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4634                  ## Ignore the token
4635                  !!!next-token;
4636                  next B;
4637              } else {              } else {
4638                #                !!!cp ('t145');
4639                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4640                  ## Ignore the token
4641                  !!!next-token;
4642                  next B;
4643              }              }
           } else {  
             #  
           }  
4644    
4645            ## As if </head> or </noscript> or <body>              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4646            if ($self->{insertion_mode} eq 'in head') {                !!!cp ('t146');
4647              pop @{$self->{open_elements}};                ## As if </noscript>
4648              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
4649            } elsif ($self->{insertion_mode} eq 'in head noscript') {                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4650              pop @{$self->{open_elements}};                
4651              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));                ## Reprocess in the "in head" insertion mode...
4652              $self->{insertion_mode} = 'in head';                ## As if </head>
4653            } else { # 'after head'                pop @{$self->{open_elements}};
4654              !!!insert-element ('body');  
4655              $self->{insertion_mode} = 'in body';                ## Reprocess in the "after head" insertion mode...
4656            }              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4657            ## reprocess                !!!cp ('t147');
4658            redo B;                ## As if </head>
4659                  pop @{$self->{open_elements}};
4660    
4661                  ## Reprocess in the "after head" insertion mode...
4662                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4663    ## ISSUE: This case cannot be reached?
4664                  !!!cp ('t148');
4665                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4666                  ## Ignore the token ## ISSUE: An issue in the spec.
4667                  !!!next-token;
4668                  next B;
4669                } else {
4670                  !!!cp ('t149');
4671                }
4672    
4673                ## "after head" insertion mode
4674                ## As if <body>
4675                !!!insert-element ('body',, $token);
4676                $self->{insertion_mode} = IN_BODY_IM;
4677                ## reprocess
4678                next B;
4679          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4680            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4681              !!!cp ('t149.1');
4682    
4683              ## NOTE: As if <head>
4684              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4685              $self->{open_elements}->[-1]->[0]->append_child
4686                  ($self->{head_element});
4687              #push @{$self->{open_elements}},
4688              #    [$self->{head_element}, $el_category->{head}];
4689              #$self->{insertion_mode} = IN_HEAD_IM;
4690              ## NOTE: Reprocess.
4691    
4692              ## NOTE: As if </head>
4693              #pop @{$self->{open_elements}};
4694              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4695              ## NOTE: Reprocess.
4696              
4697              #
4698            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4699              !!!cp ('t149.2');
4700    
4701              ## NOTE: As if </head>
4702              pop @{$self->{open_elements}};
4703              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4704              ## NOTE: Reprocess.
4705    
4706              #
4707            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4708              !!!cp ('t149.3');
4709    
4710              !!!parse-error (type => 'in noscript:#eof', token => $token);
4711    
4712              ## As if </noscript>
4713              pop @{$self->{open_elements}};
4714              #$self->{insertion_mode} = IN_HEAD_IM;
4715              ## NOTE: Reprocess.
4716    
4717              ## NOTE: As if </head>
4718              pop @{$self->{open_elements}};
4719              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4720              ## NOTE: Reprocess.
4721    
4722              #
4723            } else {
4724              !!!cp ('t149.4');
4725              #
4726            }
4727    
4728            ## NOTE: As if <body>
4729            !!!insert-element ('body',, $token);
4730            $self->{insertion_mode} = IN_BODY_IM;
4731            ## NOTE: Reprocess.
4732            next B;
4733          } else {
4734            die "$0: $token->{type}: Unknown token type";
4735          }
4736    
4737            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4738          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4739                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
4740                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4741              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4742              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4743                            
4744              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4745    
4746              !!!next-token;              !!!next-token;
4747              redo B;              next B;
4748            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4749              if ({              if ({
4750                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4751                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4752                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4753                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4754                  ## have an element in table scope                  ## have an element in table scope
4755                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4756                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4757                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4758                      $tn = $node->[1];                      !!!cp ('t151');
4759                      last INSCOPE;  
4760                    } elsif ({                      ## Close the cell
4761                              table => 1, html => 1,                      !!!back-token; # <x>
4762                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4763                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4764                    }                                line => $token->{line},
4765                  } # INSCOPE                                column => $token->{column}};
4766                    unless (defined $tn) {                      next B;
4767                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4768                      ## Ignore the token                      !!!cp ('t152');
4769                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4770                      redo B;                      last;
4771                    }                    }
4772                    }
4773    
4774                    !!!cp ('t153');
4775                    !!!parse-error (type => 'start tag not allowed',
4776                        value => $token->{tag_name}, token => $token);
4777                    ## Ignore the token
4778                    !!!nack ('t153.1');
4779                    !!!next-token;
4780                    next B;
4781                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4782                    !!!parse-error (type => 'not closed:caption', token => $token);
4783                                    
4784                  ## Close the cell                  ## NOTE: As if </caption>.
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in caption') {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
4785                  ## have a table element in table scope                  ## have a table element in table scope
4786                  my $i;                  my $i;
4787                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4788                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4789                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4790                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4791                      last INSCOPE;                        !!!cp ('t155');
4792                    } elsif ({                        $i = $_;
4793                              table => 1, html => 1,                        last INSCOPE;
4794                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4795                      last INSCOPE;                        !!!cp ('t156');
4796                          last;
4797                        }
4798                    }                    }
4799    
4800                      !!!cp ('t157');
4801                      !!!parse-error (type => 'start tag not allowed',
4802                                      value => $token->{tag_name}, token => $token);
4803                      ## Ignore the token
4804                      !!!nack ('t157.1');
4805                      !!!next-token;
4806                      next B;
4807                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4808                                    
4809                  ## generate implied end tags                  ## generate implied end tags
4810                  if ({                  while ($self->{open_elements}->[-1]->[1]
4811                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4812                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4813                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4814                  }                  }
4815    
4816                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4817                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4818                      !!!parse-error (type => 'not closed',
4819                                      value => $self->{open_elements}->[-1]->[0]
4820                                          ->manakai_local_name,
4821                                      token => $token);
4822                    } else {
4823                      !!!cp ('t160');
4824                  }                  }
4825                                    
4826                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4827                                    
4828                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4829                                    
4830                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4831                                    
4832                  ## reprocess                  ## reprocess
4833                  redo B;                  !!!ack-later;
4834                    next B;
4835                } else {                } else {
4836                    !!!cp ('t161');
4837                  #                  #
4838                }                }
4839              } else {              } else {
4840                  !!!cp ('t162');
4841                #                #
4842              }              }
4843            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4844              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4845                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4846                  ## have an element in table scope                  ## have an element in table scope
4847                  my $i;                  my $i;
4848                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4849                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4850                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4851                        !!!cp ('t163');
4852                      $i = $_;                      $i = $_;
4853                      last INSCOPE;                      last INSCOPE;
4854                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4855                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4856                      last INSCOPE;                      last INSCOPE;
4857                    }                    }
4858                  } # INSCOPE                  } # INSCOPE
4859                    unless (defined $i) {                    unless (defined $i) {
4860                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4861                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4862                      ## Ignore the token                      ## Ignore the token
4863                      !!!next-token;                      !!!next-token;
4864                      redo B;                      next B;
4865                    }                    }
4866                                    
4867                  ## generate implied end tags                  ## generate implied end tags
4868                  if ({                  while ($self->{open_elements}->[-1]->[1]
4869                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4870                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4871                       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',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4872                  }                  }
4873                    
4874                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4875                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4876                      !!!cp ('t167');
4877                      !!!parse-error (type => 'not closed',
4878                                      value => $self->{open_elements}->[-1]->[0]
4879                                          ->manakai_local_name,
4880                                      token => $token);
4881                    } else {
4882                      !!!cp ('t168');
4883                  }                  }
4884                                    
4885                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4886                                    
4887                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4888                                    
4889                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
4890                                    
4891                  !!!next-token;                  !!!next-token;
4892                  redo B;                  next B;
4893                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4894                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4895                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4896                  ## Ignore the token                  ## Ignore the token
4897                  !!!next-token;                  !!!next-token;
4898                  redo B;                  next B;
4899                } else {                } else {
4900                    !!!cp ('t170');
4901                  #                  #
4902                }                }
4903              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4904                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4905                  ## have a table element in table scope                  ## have a table element in table scope
4906                  my $i;                  my $i;
4907                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4908                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4909                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4910                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4911                      last INSCOPE;                        !!!cp ('t171');
4912                    } elsif ({                        $i = $_;
4913                              table => 1, html => 1,                        last INSCOPE;
4914                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4915                      last INSCOPE;                        !!!cp ('t172');
4916                          last;
4917                        }
4918                    }                    }
4919    
4920                      !!!cp ('t173');
4921                      !!!parse-error (type => 'unmatched end tag',
4922                                      value => $token->{tag_name}, token => $token);
4923                      ## Ignore the token
4924                      !!!next-token;
4925                      next B;
4926                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4927                                    
4928                  ## generate implied end tags                  ## generate implied end tags
4929                  if ({                  while ($self->{open_elements}->[-1]->[1]
4930                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4931                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4932                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4933                  }                  }
4934                                    
4935                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4936                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4937                      !!!parse-error (type => 'not closed',
4938                                      value => $self->{open_elements}->[-1]->[0]
4939                                          ->manakai_local_name,
4940                                      token => $token);
4941                    } else {
4942                      !!!cp ('t176');
4943                  }                  }
4944                                    
4945                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4946                                    
4947                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4948                                    
4949                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4950                                    
4951                  !!!next-token;                  !!!next-token;
4952                  redo B;                  next B;
4953                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4954                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4955                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                  ## Ignore the token                  ## Ignore the token
4957                  !!!next-token;                  !!!next-token;
4958                  redo B;                  next B;
4959                } else {                } else {
4960                    !!!cp ('t178');
4961                  #                  #
4962                }                }
4963              } elsif ({              } elsif ({
4964                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
4965                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4966                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4967                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
4968                ## have an element in table scope                ## have an element in table scope
4969                my $i;                my $i;
4970                my $tn;                my $tn;
4971                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4972                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4973                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4974                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4975                    last INSCOPE;                      !!!cp ('t179');
4976                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4977                    $tn = $node->[1];  
4978                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4979                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4980                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4981                            table => 1, html => 1,                                line => $token->{line},
4982                           }->{$node->[1]}) {                                column => $token->{column}};
4983                    last INSCOPE;                      next B;
4984                      } elsif ($node->[1] & TABLE_CELL_EL) {
4985                        !!!cp ('t180');
4986                        $tn = $node->[0]->manakai_local_name;
4987                        ## NOTE: There is exactly one |td| or |th| element
4988                        ## in scope in the stack of open elements by definition.
4989                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4990                        ## ISSUE: Can this be reached?
4991                        !!!cp ('t181');
4992                        last;
4993                      }
4994                  }                  }
4995                } # INSCOPE  
4996                unless (defined $i) {                  !!!cp ('t182');
4997                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4998                        value => $token->{tag_name}, token => $token);
4999                  ## Ignore the token                  ## Ignore the token
5000                  !!!next-token;                  !!!next-token;
5001                  redo B;                  next B;
5002                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5003              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5004                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5005                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
5006    
5007                ## As if </caption>                ## As if </caption>
5008                ## have a table element in table scope                ## have a table element in table scope
5009                my $i;                my $i;
5010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5012                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5013                      !!!cp ('t184');
5014                    $i = $_;                    $i = $_;
5015                    last INSCOPE;                    last INSCOPE;
5016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5017                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5018                    last INSCOPE;                    last INSCOPE;
5019                  }                  }
5020                } # INSCOPE                } # INSCOPE
5021                unless (defined $i) {                unless (defined $i) {
5022                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5023                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5024                  ## Ignore the token                  ## Ignore the token
5025                  !!!next-token;                  !!!next-token;
5026                  redo B;                  next B;
5027                }                }
5028                                
5029                ## generate implied end tags                ## generate implied end tags
5030                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5031                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5032                     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', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5033                }                }
5034    
5035                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5037                    !!!parse-error (type => 'not closed',
5038                                    value => $self->{open_elements}->[-1]->[0]
5039                                        ->manakai_local_name,
5040                                    token => $token);
5041                  } else {
5042                    !!!cp ('t189');
5043                }                }
5044    
5045                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5046    
5047                $clear_up_to_marker->();                $clear_up_to_marker->();
5048    
5049                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5050    
5051                ## reprocess                ## reprocess
5052                redo B;                next B;
5053              } elsif ({              } elsif ({
5054                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5055                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5056                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5057                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5058                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5059                  ## Ignore the token                  ## Ignore the token
5060                  !!!next-token;                  !!!next-token;
5061                  redo B;                  next B;
5062                } else {                } else {
5063                    !!!cp ('t191');
5064                  #                  #
5065                }                }
5066              } elsif ({              } elsif ({
5067                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5068                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5069                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5070                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5071                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5072                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5073                ## Ignore the token                ## Ignore the token
5074                !!!next-token;                !!!next-token;
5075                redo B;                next B;
5076              } else {              } else {
5077                  !!!cp ('t193');
5078                #                #
5079              }              }
5080            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5081              #          for my $entry (@{$self->{open_elements}}) {
5082              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5083                !!!cp ('t75');
5084                !!!parse-error (type => 'in body:#eof', token => $token);
5085                last;
5086            }            }
5087                      }
5088            $in_body->($insert_to_current);  
5089            redo B;          ## Stop parsing.
5090          } elsif ($self->{insertion_mode} eq 'in table') {          last B;
5091            if ($token->{type} eq 'character') {        } else {
5092              ## NOTE: There are "character in table" code clones.          die "$0: $token->{type}: Unknown token type";
5093              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        }
5094                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5095          $insert = $insert_to_current;
5096          #
5097        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5098          if ($token->{type} == CHARACTER_TOKEN) {
5099            if (not $open_tables->[-1]->[1] and # tainted
5100                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5101              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5102                                
5103                unless (length $token->{data}) {            unless (length $token->{data}) {
5104                  !!!next-token;              !!!cp ('t194');
5105                  redo B;              !!!next-token;
5106                }              next B;
5107              }            } else {
5108                !!!cp ('t195');
5109              }
5110            }
5111    
5112              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5113    
5114              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5115              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3989  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117              ## result in a new Text node.              ## result in a new Text node.
5118              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5119                            
5120              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]}) {  
5121                # MUST                # MUST
5122                my $foster_parent_element;                my $foster_parent_element;
5123                my $next_sibling;                my $next_sibling;
5124                my $prev_sibling;                my $prev_sibling;
5125                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5126                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5127                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5128                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5129                        !!!cp ('t196');
5130                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5131                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5132                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5133                    } else {                    } else {
5134                        !!!cp ('t197');
5135                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5136                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5137                    }                    }
# Line 4016  sub _tree_construction_main ($) { Line 5143  sub _tree_construction_main ($) {
5143                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5144                if (defined $prev_sibling and                if (defined $prev_sibling and
5145                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5146                    !!!cp ('t198');
5147                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5148                } else {                } else {
5149                    !!!cp ('t199');
5150                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5151                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5152                     $next_sibling);                     $next_sibling);
5153                }                }
5154              } else {            $open_tables->[-1]->[1] = 1; # tainted
5155                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5156              }            !!!cp ('t200');
5157              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5158            }
5159                            
5160              !!!next-token;          !!!next-token;
5161              redo B;          next B;
5162            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5163              if ({              if ({
5164                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5165                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
5166                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5167                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
5168                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
5169                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while (not ($self->{open_elements}->[-1]->[1]
5170                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                  & TABLE_SCOPING_EL)) {
5171                  pop @{$self->{open_elements}};                    !!!cp ('t201');
5172                }                    pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5173                  }                  }
5174                } # INSCOPE                  
5175                unless (defined $i) {                  !!!insert-element ('tbody',, $token);
5176                  !!!parse-error (type => 'unmatched end tag:table');                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5177                  ## Ignore tokens </table><table>                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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]);  
5178                }                }
5179    
5180                splice @{$self->{open_elements}}, $i;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5181                    unless ($token->{tag_name} eq 'tr') {
5182                $self->_reset_insertion_mode;                    !!!cp ('t202');
5183                      !!!parse-error (type => 'missing start tag:tr', token => $token);
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5184                  }                  }
5185                } # INSCOPE                  
5186                unless (defined $i) {                  ## Clear back to table body context
5187                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not ($self->{open_elements}->[-1]->[1]
5188                  ## Ignore the token                                  & TABLE_ROWS_SCOPING_EL)) {
5189                  !!!next-token;                    !!!cp ('t203');
5190                  redo B;                    ## ISSUE: Can this case be reached?
5191                }                    pop @{$self->{open_elements}};
5192                                  }
5193                ## generate implied end tags                  
5194                if ({                  $self->{insertion_mode} = IN_ROW_IM;
5195                     dd => 1, dt => 1, li => 1, p => 1,                  if ($token->{tag_name} eq 'tr') {
5196                     td => 1, th => 1, tr => 1,                    !!!cp ('t204');
5197                     tbody => 1, tfoot=> 1, thead => 1,                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5198                    }->{$self->{open_elements}->[-1]->[1]}) {                    !!!nack ('t204');
5199                  !!!back-token;                    !!!next-token;
5200                  $token = {type => 'end tag',                    next B;
5201                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  } else {
5202                  redo B;                    !!!cp ('t205');
5203                      !!!insert-element ('tr',, $token);
5204                      ## reprocess in the "in row" insertion mode
5205                    }
5206                  } else {
5207                    !!!cp ('t206');
5208                }                }
5209    
5210                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
5211                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                while (not ($self->{open_elements}->[-1]->[1]
5212                                  & TABLE_ROW_SCOPING_EL)) {
5213                    !!!cp ('t207');
5214                    pop @{$self->{open_elements}};
5215                }                }
5216                  
5217                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5218                  $self->{insertion_mode} = IN_CELL_IM;
5219    
5220                splice @{$self->{open_elements}}, $i;                push @$active_formatting_elements, ['#marker', ''];
5221                  
5222                $self->_reset_insertion_mode;                !!!nack ('t207.1');
   
5223                !!!next-token;                !!!next-token;
5224                redo B;                next B;
5225              } elsif ({              } elsif ({
5226                        body => 1, caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5227                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                        tbody => 1, tfoot => 1, thead => 1,
5228                        thead => 1, tr => 1,                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5229                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5230                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == IN_ROW_IM) {
5231                ## Ignore the token                  ## As if </tr>
5232                !!!next-token;                  ## have an element in table scope
5233                redo B;                  my $i;
5234              } else {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                #                    my $node = $self->{open_elements}->[$_];
5236              }                    if ($node->[1] & TABLE_ROW_EL) {
5237            } else {                      !!!cp ('t208');
5238              #                      $i = $_;
5239            }                      last INSCOPE;
5240                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5241            !!!parse-error (type => 'in table:'.$token->{tag_name});                      !!!cp ('t209');
5242            $in_body->($insert_to_foster);                      last INSCOPE;
5243            redo B;                    }
5244          } elsif ($self->{insertion_mode} eq 'in column group') {                  } # INSCOPE
5245            if ($token->{type} eq 'character') {                  unless (defined $i) {
5246              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                    !!!cp ('t210');
5247                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  ## TODO: This type is wrong.
5248                unless (length $token->{data}) {                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5249                  !!!next-token;                    ## Ignore the token
5250                  redo B;                    !!!nack ('t210.1');
5251                }                    !!!next-token;
5252              }                    next B;
5253                                }
5254              #                  
5255            } elsif ($token->{type} eq 'start tag') {                  ## Clear back to table row context
5256              if ($token->{tag_name} eq 'col') {                  while (not ($self->{open_elements}->[-1]->[1]
5257                !!!insert-element ($token->{tag_name}, $token->{attributes});                                  & TABLE_ROW_SCOPING_EL)) {
5258                pop @{$self->{open_elements}};                    !!!cp ('t211');
5259                !!!next-token;                    ## ISSUE: Can this case be reached?
5260                redo B;                    pop @{$self->{open_elements}};
5261              } else {                  }
5262                #                  
5263              }                  pop @{$self->{open_elements}}; # tr
5264            } elsif ($token->{type} eq 'end tag') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5265              if ($token->{tag_name} eq 'colgroup') {                  if ($token->{tag_name} eq 'tr') {
5266                if ($self->{open_elements}->[-1]->[1] eq 'html') {                    !!!cp ('t212');
5267                  !!!parse-error (type => 'unmatched end tag:colgroup');                    ## reprocess
5268                  ## Ignore the token                    !!!ack-later;
5269                  !!!next-token;                    next B;
5270                  redo B;                  } else {
5271                } else {                    !!!cp ('t213');
5272                  pop @{$self->{open_elements}}; # colgroup                    ## reprocess in the "in table body" insertion mode...
5273                  $self->{insertion_mode} = 'in table';                  }
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
5274                }                }
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
5275    
5276              if ({                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5277                   table => 1, tbody => 1, tfoot => 1,                  ## have an element in table scope
5278                   thead => 1, tr => 1,                  my $i;
5279                  }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5280                # MUST                    my $node = $self->{open_elements}->[$_];
5281                my $foster_parent_element;                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5282                my $next_sibling;                      !!!cp ('t214');
5283                my $prev_sibling;                      $i = $_;
5284                OE: for (reverse 0..$#{$self->{open_elements}}) {                      last INSCOPE;
5285                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5286                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                      !!!cp ('t215');
5287                    if (defined $parent and $parent->node_type == 1) {                      last INSCOPE;
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
5288                    }                    }
5289                    last OE;                  } # INSCOPE
5290                    unless (defined $i) {
5291                      !!!cp ('t216');
5292    ## TODO: This erorr type ios wrong.
5293                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5294                      ## Ignore the token
5295                      !!!nack ('t216.1');
5296                      !!!next-token;
5297                      next B;
5298                  }                  }
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
5299    
5300                ## Clear back to table body context                  ## Clear back to table body context
5301                while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5302                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5303                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5304                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5305                      pop @{$self->{open_elements}};
5306                    }
5307                    
5308                    ## As if <{current node}>
5309                    ## have an element in table scope
5310                    ## true by definition
5311                    
5312                    ## Clear back to table body context
5313                    ## nop by definition
5314                    
5315                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5316                }                  $self->{insertion_mode} = IN_TABLE_IM;
5317                                  ## reprocess in "in table" insertion mode...
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
5318                } else {                } else {
5319                  !!!insert-element ('tr');                  !!!cp ('t218');
                 ## reprocess  
5320                }                }
5321                redo B;  
5322              } elsif ({                if ($token->{tag_name} eq 'col') {
5323                        caption => 1, col => 1, colgroup => 1,                  ## Clear back to table context
5324                        tbody => 1, tfoot => 1, thead => 1,                  while (not ($self->{open_elements}->[-1]->[1]
5325                       }->{$token->{tag_name}}) {                                  & TABLE_SCOPING_EL)) {
5326                ## have an element in table scope                    !!!cp ('t219');
5327                my $i;                    ## ISSUE: Can this state be reached?
5328                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    pop @{$self->{open_elements}};
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5329                  }                  }
5330                } # INSCOPE                  
5331                unless (defined $i) {                  !!!insert-element ('colgroup',, $token);
5332                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5333                  ## Ignore the token                  ## reprocess
5334                    !!!ack-later;
5335                    next B;
5336                  } elsif ({
5337                            caption => 1,
5338                            colgroup => 1,
5339                            tbody => 1, tfoot => 1, thead => 1,
5340                           }->{$token->{tag_name}}) {
5341                    ## Clear back to table context
5342                    while (not ($self->{open_elements}->[-1]->[1]
5343                                    & TABLE_SCOPING_EL)) {
5344                      !!!cp ('t220');
5345                      ## ISSUE: Can this state be reached?
5346                      pop @{$self->{open_elements}};
5347                    }
5348                    
5349                    push @$active_formatting_elements, ['#marker', '']
5350                        if $token->{tag_name} eq 'caption';
5351                    
5352                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5353                    $self->{insertion_mode} = {
5354                                               caption => IN_CAPTION_IM,
5355                                               colgroup => IN_COLUMN_GROUP_IM,
5356                                               tbody => IN_TABLE_BODY_IM,
5357                                               tfoot => IN_TABLE_BODY_IM,
5358                                               thead => IN_TABLE_BODY_IM,
5359                                              }->{$token->{tag_name}};
5360                  !!!next-token;                  !!!next-token;
5361                  redo B;                  !!!nack ('t220.1');
5362                }                  next B;
5363                  } else {
5364                ## Clear back to table body context                  die "$0: in table: <>: $token->{tag_name}";
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5365                }                }
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
5366              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5367                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5368                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5369                                      ->manakai_local_name,
5370                                  token => $token);
5371    
5372                ## As if </table>                ## As if </table>
5373                ## have a table element in table scope                ## have a table element in table scope
5374                my $i;                my $i;
5375                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5376                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5377                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5378                      !!!cp ('t221');
5379                    $i = $_;                    $i = $_;
5380                    last INSCOPE;                    last INSCOPE;
5381                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5382                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5383                    last INSCOPE;                    last INSCOPE;
5384                  }                  }
5385                } # INSCOPE                } # INSCOPE
5386                unless (defined $i) {                unless (defined $i) {
5387                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5388    ## TODO: The following is wrong, maybe.
5389                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5390                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5391                    !!!nack ('t223.1');
5392                  !!!next-token;                  !!!next-token;
5393                  redo B;                  next B;
5394                }                }
5395                                
5396    ## TODO: Followings are removed from the latest spec.
5397                ## generate implied end tags                ## generate implied end tags
5398                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5399                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5400                     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', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5401                }                }
5402    
5403                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5404                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5405                    ## NOTE: |<table><tr><table>|
5406                    !!!parse-error (type => 'not closed',
5407                                    value => $self->{open_elements}->[-1]->[0]
5408                                        ->manakai_local_name,
5409                                    token => $token);
5410                  } else {
5411                    !!!cp ('t226');
5412                }                }
5413    
5414                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5415                  pop @{$open_tables};
5416    
5417                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5418    
5419                ## reprocess            ## reprocess
5420                redo B;            !!!ack-later;
5421              } else {            next B;
5422                #          } elsif ($token->{tag_name} eq 'style') {
5423              }            if (not $open_tables->[-1]->[1]) { # tainted
5424            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5425              if ({              ## NOTE: This is a "as if in head" code clone.
5426                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5427                  }->{$token->{tag_name}}) {              next B;
5428                ## have an element in table scope            } else {
5429                my $i;              !!!cp ('t227.7');
5430                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5431                  my $node = $self->{open_elements}->[$_];            }
5432                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5433                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5434                    last INSCOPE;              !!!cp ('t227.6');
5435                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5436                            table => 1, html => 1,              $script_start_tag->();
5437                           }->{$node->[1]}) {              next B;
5438                    last INSCOPE;            } else {
5439                  }              !!!cp ('t227.5');
5440                } # INSCOPE              #
5441                unless (defined $i) {            }
5442                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5443                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5444                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5445                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5446                }                if ($type eq 'hidden') {
5447                    !!!cp ('t227.3');
5448                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5449    
5450                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
5451    
5452                pop @{$self->{open_elements}};                  ## TODO: form element pointer
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5453    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5454                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5455    
5456                pop @{$self->{open_elements}};                  !!!next-token;
5457                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5458                ## reprocess                  next B;
5459                redo B;                } else {
5460              } elsif ({                  !!!cp ('t227.2');
5461                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5462                        html => 1, td => 1, th => 1, tr => 1,                }
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5463              } else {              } else {
5464                  !!!cp ('t227.1');
5465                #                #
5466              }              }
5467            } else {            } else {
5468                !!!cp ('t227.4');
5469              #              #
5470            }            }
5471                      } else {
5472            ## As if in table            !!!cp ('t227');
5473            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5474            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
5475    
5476              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5477    
5478              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5479              ## ISSUE: Spec says that "whenever a node would be inserted          #
5480              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5481              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5482              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5483                ## have an element in table scope                ## have an element in table scope
5484                my $i;                my $i;
5485                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5486                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5487                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5488                      !!!cp ('t228');
5489                    $i = $_;                    $i = $_;
5490                    last INSCOPE;                    last INSCOPE;
5491                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5492                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5493                    last INSCOPE;                    last INSCOPE;
5494                  }                  }
5495                } # INSCOPE                } # INSCOPE
5496                unless (defined $i) {                unless (defined $i) {
5497                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5498                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5499                  ## Ignore the token                  ## Ignore the token
5500                    !!!nack ('t230.1');
5501                  !!!next-token;                  !!!next-token;
5502                  redo B;                  next B;
5503                  } else {
5504                    !!!cp ('t232');
5505                }                }
5506    
5507                ## Clear back to table row context                ## Clear back to table row context
5508                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5509                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5510                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5511                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5513                }                }
5514    
5515                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5516                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5517                ## reprocess                !!!next-token;
5518                redo B;                !!!nack ('t231.1');
5519                  next B;
5520              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5521                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5522                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5523                    ## have an element in table scope
5524                ## As if </table>                  my $i;
5525                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5526                my $i;                    my $node = $self->{open_elements}->[$_];
5527                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5528                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5529                  if ($node->[1] eq 'table') {                      $i = $_;
5530                    $i = $_;                      last INSCOPE;
5531                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5532                  } elsif ({                      !!!cp ('t234');
5533                            table => 1, html => 1,                      last INSCOPE;
5534                           }->{$node->[1]}) {                    }
5535                    last INSCOPE;                  } # INSCOPE
5536                    unless (defined $i) {
5537                      !!!cp ('t235');
5538    ## TODO: The following is wrong.
5539                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5540                      ## Ignore the token
5541                      !!!nack ('t236.1');
5542                      !!!next-token;
5543                      next B;
5544                  }                  }
5545                } # INSCOPE                  
5546                unless (defined $i) {                  ## Clear back to table row context
5547                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5548                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5549                  !!!next-token;                    !!!cp ('t236');
5550                  redo B;  ## ISSUE: Can this state be reached?
5551                }                    pop @{$self->{open_elements}};
5552                                  }
5553                ## generate implied end tags                  
5554                if ({                  pop @{$self->{open_elements}}; # tr
5555                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5556                     td => 1, th => 1, tr => 1,                  ## reprocess in the "in table body" insertion mode...
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5557                }                }
5558    
5559                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5560                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## have an element in table scope
5561                    my $i;
5562                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5563                      my $node = $self->{open_elements}->[$_];
5564                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5565                        !!!cp ('t237');
5566                        $i = $_;
5567                        last INSCOPE;
5568                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5569                        !!!cp ('t238');
5570                        last INSCOPE;
5571                      }
5572                    } # INSCOPE
5573                    unless (defined $i) {
5574                      !!!cp ('t239');
5575                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5576                      ## Ignore the token
5577                      !!!nack ('t239.1');
5578                      !!!next-token;
5579                      next B;
5580                    }
5581                    
5582                    ## Clear back to table body context
5583                    while (not ($self->{open_elements}->[-1]->[1]
5584                                    & TABLE_ROWS_SCOPING_EL)) {
5585                      !!!cp ('t240');
5586                      pop @{$self->{open_elements}};
5587                    }
5588                    
5589                    ## As if <{current node}>
5590                    ## have an element in table scope
5591                    ## true by definition
5592                    
5593                    ## Clear back to table body context
5594                    ## nop by definition
5595                    
5596                    pop @{$self->{open_elements}};
5597                    $self->{insertion_mode} = IN_TABLE_IM;
5598                    ## reprocess in the "in table" insertion mode...
5599                }                }
5600    
5601                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
5602                  ## When you edit the code fragment below, please ensure that
5603                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
5604                  ## is synced with it.
5605    
5606                ## reprocess                ## have a table element in table scope
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
5607                my $i;                my $i;
5608                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5609                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5610                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5611                      !!!cp ('t241');
5612                    $i = $_;                    $i = $_;
5613                    last INSCOPE;                    last INSCOPE;
5614                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5615                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5616                    last INSCOPE;                    last INSCOPE;
5617                  }                  }
5618                } # INSCOPE                } # INSCOPE
5619                unless (defined $i) {                unless (defined $i) {
5620                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5621                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5622                  ## Ignore the token                  ## Ignore the token
5623                    !!!nack ('t243.1');
5624                  !!!next-token;                  !!!next-token;
5625                  redo B;                  next B;
5626                }                }
5627                    
5628                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5629                while (not {                pop @{$open_tables};
5630                  tr => 1, html => 1,                
5631                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5632                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5633                  pop @{$self->{open_elements}};                !!!next-token;
5634                  next B;
5635                } elsif ({
5636                          tbody => 1, tfoot => 1, thead => 1,
5637                         }->{$token->{tag_name}} and
5638                         $self->{insertion_mode} & ROW_IMS) {
5639                  if ($self->{insertion_mode} == IN_ROW_IM) {
5640                    ## have an element in table scope
5641                    my $i;
5642                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5643                      my $node = $self->{open_elements}->[$_];
5644                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5645                        !!!cp ('t247');
5646                        $i = $_;
5647                        last INSCOPE;
5648                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5649                        !!!cp ('t248');
5650                        last INSCOPE;
5651                      }
5652                    } # INSCOPE
5653                      unless (defined $i) {
5654                        !!!cp ('t249');
5655                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5656                        ## Ignore the token
5657                        !!!nack ('t249.1');
5658                        !!!next-token;
5659                        next B;
5660                      }
5661                    
5662                    ## As if </tr>
5663                    ## have an element in table scope
5664                    my $i;
5665                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5666                      my $node = $self->{open_elements}->[$_];
5667                      if ($node->[1] & TABLE_ROW_EL) {
5668                        !!!cp ('t250');
5669                        $i = $_;
5670                        last INSCOPE;
5671                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5672                        !!!cp ('t251');
5673                        last INSCOPE;
5674                      }
5675                    } # INSCOPE
5676                      unless (defined $i) {
5677                        !!!cp ('t252');
5678                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5679                        ## Ignore the token
5680                        !!!nack ('t252.1');
5681                        !!!next-token;
5682                        next B;
5683                      }
5684                    
5685                    ## Clear back to table row context
5686                    while (not ($self->{open_elements}->[-1]->[1]
5687                                    & TABLE_ROW_SCOPING_EL)) {
5688                      !!!cp ('t253');
5689    ## ISSUE: Can this case be reached?
5690                      pop @{$self->{open_elements}};
5691                    }
5692                    
5693                    pop @{$self->{open_elements}}; # tr
5694                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5695                    ## reprocess in the "in table body" insertion mode...
5696                }                }
5697    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
5698                ## have an element in table scope                ## have an element in table scope
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5703                      !!!cp ('t254');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5707                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
5710                } # INSCOPE                } # INSCOPE
5711                unless (defined $i) {                unless (defined $i) {
5712                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t256');
5713                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5714                  ## Ignore the token                  ## Ignore the token
5715                    !!!nack ('t256.1');
5716                  !!!next-token;                  !!!next-token;
5717                  redo B;                  next B;
5718                }                }
5719    
5720                ## Clear back to table row context                ## Clear back to table body context
5721                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5722                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5723                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5724                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5726                }                }
5727    
5728                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5729                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5730                ## reprocess                !!!nack ('t257.1');
5731                redo B;                !!!next-token;
5732                  next B;
5733              } elsif ({              } elsif ({
5734                        tbody => 1, tfoot => 1, thead => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5735                          html => 1, td => 1, th => 1,
5736                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5737                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5738                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5739                ## have an element in table scope            !!!cp ('t258');
5740                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5741                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## Ignore the token
5742                  my $node = $self->{open_elements}->[$_];            !!!nack ('t258.1');
5743                  if ($node->[1] eq $token->{tag_name}) {             !!!next-token;
5744                    $i = $_;            next B;
5745                    last INSCOPE;          } else {
5746                  } elsif ({            !!!cp ('t259');
5747                            table => 1, html => 1,            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5748                           }->{$node->[1]}) {  
5749                    last INSCOPE;            $insert = $insert_to_foster;
5750                  }            #
5751                } # INSCOPE          }
5752                unless (defined $i) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5753                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5754                  ## Ignore the token                  @{$self->{open_elements}} == 1) { # redundant, maybe
5755              !!!parse-error (type => 'in body:#eof', token => $token);
5756              !!!cp ('t259.1');
5757              #
5758            } else {
5759              !!!cp ('t259.2');
5760              #
5761            }
5762    
5763            ## Stop parsing
5764            last B;
5765          } else {
5766            die "$0: $token->{type}: Unknown token type";
5767          }
5768        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5769              if ($token->{type} == CHARACTER_TOKEN) {
5770                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5771                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5772                  unless (length $token->{data}) {
5773                    !!!cp ('t260');
5774                  !!!next-token;                  !!!next-token;
5775                  redo B;                  next B;
5776                }                }
5777                }
5778                ## As if </tr>              
5779                ## have an element in table scope              !!!cp ('t261');
5780                my $i;              #
5781                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5782                  my $node = $self->{open_elements}->[$_];              if ($token->{tag_name} eq 'col') {
5783                  if ($node->[1] eq 'tr') {                !!!cp ('t262');
5784                    $i = $_;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5785                    last INSCOPE;                pop @{$self->{open_elements}};
5786                  } elsif ({                !!!ack ('t262.1');
5787                            table => 1, html => 1,                !!!next-token;
5788                           }->{$node->[1]}) {                next B;
5789                    last INSCOPE;              } else {
5790                  }                !!!cp ('t263');
5791                } # INSCOPE                #
5792                unless (defined $i) {              }
5793                  !!!parse-error (type => 'unmatched end tag:tr');            } elsif ($token->{type} == END_TAG_TOKEN) {
5794                if ($token->{tag_name} eq 'colgroup') {
5795                  if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5796                    !!!cp ('t264');
5797                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5798                  ## Ignore the token                  ## Ignore the token
5799                  !!!next-token;                  !!!next-token;
5800                  redo B;                  next B;
5801                }                } else {
5802                    !!!cp ('t265');
5803                ## Clear back to table row context                  pop @{$self->{open_elements}}; # colgroup
5804                while (not {                  $self->{insertion_mode} = IN_TABLE_IM;
5805                  tr => 1, html => 1,                  !!!next-token;
5806                }->{$self->{open_elements}->[-1]->[1]}) {                  next B;            
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5807                }                }
5808                } elsif ($token->{tag_name} eq 'col') {
5809                pop @{$self->{open_elements}}; # tr                !!!cp ('t266');
5810                $self->{insertion_mode} = 'in table body';                !!!parse-error (type => 'unmatched end tag:col', token => $token);
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5811                ## Ignore the token                ## Ignore the token
5812                !!!next-token;                !!!next-token;
5813                redo B;                next B;
5814              } else {              } else {
5815                #                !!!cp ('t267');
5816                  #
5817              }              }
5818            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5819              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5820            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5821              !!!cp ('t270.2');
5822              ## Stop parsing.
5823              last B;
5824            } else {
5825              ## NOTE: As if </colgroup>.
5826              !!!cp ('t270.1');
5827              pop @{$self->{open_elements}}; # colgroup
5828              $self->{insertion_mode} = IN_TABLE_IM;
5829              ## Reprocess.
5830              next B;
5831            }
5832          } else {
5833            die "$0: $token->{type}: Unknown token type";
5834          }
5835    
5836            ## As if in table            ## As if </colgroup>
5837            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5838            $in_body->($insert_to_foster);              !!!cp ('t269');
5839            redo B;  ## TODO: Wrong error type?
5840          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5841            if ($token->{type} eq 'character') {              ## Ignore the token
5842              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              !!!nack ('t269.1');
5843              !!!next-token;              !!!next-token;
5844              redo B;              next B;
5845            } elsif ($token->{type} eq 'start tag') {            } else {
5846              if ($token->{tag_name} eq 'option') {              !!!cp ('t270');
5847                if ($self->{open_elements}->[-1]->[1] eq 'option') {              pop @{$self->{open_elements}}; # colgroup
5848                  ## As if </option>              $self->{insertion_mode} = IN_TABLE_IM;
5849                  pop @{$self->{open_elements}};              !!!ack-later;
5850                }              ## reprocess
5851                next B;
5852              }
5853        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5854          if ($token->{type} == CHARACTER_TOKEN) {
5855            !!!cp ('t271');
5856            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5857            !!!next-token;
5858            next B;
5859          } elsif ($token->{type} == START_TAG_TOKEN) {
5860            if ($token->{tag_name} eq 'option') {
5861              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5862                !!!cp ('t272');
5863                ## As if </option>
5864                pop @{$self->{open_elements}};
5865              } else {
5866                !!!cp ('t273');
5867              }
5868    
5869                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5870                !!!next-token;            !!!nack ('t273.1');
5871                redo B;            !!!next-token;
5872              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5873                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5874                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5875                  pop @{$self->{open_elements}};              !!!cp ('t274');
5876                }              ## As if </option>
5877                pop @{$self->{open_elements}};
5878              } else {
5879                !!!cp ('t275');
5880              }
5881    
5882                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5883                  ## As if </optgroup>              !!!cp ('t276');
5884                  pop @{$self->{open_elements}};              ## As if </optgroup>
5885                }              pop @{$self->{open_elements}};
5886              } else {
5887                !!!cp ('t277');
5888              }
5889    
5890                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5891                !!!next-token;            !!!nack ('t277.1');
5892                redo B;            !!!next-token;
5893              } elsif ($token->{tag_name} eq 'select') {            next B;
5894                !!!parse-error (type => 'not closed:select');          } elsif ({
5895                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
5896                ## have an element in table scope                   }->{$token->{tag_name}} or
5897                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5898                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
5899                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
5900                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
5901                    $i = $_;                     tr => 1, td => 1, th => 1,
5902                    last INSCOPE;                    }->{$token->{tag_name}})) {
5903                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
5904                            table => 1, html => 1,            !!!parse-error (type => 'not closed:select', token => $token);
5905                           }->{$node->[1]}) {            ## NOTE: As if the token were </select> (<select> case) or
5906                    last INSCOPE;            ## as if there were </select> (otherwise).
5907                  }            ## have an element in table scope
5908                } # INSCOPE            my $i;
5909                unless (defined $i) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5910                  !!!parse-error (type => 'unmatched end tag:select');              my $node = $self->{open_elements}->[$_];
5911                  ## Ignore the token              if ($node->[1] & SELECT_EL) {
5912                  !!!next-token;                !!!cp ('t278');
5913                  redo B;                $i = $_;
5914                }                last INSCOPE;
5915                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5916                  !!!cp ('t279');
5917                  last INSCOPE;
5918                }
5919              } # INSCOPE
5920              unless (defined $i) {
5921                !!!cp ('t280');
5922                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5923                ## Ignore the token
5924                !!!nack ('t280.1');
5925                !!!next-token;
5926                next B;
5927              }
5928                                
5929                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5930              splice @{$self->{open_elements}}, $i;
5931    
5932                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5933    
5934                !!!next-token;            if ($token->{tag_name} eq 'select') {
5935                redo B;              !!!nack ('t281.2');
5936              } else {              !!!next-token;
5937                #              next B;
5938              } else {
5939                !!!cp ('t281.1');
5940                !!!ack-later;
5941                ## Reprocess the token.
5942                next B;
5943              }
5944            } else {
5945              !!!cp ('t282');
5946              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5947              ## Ignore the token
5948              !!!nack ('t282.1');
5949              !!!next-token;
5950              next B;
5951            }
5952          } elsif ($token->{type} == END_TAG_TOKEN) {
5953            if ($token->{tag_name} eq 'optgroup') {
5954              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5955                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5956                !!!cp ('t283');
5957                ## As if </option>
5958                splice @{$self->{open_elements}}, -2;
5959              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5960                !!!cp ('t284');
5961                pop @{$self->{open_elements}};
5962              } else {
5963                !!!cp ('t285');
5964                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5965                ## Ignore the token
5966              }
5967              !!!nack ('t285.1');
5968              !!!next-token;
5969              next B;
5970            } elsif ($token->{tag_name} eq 'option') {
5971              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5972                !!!cp ('t286');
5973                pop @{$self->{open_elements}};
5974              } else {
5975                !!!cp ('t287');
5976                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5977                ## Ignore the token
5978              }
5979              !!!nack ('t287.1');
5980              !!!next-token;
5981              next B;
5982            } elsif ($token->{tag_name} eq 'select') {
5983              ## have an element in table scope
5984              my $i;
5985              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5986                my $node = $self->{open_elements}->[$_];
5987                if ($node->[1] & SELECT_EL) {
5988                  !!!cp ('t288');
5989                  $i = $_;
5990                  last INSCOPE;
5991                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5992                  !!!cp ('t289');
5993                  last INSCOPE;
5994              }              }
5995            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5996              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5997                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5998                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5999                  ## As if </option>              ## Ignore the token
6000                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
6001                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
6002                  pop @{$self->{open_elements}};              next B;
6003                } else {            }
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6004                                
6005                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6006              splice @{$self->{open_elements}}, $i;
6007    
6008                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6009    
6010                !!!next-token;            !!!nack ('t291.1');
6011                redo B;            !!!next-token;
6012              } elsif ({            next B;
6013                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6014                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6015                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6016                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6017                                   }->{$token->{tag_name}}) {
6018                ## have an element in table scope  ## TODO: The following is wrong?
6019                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6020                                
6021                ## As if </select>            ## have an element in table scope
6022                ## have an element in table scope            my $i;
6023                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6024                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6025                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6026                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6027                    $i = $_;                $i = $_;
6028                    last INSCOPE;                last INSCOPE;
6029                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6030                            table => 1, html => 1,                !!!cp ('t293');
6031                           }->{$node->[1]}) {                last INSCOPE;
6032                    last INSCOPE;              }
6033                  }            } # INSCOPE
6034                } # INSCOPE            unless (defined $i) {
6035                unless (defined $i) {              !!!cp ('t294');
6036                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6037                  ## Ignore the </select> token              !!!nack ('t294.1');
6038                  !!!next-token; ## TODO: ok?              !!!next-token;
6039                  redo B;              next B;
6040                }            }
6041                                
6042                splice @{$self->{open_elements}}, $i;            ## As if </select>
6043              ## have an element in table scope
6044                $self->_reset_insertion_mode;            undef $i;
6045              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                ## reprocess              my $node = $self->{open_elements}->[$_];
6047                redo B;              if ($node->[1] & SELECT_EL) {
6048              } else {                !!!cp ('t295');
6049                #                $i = $_;
6050                  last INSCOPE;
6051                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6052    ## ISSUE: Can this state be reached?
6053                  !!!cp ('t296');
6054                  last INSCOPE;
6055              }              }
6056            } else {            } # INSCOPE
6057              #            unless (defined $i) {
6058                !!!cp ('t297');
6059    ## TODO: The following error type is correct?
6060                !!!parse-error (type => 'unmatched end tag:select', token => $token);
6061                ## Ignore the </select> token
6062                !!!nack ('t297.1');
6063                !!!next-token; ## TODO: ok?
6064                next B;
6065            }            }
6066                  
6067              !!!cp ('t298');
6068              splice @{$self->{open_elements}}, $i;
6069    
6070            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6071    
6072              !!!ack-later;
6073              ## reprocess
6074              next B;
6075            } else {
6076              !!!cp ('t299');
6077              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6078            ## Ignore the token            ## Ignore the token
6079              !!!nack ('t299.3');
6080            !!!next-token;            !!!next-token;
6081            redo B;            next B;
6082          } elsif ($self->{insertion_mode} eq 'after body') {          }
6083            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6084              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6085                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6086                ## As if in body            !!!cp ('t299.1');
6087                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6088            } else {
6089              !!!cp ('t299.2');
6090            }
6091    
6092            ## Stop parsing.
6093            last B;
6094          } else {
6095            die "$0: $token->{type}: Unknown token type";
6096          }
6097        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6098          if ($token->{type} == CHARACTER_TOKEN) {
6099            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6100              my $data = $1;
6101              ## As if in body
6102              $reconstruct_active_formatting_elements->($insert_to_current);
6103                                
6104                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6105              
6106              unless (length $token->{data}) {
6107                !!!cp ('t300');
6108                !!!next-token;
6109                next B;
6110              }
6111            }
6112            
6113            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6114              !!!cp ('t301');
6115              !!!parse-error (type => 'after html:#character', token => $token);
6116    
6117                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6118                  !!!next-token;          } else {
6119                  redo B;            !!!cp ('t302');
6120                }          }
6121              }          
6122                        ## "after body" insertion mode
6123              #          !!!parse-error (type => 'after body:#character', token => $token);
6124              !!!parse-error (type => 'after body:#character');  
6125            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6126              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6127              #          next B;
6128            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6129              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6130                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6131                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6132                  ## Ignore the token            
6133                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
6134                  redo B;          } else {
6135                } else {            !!!cp ('t304');
6136                  $previous_insertion_mode = $self->{insertion_mode};          }
6137                  $self->{insertion_mode} = 'trailing end';  
6138                  !!!next-token;          ## "after body" insertion mode
6139                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6140                }  
6141              } else {          $self->{insertion_mode} = IN_BODY_IM;
6142                !!!parse-error (type => 'after body:/'.$token->{tag_name});          !!!ack-later;
6143              }          ## reprocess
6144            next B;
6145          } elsif ($token->{type} == END_TAG_TOKEN) {
6146            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6147              !!!cp ('t305');
6148              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6149              
6150              $self->{insertion_mode} = AFTER_BODY_IM;
6151              ## Reprocess in the "after body" insertion mode.
6152            } else {
6153              !!!cp ('t306');
6154            }
6155    
6156            ## "after body" insertion mode
6157            if ($token->{tag_name} eq 'html') {
6158              if (defined $self->{inner_html_node}) {
6159                !!!cp ('t307');
6160                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6161                ## Ignore the token
6162                !!!next-token;
6163                next B;
6164            } else {            } else {
6165              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6166                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6167                !!!next-token;
6168                next B;
6169            }            }
6170            } else {
6171              !!!cp ('t309');
6172              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6173    
6174            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6175            ## reprocess            ## reprocess
6176            redo B;            next B;
6177      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6178        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6179            !!!cp ('t309.2');
6180            ## Stop parsing
6181            last B;
6182          } else {
6183            die "$0: $token->{type}: Unknown token type";
6184          }
6185        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6186          if ($token->{type} == CHARACTER_TOKEN) {
6187          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6188            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6189              
6190            unless (length $token->{data}) {            unless (length $token->{data}) {
6191                !!!cp ('t310');
6192              !!!next-token;              !!!next-token;
6193              redo B;              next B;
6194            }            }
6195          }          }
6196            
6197          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6198          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6199          !!!next-token;              !!!cp ('t311');
6200          redo B;              !!!parse-error (type => 'in frameset:#character', token => $token);
6201        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6202          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6203            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#character', token => $token);
6204              } else { # "after html frameset"
6205                !!!cp ('t313');
6206                !!!parse-error (type => 'after html:#character', token => $token);
6207    
6208                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6209                ## Reprocess in the "after frameset" insertion mode.
6210                !!!parse-error (type => 'after frameset:#character', token => $token);
6211              }
6212              
6213              ## Ignore the token.
6214              if (length $token->{data}) {
6215                !!!cp ('t314');
6216                ## reprocess the rest of characters
6217              } else {
6218                !!!cp ('t315');
6219                !!!next-token;
6220              }
6221              next B;
6222            }
6223            
6224            die qq[$0: Character "$token->{data}"];
6225          } elsif ($token->{type} == START_TAG_TOKEN) {
6226            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6227              !!!cp ('t316');
6228              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6229    
6230              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6231              ## Process in the "after frameset" insertion mode.
6232            } else {
6233              !!!cp ('t317');
6234            }
6235    
6236            if ($token->{tag_name} eq 'frameset' and
6237                $self->{insertion_mode} == IN_FRAMESET_IM) {
6238              !!!cp ('t318');
6239              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6240              !!!nack ('t318.1');
6241            !!!next-token;            !!!next-token;
6242            redo B;            next B;
6243          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6244            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6245              !!!cp ('t319');
6246              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6247            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6248              !!!ack ('t319.1');
6249            !!!next-token;            !!!next-token;
6250            redo B;            next B;
6251          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6252            $in_body->($insert_to_current);            !!!cp ('t320');
6253            redo B;            ## NOTE: As if in head.
6254          } else {            $parse_rcdata->(CDATA_CONTENT_MODEL);
6255            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            next B;
6256            } else {
6257              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6258                !!!cp ('t321');
6259                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6260              } else {
6261                !!!cp ('t322');
6262                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6263              }
6264            ## Ignore the token            ## Ignore the token
6265              !!!nack ('t322.1');
6266            !!!next-token;            !!!next-token;
6267            redo B;            next B;
6268            }
6269          } elsif ($token->{type} == END_TAG_TOKEN) {
6270            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6271              !!!cp ('t323');
6272              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6273    
6274              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275              ## Process in the "after frameset" insertion mode.
6276            } else {
6277              !!!cp ('t324');
6278          }          }
6279        } elsif ($token->{type} eq 'end tag') {  
6280          if ($token->{tag_name} eq 'frameset') {          if ($token->{tag_name} eq 'frameset' and
6281            if ($self->{open_elements}->[-1]->[1] eq 'html' and              $self->{insertion_mode} == IN_FRAMESET_IM) {
6282              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6283                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6284              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6285                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6286              ## Ignore the token              ## Ignore the token
6287              !!!next-token;              !!!next-token;
6288            } else {            } else {
6289                !!!cp ('t326');
6290              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6291              !!!next-token;              !!!next-token;
6292            }            }
6293    
6294            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6295                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6296              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6297                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6298              } else {
6299                !!!cp ('t328');
6300            }            }
6301            redo B;            next B;
6302            } elsif ($token->{tag_name} eq 'html' and
6303                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6304              !!!cp ('t329');
6305              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6306              !!!next-token;
6307              next B;
6308          } else {          } else {
6309            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6310                !!!cp ('t330');
6311                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6312              } else {
6313                !!!cp ('t331');
6314                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6315              }
6316            ## Ignore the token            ## Ignore the token
6317            !!!next-token;            !!!next-token;
6318            redo B;            next B;
6319          }          }
6320          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6321            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6322                    @{$self->{open_elements}} == 1) { # redundant, maybe
6323              !!!cp ('t331.1');
6324              !!!parse-error (type => 'in body:#eof', token => $token);
6325            } else {
6326              !!!cp ('t331.2');
6327            }
6328            
6329            ## Stop parsing
6330            last B;
6331        } else {        } else {
6332          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6333        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
6334    
6335                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6336                  !!!next-token;      } else {
6337                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6338                }      }
             }  
6339    
6340              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6341                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6342          if ($token->{tag_name} eq 'script') {
6343            !!!cp ('t332');
6344            ## NOTE: This is an "as if in head" code clone
6345            $script_start_tag->();
6346            next B;
6347          } elsif ($token->{tag_name} eq 'style') {
6348            !!!cp ('t333');
6349            ## NOTE: This is an "as if in head" code clone
6350            $parse_rcdata->(CDATA_CONTENT_MODEL);
6351            next B;
6352          } elsif ({
6353                    base => 1, link => 1,
6354                   }->{$token->{tag_name}}) {
6355            !!!cp ('t334');
6356            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6357            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6358            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6359            !!!ack ('t334.1');
6360            !!!next-token;
6361            next B;
6362          } elsif ($token->{tag_name} eq 'meta') {
6363            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6364            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6365            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6366    
6367                ## Ignore the token.          unless ($self->{confident}) {
6368                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6369                  ## reprocess the rest of characters              !!!cp ('t335');
6370                } else {              ## NOTE: Whether the encoding is supported or not is handled
6371                  !!!next-token;              ## in the {change_encoding} callback.
6372                }              $self->{change_encoding}
6373                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6374                
6375                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6376                    ->set_user_data (manakai_has_reference =>
6377                                         $token->{attributes}->{charset}
6378                                             ->{has_reference});
6379              } elsif ($token->{attributes}->{content}) {
6380                if ($token->{attributes}->{content}->{value}
6381                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6382                        [\x09-\x0D\x20]*=
6383                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6384                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6385                  !!!cp ('t336');
6386                  ## NOTE: Whether the encoding is supported or not is handled
6387                  ## in the {change_encoding} callback.
6388                  $self->{change_encoding}
6389                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6390                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6391                      ->set_user_data (manakai_has_reference =>
6392                                           $token->{attributes}->{content}
6393                                                 ->{has_reference});
6394              }              }
6395              }
6396            } else {
6397              if ($token->{attributes}->{charset}) {
6398                !!!cp ('t337');
6399                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6400                    ->set_user_data (manakai_has_reference =>
6401                                         $token->{attributes}->{charset}
6402                                             ->{has_reference});
6403              }
6404              if ($token->{attributes}->{content}) {
6405                !!!cp ('t338');
6406                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6407                    ->set_user_data (manakai_has_reference =>
6408                                         $token->{attributes}->{content}
6409                                             ->{has_reference});
6410              }
6411            }
6412    
6413          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6414        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6415          if ($token->{tag_name} eq 'noframes') {          next B;
6416            $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'title') {
6417            redo B;          !!!cp ('t341');
6418            ## NOTE: This is an "as if in head" code clone
6419            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6420            next B;
6421          } elsif ($token->{tag_name} eq 'body') {
6422            !!!parse-error (type => 'in body:body', token => $token);
6423                  
6424            if (@{$self->{open_elements}} == 1 or
6425                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6426              !!!cp ('t342');
6427              ## Ignore the token
6428          } else {          } else {
6429            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6430              for my $attr_name (keys %{$token->{attributes}}) {
6431                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6432                  !!!cp ('t343');
6433                  $body_el->set_attribute_ns
6434                    (undef, [undef, $attr_name],
6435                     $token->{attributes}->{$attr_name}->{value});
6436                }
6437              }
6438            }
6439            !!!nack ('t343.1');
6440            !!!next-token;
6441            next B;
6442          } elsif ({
6443                    address => 1, blockquote => 1, center => 1, dir => 1,
6444                    div => 1, dl => 1, fieldset => 1,
6445                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6446                    menu => 1, ol => 1, p => 1, ul => 1,
6447                    pre => 1, listing => 1,
6448                    form => 1,
6449                    table => 1,
6450                    hr => 1,
6451                   }->{$token->{tag_name}}) {
6452            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6453              !!!cp ('t350');
6454              !!!parse-error (type => 'in form:form', token => $token);
6455            ## Ignore the token            ## Ignore the token
6456              !!!nack ('t350.1');
6457            !!!next-token;            !!!next-token;
6458            redo B;            next B;
6459          }          }
6460        } elsif ($token->{type} eq 'end tag') {  
6461          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6462            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6463            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6464                !!!cp ('t344');
6465                !!!back-token; # <form>
6466                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6467                          line => $token->{line}, column => $token->{column}};
6468                next B;
6469              } elsif ($_->[1] & SCOPING_EL) {
6470                !!!cp ('t345');
6471                last INSCOPE;
6472              }
6473            } # INSCOPE
6474              
6475            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6476            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6477              !!!nack ('t346.1');
6478              !!!next-token;
6479              if ($token->{type} == CHARACTER_TOKEN) {
6480                $token->{data} =~ s/^\x0A//;
6481                unless (length $token->{data}) {
6482                  !!!cp ('t346');
6483                  !!!next-token;
6484                } else {
6485                  !!!cp ('t349');
6486                }
6487              } else {
6488                !!!cp ('t348');
6489              }
6490            } elsif ($token->{tag_name} eq 'form') {
6491              !!!cp ('t347.1');
6492              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6493    
6494              !!!nack ('t347.2');
6495              !!!next-token;
6496            } elsif ($token->{tag_name} eq 'table') {
6497              !!!cp ('t382');
6498              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6499              
6500              $self->{insertion_mode} = IN_TABLE_IM;
6501    
6502              !!!nack ('t382.1');
6503              !!!next-token;
6504            } elsif ($token->{tag_name} eq 'hr') {
6505              !!!cp ('t386');
6506              pop @{$self->{open_elements}};
6507            
6508              !!!nack ('t386.1');
6509            !!!next-token;            !!!next-token;
           redo B;  
6510          } else {          } else {
6511            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
           ## Ignore the token  
6512            !!!next-token;            !!!next-token;
           redo B;  
6513          }          }
6514        } else {          next B;
6515          die "$0: $token->{type}: Unknown token type";        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6516        }          ## has a p element in scope
6517            INSCOPE: for (reverse @{$self->{open_elements}}) {
6518              if ($_->[1] & P_EL) {
6519                !!!cp ('t353');
6520                !!!back-token; # <x>
6521                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6522                          line => $token->{line}, column => $token->{column}};
6523                next B;
6524              } elsif ($_->[1] & SCOPING_EL) {
6525                !!!cp ('t354');
6526                last INSCOPE;
6527              }
6528            } # INSCOPE
6529              
6530            ## Step 1
6531            my $i = -1;
6532            my $node = $self->{open_elements}->[$i];
6533            my $li_or_dtdd = {li => {li => 1},
6534                              dt => {dt => 1, dd => 1},
6535                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6536            LI: {
6537              ## Step 2
6538              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6539                if ($i != -1) {
6540                  !!!cp ('t355');
6541                  !!!parse-error (type => 'not closed',
6542                                  value => $self->{open_elements}->[-1]->[0]
6543                                      ->manakai_local_name,
6544                                  token => $token);
6545                } else {
6546                  !!!cp ('t356');
6547                }
6548                splice @{$self->{open_elements}}, $i;
6549                last LI;
6550              } else {
6551                !!!cp ('t357');
6552              }
6553              
6554              ## Step 3
6555              if (not ($node->[1] & FORMATTING_EL) and
6556                  #not $phrasing_category->{$node->[1]} and
6557                  ($node->[1] & SPECIAL_EL or
6558                   $node->[1] & SCOPING_EL) and
6559                  not ($node->[1] & ADDRESS_EL) and
6560                  not ($node->[1] & DIV_EL)) {
6561                !!!cp ('t358');
6562                last LI;
6563              }
6564              
6565              !!!cp ('t359');
6566              ## Step 4
6567              $i--;
6568              $node = $self->{open_elements}->[$i];
6569              redo LI;
6570            } # LI
6571              
6572            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6573            !!!nack ('t359.1');
6574            !!!next-token;
6575            next B;
6576          } elsif ($token->{tag_name} eq 'plaintext') {
6577            ## has a p element in scope
6578            INSCOPE: for (reverse @{$self->{open_elements}}) {
6579              if ($_->[1] & P_EL) {
6580                !!!cp ('t367');
6581                !!!back-token; # <plaintext>
6582                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6583                          line => $token->{line}, column => $token->{column}};
6584                next B;
6585              } elsif ($_->[1] & SCOPING_EL) {
6586                !!!cp ('t368');
6587                last INSCOPE;
6588              }
6589            } # INSCOPE
6590              
6591            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6592              
6593            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6594              
6595            !!!nack ('t368.1');
6596            !!!next-token;
6597            next B;
6598          } elsif ($token->{tag_name} eq 'a') {
6599            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6600              my $node = $active_formatting_elements->[$i];
6601              if ($node->[1] & A_EL) {
6602                !!!cp ('t371');
6603                !!!parse-error (type => 'in a:a', token => $token);
6604                
6605                !!!back-token; # <a>
6606                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6607                          line => $token->{line}, column => $token->{column}};
6608                $formatting_end_tag->($token);
6609                
6610                AFE2: for (reverse 0..$#$active_formatting_elements) {
6611                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6612                    !!!cp ('t372');
6613                    splice @$active_formatting_elements, $_, 1;
6614                    last AFE2;
6615                  }
6616                } # AFE2
6617                OE: for (reverse 0..$#{$self->{open_elements}}) {
6618                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6619                    !!!cp ('t373');
6620                    splice @{$self->{open_elements}}, $_, 1;
6621                    last OE;
6622                  }
6623                } # OE
6624                last AFE;
6625              } elsif ($node->[0] eq '#marker') {
6626                !!!cp ('t374');
6627                last AFE;
6628              }
6629            } # AFE
6630              
6631            $reconstruct_active_formatting_elements->($insert_to_current);
6632    
6633        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6634      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
6635        ## states in the main stage is preserved yet # MUST  
6636                  !!!nack ('t374.1');
6637        if ($token->{type} eq 'character') {          !!!next-token;
6638          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          next B;
6639            my $data = $1;        } elsif ($token->{tag_name} eq 'nobr') {
6640            ## As if in the main phase.          $reconstruct_active_formatting_elements->($insert_to_current);
6641            ## NOTE: The insertion mode in the main phase  
6642            ## just before the phase has been changed to the trailing          ## has a |nobr| element in scope
6643            ## end phase is either "after body" or "after frameset".          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644            $reconstruct_active_formatting_elements->($insert_to_current);            my $node = $self->{open_elements}->[$_];
6645              if ($node->[1] & NOBR_EL) {
6646                !!!cp ('t376');
6647                !!!parse-error (type => 'in nobr:nobr', token => $token);
6648                !!!back-token; # <nobr>
6649                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6650                          line => $token->{line}, column => $token->{column}};
6651                next B;
6652              } elsif ($node->[1] & SCOPING_EL) {
6653                !!!cp ('t377');
6654                last INSCOPE;
6655              }
6656            } # INSCOPE
6657            
6658            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6659            push @$active_formatting_elements, $self->{open_elements}->[-1];
6660            
6661            !!!nack ('t377.1');
6662            !!!next-token;
6663            next B;
6664          } elsif ($token->{tag_name} eq 'button') {
6665            ## has a button element in scope
6666            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6667              my $node = $self->{open_elements}->[$_];
6668              if ($node->[1] & BUTTON_EL) {
6669                !!!cp ('t378');
6670                !!!parse-error (type => 'in button:button', token => $token);
6671                !!!back-token; # <button>
6672                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6673                          line => $token->{line}, column => $token->{column}};
6674                next B;
6675              } elsif ($node->[1] & SCOPING_EL) {
6676                !!!cp ('t379');
6677                last INSCOPE;
6678              }
6679            } # INSCOPE
6680                        
6681            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
6682                        
6683            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6684    
6685            ## TODO: associate with $self->{form_element} if defined
6686    
6687            push @$active_formatting_elements, ['#marker', ''];
6688    
6689            !!!nack ('t379.1');
6690            !!!next-token;
6691            next B;
6692          } elsif ({
6693                    xmp => 1,
6694                    iframe => 1,
6695                    noembed => 1,
6696                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6697                    noscript => 0, ## TODO: 1 if scripting is enabled
6698                   }->{$token->{tag_name}}) {
6699            if ($token->{tag_name} eq 'xmp') {
6700              !!!cp ('t381');
6701              $reconstruct_active_formatting_elements->($insert_to_current);
6702            } else {
6703              !!!cp ('t399');
6704            }
6705            ## NOTE: There is an "as if in body" code clone.
6706            $parse_rcdata->(CDATA_CONTENT_MODEL);
6707            next B;
6708          } elsif ($token->{tag_name} eq 'isindex') {
6709            !!!parse-error (type => 'isindex', token => $token);
6710            
6711            if (defined $self->{form_element}) {
6712              !!!cp ('t389');
6713              ## Ignore the token
6714              !!!nack ('t389'); ## NOTE: Not acknowledged.
6715              !!!next-token;
6716              next B;
6717            } else {
6718              !!!ack ('t391.1');
6719    
6720              my $at = $token->{attributes};
6721              my $form_attrs;
6722              $form_attrs->{action} = $at->{action} if $at->{action};
6723              my $prompt_attr = $at->{prompt};
6724              $at->{name} = {name => 'name', value => 'isindex'};
6725              delete $at->{action};
6726              delete $at->{prompt};
6727              my @tokens = (
6728                            {type => START_TAG_TOKEN, tag_name => 'form',
6729                             attributes => $form_attrs,
6730                             line => $token->{line}, column => $token->{column}},
6731                            {type => START_TAG_TOKEN, tag_name => 'hr',
6732                             line => $token->{line}, column => $token->{column}},
6733                            {type => START_TAG_TOKEN, tag_name => 'p',
6734                             line => $token->{line}, column => $token->{column}},
6735                            {type => START_TAG_TOKEN, tag_name => 'label',
6736                             line => $token->{line}, column => $token->{column}},
6737                           );
6738              if ($prompt_attr) {
6739                !!!cp ('t390');
6740                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6741                               #line => $token->{line}, column => $token->{column},
6742                              };
6743              } else {
6744                !!!cp ('t391');
6745                push @tokens, {type => CHARACTER_TOKEN,
6746                               data => 'This is a searchable index. Insert your search keywords here: ',
6747                               #line => $token->{line}, column => $token->{column},
6748                              }; # SHOULD
6749                ## TODO: make this configurable
6750              }
6751              push @tokens,
6752                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6753                             line => $token->{line}, column => $token->{column}},
6754                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6755                            {type => END_TAG_TOKEN, tag_name => 'label',
6756                             line => $token->{line}, column => $token->{column}},
6757                            {type => END_TAG_TOKEN, tag_name => 'p',
6758                             line => $token->{line}, column => $token->{column}},
6759                            {type => START_TAG_TOKEN, tag_name => 'hr',
6760                             line => $token->{line}, column => $token->{column}},
6761                            {type => END_TAG_TOKEN, tag_name => 'form',
6762                             line => $token->{line}, column => $token->{column}};
6763              !!!back-token (@tokens);
6764              !!!next-token;
6765              next B;
6766            }
6767          } elsif ($token->{tag_name} eq 'textarea') {
6768            my $tag_name = $token->{tag_name};
6769            my $el;
6770            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6771            
6772            ## TODO: $self->{form_element} if defined
6773            $self->{content_model} = RCDATA_CONTENT_MODEL;
6774            delete $self->{escape}; # MUST
6775            
6776            $insert->($el);
6777            
6778            my $text = '';
6779            !!!nack ('t392.1');
6780            !!!next-token;
6781            if ($token->{type} == CHARACTER_TOKEN) {
6782              $token->{data} =~ s/^\x0A//;
6783            unless (length $token->{data}) {            unless (length $token->{data}) {
6784                !!!cp ('t392');
6785              !!!next-token;              !!!next-token;
6786              redo B;            } else {
6787                !!!cp ('t393');
6788            }            }
6789            } else {
6790              !!!cp ('t394');
6791            }
6792            while ($token->{type} == CHARACTER_TOKEN) {
6793              !!!cp ('t395');
6794              $text .= $token->{data};
6795              !!!next-token;
6796            }
6797            if (length $text) {
6798              !!!cp ('t396');
6799              $el->manakai_append_text ($text);
6800            }
6801            
6802            $self->{content_model} = PCDATA_CONTENT_MODEL;
6803            
6804            if ($token->{type} == END_TAG_TOKEN and
6805                $token->{tag_name} eq $tag_name) {
6806              !!!cp ('t397');
6807              ## Ignore the token
6808            } else {
6809              !!!cp ('t398');
6810              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6811          }          }
6812            !!!next-token;
6813            next B;
6814          } elsif ($token->{tag_name} eq 'rt' or
6815                   $token->{tag_name} eq 'rp') {
6816            ## has a |ruby| element in scope
6817            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6818              my $node = $self->{open_elements}->[$_];
6819              if ($node->[1] & RUBY_EL) {
6820                !!!cp ('t398.1');
6821                ## generate implied end tags
6822                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6823                  !!!cp ('t398.2');
6824                  pop @{$self->{open_elements}};
6825                }
6826                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6827                  !!!cp ('t398.3');
6828                  !!!parse-error (type => 'not closed',
6829                                  value => $self->{open_elements}->[-1]->[0]
6830                                      ->manakai_local_name,
6831                                  token => $token);
6832                  pop @{$self->{open_elements}}
6833                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6834                }
6835                last INSCOPE;
6836              } elsif ($node->[1] & SCOPING_EL) {
6837                !!!cp ('t398.4');
6838                last INSCOPE;
6839              }
6840            } # INSCOPE
6841    
6842          !!!parse-error (type => 'after html:#character');          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843          $self->{insertion_mode} = $previous_insertion_mode;  
6844          ## reprocess          !!!nack ('t398.5');
6845          redo B;          !!!next-token;
       } elsif ($token->{type} eq 'start tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:/'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
6846          redo B;          redo B;
6847          } elsif ($token->{tag_name} eq 'math' or
6848                   $token->{tag_name} eq 'svg') {
6849            $reconstruct_active_formatting_elements->($insert_to_current);
6850    
6851            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6852    
6853            ## "adjust foreign attributes" - done in insert-element-f
6854            
6855            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6856            
6857            if ($self->{self_closing}) {
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t398.1');
6860            } else {
6861              !!!cp ('t398.2');
6862              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6863              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6864              ## mode, "in body" (not "in foreign content") secondary insertion
6865              ## mode, maybe.
6866            }
6867    
6868            !!!next-token;
6869            next B;
6870          } elsif ({
6871                    caption => 1, col => 1, colgroup => 1, frame => 1,
6872                    frameset => 1, head => 1, option => 1, optgroup => 1,
6873                    tbody => 1, td => 1, tfoot => 1, th => 1,
6874                    thead => 1, tr => 1,
6875                   }->{$token->{tag_name}}) {
6876            !!!cp ('t401');
6877            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6878            ## Ignore the token
6879            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6880            !!!next-token;
6881            next B;
6882            
6883            ## ISSUE: An issue on HTML5 new elements in the spec.
6884        } else {        } else {
6885          die "$0: $token->{type}: Unknown token";          if ($token->{tag_name} eq 'image') {
6886              !!!cp ('t384');
6887              !!!parse-error (type => 'image', token => $token);
6888              $token->{tag_name} = 'img';
6889            } else {
6890              !!!cp ('t385');
6891            }
6892    
6893            ## NOTE: There is an "as if <br>" code clone.
6894            $reconstruct_active_formatting_elements->($insert_to_current);
6895            
6896            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6897    
6898            if ({
6899                 applet => 1, marquee => 1, object => 1,
6900                }->{$token->{tag_name}}) {
6901              !!!cp ('t380');
6902              push @$active_formatting_elements, ['#marker', ''];
6903              !!!nack ('t380.1');
6904            } elsif ({
6905                      b => 1, big => 1, em => 1, font => 1, i => 1,
6906                      s => 1, small => 1, strile => 1,
6907                      strong => 1, tt => 1, u => 1,
6908                     }->{$token->{tag_name}}) {
6909              !!!cp ('t375');
6910              push @$active_formatting_elements, $self->{open_elements}->[-1];
6911              !!!nack ('t375.1');
6912            } elsif ($token->{tag_name} eq 'input') {
6913              !!!cp ('t388');
6914              ## TODO: associate with $self->{form_element} if defined
6915              pop @{$self->{open_elements}};
6916              !!!ack ('t388.2');
6917            } elsif ({
6918                      area => 1, basefont => 1, bgsound => 1, br => 1,
6919                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6920                      #image => 1,
6921                     }->{$token->{tag_name}}) {
6922              !!!cp ('t388.1');
6923              pop @{$self->{open_elements}};
6924              !!!ack ('t388.3');
6925            } elsif ($token->{tag_name} eq 'select') {
6926              ## TODO: associate with $self->{form_element} if defined
6927            
6928              if ($self->{insertion_mode} & TABLE_IMS or
6929                  $self->{insertion_mode} & BODY_TABLE_IMS or
6930                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6931                !!!cp ('t400.1');
6932                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6933              } else {
6934                !!!cp ('t400.2');
6935                $self->{insertion_mode} = IN_SELECT_IM;
6936              }
6937              !!!nack ('t400.3');
6938            } else {
6939              !!!nack ('t402');
6940            }
6941            
6942            !!!next-token;
6943            next B;
6944          }
6945        } elsif ($token->{type} == END_TAG_TOKEN) {
6946          if ($token->{tag_name} eq 'body') {
6947            ## has a |body| element in scope
6948            my $i;
6949            INSCOPE: {
6950              for (reverse @{$self->{open_elements}}) {
6951                if ($_->[1] & BODY_EL) {
6952                  !!!cp ('t405');
6953                  $i = $_;
6954                  last INSCOPE;
6955                } elsif ($_->[1] & SCOPING_EL) {
6956                  !!!cp ('t405.1');
6957                  last;
6958                }
6959              }
6960    
6961              !!!parse-error (type => 'start tag not allowed',
6962                              value => $token->{tag_name}, token => $token);
6963              ## NOTE: Ignore the token.
6964              !!!next-token;
6965              next B;
6966            } # INSCOPE
6967    
6968            for (@{$self->{open_elements}}) {
6969              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6970                !!!cp ('t403');
6971                !!!parse-error (type => 'not closed',
6972                                value => $_->[0]->manakai_local_name,
6973                                token => $token);
6974                last;
6975              } else {
6976                !!!cp ('t404');
6977              }
6978            }
6979    
6980            $self->{insertion_mode} = AFTER_BODY_IM;
6981            !!!next-token;
6982            next B;
6983          } elsif ($token->{tag_name} eq 'html') {
6984            ## TODO: Update this code.  It seems that the code below is not
6985            ## up-to-date, though it has same effect as speced.
6986            if (@{$self->{open_elements}} > 1 and
6987                $self->{open_elements}->[1]->[1] & BODY_EL) {
6988              ## ISSUE: There is an issue in the spec.
6989              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6990                !!!cp ('t406');
6991                !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995              } else {
6996                !!!cp ('t407');
6997              }
6998              $self->{insertion_mode} = AFTER_BODY_IM;
6999              ## reprocess
7000              next B;
7001            } else {
7002              !!!cp ('t408');
7003              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7004              ## Ignore the token
7005              !!!next-token;
7006              next B;
7007            }
7008          } elsif ({
7009                    address => 1, blockquote => 1, center => 1, dir => 1,
7010                    div => 1, dl => 1, fieldset => 1, listing => 1,
7011                    menu => 1, ol => 1, pre => 1, ul => 1,
7012                    dd => 1, dt => 1, li => 1,
7013                    applet => 1, button => 1, marquee => 1, object => 1,
7014                   }->{$token->{tag_name}}) {
7015            ## has an element in scope
7016            my $i;
7017            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7018              my $node = $self->{open_elements}->[$_];
7019              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7020                !!!cp ('t410');
7021                $i = $_;
7022                last INSCOPE;
7023              } elsif ($node->[1] & SCOPING_EL) {
7024                !!!cp ('t411');
7025                last INSCOPE;
7026              }
7027            } # INSCOPE
7028    
7029            unless (defined $i) { # has an element in scope
7030              !!!cp ('t413');
7031              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7032            } else {
7033              ## Step 1. generate implied end tags
7034              while ({
7035                      ## END_TAG_OPTIONAL_EL
7036                      dd => ($token->{tag_name} ne 'dd'),
7037                      dt => ($token->{tag_name} ne 'dt'),
7038                      li => ($token->{tag_name} ne 'li'),
7039                      p => 1,
7040                      rt => 1,
7041                      rp => 1,
7042                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7043                !!!cp ('t409');
7044                pop @{$self->{open_elements}};
7045              }
7046    
7047              ## Step 2.
7048              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7049                      ne $token->{tag_name}) {
7050                !!!cp ('t412');
7051                !!!parse-error (type => 'not closed',
7052                                value => $self->{open_elements}->[-1]->[0]
7053                                    ->manakai_local_name,
7054                                token => $token);
7055              } else {
7056                !!!cp ('t414');
7057              }
7058    
7059              ## Step 3.
7060              splice @{$self->{open_elements}}, $i;
7061    
7062              ## Step 4.
7063              $clear_up_to_marker->()
7064                  if {
7065                    applet => 1, button => 1, marquee => 1, object => 1,
7066                  }->{$token->{tag_name}};
7067            }
7068            !!!next-token;
7069            next B;
7070          } elsif ($token->{tag_name} eq 'form') {
7071            undef $self->{form_element};
7072    
7073            ## has an element in scope
7074            my $i;
7075            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7076              my $node = $self->{open_elements}->[$_];
7077              if ($node->[1] & FORM_EL) {
7078                !!!cp ('t418');
7079                $i = $_;
7080                last INSCOPE;
7081              } elsif ($node->[1] & SCOPING_EL) {
7082                !!!cp ('t419');
7083                last INSCOPE;
7084              }
7085            } # INSCOPE
7086    
7087            unless (defined $i) { # has an element in scope
7088              !!!cp ('t421');
7089              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7090            } else {
7091              ## Step 1. generate implied end tags
7092              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7093                !!!cp ('t417');
7094                pop @{$self->{open_elements}};
7095              }
7096              
7097              ## Step 2.
7098              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7099                      ne $token->{tag_name}) {
7100                !!!cp ('t417.1');
7101                !!!parse-error (type => 'not closed',
7102                                value => $self->{open_elements}->[-1]->[0]
7103                                    ->manakai_local_name,
7104                                token => $token);
7105              } else {
7106                !!!cp ('t420');
7107              }  
7108              
7109              ## Step 3.
7110              splice @{$self->{open_elements}}, $i;
7111            }
7112    
7113            !!!next-token;
7114            next B;
7115          } elsif ({
7116                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7117                   }->{$token->{tag_name}}) {
7118            ## has an element in scope
7119            my $i;
7120            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7121              my $node = $self->{open_elements}->[$_];
7122              if ($node->[1] & HEADING_EL) {
7123                !!!cp ('t423');
7124                $i = $_;
7125                last INSCOPE;
7126              } elsif ($node->[1] & SCOPING_EL) {
7127                !!!cp ('t424');
7128                last INSCOPE;
7129              }
7130            } # INSCOPE
7131    
7132            unless (defined $i) { # has an element in scope
7133              !!!cp ('t425.1');
7134              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7135            } else {
7136              ## Step 1. generate implied end tags
7137              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7138                !!!cp ('t422');
7139                pop @{$self->{open_elements}};
7140              }
7141              
7142              ## Step 2.
7143              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7144                      ne $token->{tag_name}) {
7145                !!!cp ('t425');
7146                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7147              } else {
7148                !!!cp ('t426');
7149              }
7150    
7151              ## Step 3.
7152              splice @{$self->{open_elements}}, $i;
7153            }
7154            
7155            !!!next-token;
7156            next B;
7157          } elsif ($token->{tag_name} eq 'p') {
7158            ## has an element in scope
7159            my $i;
7160            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7161              my $node = $self->{open_elements}->[$_];
7162              if ($node->[1] & P_EL) {
7163                !!!cp ('t410.1');
7164                $i = $_;
7165                last INSCOPE;
7166              } elsif ($node->[1] & SCOPING_EL) {
7167                !!!cp ('t411.1');
7168                last INSCOPE;
7169              }
7170            } # INSCOPE
7171    
7172            if (defined $i) {
7173              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7174                      ne $token->{tag_name}) {
7175                !!!cp ('t412.1');
7176                !!!parse-error (type => 'not closed',
7177                                value => $self->{open_elements}->[-1]->[0]
7178                                    ->manakai_local_name,
7179                                token => $token);
7180              } else {
7181                !!!cp ('t414.1');
7182              }
7183    
7184              splice @{$self->{open_elements}}, $i;
7185            } else {
7186              !!!cp ('t413.1');
7187              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7188    
7189              !!!cp ('t415.1');
7190              ## As if <p>, then reprocess the current token
7191              my $el;
7192              !!!create-element ($el, $HTML_NS, 'p',, $token);
7193              $insert->($el);
7194              ## NOTE: Not inserted into |$self->{open_elements}|.
7195            }
7196    
7197            !!!next-token;
7198            next B;
7199          } elsif ({
7200                    a => 1,
7201                    b => 1, big => 1, em => 1, font => 1, i => 1,
7202                    nobr => 1, s => 1, small => 1, strile => 1,
7203                    strong => 1, tt => 1, u => 1,
7204                   }->{$token->{tag_name}}) {
7205            !!!cp ('t427');
7206            $formatting_end_tag->($token);
7207            next B;
7208          } elsif ($token->{tag_name} eq 'br') {
7209            !!!cp ('t428');
7210            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7211    
7212            ## As if <br>
7213            $reconstruct_active_formatting_elements->($insert_to_current);
7214            
7215            my $el;
7216            !!!create-element ($el, $HTML_NS, 'br',, $token);
7217            $insert->($el);
7218            
7219            ## Ignore the token.
7220            !!!next-token;
7221            next B;
7222          } elsif ({
7223                    caption => 1, col => 1, colgroup => 1, frame => 1,
7224                    frameset => 1, head => 1, option => 1, optgroup => 1,
7225                    tbody => 1, td => 1, tfoot => 1, th => 1,
7226                    thead => 1, tr => 1,
7227                    area => 1, basefont => 1, bgsound => 1,
7228                    embed => 1, hr => 1, iframe => 1, image => 1,
7229                    img => 1, input => 1, isindex => 1, noembed => 1,
7230                    noframes => 1, param => 1, select => 1, spacer => 1,
7231                    table => 1, textarea => 1, wbr => 1,
7232                    noscript => 0, ## TODO: if scripting is enabled
7233                   }->{$token->{tag_name}}) {
7234            !!!cp ('t429');
7235            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7236            ## Ignore the token
7237            !!!next-token;
7238            next B;
7239            
7240            ## ISSUE: Issue on HTML5 new elements in spec
7241            
7242          } else {
7243            ## Step 1
7244            my $node_i = -1;
7245            my $node = $self->{open_elements}->[$node_i];
7246    
7247            ## Step 2
7248            S2: {
7249              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7250                ## Step 1
7251                ## generate implied end tags
7252                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7253                  !!!cp ('t430');
7254                  ## NOTE: |<ruby><rt></ruby>|.
7255                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7256                  ## which seems wrong.
7257                  pop @{$self->{open_elements}};
7258                  $node_i++;
7259                }
7260            
7261                ## Step 2
7262                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7263                        ne $token->{tag_name}) {
7264                  !!!cp ('t431');
7265                  ## NOTE: <x><y></x>
7266                  !!!parse-error (type => 'not closed',
7267                                  value => $self->{open_elements}->[-1]->[0]
7268                                      ->manakai_local_name,
7269                                  token => $token);
7270                } else {
7271                  !!!cp ('t432');
7272                }
7273                
7274                ## Step 3
7275                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7276    
7277                !!!next-token;
7278                last S2;
7279              } else {
7280                ## Step 3
7281                if (not ($node->[1] & FORMATTING_EL) and
7282                    #not $phrasing_category->{$node->[1]} and
7283                    ($node->[1] & SPECIAL_EL or
7284                     $node->[1] & SCOPING_EL)) {
7285                  !!!cp ('t433');
7286                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7287                  ## Ignore the token
7288                  !!!next-token;
7289                  last S2;
7290                }
7291    
7292                !!!cp ('t434');
7293              }
7294              
7295              ## Step 4
7296              $node_i--;
7297              $node = $self->{open_elements}->[$node_i];
7298              
7299              ## Step 5;
7300              redo S2;
7301            } # S2
7302            next B;
7303        }        }
7304      } else {      }
7305        die "$0: $self->{insertion_mode}: Unknown insertion mode";      next B;
7306      } continue { # B
7307        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7308          ## NOTE: The code below is executed in cases where it does not have
7309          ## to be, but it it is harmless even in those cases.
7310          ## has an element in scope
7311          INSCOPE: {
7312            for (reverse 0..$#{$self->{open_elements}}) {
7313              my $node = $self->{open_elements}->[$_];
7314              if ($node->[1] & FOREIGN_EL) {
7315                last INSCOPE;
7316              } elsif ($node->[1] & SCOPING_EL) {
7317                last;
7318              }
7319            }
7320            
7321            ## NOTE: No foreign element in scope.
7322            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7323          } # INSCOPE
7324      }      }
7325    } # B    } # B
7326    
# Line 5211  sub set_inner_html ($$$) { Line 7335  sub set_inner_html ($$$) {
7335    my $s = \$_[0];    my $s = \$_[0];
7336    my $onerror = $_[1];    my $onerror = $_[1];
7337    
7338      ## ISSUE: Should {confident} be true?
7339    
7340    my $nt = $node->node_type;    my $nt = $node->node_type;
7341    if ($nt == 9) {    if ($nt == 9) {
7342      # MUST      # MUST
# Line 5239  sub set_inner_html ($$$) { Line 7365  sub set_inner_html ($$$) {
7365      my $p = $class->new;      my $p = $class->new;
7366      $p->{document} = $doc;      $p->{document} = $doc;
7367    
7368      ## Step 9 # MUST      ## Step 8 # MUST
7369      my $i = 0;      my $i = 0;
7370      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7371      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7372      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7373        my $self = shift;        my $self = shift;
7374    
7375        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7376        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7377    
7378          $self->{next_char} = -1 and return if $i >= length $$s;
7379          $self->{next_char} = ord substr $$s, $i++, 1;
7380    
7381        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7382        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7383        $column++;  
7384          if ($self->{next_char} == 0x000A) { # LF
7385        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7386          $line++;          $p->{column} = 0;
7387          $column = 0;          !!!cp ('i1');
7388        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7389          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7390          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7391          $line++;          $p->{line}++;
7392          $column = 0;          $p->{column} = 0;
7393        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7394          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7395        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7396            !!!cp ('i3');
7397          } elsif ($self->{next_char} == 0x0000) { # NULL
7398            !!!cp ('i4');
7399          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7400          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7401          } elsif ($self->{next_char} <= 0x0008 or
7402                   (0x000E <= $self->{next_char} and
7403                    $self->{next_char} <= 0x001F) or
7404                   (0x007F <= $self->{next_char} and
7405                    $self->{next_char} <= 0x009F) or
7406                   (0xD800 <= $self->{next_char} and
7407                    $self->{next_char} <= 0xDFFF) or
7408                   (0xFDD0 <= $self->{next_char} and
7409                    $self->{next_char} <= 0xFDDF) or
7410                   {
7411                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7412                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7413                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7414                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7415                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7416                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7417                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7418                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7419                    0x10FFFE => 1, 0x10FFFF => 1,
7420                   }->{$self->{next_char}}) {
7421            !!!cp ('i4.1');
7422            !!!parse-error (type => 'control char', level => $self->{must_level});
7423    ## TODO: error type documentation
7424        }        }
7425      };      };
7426      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7427      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7428            
7429      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7430        my (%opt) = @_;        my (%opt) = @_;
7431        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7432          my $column = $opt{column};
7433          if (defined $opt{token} and defined $opt{token}->{line}) {
7434            $line = $opt{token}->{line};
7435            $column = $opt{token}->{column};
7436          }
7437          warn "Parse error ($opt{type}) at line $line column $column\n";
7438      };      };
7439      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7440        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7441      };      };
7442            
7443      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7444      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7445    
7446      ## Step 2      ## Step 2
7447      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7448      $p->{content_model} = {      $p->{content_model} = {
7449        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7450        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5300  sub set_inner_html ($$$) { Line 7461  sub set_inner_html ($$$) {
7461          unless defined $p->{content_model};          unless defined $p->{content_model};
7462          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7463    
7464      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7465          ## TODO: Foreign element OK?
7466    
7467      ## Step 4      ## Step 3
7468      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7469        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7470    
7471      ## Step 5 # MUST      ## Step 4 # MUST
7472      $doc->append_child ($root);      $doc->append_child ($root);
7473    
7474      ## Step 6 # MUST      ## Step 5 # MUST
7475      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7476    
7477      undef $p->{head_element};      undef $p->{head_element};
7478    
7479      ## Step 7 # MUST      ## Step 6 # MUST
7480      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7481    
7482      ## Step 8 # MUST      ## Step 7 # MUST
7483      my $anode = $node;      my $anode = $node;
7484      AN: while (defined $anode) {      AN: while (defined $anode) {
7485        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7486          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7487          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7488            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7489                !!!cp ('i5');
7490              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7491              last AN;              last AN;
7492            }            }
# Line 5332  sub set_inner_html ($$$) { Line 7495  sub set_inner_html ($$$) {
7495        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7496      } # AN      } # AN
7497            
7498      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7499      {      {
7500        my $self = $p;        my $self = $p;
7501        !!!next-token;        !!!next-token;
7502      }      }
7503      $p->_tree_construction_main;      $p->_tree_construction_main;
7504    
7505      ## Step 11 # MUST      ## Step 10 # MUST
7506      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7507      for (@cn) {      for (@cn) {
7508        $node->remove_child ($_);        $node->remove_child ($_);
7509      }      }
7510      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7511    
7512      ## Step 12 # MUST      ## Step 11 # MUST
7513      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7514      for (@cn) {      for (@cn) {
7515        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5356  sub set_inner_html ($$$) { Line 7518  sub set_inner_html ($$$) {
7518      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7519    
7520      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7521    
7522        delete $p->{parse_error}; # delete loop
7523    } else {    } else {
7524      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";
7525    }    }
# Line 5363  sub set_inner_html ($$$) { Line 7527  sub set_inner_html ($$$) {
7527    
7528  } # tree construction stage  } # tree construction stage
7529    
7530  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7531    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
7532    
7533  1;  1;
7534  # $Date$  # $Date$

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.152

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24