/[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.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.143 by wakaba, Sat May 24 10:48:57 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  ## TODO: 1252 parse error (revision 1264)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 8859-11 = 874 (revision 1271)
13  ## is not yet clear.  
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  require IO::Handle;
15  ## "{U+FEFF}..." in GB18030?  
16    my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
17  my $permitted_slash_tag_name = {  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
18    base => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
19    link => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
20    meta => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
21    hr => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
22    br => 1,  
23    img=> 1,  sub A_EL () { 0b1 }
24    embed => 1,  sub ADDRESS_EL () { 0b10 }
25    param => 1,  sub BODY_EL () { 0b100 }
26    area => 1,  sub BUTTON_EL () { 0b1000 }
27    col => 1,  sub CAPTION_EL () { 0b10000 }
28    input => 1,  sub DD_EL () { 0b100000 }
29    sub DIV_EL () { 0b1000000 }
30    sub DT_EL () { 0b10000000 }
31    sub FORM_EL () { 0b100000000 }
32    sub FORMATTING_EL () { 0b1000000000 }
33    sub FRAMESET_EL () { 0b10000000000 }
34    sub HEADING_EL () { 0b100000000000 }
35    sub HTML_EL () { 0b1000000000000 }
36    sub LI_EL () { 0b10000000000000 }
37    sub NOBR_EL () { 0b100000000000000 }
38    sub OPTION_EL () { 0b1000000000000000 }
39    sub OPTGROUP_EL () { 0b10000000000000000 }
40    sub P_EL () { 0b100000000000000000 }
41    sub SELECT_EL () { 0b1000000000000000000 }
42    sub TABLE_EL () { 0b10000000000000000000 }
43    sub TABLE_CELL_EL () { 0b100000000000000000000 }
44    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
45    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
46    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
47    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
48    sub FOREIGN_EL () { 0b10000000000000000000000000 }
49    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
50    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
51    
52    sub TABLE_ROWS_EL () {
53      TABLE_EL |
54      TABLE_ROW_EL |
55      TABLE_ROW_GROUP_EL
56    }
57    
58    sub END_TAG_OPTIONAL_EL () {
59      DD_EL |
60      DT_EL |
61      LI_EL |
62      P_EL
63    }
64    
65    sub ALL_END_TAG_OPTIONAL_EL () {
66      END_TAG_OPTIONAL_EL |
67      BODY_EL |
68      HTML_EL |
69      TABLE_CELL_EL |
70      TABLE_ROW_EL |
71      TABLE_ROW_GROUP_EL
72    }
73    
74    sub SCOPING_EL () {
75      BUTTON_EL |
76      CAPTION_EL |
77      HTML_EL |
78      TABLE_EL |
79      TABLE_CELL_EL |
80      MISC_SCOPING_EL
81    }
82    
83    sub TABLE_SCOPING_EL () {
84      HTML_EL |
85      TABLE_EL
86    }
87    
88    sub TABLE_ROWS_SCOPING_EL () {
89      HTML_EL |
90      TABLE_ROW_GROUP_EL
91    }
92    
93    sub TABLE_ROW_SCOPING_EL () {
94      HTML_EL |
95      TABLE_ROW_EL
96    }
97    
98    sub SPECIAL_EL () {
99      ADDRESS_EL |
100      BODY_EL |
101      DIV_EL |
102      END_TAG_OPTIONAL_EL |
103      FORM_EL |
104      FRAMESET_EL |
105      HEADING_EL |
106      OPTION_EL |
107      OPTGROUP_EL |
108      SELECT_EL |
109      TABLE_ROW_EL |
110      TABLE_ROW_GROUP_EL |
111      MISC_SPECIAL_EL
112    }
113    
114    my $el_category = {
115      a => A_EL | FORMATTING_EL,
116      address => ADDRESS_EL,
117      applet => MISC_SCOPING_EL,
118      area => MISC_SPECIAL_EL,
119      b => FORMATTING_EL,
120      base => MISC_SPECIAL_EL,
121      basefont => MISC_SPECIAL_EL,
122      bgsound => MISC_SPECIAL_EL,
123      big => FORMATTING_EL,
124      blockquote => MISC_SPECIAL_EL,
125      body => BODY_EL,
126      br => MISC_SPECIAL_EL,
127      button => BUTTON_EL,
128      caption => CAPTION_EL,
129      center => MISC_SPECIAL_EL,
130      col => MISC_SPECIAL_EL,
131      colgroup => MISC_SPECIAL_EL,
132      dd => DD_EL,
133      dir => MISC_SPECIAL_EL,
134      div => DIV_EL,
135      dl => MISC_SPECIAL_EL,
136      dt => DT_EL,
137      em => FORMATTING_EL,
138      embed => MISC_SPECIAL_EL,
139      fieldset => MISC_SPECIAL_EL,
140      font => FORMATTING_EL,
141      form => FORM_EL,
142      frame => MISC_SPECIAL_EL,
143      frameset => FRAMESET_EL,
144      h1 => HEADING_EL,
145      h2 => HEADING_EL,
146      h3 => HEADING_EL,
147      h4 => HEADING_EL,
148      h5 => HEADING_EL,
149      h6 => HEADING_EL,
150      head => MISC_SPECIAL_EL,
151      hr => MISC_SPECIAL_EL,
152      html => HTML_EL,
153      i => FORMATTING_EL,
154      iframe => MISC_SPECIAL_EL,
155      img => MISC_SPECIAL_EL,
156      input => MISC_SPECIAL_EL,
157      isindex => MISC_SPECIAL_EL,
158      li => LI_EL,
159      link => MISC_SPECIAL_EL,
160      listing => MISC_SPECIAL_EL,
161      marquee => MISC_SCOPING_EL,
162      menu => MISC_SPECIAL_EL,
163      meta => MISC_SPECIAL_EL,
164      nobr => NOBR_EL | FORMATTING_EL,
165      noembed => MISC_SPECIAL_EL,
166      noframes => MISC_SPECIAL_EL,
167      noscript => MISC_SPECIAL_EL,
168      object => MISC_SCOPING_EL,
169      ol => MISC_SPECIAL_EL,
170      optgroup => OPTGROUP_EL,
171      option => OPTION_EL,
172      p => P_EL,
173      param => MISC_SPECIAL_EL,
174      plaintext => MISC_SPECIAL_EL,
175      pre => MISC_SPECIAL_EL,
176      s => FORMATTING_EL,
177      script => MISC_SPECIAL_EL,
178      select => SELECT_EL,
179      small => FORMATTING_EL,
180      spacer => MISC_SPECIAL_EL,
181      strike => FORMATTING_EL,
182      strong => FORMATTING_EL,
183      style => MISC_SPECIAL_EL,
184      table => TABLE_EL,
185      tbody => TABLE_ROW_GROUP_EL,
186      td => TABLE_CELL_EL,
187      textarea => MISC_SPECIAL_EL,
188      tfoot => TABLE_ROW_GROUP_EL,
189      th => TABLE_CELL_EL,
190      thead => TABLE_ROW_GROUP_EL,
191      title => MISC_SPECIAL_EL,
192      tr => TABLE_ROW_EL,
193      tt => FORMATTING_EL,
194      u => FORMATTING_EL,
195      ul => MISC_SPECIAL_EL,
196      wbr => MISC_SPECIAL_EL,
197    };
198    
199    my $el_category_f = {
200      $MML_NS => {
201        'annotation-xml' => MML_AXML_EL,
202        mi => FOREIGN_FLOW_CONTENT_EL,
203        mo => FOREIGN_FLOW_CONTENT_EL,
204        mn => FOREIGN_FLOW_CONTENT_EL,
205        ms => FOREIGN_FLOW_CONTENT_EL,
206        mtext => FOREIGN_FLOW_CONTENT_EL,
207      },
208      $SVG_NS => {
209        foreignObject => FOREIGN_FLOW_CONTENT_EL,
210        desc => FOREIGN_FLOW_CONTENT_EL,
211        title => FOREIGN_FLOW_CONTENT_EL,
212      },
213      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
214    };
215    
216    my $svg_attr_name = {
217      attributetype => 'attributeType',
218      basefrequency => 'baseFrequency',
219      baseprofile => 'baseProfile',
220      calcmode => 'calcMode',
221      clippathunits => 'clipPathUnits',
222      contentscripttype => 'contentScriptType',
223      contentstyletype => 'contentStyleType',
224      diffuseconstant => 'diffuseConstant',
225      edgemode => 'edgeMode',
226      externalresourcesrequired => 'externalResourcesRequired',
227      fecolormatrix => 'feColorMatrix',
228      fecomposite => 'feComposite',
229      fegaussianblur => 'feGaussianBlur',
230      femorphology => 'feMorphology',
231      fetile => 'feTile',
232      filterres => 'filterRes',
233      filterunits => 'filterUnits',
234      glyphref => 'glyphRef',
235      gradienttransform => 'gradientTransform',
236      gradientunits => 'gradientUnits',
237      kernelmatrix => 'kernelMatrix',
238      kernelunitlength => 'kernelUnitLength',
239      keypoints => 'keyPoints',
240      keysplines => 'keySplines',
241      keytimes => 'keyTimes',
242      lengthadjust => 'lengthAdjust',
243      limitingconeangle => 'limitingConeAngle',
244      markerheight => 'markerHeight',
245      markerunits => 'markerUnits',
246      markerwidth => 'markerWidth',
247      maskcontentunits => 'maskContentUnits',
248      maskunits => 'maskUnits',
249      numoctaves => 'numOctaves',
250      pathlength => 'pathLength',
251      patterncontentunits => 'patternContentUnits',
252      patterntransform => 'patternTransform',
253      patternunits => 'patternUnits',
254      pointsatx => 'pointsAtX',
255      pointsaty => 'pointsAtY',
256      pointsatz => 'pointsAtZ',
257      preservealpha => 'preserveAlpha',
258      preserveaspectratio => 'preserveAspectRatio',
259      primitiveunits => 'primitiveUnits',
260      refx => 'refX',
261      refy => 'refY',
262      repeatcount => 'repeatCount',
263      repeatdur => 'repeatDur',
264      requiredextensions => 'requiredExtensions',
265      specularconstant => 'specularConstant',
266      specularexponent => 'specularExponent',
267      spreadmethod => 'spreadMethod',
268      startoffset => 'startOffset',
269      stddeviation => 'stdDeviation',
270      stitchtiles => 'stitchTiles',
271      surfacescale => 'surfaceScale',
272      systemlanguage => 'systemLanguage',
273      tablevalues => 'tableValues',
274      targetx => 'targetX',
275      targety => 'targetY',
276      textlength => 'textLength',
277      viewbox => 'viewBox',
278      viewtarget => 'viewTarget',
279      xchannelselector => 'xChannelSelector',
280      ychannelselector => 'yChannelSelector',
281      zoomandpan => 'zoomAndPan',
282    };
283    
284    my $foreign_attr_xname = {
285      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
286      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
287      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
288      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
289      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
290      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
291      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
292      'xml:base' => [$XML_NS, ['xml', 'base']],
293      'xml:lang' => [$XML_NS, ['xml', 'lang']],
294      'xml:space' => [$XML_NS, ['xml', 'space']],
295      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
296      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
297  };  };
298    
299    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
300    
301  my $c1_entity_char = {  my $c1_entity_char = {
302    0x80 => 0x20AC,    0x80 => 0x20AC,
303    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 333  my $c1_entity_char = {
333    0x9F => 0x0178,    0x9F => 0x0178,
334  }; # $c1_entity_char  }; # $c1_entity_char
335    
336  my $special_category = {  sub parse_byte_string ($$$$;$) {
337    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
338    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
339    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
340    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
341    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
342    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
343    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
344    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
345    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
346    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  
347    
348  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
349    my $self = shift->new;      my (%opt) = @_;
350    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
351      };
352      $self->{parse_error} = $onerror; # updated later by parse_char_string
353    
354      ## HTML5 encoding sniffing algorithm
355      require Message::Charset::Info;
356      my $charset;
357      my $buffer;
358      my ($char_stream, $e_status);
359    
360      SNIFFING: {
361    
362        ## Step 1
363        if (defined $charset_name) {
364          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
365    
366          ## ISSUE: Unsupported encoding is not ignored according to the spec.
367          ($char_stream, $e_status) = $charset->get_decode_handle
368              ($byte_stream, allow_error_reporting => 1,
369               allow_fallback => 1);
370          if ($char_stream) {
371            $self->{confident} = 1;
372            last SNIFFING;
373          } else {
374            ## TODO: unsupported error
375          }
376        }
377    
378        ## Step 2
379        my $byte_buffer = '';
380        for (1..1024) {
381          my $char = $byte_stream->getc;
382          last unless defined $char;
383          $byte_buffer .= $char;
384        } ## TODO: timeout
385    
386        ## Step 3
387        if ($byte_buffer =~ /^\xFE\xFF/) {
388          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
389          ($char_stream, $e_status) = $charset->get_decode_handle
390              ($byte_stream, allow_error_reporting => 1,
391               allow_fallback => 1, byte_buffer => \$byte_buffer);
392          $self->{confident} = 1;
393          last SNIFFING;
394        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
395          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
396          ($char_stream, $e_status) = $charset->get_decode_handle
397              ($byte_stream, allow_error_reporting => 1,
398               allow_fallback => 1, byte_buffer => \$byte_buffer);
399          $self->{confident} = 1;
400          last SNIFFING;
401        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        }
409    
410        ## Step 4
411        ## TODO: <meta charset>
412    
413        ## Step 5
414        ## TODO: from history
415    
416        ## Step 6
417        require Whatpm::Charset::UniversalCharDet;
418        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
419            ($byte_buffer);
420        if (defined $charset_name) {
421          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
422    
423          ## ISSUE: Unsupported encoding is not ignored according to the spec.
424          require Whatpm::Charset::DecodeHandle;
425          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
426              ($byte_stream);
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($buffer, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          if ($char_stream) {
431            $buffer->{buffer} = $byte_buffer;
432            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
433                            value => $charset_name,
434                            level => $self->{info_level},
435                            line => 1, column => 1);
436            $self->{confident} = 0;
437            last SNIFFING;
438          }
439        }
440    
441        ## Step 7: default
442        ## TODO: Make this configurable.
443        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
444            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
445            ## detectable in the step 6.
446        require Whatpm::Charset::DecodeHandle;
447        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
448            ($byte_stream);
449        ($char_stream, $e_status)
450            = $charset->get_decode_handle ($buffer,
451                                           allow_error_reporting => 1,
452                                           allow_fallback => 1,
453                                           byte_buffer => \$byte_buffer);
454        $buffer->{buffer} = $byte_buffer;
455        !!!parse-error (type => 'sniffing:default', ## TODO: type name
456                        value => 'windows-1252',
457                        level => $self->{info_level},
458                        line => 1, column => 1);
459        $self->{confident} = 0;
460      } # SNIFFING
461    
462      $self->{input_encoding} = $charset->get_iana_name;
463      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
464        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
465                        value => $self->{input_encoding},
466                        level => $self->{unsupported_level},
467                        line => 1, column => 1);
468      } elsif (not ($e_status &
469                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
470        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
471                        value => $self->{input_encoding},
472                        level => $self->{unsupported_level},
473                        line => 1, column => 1);
474      }
475    
476      $self->{change_encoding} = sub {
477        my $self = shift;
478        $charset_name = shift;
479        my $token = shift;
480    
481        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
482        ($char_stream, $e_status) = $charset->get_decode_handle
483            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
484             byte_buffer => \ $buffer->{buffer});
485        
486        if ($char_stream) { # if supported
487          ## "Change the encoding" algorithm:
488    
489          ## Step 1    
490          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
491            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
492            ($char_stream, $e_status) = $charset->get_decode_handle
493                ($byte_stream,
494                 byte_buffer => \ $buffer->{buffer});
495          }
496          $charset_name = $charset->get_iana_name;
497          
498          ## Step 2
499          if (defined $self->{input_encoding} and
500              $self->{input_encoding} eq $charset_name) {
501            !!!parse-error (type => 'charset label:matching', ## TODO: type
502                            value => $charset_name,
503                            level => $self->{info_level});
504            $self->{confident} = 1;
505            return;
506          }
507    
508          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
509              ':'.$charset_name, level => 'w', token => $token);
510          
511          ## Step 3
512          # if (can) {
513            ## change the encoding on the fly.
514            #$self->{confident} = 1;
515            #return;
516          # }
517          
518          ## Step 4
519          throw Whatpm::HTML::RestartParser ();
520        }
521      }; # $self->{change_encoding}
522    
523      my $char_onerror = sub {
524        my (undef, $type, %opt) = @_;
525        !!!parse-error (%opt, type => $type,
526                        line => $self->{line}, column => $self->{column} + 1);
527        if ($opt{octets}) {
528          ${$opt{octets}} = "\x{FFFD}"; # relacement character
529        }
530      };
531      $char_stream->onerror ($char_onerror);
532    
533      my @args = @_; shift @args; # $s
534      my $return;
535      try {
536        $return = $self->parse_char_stream ($char_stream, @args);  
537      } catch Whatpm::HTML::RestartParser with {
538        ## NOTE: Invoked after {change_encoding}.
539    
540        $self->{input_encoding} = $charset->get_iana_name;
541        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
542          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
543                          value => $self->{input_encoding},
544                          level => $self->{unsupported_level},
545                          line => 1, column => 1);
546        } elsif (not ($e_status &
547                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
548          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
549                          value => $self->{input_encoding},
550                          level => $self->{unsupported_level},
551                          line => 1, column => 1);
552        }
553        $self->{confident} = 1;
554        $char_stream->onerror ($char_onerror);
555        $return = $self->parse_char_stream ($char_stream, @args);
556      };
557      return $return;
558    } # parse_byte_stream
559    
560    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
561    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
562    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
563    ## because the core part of our HTML parser expects a string of character,
564    ## not a string of bytes or code units or anything which might contain a BOM.
565    ## Therefore, any parser interface that accepts a string of bytes,
566    ## such as |parse_byte_string| in this module, must ensure that it does
567    ## strip the BOM and never strip any ZWNBSP.
568    
569    sub parse_char_string ($$$;$) {
570      my $self = shift;
571      require utf8;
572      my $s = ref $_[0] ? $_[0] : \($_[0]);
573      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
574      return $self->parse_char_stream ($input, @_[1..$#_]);
575    } # parse_char_string
576    *parse_string = \&parse_char_string;
577    
578    sub parse_char_stream ($$$;$) {
579      my $self = ref $_[0] ? shift : shift->new;
580      my $input = $_[0];
581    $self->{document} = $_[1];    $self->{document} = $_[1];
582      @{$self->{document}->child_nodes} = ();
583    
584    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
585    
586      $self->{confident} = 1 unless exists $self->{confident};
587      $self->{document}->input_encoding ($self->{input_encoding})
588          if defined $self->{input_encoding};
589    
590    my $i = 0;    my $i = 0;
591    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
592    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
593    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
594      my $self = shift;      my $self = shift;
595    
596      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
597      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
598    
599      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
600      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
601      $column++;        $char = $self->{next_next_char};
602          delete $self->{next_next_char};
603        } else {
604          $char = $input->getc;
605        }
606        $self->{next_char} = -1 and return unless defined $char;
607        $self->{next_char} = ord $char;
608    
609        ($self->{line_prev}, $self->{column_prev})
610            = ($self->{line}, $self->{column});
611        $self->{column}++;
612            
613      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
614        $line++;        !!!cp ('j1');
615        $column = 0;        $self->{line}++;
616      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
617        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
618        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
619        $line++;        my $next = $input->getc;
620        $column = 0;        if (defined $next and $next ne "\x0A") {
621      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
622        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
623      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
624          $self->{line}++;
625          $self->{column} = 0;
626        } elsif ($self->{next_char} > 0x10FFFF) {
627          !!!cp ('j3');
628          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
629        } elsif ($self->{next_char} == 0x0000) { # NULL
630          !!!cp ('j4');
631        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
632        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
633        } elsif ($self->{next_char} <= 0x0008 or
634                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
635                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
636                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
637                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
638                 {
639                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
640                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
641                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
642                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
643                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
644                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
645                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
646                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
647                  0x10FFFE => 1, 0x10FFFF => 1,
648                 }->{$self->{next_char}}) {
649          !!!cp ('j5');
650          !!!parse-error (type => 'control char', level => $self->{must_level});
651    ## TODO: error type documentation
652      }      }
653    };    };
654    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
655    $self->{next_input_character} = -1;    $self->{next_char} = -1;
656    
657    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
658      my (%opt) = @_;      my (%opt) = @_;
659      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
660        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
661        warn "Parse error ($opt{type}) at line $line column $column\n";
662    };    };
663    $self->{parse_error} = sub {    $self->{parse_error} = sub {
664      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
665    };    };
666    
667    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669    $self->_construct_tree;    $self->_construct_tree;
670    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
671    
672      delete $self->{parse_error}; # remove loop
673    
674    return $self->{document};    return $self->{document};
675  } # parse_string  } # parse_char_stream
676    
677  sub new ($) {  sub new ($) {
678    my $class = shift;    my $class = shift;
679    my $self = bless {}, $class;    my $self = bless {
680    $self->{set_next_input_character} = sub {      must_level => 'm',
681      $self->{next_input_character} = -1;      should_level => 's',
682        good_level => 'w',
683        warn_level => 'w',
684        info_level => 'i',
685        unsupported_level => 'u',
686      }, $class;
687      $self->{set_next_char} = sub {
688        $self->{next_char} = -1;
689    };    };
690    $self->{parse_error} = sub {    $self->{parse_error} = sub {
691      #      #
692    };    };
693      $self->{change_encoding} = sub {
694        # if ($_[0] is a supported encoding) {
695        #   run "change the encoding" algorithm;
696        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
697        # }
698      };
699      $self->{application_cache_selection} = sub {
700        #
701      };
702    return $self;    return $self;
703  } # new  } # new
704    
705    sub CM_ENTITY () { 0b001 } # & markup in data
706    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
707    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
708    
709    sub PLAINTEXT_CONTENT_MODEL () { 0 }
710    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
711    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
712    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
713    
714    sub DATA_STATE () { 0 }
715    sub ENTITY_DATA_STATE () { 1 }
716    sub TAG_OPEN_STATE () { 2 }
717    sub CLOSE_TAG_OPEN_STATE () { 3 }
718    sub TAG_NAME_STATE () { 4 }
719    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
720    sub ATTRIBUTE_NAME_STATE () { 6 }
721    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
722    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
723    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
724    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
725    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
726    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
727    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
728    sub COMMENT_START_STATE () { 14 }
729    sub COMMENT_START_DASH_STATE () { 15 }
730    sub COMMENT_STATE () { 16 }
731    sub COMMENT_END_STATE () { 17 }
732    sub COMMENT_END_DASH_STATE () { 18 }
733    sub BOGUS_COMMENT_STATE () { 19 }
734    sub DOCTYPE_STATE () { 20 }
735    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
736    sub DOCTYPE_NAME_STATE () { 22 }
737    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
738    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
739    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
740    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
741    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
742    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
743    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
744    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
745    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
746    sub BOGUS_DOCTYPE_STATE () { 32 }
747    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
748    sub SELF_CLOSING_START_TAG_STATE () { 34 }
749    sub CDATA_BLOCK_STATE () { 35 }
750    
751    sub DOCTYPE_TOKEN () { 1 }
752    sub COMMENT_TOKEN () { 2 }
753    sub START_TAG_TOKEN () { 3 }
754    sub END_TAG_TOKEN () { 4 }
755    sub END_OF_FILE_TOKEN () { 5 }
756    sub CHARACTER_TOKEN () { 6 }
757    
758    sub AFTER_HTML_IMS () { 0b100 }
759    sub HEAD_IMS ()       { 0b1000 }
760    sub BODY_IMS ()       { 0b10000 }
761    sub BODY_TABLE_IMS () { 0b100000 }
762    sub TABLE_IMS ()      { 0b1000000 }
763    sub ROW_IMS ()        { 0b10000000 }
764    sub BODY_AFTER_IMS () { 0b100000000 }
765    sub FRAME_IMS ()      { 0b1000000000 }
766    sub SELECT_IMS ()     { 0b10000000000 }
767    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
768        ## NOTE: "in foreign content" insertion mode is special; it is combined
769        ## with the secondary insertion mode.  In this parser, they are stored
770        ## together in the bit-or'ed form.
771    
772    ## NOTE: "initial" and "before html" insertion modes have no constants.
773    
774    ## NOTE: "after after body" insertion mode.
775    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
776    
777    ## NOTE: "after after frameset" insertion mode.
778    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
779    
780    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
781    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
782    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
783    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
784    sub IN_BODY_IM () { BODY_IMS }
785    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
786    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
787    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
788    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
789    sub IN_TABLE_IM () { TABLE_IMS }
790    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
791    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
792    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
793    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
794    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
795    sub IN_COLUMN_GROUP_IM () { 0b10 }
796    
797  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
798    
799  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
800    my $self = shift;    my $self = shift;
801    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
802    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
803    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
804    undef $self->{current_attribute};    undef $self->{current_attribute};
805    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
806    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
807      delete $self->{self_closing};
808    $self->{char} = [];    $self->{char} = [];
809    # $self->{next_input_character}    # $self->{next_char}
810    !!!next-input-character;    !!!next-input-character;
811    $self->{token} = [];    $self->{token} = [];
812    # $self->{escape}    # $self->{escape}
813  } # _initialize_tokenizer  } # _initialize_tokenizer
814    
815  ## A token has:  ## A token has:
816  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
817  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
818  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
819  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
820  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
821  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
822  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
823  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
824    ##        ->{name}
825    ##        ->{value}
826    ##        ->{has_reference} == 1 or 0
827    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
828    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
829    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
830    ##     while the token is pushed back to the stack.
831    
832    ## ISSUE: "When a DOCTYPE token is created, its
833    ## <i>self-closing flag</i> must be unset (its other state is that it
834    ## be set), and its attributes list must be empty.": Wrong subject?
835    
836  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
837    
# Line 185  sub _initialize_tokenizer ($) { Line 841  sub _initialize_tokenizer ($) {
841  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
842  ## and removed from the list.  ## and removed from the list.
843    
844    ## NOTE: HTML5 "Writing HTML documents" section, applied to
845    ## documents and not to user agents and conformance checkers,
846    ## contains some requirements that are not detected by the
847    ## parsing algorithm:
848    ## - Some requirements on character encoding declarations. ## TODO
849    ## - "Elements MUST NOT contain content that their content model disallows."
850    ##   ... Some are parse error, some are not (will be reported by c.c.).
851    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
852    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
853    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
854    
855    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
856    ## be detected by the HTML5 parsing algorithm:
857    ## - Text,
858    
859  sub _get_next_token ($) {  sub _get_next_token ($) {
860    my $self = shift;    my $self = shift;
861    
862      if ($self->{self_closing}) {
863        !!!parse-error (type => 'nestc', token => $self->{current_token});
864        ## NOTE: The |self_closing| flag is only set by start tag token.
865        ## In addition, when a start tag token is emitted, it is always set to
866        ## |current_token|.
867        delete $self->{self_closing};
868      }
869    
870    if (@{$self->{token}}) {    if (@{$self->{token}}) {
871        $self->{self_closing} = $self->{token}->[0]->{self_closing};
872      return shift @{$self->{token}};      return shift @{$self->{token}};
873    }    }
874    
875    A: {    A: {
876      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
877        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
878          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
879              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
880            $self->{state} = 'entity data';            !!!cp (1);
881              $self->{state} = ENTITY_DATA_STATE;
882            !!!next-input-character;            !!!next-input-character;
883            redo A;            redo A;
884          } else {          } else {
885              !!!cp (2);
886            #            #
887          }          }
888        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
889          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
890            unless ($self->{escape}) {            unless ($self->{escape}) {
891              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
892                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
893                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
894                  !!!cp (3);
895                $self->{escape} = 1;                $self->{escape} = 1;
896                } else {
897                  !!!cp (4);
898              }              }
899              } else {
900                !!!cp (5);
901            }            }
902          }          }
903                    
904          #          #
905        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
906          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
907              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
908               not $self->{escape})) {               not $self->{escape})) {
909            $self->{state} = 'tag open';            !!!cp (6);
910              $self->{state} = TAG_OPEN_STATE;
911            !!!next-input-character;            !!!next-input-character;
912            redo A;            redo A;
913          } else {          } else {
914              !!!cp (7);
915            #            #
916          }          }
917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
918          if ($self->{escape} and          if ($self->{escape} and
919              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
920               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
921            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
922                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
923              delete $self->{escape};              delete $self->{escape};
924              } else {
925                !!!cp (9);
926            }            }
927            } else {
928              !!!cp (10);
929          }          }
930                    
931          #          #
932        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
933          !!!emit ({type => 'end-of-file'});          !!!cp (11);
934            !!!emit ({type => END_OF_FILE_TOKEN,
935                      line => $self->{line}, column => $self->{column}});
936          last A; ## TODO: ok?          last A; ## TODO: ok?
937          } else {
938            !!!cp (12);
939        }        }
940        # Anything else        # Anything else
941        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
942                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
943                       line => $self->{line}, column => $self->{column},
944                      };
945        ## Stay in the data state        ## Stay in the data state
946        !!!next-input-character;        !!!next-input-character;
947    
948        !!!emit ($token);        !!!emit ($token);
949    
950        redo A;        redo A;
951      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
952        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
953    
954          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
955                
956        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
957    
958        $self->{state} = 'data';        $self->{state} = DATA_STATE;
959        # next-input-character is already done        # next-input-character is already done
960    
961        unless (defined $token) {        unless (defined $token) {
962          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
963            !!!emit ({type => CHARACTER_TOKEN, data => '&',
964                      line => $l, column => $c,
965                     });
966        } else {        } else {
967            !!!cp (14);
968          !!!emit ($token);          !!!emit ($token);
969        }        }
970    
971        redo A;        redo A;
972      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
973        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
974            $self->{content_model_flag} eq 'CDATA') {          if ($self->{next_char} == 0x002F) { # /
975          if ($self->{next_input_character} == 0x002F) { # /            !!!cp (15);
976            !!!next-input-character;            !!!next-input-character;
977            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
978            redo A;            redo A;
979          } else {          } else {
980              !!!cp (16);
981            ## reconsume            ## reconsume
982            $self->{state} = 'data';            $self->{state} = DATA_STATE;
983    
984            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
985                        line => $self->{line_prev},
986                        column => $self->{column_prev},
987                       });
988    
989            redo A;            redo A;
990          }          }
991        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
992          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
993            $self->{state} = 'markup declaration open';            !!!cp (17);
994              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
995            !!!next-input-character;            !!!next-input-character;
996            redo A;            redo A;
997          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
998            $self->{state} = 'close tag open';            !!!cp (18);
999              $self->{state} = CLOSE_TAG_OPEN_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1003                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1004              !!!cp (19);
1005            $self->{current_token}            $self->{current_token}
1006              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1007                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1008            $self->{state} = 'tag name';                 line => $self->{line_prev},
1009                   column => $self->{column_prev}};
1010              $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1014                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1015            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1016                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1017            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1018                                        line => $self->{line_prev},
1019                                        column => $self->{column_prev}};
1020              $self->{state} = TAG_NAME_STATE;
1021            !!!next-input-character;            !!!next-input-character;
1022            redo A;            redo A;
1023          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1024            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1025            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1026                              line => $self->{line_prev},
1027                              column => $self->{column_prev});
1028              $self->{state} = DATA_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030    
1031            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1032                        line => $self->{line_prev},
1033                        column => $self->{column_prev},
1034                       });
1035    
1036            redo A;            redo A;
1037          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1038            !!!parse-error (type => 'pio');            !!!cp (22);
1039            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1040            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1041                              column => $self->{column_prev});
1042              $self->{state} = BOGUS_COMMENT_STATE;
1043              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1044                                        line => $self->{line_prev},
1045                                        column => $self->{column_prev},
1046                                       };
1047              ## $self->{next_char} is intentionally left as is
1048            redo A;            redo A;
1049          } else {          } else {
1050            !!!parse-error (type => 'bare stago');            !!!cp (23);
1051            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1052                              line => $self->{line_prev},
1053                              column => $self->{column_prev});
1054              $self->{state} = DATA_STATE;
1055            ## reconsume            ## reconsume
1056    
1057            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1058                        line => $self->{line_prev},
1059                        column => $self->{column_prev},
1060                       });
1061    
1062            redo A;            redo A;
1063          }          }
1064        } else {        } else {
1065          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1066        }        }
1067      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1068        if ($self->{content_model_flag} eq 'RCDATA' or        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1069            $self->{content_model_flag} eq 'CDATA') {        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1070          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1071    
1072            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1073            my @next_char;            my @next_char;
1074            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1075              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1076              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1077              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1078              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1079                  !!!cp (24);
1080                !!!next-input-character;                !!!next-input-character;
1081                next TAGNAME;                next TAGNAME;
1082              } else {              } else {
1083                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1084                  $self->{next_char} = shift @next_char; # reconsume
1085                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1086                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1087    
1088                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1089                            line => $l, column => $c,
1090                           });
1091        
1092                redo A;                redo A;
1093              }              }
1094            }            }
1095            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1096                
1097            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1098                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1099                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1100                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1101                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1102                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1103                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1104                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1105              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1106                $self->{next_char} = shift @next_char; # reconsume
1107              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1108              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1109              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1110                          line => $l, column => $c,
1111                         });
1112              redo A;              redo A;
1113            } else {            } else {
1114              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1115                $self->{next_char} = shift @next_char;
1116              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1117              # and consume...              # and consume...
1118            }            }
1119          } else {          } else {
1120            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1121              !!!cp (28);
1122            # next-input-character is already done            # next-input-character is already done
1123            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1124            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1125                        line => $l, column => $c,
1126                       });
1127            redo A;            redo A;
1128          }          }
1129        }        }
1130                
1131        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1132            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1133          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1134                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1135          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1136          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1137          redo A;                 line => $l, column => $c};
1138        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1139                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1140          $self->{current_token} = {type => 'end tag',          redo A;
1141                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1142          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1143          !!!next-input-character;          !!!cp (30);
1144          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1145        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1146          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1147          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1148            !!!next-input-character;
1149            redo A;
1150          } elsif ($self->{next_char} == 0x003E) { # >
1151            !!!cp (31);
1152            !!!parse-error (type => 'empty end tag',
1153                            line => $self->{line_prev}, ## "<" in "</>"
1154                            column => $self->{column_prev} - 1);
1155            $self->{state} = DATA_STATE;
1156          !!!next-input-character;          !!!next-input-character;
1157          redo A;          redo A;
1158        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1159            !!!cp (32);
1160          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1161          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1162          # reconsume          # reconsume
1163    
1164          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1165                      line => $l, column => $c,
1166                     });
1167    
1168          redo A;          redo A;
1169        } else {        } else {
1170            !!!cp (33);
1171          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1172          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1173          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1174          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1175        }                                    column => $self->{column_prev} - 1,
1176      } elsif ($self->{state} eq 'tag name') {                                   };
1177        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1178            $self->{next_input_character} == 0x000A or # LF          redo A;
1179            $self->{next_input_character} == 0x000B or # VT        }
1180            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1181            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1182          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1183          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1184          redo A;            $self->{next_char} == 0x000C or # FF
1185        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1186          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1187            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1188                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1189            redo A;
1190          } elsif ($self->{next_char} == 0x003E) { # >
1191            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1192              !!!cp (35);
1193            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1194          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1195            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1196            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1197              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1198            }            #  !!! cp (36);
1199              #  !!! parse-error (type => 'end tag attribute');
1200              #} else {
1201                !!!cp (37);
1202              #}
1203          } else {          } else {
1204            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1205          }          }
1206          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1207          !!!next-input-character;          !!!next-input-character;
1208    
1209          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1210    
1211          redo A;          redo A;
1212        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1213                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1214          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1215            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1216            # start tag or end tag            # start tag or end tag
1217          ## Stay in this state          ## Stay in this state
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1221          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1222          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1223            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1224            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1225          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1226            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1227            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1228              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1229            }            #  !!! cp (40);
1230              #  !!! parse-error (type => 'end tag attribute');
1231              #} else {
1232                !!!cp (41);
1233              #}
1234          } else {          } else {
1235            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1236          }          }
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          # reconsume          # reconsume
1239    
1240          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1241    
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1244            !!!cp (42);
1245            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1246          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} 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  
1247          redo A;          redo A;
1248        } else {        } else {
1249          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1250            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1251            # start tag or end tag            # start tag or end tag
1252          ## Stay in the state          ## Stay in the state
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        }        }
1256      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1257        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1258            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1259            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1260            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1261            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1262            !!!cp (45);
1263          ## Stay in the state          ## Stay in the state
1264          !!!next-input-character;          !!!next-input-character;
1265          redo A;          redo A;
1266        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1267          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1268            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1269            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1270          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1271            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1272            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1273                !!!cp (47);
1274              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1275              } else {
1276                !!!cp (48);
1277            }            }
1278          } else {          } else {
1279            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1280          }          }
1281          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1282          !!!next-input-character;          !!!next-input-character;
1283    
1284          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1285    
1286          redo A;          redo A;
1287        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1288                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1289          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1290                                value => ''};          $self->{current_attribute}
1291          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1292                   value => '',
1293                   line => $self->{line}, column => $self->{column}};
1294            $self->{state} = ATTRIBUTE_NAME_STATE;
1295            !!!next-input-character;
1296            redo A;
1297          } elsif ($self->{next_char} == 0x002F) { # /
1298            !!!cp (50);
1299            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } 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) {  
1303          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1304          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1310                !!!cp (53);
1311              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1312              } else {
1313                !!!cp (54);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1317          }          }
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          # reconsume          # reconsume
1320    
1321          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1322    
1323          redo A;          redo A;
1324        } else {        } else {
1325          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1326                                value => ''};               0x0022 => 1, # "
1327          $self->{state} = 'attribute name';               0x0027 => 1, # '
1328                 0x003D => 1, # =
1329                }->{$self->{next_char}}) {
1330              !!!cp (55);
1331              !!!parse-error (type => 'bad attribute name');
1332            } else {
1333              !!!cp (56);
1334            }
1335            $self->{current_attribute}
1336                = {name => chr ($self->{next_char}),
1337                   value => '',
1338                   line => $self->{line}, column => $self->{column}};
1339            $self->{state} = ATTRIBUTE_NAME_STATE;
1340          !!!next-input-character;          !!!next-input-character;
1341          redo A;          redo A;
1342        }        }
1343      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1344        my $before_leave = sub {        my $before_leave = sub {
1345          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1346              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1347            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1348              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1349            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1350          } else {          } else {
1351              !!!cp (58);
1352            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1353              = $self->{current_attribute};              = $self->{current_attribute};
1354          }          }
1355        }; # $before_leave        }; # $before_leave
1356    
1357        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1358            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1359            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1360            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1361            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1362            !!!cp (59);
1363          $before_leave->();          $before_leave->();
1364          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366          redo A;          redo A;
1367        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1368            !!!cp (60);
1369          $before_leave->();          $before_leave->();
1370          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1371          !!!next-input-character;          !!!next-input-character;
1372          redo A;          redo A;
1373        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1374          $before_leave->();          $before_leave->();
1375          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1380              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1381            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1382              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1383            }            }
1384          } else {          } else {
1385            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1386          }          }
1387          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1388          !!!next-input-character;          !!!next-input-character;
1389    
1390          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1391    
1392          redo A;          redo A;
1393        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1394                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1395          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1396            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1397          ## Stay in the state          ## Stay in the state
1398          !!!next-input-character;          !!!next-input-character;
1399          redo A;          redo A;
1400        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1401            !!!cp (64);
1402          $before_leave->();          $before_leave->();
1403            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1404          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} 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  
1405          redo A;          redo A;
1406        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1407          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1408          $before_leave->();          $before_leave->();
1409          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1410            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1411            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1412          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1413            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1414            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1415                !!!cp (67);
1416              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1417              } else {
1418                ## NOTE: This state should never be reached.
1419                !!!cp (68);
1420            }            }
1421          } else {          } else {
1422            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1423          }          }
1424          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1425          # reconsume          # reconsume
1426    
1427          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1428    
1429          redo A;          redo A;
1430        } else {        } else {
1431          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1432                $self->{next_char} == 0x0027) { # '
1433              !!!cp (69);
1434              !!!parse-error (type => 'bad attribute name');
1435            } else {
1436              !!!cp (70);
1437            }
1438            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1439          ## Stay in the state          ## Stay in the state
1440          !!!next-input-character;          !!!next-input-character;
1441          redo A;          redo A;
1442        }        }
1443      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1444        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1445            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1446            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1447            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1448            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1449            !!!cp (71);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1454          $self->{state} = 'before attribute value';          !!!cp (72);
1455            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1456          !!!next-input-character;          !!!next-input-character;
1457          redo A;          redo A;
1458        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1459          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1460            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1461            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1462          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1463            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1464            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1465                !!!cp (74);
1466              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1467              } else {
1468                ## NOTE: This state should never be reached.
1469                !!!cp (75);
1470            }            }
1471          } else {          } else {
1472            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1473          }          }
1474          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1475          !!!next-input-character;          !!!next-input-character;
1476    
1477          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1478    
1479          redo A;          redo A;
1480        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1481                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1482          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1483                                value => ''};          $self->{current_attribute}
1484          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1485                   value => '',
1486                   line => $self->{line}, column => $self->{column}};
1487            $self->{state} = ATTRIBUTE_NAME_STATE;
1488            !!!next-input-character;
1489            redo A;
1490          } elsif ($self->{next_char} == 0x002F) { # /
1491            !!!cp (77);
1492            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        } 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) {  
1496          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1497          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1498            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1499            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1500          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1501            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1502            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1503                !!!cp (80);
1504              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1505              } else {
1506                ## NOTE: This state should never be reached.
1507                !!!cp (81);
1508            }            }
1509          } else {          } else {
1510            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1511          }          }
1512          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1513          # reconsume          # reconsume
1514    
1515          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1516    
1517          redo A;          redo A;
1518        } else {        } else {
1519          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1520                                value => ''};          $self->{current_attribute}
1521          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524            $self->{state} = ATTRIBUTE_NAME_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;                  redo A;        
1527        }        }
1528      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1529        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1530            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1531            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1532            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1533            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1534            !!!cp (83);
1535          ## Stay in the state          ## Stay in the state
1536          !!!next-input-character;          !!!next-input-character;
1537          redo A;          redo A;
1538        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1539          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1540            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1544          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1545            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1546          ## reconsume          ## reconsume
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1549          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1550            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1551          !!!next-input-character;          !!!next-input-character;
1552          redo A;          redo A;
1553        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1554          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1555            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1556            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1557          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1558            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1559            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1560                !!!cp (88);
1561              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1562              } else {
1563                ## NOTE: This state should never be reached.
1564                !!!cp (89);
1565            }            }
1566          } else {          } else {
1567            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1568          }          }
1569          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1570          !!!next-input-character;          !!!next-input-character;
1571    
1572          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1573    
1574          redo A;          redo A;
1575        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1576          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1577          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1582            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1583                !!!cp (91);
1584              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1585              } else {
1586                ## NOTE: This state should never be reached.
1587                !!!cp (92);
1588            }            }
1589          } else {          } else {
1590            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1591          }          }
1592          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1593          ## reconsume          ## reconsume
1594    
1595          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1596    
1597          redo A;          redo A;
1598        } else {        } else {
1599          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1600          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1601              !!!parse-error (type => 'bad attribute value');
1602            } else {
1603              !!!cp (94);
1604            }
1605            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1606            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        }        }
1610      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1611        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1612          $self->{state} = 'before attribute name';          !!!cp (95);
1613            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1617          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1618          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1619            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1620          !!!next-input-character;          !!!next-input-character;
1621          redo A;          redo A;
1622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1623          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1624          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1625            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1626            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1627          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1628            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1629            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1630                !!!cp (98);
1631              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1632              } else {
1633                ## NOTE: This state should never be reached.
1634                !!!cp (99);
1635            }            }
1636          } else {          } else {
1637            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1638          }          }
1639          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1640          ## reconsume          ## reconsume
1641    
1642          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1643    
1644          redo A;          redo A;
1645        } else {        } else {
1646          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1647            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1648          ## Stay in the state          ## Stay in the state
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;          redo A;
1651        }        }
1652      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1653        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1654          $self->{state} = 'before attribute name';          !!!cp (101);
1655            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1656          !!!next-input-character;          !!!next-input-character;
1657          redo A;          redo A;
1658        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1659          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1660          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1661            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1666          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1668            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1671            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1672                !!!cp (104);
1673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1674              } else {
1675                ## NOTE: This state should never be reached.
1676                !!!cp (105);
1677            }            }
1678          } else {          } else {
1679            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1680          }          }
1681          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1682          ## reconsume          ## reconsume
1683    
1684          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1685    
1686          redo A;          redo A;
1687        } else {        } else {
1688          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1689            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1690          ## Stay in the state          ## Stay in the state
1691          !!!next-input-character;          !!!next-input-character;
1692          redo A;          redo A;
1693        }        }
1694      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1695        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1696            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1697            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1698            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1699            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1700          $self->{state} = 'before attribute name';          !!!cp (107);
1701          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1702          redo A;          !!!next-input-character;
1703        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1704          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1705          $self->{state} = 'entity in attribute value';          !!!cp (108);
1706          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1707          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1708        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1709          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1710            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1711                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1712              !!!cp (109);
1713            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1714          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1715            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1716            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1717                !!!cp (110);
1718              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1719              } else {
1720                ## NOTE: This state should never be reached.
1721                !!!cp (111);
1722            }            }
1723          } else {          } else {
1724            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1725          }          }
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          !!!next-input-character;          !!!next-input-character;
1728    
1729          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1730    
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1733          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1734          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1735            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1736            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1737          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1738            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1739            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1740                !!!cp (113);
1741              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1742              } else {
1743                ## NOTE: This state should never be reached.
1744                !!!cp (114);
1745            }            }
1746          } else {          } else {
1747            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1748          }          }
1749          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1750          ## reconsume          ## reconsume
1751    
1752          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1753    
1754          redo A;          redo A;
1755        } else {        } else {
1756          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1757                 0x0022 => 1, # "
1758                 0x0027 => 1, # '
1759                 0x003D => 1, # =
1760                }->{$self->{next_char}}) {
1761              !!!cp (115);
1762              !!!parse-error (type => 'bad attribute value');
1763            } else {
1764              !!!cp (116);
1765            }
1766            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1767          ## Stay in the state          ## Stay in the state
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
1770        }        }
1771      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1772        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1773              (1,
1774               $self->{last_attribute_value_state}
1775                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1776               $self->{last_attribute_value_state}
1777                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1778               -1);
1779    
1780        unless (defined $token) {        unless (defined $token) {
1781            !!!cp (117);
1782          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1783        } else {        } else {
1784            !!!cp (118);
1785          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1786            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1787          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1788        }        }
1789    
1790        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1791        # next-input-character is already done        # next-input-character is already done
1792        redo A;        redo A;
1793      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1794          if ($self->{next_char} == 0x0009 or # HT
1795              $self->{next_char} == 0x000A or # LF
1796              $self->{next_char} == 0x000B or # VT
1797              $self->{next_char} == 0x000C or # FF
1798              $self->{next_char} == 0x0020) { # SP
1799            !!!cp (118);
1800            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1801            !!!next-input-character;
1802            redo A;
1803          } elsif ($self->{next_char} == 0x003E) { # >
1804            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1805              !!!cp (119);
1806              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1807            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1808              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1809              if ($self->{current_token}->{attributes}) {
1810                !!!cp (120);
1811                !!!parse-error (type => 'end tag attribute');
1812              } else {
1813                ## NOTE: This state should never be reached.
1814                !!!cp (121);
1815              }
1816            } else {
1817              die "$0: $self->{current_token}->{type}: Unknown token type";
1818            }
1819            $self->{state} = DATA_STATE;
1820            !!!next-input-character;
1821    
1822            !!!emit ($self->{current_token}); # start tag or end tag
1823    
1824            redo A;
1825          } elsif ($self->{next_char} == 0x002F) { # /
1826            !!!cp (122);
1827            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1828            !!!next-input-character;
1829            redo A;
1830          } elsif ($self->{next_char} == -1) {
1831            !!!parse-error (type => 'unclosed tag');
1832            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1833              !!!cp (122.3);
1834              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1835            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1836              if ($self->{current_token}->{attributes}) {
1837                !!!cp (122.1);
1838                !!!parse-error (type => 'end tag attribute');
1839              } else {
1840                ## NOTE: This state should never be reached.
1841                !!!cp (122.2);
1842              }
1843            } else {
1844              die "$0: $self->{current_token}->{type}: Unknown token type";
1845            }
1846            $self->{state} = DATA_STATE;
1847            ## Reconsume.
1848            !!!emit ($self->{current_token}); # start tag or end tag
1849            redo A;
1850          } else {
1851            !!!cp ('124.1');
1852            !!!parse-error (type => 'no space between attributes');
1853            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1854            ## reconsume
1855            redo A;
1856          }
1857        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1858          if ($self->{next_char} == 0x003E) { # >
1859            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1860              !!!cp ('124.2');
1861              !!!parse-error (type => 'nestc', token => $self->{current_token});
1862              ## TODO: Different type than slash in start tag
1863              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1864              if ($self->{current_token}->{attributes}) {
1865                !!!cp ('124.4');
1866                !!!parse-error (type => 'end tag attribute');
1867              } else {
1868                !!!cp ('124.5');
1869              }
1870              ## TODO: Test |<title></title/>|
1871            } else {
1872              !!!cp ('124.3');
1873              $self->{self_closing} = 1;
1874            }
1875    
1876            $self->{state} = DATA_STATE;
1877            !!!next-input-character;
1878    
1879            !!!emit ($self->{current_token}); # start tag or end tag
1880    
1881            redo A;
1882          } elsif ($self->{next_char} == -1) {
1883            !!!parse-error (type => 'unclosed tag');
1884            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1885              !!!cp (124.7);
1886              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1887            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1888              if ($self->{current_token}->{attributes}) {
1889                !!!cp (124.5);
1890                !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                ## NOTE: This state should never be reached.
1893                !!!cp (124.6);
1894              }
1895            } else {
1896              die "$0: $self->{current_token}->{type}: Unknown token type";
1897            }
1898            $self->{state} = DATA_STATE;
1899            ## Reconsume.
1900            !!!emit ($self->{current_token}); # start tag or end tag
1901            redo A;
1902          } else {
1903            !!!cp ('124.4');
1904            !!!parse-error (type => 'nestc');
1905            ## TODO: This error type is wrong.
1906            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1907            ## Reconsume.
1908            redo A;
1909          }
1910        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1911        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1912                
1913        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1914          #my $token = {type => COMMENT_TOKEN, data => ''};
1915    
1916        BC: {        BC: {
1917          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1918            $self->{state} = 'data';            !!!cp (124);
1919              $self->{state} = DATA_STATE;
1920            !!!next-input-character;            !!!next-input-character;
1921    
1922            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1923    
1924            redo A;            redo A;
1925          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1926            $self->{state} = 'data';            !!!cp (125);
1927              $self->{state} = DATA_STATE;
1928            ## reconsume            ## reconsume
1929    
1930            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1931    
1932            redo A;            redo A;
1933          } else {          } else {
1934            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1935              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1936            !!!next-input-character;            !!!next-input-character;
1937            redo BC;            redo BC;
1938          }          }
1939        } # BC        } # BC
1940      } elsif ($self->{state} eq 'markup declaration open') {  
1941          die "$0: _get_next_token: unexpected case [BC]";
1942        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1943        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1944    
1945          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1946    
1947        my @next_char;        my @next_char;
1948        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1949                
1950        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1951          !!!next-input-character;          !!!next-input-character;
1952          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1953          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1954            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1955            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1956                                        line => $l, column => $c,
1957                                       };
1958              $self->{state} = COMMENT_START_STATE;
1959            !!!next-input-character;            !!!next-input-character;
1960            redo A;            redo A;
1961            } else {
1962              !!!cp (128);
1963            }
1964          } elsif ($self->{next_char} == 0x0044 or # D
1965                   $self->{next_char} == 0x0064) { # d
1966            !!!next-input-character;
1967            push @next_char, $self->{next_char};
1968            if ($self->{next_char} == 0x004F or # O
1969                $self->{next_char} == 0x006F) { # o
1970              !!!next-input-character;
1971              push @next_char, $self->{next_char};
1972              if ($self->{next_char} == 0x0043 or # C
1973                  $self->{next_char} == 0x0063) { # c
1974                !!!next-input-character;
1975                push @next_char, $self->{next_char};
1976                if ($self->{next_char} == 0x0054 or # T
1977                    $self->{next_char} == 0x0074) { # t
1978                  !!!next-input-character;
1979                  push @next_char, $self->{next_char};
1980                  if ($self->{next_char} == 0x0059 or # Y
1981                      $self->{next_char} == 0x0079) { # y
1982                    !!!next-input-character;
1983                    push @next_char, $self->{next_char};
1984                    if ($self->{next_char} == 0x0050 or # P
1985                        $self->{next_char} == 0x0070) { # p
1986                      !!!next-input-character;
1987                      push @next_char, $self->{next_char};
1988                      if ($self->{next_char} == 0x0045 or # E
1989                          $self->{next_char} == 0x0065) { # e
1990                        !!!cp (129);
1991                        ## TODO: What a stupid code this is!
1992                        $self->{state} = DOCTYPE_STATE;
1993                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1994                                                  quirks => 1,
1995                                                  line => $l, column => $c,
1996                                                 };
1997                        !!!next-input-character;
1998                        redo A;
1999                      } else {
2000                        !!!cp (130);
2001                      }
2002                    } else {
2003                      !!!cp (131);
2004                    }
2005                  } else {
2006                    !!!cp (132);
2007                  }
2008                } else {
2009                  !!!cp (133);
2010                }
2011              } else {
2012                !!!cp (134);
2013              }
2014            } else {
2015              !!!cp (135);
2016          }          }
2017        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2018                 $self->{next_input_character} == 0x0064) { # d                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2019                   $self->{next_char} == 0x005B) { # [
2020          !!!next-input-character;          !!!next-input-character;
2021          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2022          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x0043) { # C
             $self->{next_input_character} == 0x006F) { # o  
2023            !!!next-input-character;            !!!next-input-character;
2024            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2025            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0044) { # D
               $self->{next_input_character} == 0x0063) { # c  
2026              !!!next-input-character;              !!!next-input-character;
2027              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2028              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0041) { # A
                 $self->{next_input_character} == 0x0074) { # t  
2029                !!!next-input-character;                !!!next-input-character;
2030                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2031                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0054) { # T
                   $self->{next_input_character} == 0x0079) { # y  
2032                  !!!next-input-character;                  !!!next-input-character;
2033                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2034                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0041) { # A
                     $self->{next_input_character} == 0x0070) { # p  
2035                    !!!next-input-character;                    !!!next-input-character;
2036                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2037                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x005B) { # [
2038                        $self->{next_input_character} == 0x0065) { # e                      !!!cp (135.1);
2039                      ## ISSUE: What a stupid code this is!                      $self->{state} = CDATA_BLOCK_STATE;
                     $self->{state} = 'DOCTYPE';  
2040                      !!!next-input-character;                      !!!next-input-character;
2041                      redo A;                      redo A;
2042                      } else {
2043                        !!!cp (135.2);
2044                    }                    }
2045                    } else {
2046                      !!!cp (135.3);
2047                  }                  }
2048                  } else {
2049                    !!!cp (135.4);                
2050                }                }
2051                } else {
2052                  !!!cp (135.5);
2053              }              }
2054              } else {
2055                !!!cp (135.6);
2056            }            }
2057            } else {
2058              !!!cp (135.7);
2059          }          }
2060          } else {
2061            !!!cp (136);
2062        }        }
2063    
2064        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2065        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2066        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2067        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2068          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2069                                    line => $l, column => $c,
2070                                   };
2071        redo A;        redo A;
2072                
2073        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2074        ## 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?
2075      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2076        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2077          $self->{state} = 'comment start dash';          !!!cp (137);
2078            $self->{state} = COMMENT_START_DASH_STATE;
2079          !!!next-input-character;          !!!next-input-character;
2080          redo A;          redo A;
2081        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2082            !!!cp (138);
2083          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2084          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2085          !!!next-input-character;          !!!next-input-character;
2086    
2087          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2088    
2089          redo A;          redo A;
2090        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2091            !!!cp (139);
2092          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2093          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2094          ## reconsume          ## reconsume
2095    
2096          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2097    
2098          redo A;          redo A;
2099        } else {        } else {
2100            !!!cp (140);
2101          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2102              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2103          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2104          !!!next-input-character;          !!!next-input-character;
2105          redo A;          redo A;
2106        }        }
2107      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2108        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2109          $self->{state} = 'comment end';          !!!cp (141);
2110            $self->{state} = COMMENT_END_STATE;
2111          !!!next-input-character;          !!!next-input-character;
2112          redo A;          redo A;
2113        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2114            !!!cp (142);
2115          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2116          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2117          !!!next-input-character;          !!!next-input-character;
2118    
2119          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2120    
2121          redo A;          redo A;
2122        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2123            !!!cp (143);
2124          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2125          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2126          ## reconsume          ## reconsume
2127    
2128          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2129    
2130          redo A;          redo A;
2131        } else {        } else {
2132            !!!cp (144);
2133          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2134              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2135          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2136          !!!next-input-character;          !!!next-input-character;
2137          redo A;          redo A;
2138        }        }
2139      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2140        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2141          $self->{state} = 'comment end dash';          !!!cp (145);
2142            $self->{state} = COMMENT_END_DASH_STATE;
2143          !!!next-input-character;          !!!next-input-character;
2144          redo A;          redo A;
2145        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2146            !!!cp (146);
2147          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2148          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2149          ## reconsume          ## reconsume
2150    
2151          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2152    
2153          redo A;          redo A;
2154        } else {        } else {
2155          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2156            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2157          ## Stay in the state          ## Stay in the state
2158          !!!next-input-character;          !!!next-input-character;
2159          redo A;          redo A;
2160        }        }
2161      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2162        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2163          $self->{state} = 'comment end';          !!!cp (148);
2164            $self->{state} = COMMENT_END_STATE;
2165          !!!next-input-character;          !!!next-input-character;
2166          redo A;          redo A;
2167        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2168            !!!cp (149);
2169          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2170          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2171          ## reconsume          ## reconsume
2172    
2173          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2174    
2175          redo A;          redo A;
2176        } else {        } else {
2177          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2178          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2179            $self->{state} = COMMENT_STATE;
2180          !!!next-input-character;          !!!next-input-character;
2181          redo A;          redo A;
2182        }        }
2183      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2184        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2185          $self->{state} = 'data';          !!!cp (151);
2186            $self->{state} = DATA_STATE;
2187          !!!next-input-character;          !!!next-input-character;
2188    
2189          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2190    
2191          redo A;          redo A;
2192        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2193          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2194            !!!parse-error (type => 'dash in comment',
2195                            line => $self->{line_prev},
2196                            column => $self->{column_prev});
2197          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2198          ## Stay in the state          ## Stay in the state
2199          !!!next-input-character;          !!!next-input-character;
2200          redo A;          redo A;
2201        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2202            !!!cp (153);
2203          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2204          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2205          ## reconsume          ## reconsume
2206    
2207          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2208    
2209          redo A;          redo A;
2210        } else {        } else {
2211          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2212          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2213          $self->{state} = 'comment';                          line => $self->{line_prev},
2214                            column => $self->{column_prev});
2215            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2216            $self->{state} = COMMENT_STATE;
2217          !!!next-input-character;          !!!next-input-character;
2218          redo A;          redo A;
2219        }        }
2220      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2221        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2222            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2223            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2224            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2225            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2226          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2227            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2228          !!!next-input-character;          !!!next-input-character;
2229          redo A;          redo A;
2230        } else {        } else {
2231            !!!cp (156);
2232          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2233          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2234          ## reconsume          ## reconsume
2235          redo A;          redo A;
2236        }        }
2237      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2238        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2239            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2240            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2241            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2242            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2243            !!!cp (157);
2244          ## Stay in the state          ## Stay in the state
2245          !!!next-input-character;          !!!next-input-character;
2246          redo A;          redo A;
2247        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2248            !!!cp (158);
2249          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2251          !!!next-input-character;          !!!next-input-character;
2252    
2253          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2254    
2255          redo A;          redo A;
2256        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2257            !!!cp (159);
2258          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2259          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2260          ## reconsume          ## reconsume
2261    
2262          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2263    
2264          redo A;          redo A;
2265        } else {        } else {
2266          $self->{current_token}          !!!cp (160);
2267              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2268                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2269  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2270          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2271          !!!next-input-character;          !!!next-input-character;
2272          redo A;          redo A;
2273        }        }
2274      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2275  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2276        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2277            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2278            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2279            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2280            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2281          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2282            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284          redo A;          redo A;
2285        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2286          $self->{state} = 'data';          !!!cp (162);
2287            $self->{state} = DATA_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289    
2290          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2291    
2292          redo A;          redo A;
2293        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2294            !!!cp (163);
2295          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2296          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2297          ## reconsume          ## reconsume
2298    
2299          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2300          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2301    
2302          redo A;          redo A;
2303        } else {        } else {
2304            !!!cp (164);
2305          $self->{current_token}->{name}          $self->{current_token}->{name}
2306            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2307          ## Stay in the state          ## Stay in the state
2308          !!!next-input-character;          !!!next-input-character;
2309          redo A;          redo A;
2310        }        }
2311      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2312        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2313            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2314            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2315            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2316            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2317            !!!cp (165);
2318          ## Stay in the state          ## Stay in the state
2319          !!!next-input-character;          !!!next-input-character;
2320          redo A;          redo A;
2321        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2322          $self->{state} = 'data';          !!!cp (166);
2323            $self->{state} = DATA_STATE;
2324          !!!next-input-character;          !!!next-input-character;
2325    
2326          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2327    
2328          redo A;          redo A;
2329        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2330            !!!cp (167);
2331          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2332          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2333          ## reconsume          ## reconsume
2334    
2335          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2336          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2337    
2338          redo A;          redo A;
2339        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2340                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2341          !!!next-input-character;          !!!next-input-character;
2342          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2343              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2344            !!!next-input-character;            !!!next-input-character;
2345            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2346                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2347              !!!next-input-character;              !!!next-input-character;
2348              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2349                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2350                !!!next-input-character;                !!!next-input-character;
2351                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2352                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2353                  !!!next-input-character;                  !!!next-input-character;
2354                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2355                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2356                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2357                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2358                    !!!next-input-character;                    !!!next-input-character;
2359                    redo A;                    redo A;
2360                    } else {
2361                      !!!cp (169);
2362                  }                  }
2363                  } else {
2364                    !!!cp (170);
2365                }                }
2366                } else {
2367                  !!!cp (171);
2368              }              }
2369              } else {
2370                !!!cp (172);
2371            }            }
2372            } else {
2373              !!!cp (173);
2374          }          }
2375    
2376          #          #
2377        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2378                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2379          !!!next-input-character;          !!!next-input-character;
2380          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2381              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2382            !!!next-input-character;            !!!next-input-character;
2383            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2384                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2385              !!!next-input-character;              !!!next-input-character;
2386              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2387                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2388                !!!next-input-character;                !!!next-input-character;
2389                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2390                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2391                  !!!next-input-character;                  !!!next-input-character;
2392                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2393                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2394                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2395                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2396                    !!!next-input-character;                    !!!next-input-character;
2397                    redo A;                    redo A;
2398                    } else {
2399                      !!!cp (175);
2400                  }                  }
2401                  } else {
2402                    !!!cp (176);
2403                }                }
2404                } else {
2405                  !!!cp (177);
2406              }              }
2407              } else {
2408                !!!cp (178);
2409            }            }
2410            } else {
2411              !!!cp (179);
2412          }          }
2413    
2414          #          #
2415        } else {        } else {
2416            !!!cp (180);
2417          !!!next-input-character;          !!!next-input-character;
2418          #          #
2419        }        }
2420    
2421        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2422        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2423    
2424          $self->{state} = BOGUS_DOCTYPE_STATE;
2425        # next-input-character is already done        # next-input-character is already done
2426        redo A;        redo A;
2427      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2428        if ({        if ({
2429              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2430              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2431            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2432            !!!cp (181);
2433          ## Stay in the state          ## Stay in the state
2434          !!!next-input-character;          !!!next-input-character;
2435          redo A;          redo A;
2436        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2437            !!!cp (182);
2438          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2439          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2440          !!!next-input-character;          !!!next-input-character;
2441          redo A;          redo A;
2442        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2443            !!!cp (183);
2444          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2445          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2446          !!!next-input-character;          !!!next-input-character;
2447          redo A;          redo A;
2448        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2449            !!!cp (184);
2450          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2451    
2452          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2453          !!!next-input-character;          !!!next-input-character;
2454    
2455          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2456          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2457    
2458          redo A;          redo A;
2459        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2460            !!!cp (185);
2461          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2462    
2463          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2464          ## reconsume          ## reconsume
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        } else {        } else {
2471            !!!cp (186);
2472          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2473          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2474    
2475            $self->{state} = BOGUS_DOCTYPE_STATE;
2476          !!!next-input-character;          !!!next-input-character;
2477          redo A;          redo A;
2478        }        }
2479      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2480        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2481          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2482            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2483          !!!next-input-character;          !!!next-input-character;
2484          redo A;          redo A;
2485        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2486            !!!cp (188);
2487          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2488    
2489          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2490            !!!next-input-character;
2491    
2492            $self->{current_token}->{quirks} = 1;
2493            !!!emit ($self->{current_token}); # DOCTYPE
2494    
2495            redo A;
2496          } elsif ($self->{next_char} == -1) {
2497            !!!cp (189);
2498            !!!parse-error (type => 'unclosed PUBLIC literal');
2499    
2500            $self->{state} = DATA_STATE;
2501          ## reconsume          ## reconsume
2502    
2503          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2504          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2505    
2506          redo A;          redo A;
2507        } else {        } else {
2508            !!!cp (190);
2509          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2510              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2511          ## Stay in the state          ## Stay in the state
2512          !!!next-input-character;          !!!next-input-character;
2513          redo A;          redo A;
2514        }        }
2515      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2516        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2517          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2518            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2519          !!!next-input-character;          !!!next-input-character;
2520          redo A;          redo A;
2521        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2522            !!!cp (192);
2523          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2524    
2525          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2526            !!!next-input-character;
2527    
2528            $self->{current_token}->{quirks} = 1;
2529            !!!emit ($self->{current_token}); # DOCTYPE
2530    
2531            redo A;
2532          } elsif ($self->{next_char} == -1) {
2533            !!!cp (193);
2534            !!!parse-error (type => 'unclosed PUBLIC literal');
2535    
2536            $self->{state} = DATA_STATE;
2537          ## reconsume          ## reconsume
2538    
2539          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2540          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2541    
2542          redo A;          redo A;
2543        } else {        } else {
2544            !!!cp (194);
2545          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2546              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2547          ## Stay in the state          ## Stay in the state
2548          !!!next-input-character;          !!!next-input-character;
2549          redo A;          redo A;
2550        }        }
2551      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2552        if ({        if ({
2553              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2554              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2555            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2556            !!!cp (195);
2557          ## Stay in the state          ## Stay in the state
2558          !!!next-input-character;          !!!next-input-character;
2559          redo A;          redo A;
2560        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2561            !!!cp (196);
2562          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2563          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2564          !!!next-input-character;          !!!next-input-character;
2565          redo A;          redo A;
2566        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2567            !!!cp (197);
2568          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2569          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2570          !!!next-input-character;          !!!next-input-character;
2571          redo A;          redo A;
2572        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2573          $self->{state} = 'data';          !!!cp (198);
2574            $self->{state} = DATA_STATE;
2575          !!!next-input-character;          !!!next-input-character;
2576    
2577          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2578    
2579          redo A;          redo A;
2580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2581            !!!cp (199);
2582          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2583    
2584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2585          ## reconsume          ## reconsume
2586    
2587          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2588          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2589    
2590          redo A;          redo A;
2591        } else {        } else {
2592            !!!cp (200);
2593          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2594          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2595    
2596            $self->{state} = BOGUS_DOCTYPE_STATE;
2597          !!!next-input-character;          !!!next-input-character;
2598          redo A;          redo A;
2599        }        }
2600      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2601        if ({        if ({
2602              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2603              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2604            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2605            !!!cp (201);
2606          ## Stay in the state          ## Stay in the state
2607          !!!next-input-character;          !!!next-input-character;
2608          redo A;          redo A;
2609        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2610            !!!cp (202);
2611          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2612          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2613          !!!next-input-character;          !!!next-input-character;
2614          redo A;          redo A;
2615        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2616            !!!cp (203);
2617          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2618          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2619          !!!next-input-character;          !!!next-input-character;
2620          redo A;          redo A;
2621        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2622            !!!cp (204);
2623          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2624          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2625          !!!next-input-character;          !!!next-input-character;
2626    
2627          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2628          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2629    
2630          redo A;          redo A;
2631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2632            !!!cp (205);
2633          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2634    
2635          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2636          ## reconsume          ## reconsume
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        } else {        } else {
2643            !!!cp (206);
2644          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2645          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2646    
2647            $self->{state} = BOGUS_DOCTYPE_STATE;
2648          !!!next-input-character;          !!!next-input-character;
2649          redo A;          redo A;
2650        }        }
2651      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2652        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2653          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2654            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2655            !!!next-input-character;
2656            redo A;
2657          } elsif ($self->{next_char} == 0x003E) { # >
2658            !!!cp (208);
2659            !!!parse-error (type => 'unclosed PUBLIC literal');
2660    
2661            $self->{state} = DATA_STATE;
2662          !!!next-input-character;          !!!next-input-character;
2663    
2664            $self->{current_token}->{quirks} = 1;
2665            !!!emit ($self->{current_token}); # DOCTYPE
2666    
2667          redo A;          redo A;
2668        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2669            !!!cp (209);
2670          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2671    
2672          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2673          ## reconsume          ## reconsume
2674    
2675          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2676          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2677    
2678          redo A;          redo A;
2679        } else {        } else {
2680            !!!cp (210);
2681          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2682              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2683          ## Stay in the state          ## Stay in the state
2684          !!!next-input-character;          !!!next-input-character;
2685          redo A;          redo A;
2686        }        }
2687      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2688        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2689          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2690            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2691            !!!next-input-character;
2692            redo A;
2693          } elsif ($self->{next_char} == 0x003E) { # >
2694            !!!cp (212);
2695            !!!parse-error (type => 'unclosed PUBLIC literal');
2696    
2697            $self->{state} = DATA_STATE;
2698          !!!next-input-character;          !!!next-input-character;
2699    
2700            $self->{current_token}->{quirks} = 1;
2701            !!!emit ($self->{current_token}); # DOCTYPE
2702    
2703          redo A;          redo A;
2704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2705            !!!cp (213);
2706          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2707    
2708          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2709          ## reconsume          ## reconsume
2710    
2711          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2712          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2713    
2714          redo A;          redo A;
2715        } else {        } else {
2716            !!!cp (214);
2717          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2718              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2719          ## Stay in the state          ## Stay in the state
2720          !!!next-input-character;          !!!next-input-character;
2721          redo A;          redo A;
2722        }        }
2723      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2724        if ({        if ({
2725              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2726              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2727            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2728            !!!cp (215);
2729          ## Stay in the state          ## Stay in the state
2730          !!!next-input-character;          !!!next-input-character;
2731          redo A;          redo A;
2732        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2733          $self->{state} = 'data';          !!!cp (216);
2734            $self->{state} = DATA_STATE;
2735          !!!next-input-character;          !!!next-input-character;
2736    
2737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2738    
2739          redo A;          redo A;
2740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2741          !!!parse-error (type => 'unclosed DOCTYPE');          !!!cp (217);
2742    
2743          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2744          ## reconsume          ## reconsume
2745    
2746          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2747          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2748    
2749          redo A;          redo A;
2750        } else {        } else {
2751            !!!cp (218);
2752          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2753          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2754    
2755            $self->{state} = BOGUS_DOCTYPE_STATE;
2756          !!!next-input-character;          !!!next-input-character;
2757          redo A;          redo A;
2758        }        }
2759      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2760        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2761          $self->{state} = 'data';          !!!cp (219);
2762            $self->{state} = DATA_STATE;
2763          !!!next-input-character;          !!!next-input-character;
2764    
         delete $self->{current_token}->{correct};  
2765          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2766    
2767          redo A;          redo A;
2768        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2769            !!!cp (220);
2770          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2771          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2772          ## reconsume          ## reconsume
2773    
         delete $self->{current_token}->{correct};  
2774          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2775    
2776          redo A;          redo A;
2777        } else {        } else {
2778            !!!cp (221);
2779          ## Stay in the state          ## Stay in the state
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        }        }
2783        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2784          my $s = '';
2785          
2786          my ($l, $c) = ($self->{line}, $self->{column});
2787    
2788          CS: while ($self->{next_char} != -1) {
2789            if ($self->{next_char} == 0x005D) { # ]
2790              !!!next-input-character;
2791              if ($self->{next_char} == 0x005D) { # ]
2792                !!!next-input-character;
2793                MDC: {
2794                  if ($self->{next_char} == 0x003E) { # >
2795                    !!!cp (221.1);
2796                    !!!next-input-character;
2797                    last CS;
2798                  } elsif ($self->{next_char} == 0x005D) { # ]
2799                    !!!cp (221.2);
2800                    $s .= ']';
2801                    !!!next-input-character;
2802                    redo MDC;
2803                  } else {
2804                    !!!cp (221.3);
2805                    $s .= ']]';
2806                    #
2807                  }
2808                } # MDC
2809              } else {
2810                !!!cp (221.4);
2811                $s .= ']';
2812                #
2813              }
2814            } else {
2815              !!!cp (221.5);
2816              #
2817            }
2818            $s .= chr $self->{next_char};
2819            !!!next-input-character;
2820          } # CS
2821    
2822          $self->{state} = DATA_STATE;
2823          ## next-input-character done or EOF, which is reconsumed.
2824    
2825          if (length $s) {
2826            !!!cp (221.6);
2827            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2828                      line => $l, column => $c});
2829          } else {
2830            !!!cp (221.7);
2831          }
2832    
2833          redo A;
2834    
2835          ## ISSUE: "text tokens" in spec.
2836          ## TODO: Streaming support
2837      } else {      } else {
2838        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2839      }      }
# Line 1606  sub _get_next_token ($) { Line 2842  sub _get_next_token ($) {
2842    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2843  } # _get_next_token  } # _get_next_token
2844    
2845  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2846    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2847    
2848      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2849    
2850    if ({    if ({
2851         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2852         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2853        }->{$self->{next_input_character}}) {         $additional => 1,
2854          }->{$self->{next_char}}) {
2855        !!!cp (1001);
2856      ## Don't consume      ## Don't consume
2857      ## No error      ## No error
2858      return undef;      return undef;
2859    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2860      !!!next-input-character;      !!!next-input-character;
2861      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2862          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2863        my $code;        my $code;
2864        X: {        X: {
2865          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2866          !!!next-input-character;          !!!next-input-character;
2867          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2868              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2869              !!!cp (1002);
2870            $code ||= 0;            $code ||= 0;
2871            $code *= 0x10;            $code *= 0x10;
2872            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2873            redo X;            redo X;
2874          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2875                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2876              !!!cp (1003);
2877            $code ||= 0;            $code ||= 0;
2878            $code *= 0x10;            $code *= 0x10;
2879            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2880            redo X;            redo X;
2881          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2882                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2883              !!!cp (1004);
2884            $code ||= 0;            $code ||= 0;
2885            $code *= 0x10;            $code *= 0x10;
2886            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2887            redo X;            redo X;
2888          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2889            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2890            $self->{next_input_character} = 0x0023; # #            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2891            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char, $self->{next_char});
2892              $self->{next_char} = 0x0023; # #
2893            return undef;            return undef;
2894          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2895              !!!cp (1006);
2896            !!!next-input-character;            !!!next-input-character;
2897          } else {          } else {
2898            !!!parse-error (type => 'no refc');            !!!cp (1007);
2899              !!!parse-error (type => 'no refc', line => $l, column => $c);
2900          }          }
2901    
2902          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2903            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2904              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2905            $code = 0xFFFD;            $code = 0xFFFD;
2906          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2907            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2908              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2909            $code = 0xFFFD;            $code = 0xFFFD;
2910          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2911            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2912              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2913            $code = 0x000A;            $code = 0x000A;
2914          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2915            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2916              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2917            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2918          }          }
2919    
2920          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2921                    has_reference => 1,
2922                    line => $l, column => $c,
2923                   };
2924        } # X        } # X
2925      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2926               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2927        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2928        !!!next-input-character;        !!!next-input-character;
2929                
2930        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2931                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2932            !!!cp (1012);
2933          $code *= 10;          $code *= 10;
2934          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2935                    
2936          !!!next-input-character;          !!!next-input-character;
2937        }        }
2938    
2939        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2940            !!!cp (1013);
2941          !!!next-input-character;          !!!next-input-character;
2942        } else {        } else {
2943          !!!parse-error (type => 'no refc');          !!!cp (1014);
2944            !!!parse-error (type => 'no refc', line => $l, column => $c);
2945        }        }
2946    
2947        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2948          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2949            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2950          $code = 0xFFFD;          $code = 0xFFFD;
2951        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2952          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2953            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2954          $code = 0xFFFD;          $code = 0xFFFD;
2955        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2956          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2957            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2958          $code = 0x000A;          $code = 0x000A;
2959        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2960          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2961            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2962          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2963        }        }
2964                
2965        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2966                  line => $l, column => $c,
2967                 };
2968      } else {      } else {
2969        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2970        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2971        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2972          $self->{next_char} = 0x0023; # #
2973        return undef;        return undef;
2974      }      }
2975    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2976              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2977             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2978              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2979      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2980      !!!next-input-character;      !!!next-input-character;
2981    
2982      my $value = $entity_name;      my $value = $entity_name;
2983      my $match;      my $match = 0;
2984      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2985      our $EntityChar;      our $EntityChar;
2986    
2987      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2988             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2989             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2990               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2991              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2992               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2993              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2994               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2995              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2996        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2997        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2998          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2999              !!!cp (1020);
3000            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3001            $match = 1;            $match = 1;
3002            !!!next-input-character;            !!!next-input-character;
3003            last;            last;
3004          } elsif (not $in_attr) {          } else {
3005              !!!cp (1021);
3006            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3007            $match = -1;            $match = -1;
3008          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
3009          }          }
3010        } else {        } else {
3011          $value .= chr $self->{next_input_character};          !!!cp (1022);
3012            $value .= chr $self->{next_char};
3013            $match *= 2;
3014            !!!next-input-character;
3015        }        }
       !!!next-input-character;  
3016      }      }
3017            
3018      if ($match > 0) {      if ($match > 0) {
3019        return {type => 'character', data => $value};        !!!cp (1023);
3020          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3021                  line => $l, column => $c,
3022                 };
3023      } elsif ($match < 0) {      } elsif ($match < 0) {
3024        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3025        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
3026            !!!cp (1024);
3027            return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3028                    line => $l, column => $c,
3029                   };
3030          } else {
3031            !!!cp (1025);
3032            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3033                    line => $l, column => $c,
3034                   };
3035          }
3036      } else {      } else {
3037        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3038        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3039        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3040          return {type => CHARACTER_TOKEN, data => '&'.$value,
3041                  line => $l, column => $c,
3042                 };
3043      }      }
3044    } else {    } else {
3045        !!!cp (1027);
3046      ## no characters are consumed      ## no characters are consumed
3047      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3048      return undef;      return undef;
3049    }    }
3050  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1799  sub _construct_tree ($) { Line 3082  sub _construct_tree ($) {
3082        
3083    !!!next-token;    !!!next-token;
3084    
   $self->{insertion_mode} = 'before head';  
3085    undef $self->{form_element};    undef $self->{form_element};
3086    undef $self->{head_element};    undef $self->{head_element};
3087    $self->{open_elements} = [];    $self->{open_elements} = [];
3088    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3089    
3090      ## NOTE: The "initial" insertion mode.
3091    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3092    
3093      ## NOTE: The "before html" insertion mode.
3094    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3095      $self->{insertion_mode} = BEFORE_HEAD_IM;
3096    
3097      ## NOTE: The "before head" insertion mode and so on.
3098    $self->_tree_construction_main;    $self->_tree_construction_main;
3099  } # _construct_tree  } # _construct_tree
3100    
3101  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3102    my $self = shift;    my $self = shift;
3103    
3104      ## NOTE: "initial" insertion mode
3105    
3106    INITIAL: {    INITIAL: {
3107      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3108        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3109        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3110        ## language.        ## language.
# Line 1823  sub _tree_construction_initial ($) { Line 3114  sub _tree_construction_initial ($) {
3114        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3115            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3116            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3117          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3118            !!!parse-error (type => 'not HTML5', token => $token);
3119        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3120            !!!cp ('t2');
3121          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3122          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3123          } else {
3124            !!!cp ('t3');
3125        }        }
3126                
3127        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3128          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3129          ## NOTE: Default value for both |public_id| and |system_id| attributes
3130          ## are empty strings, so that we don't set any value in missing cases.
3131        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3132            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3133        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1839  sub _tree_construction_initial ($) { Line 3136  sub _tree_construction_initial ($) {
3136        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3137        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3138                
3139        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3140            !!!cp ('t4');
3141          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3142        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3143          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3144          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3145          if ({          my $prefix = [
3146            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3147            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3148            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3149            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3150            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3151            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3152            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3153            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3154            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3155            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3156            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3157            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3158            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3159            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3160            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3161            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3162            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3163            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3164            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3165            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3166            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3167            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3168            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3171            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3175            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3176            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3177            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3178            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3179            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3180            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3181            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3182            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3183            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3187            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3188            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3189            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3190            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3191            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3192            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3193            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3194            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3195            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3196            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3197            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3198            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3199            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3200            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3201            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3202            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3203            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3204            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3205            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3206            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3207            "-//W3C//DTD W3 HTML//EN" => 1,            }
3208            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3209            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3210            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3211            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3212            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3213            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3214            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3215          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3216                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3217            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3218                !!!cp ('t6');
3219              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3220            } else {            } else {
3221                !!!cp ('t7');
3222              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3223            }            }
3224          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3225                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3226              !!!cp ('t8');
3227            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3228            } else {
3229              !!!cp ('t9');
3230          }          }
3231          } else {
3232            !!!cp ('t10');
3233        }        }
3234        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3235          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3236          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3237          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") {
3238              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3239              ## marked as quirks.
3240            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3241              !!!cp ('t11');
3242            } else {
3243              !!!cp ('t12');
3244          }          }
3245          } else {
3246            !!!cp ('t13');
3247        }        }
3248                
3249        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3250        !!!next-token;        !!!next-token;
3251        return;        return;
3252      } elsif ({      } elsif ({
3253                'start tag' => 1,                START_TAG_TOKEN, 1,
3254                'end tag' => 1,                END_TAG_TOKEN, 1,
3255                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3256               }->{$token->{type}}) {               }->{$token->{type}}) {
3257        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3258          !!!parse-error (type => 'no DOCTYPE', token => $token);
3259        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3260        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3261        ## reprocess        ## reprocess
3262          !!!ack-later;
3263        return;        return;
3264      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3265        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3266          ## Ignore the token          ## Ignore the token
3267    
3268          unless (length $token->{data}) {          unless (length $token->{data}) {
3269            ## Stay in the phase            !!!cp ('t15');
3270              ## Stay in the insertion mode.
3271            !!!next-token;            !!!next-token;
3272            redo INITIAL;            redo INITIAL;
3273            } else {
3274              !!!cp ('t16');
3275          }          }
3276          } else {
3277            !!!cp ('t17');
3278        }        }
3279    
3280        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3281        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3282        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3283        ## reprocess        ## reprocess
3284        return;        return;
3285      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3286          !!!cp ('t18');
3287        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3288        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3289                
3290        ## Stay in the phase.        ## Stay in the insertion mode.
3291        !!!next-token;        !!!next-token;
3292        redo INITIAL;        redo INITIAL;
3293      } else {      } else {
3294        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3295      }      }
3296    } # INITIAL    } # INITIAL
3297    
3298      die "$0: _tree_construction_initial: This should be never reached";
3299  } # _tree_construction_initial  } # _tree_construction_initial
3300    
3301  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3302    my $self = shift;    my $self = shift;
3303    
3304      ## NOTE: "before html" insertion mode.
3305        
3306    B: {    B: {
3307        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3308          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3309            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3310          ## Ignore the token          ## Ignore the token
3311          ## Stay in the phase          ## Stay in the insertion mode.
3312          !!!next-token;          !!!next-token;
3313          redo B;          redo B;
3314        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3315            !!!cp ('t20');
3316          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3317          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3318          ## Stay in the phase          ## Stay in the insertion mode.
3319          !!!next-token;          !!!next-token;
3320          redo B;          redo B;
3321        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3322          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3323            ## Ignore the token.            ## Ignore the token.
3324    
3325            unless (length $token->{data}) {            unless (length $token->{data}) {
3326              ## Stay in the phase              !!!cp ('t21');
3327                ## Stay in the insertion mode.
3328              !!!next-token;              !!!next-token;
3329              redo B;              redo B;
3330              } else {
3331                !!!cp ('t22');
3332            }            }
3333            } else {
3334              !!!cp ('t23');
3335          }          }
3336    
3337            $self->{application_cache_selection}->(undef);
3338    
3339          #          #
3340          } elsif ($token->{type} == START_TAG_TOKEN) {
3341            if ($token->{tag_name} eq 'html') {
3342              my $root_element;
3343              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3344              $self->{document}->append_child ($root_element);
3345              push @{$self->{open_elements}},
3346                  [$root_element, $el_category->{html}];
3347    
3348              if ($token->{attributes}->{manifest}) {
3349                !!!cp ('t24');
3350                $self->{application_cache_selection}
3351                    ->($token->{attributes}->{manifest}->{value});
3352                ## ISSUE: Spec is unclear on relative references.
3353                ## According to Hixie (#whatwg 2008-03-19), it should be
3354                ## resolved against the base URI of the document in HTML
3355                ## or xml:base of the element in XHTML.
3356              } else {
3357                !!!cp ('t25');
3358                $self->{application_cache_selection}->(undef);
3359              }
3360    
3361              !!!nack ('t25c');
3362    
3363              !!!next-token;
3364              return; ## Go to the "before head" insertion mode.
3365            } else {
3366              !!!cp ('t25.1');
3367              #
3368            }
3369        } elsif ({        } elsif ({
3370                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3371                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3372                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3373          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3374          #          #
3375        } else {        } else {
3376          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3377        }        }
3378        my $root_element; !!!create-element ($root_element, 'html');  
3379        $self->{document}->append_child ($root_element);      my $root_element;
3380        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3381        ## reprocess      $self->{document}->append_child ($root_element);
3382        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3383        return; ## Go to the main phase.  
3384        $self->{application_cache_selection}->(undef);
3385    
3386        ## NOTE: Reprocess the token.
3387        !!!ack-later;
3388        return; ## Go to the "before head" insertion mode.
3389    
3390        ## ISSUE: There is an issue in the spec
3391    } # B    } # B
3392    
3393      die "$0: _tree_construction_root_element: This should never be reached";
3394  } # _tree_construction_root_element  } # _tree_construction_root_element
3395    
3396  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2036  sub _reset_insertion_mode ($) { Line 3405  sub _reset_insertion_mode ($) {
3405            
3406      ## Step 3      ## Step 3
3407      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"!?  
3408        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3409          $last = 1;          $last = 1;
3410          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3411            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3412                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3413              #          } else {
3414            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3415          }          }
3416        }        }
3417              
3418        ## Step 4..13        ## Step 4..14
3419        my $new_mode = {        my $new_mode;
3420                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3421                        td => 'in cell',          !!!cp ('t28.1');
3422                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3423                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3424                        tbody => 'in table body',          ## SVG elements as foreigners.
3425                        thead => 'in table head',          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3426                        tfoot => 'in table foot',          ## ISSUE: What is set as the secondary insertion mode?
3427                        caption => 'in caption',        } elsif ($node->[1] & TABLE_CELL_EL) {
3428                        colgroup => 'in column group',          if ($last) {
3429                        table => 'in table',            !!!cp ('t28.2');
3430                        head => 'in body', # not in head!            #
3431                        body => 'in body',          } else {
3432                        frameset => 'in frameset',            !!!cp ('t28.3');
3433                       }->{$node->[1]};            $new_mode = IN_CELL_IM;
3434            }
3435          } else {
3436            !!!cp ('t28.4');
3437            $new_mode = {
3438                          select => IN_SELECT_IM,
3439                          ## NOTE: |option| and |optgroup| do not set
3440                          ## insertion mode to "in select" by themselves.
3441                          tr => IN_ROW_IM,
3442                          tbody => IN_TABLE_BODY_IM,
3443                          thead => IN_TABLE_BODY_IM,
3444                          tfoot => IN_TABLE_BODY_IM,
3445                          caption => IN_CAPTION_IM,
3446                          colgroup => IN_COLUMN_GROUP_IM,
3447                          table => IN_TABLE_IM,
3448                          head => IN_BODY_IM, # not in head!
3449                          body => IN_BODY_IM,
3450                          frameset => IN_FRAMESET_IM,
3451                         }->{$node->[0]->manakai_local_name};
3452          }
3453        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3454                
3455        ## Step 14        ## Step 15
3456        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3457          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3458            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3459              $self->{insertion_mode} = BEFORE_HEAD_IM;
3460          } else {          } else {
3461            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3462              !!!cp ('t30');
3463              $self->{insertion_mode} = AFTER_HEAD_IM;
3464          }          }
3465          return;          return;
3466          } else {
3467            !!!cp ('t31');
3468        }        }
3469                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3470        ## Step 16        ## Step 16
3471          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3472          
3473          ## Step 17
3474        $i--;        $i--;
3475        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3476                
3477        ## Step 17        ## Step 18
3478        redo S3;        redo S3;
3479      } # S3      } # S3
3480    
3481      die "$0: _reset_insertion_mode: This line should never be reached";
3482  } # _reset_insertion_mode  } # _reset_insertion_mode
3483    
3484  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3485    my $self = shift;    my $self = shift;
3486    
   my $previous_insertion_mode;  
   
3487    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3488    
3489    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2114  sub _tree_construction_main ($) { Line 3500  sub _tree_construction_main ($) {
3500      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3501      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3502        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3503            !!!cp ('t32');
3504          return;          return;
3505        }        }
3506      }      }
# Line 2128  sub _tree_construction_main ($) { Line 3515  sub _tree_construction_main ($) {
3515    
3516        ## Step 6        ## Step 6
3517        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3518            !!!cp ('t33_1');
3519          #          #
3520        } else {        } else {
3521          my $in_open_elements;          my $in_open_elements;
3522          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3523            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3524                !!!cp ('t33');
3525              $in_open_elements = 1;              $in_open_elements = 1;
3526              last OE;              last OE;
3527            }            }
3528          }          }
3529          if ($in_open_elements) {          if ($in_open_elements) {
3530              !!!cp ('t34');
3531            #            #
3532          } else {          } else {
3533              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3534              !!!cp ('t35');
3535            redo S4;            redo S4;
3536          }          }
3537        }        }
# Line 2162  sub _tree_construction_main ($) { Line 3554  sub _tree_construction_main ($) {
3554    
3555        ## Step 11        ## Step 11
3556        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3557            !!!cp ('t36');
3558          ## Step 7'          ## Step 7'
3559          $i++;          $i++;
3560          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3561                    
3562          redo S7;          redo S7;
3563        }        }
3564    
3565          !!!cp ('t37');
3566      } # S7      } # S7
3567    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3568    
3569    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3570      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3571        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3572            !!!cp ('t38');
3573          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3574          return;          return;
3575        }        }
3576      }      }
3577    
3578        !!!cp ('t39');
3579    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3580    
3581    my $parse_rcdata = sub ($$) {    my $insert;
3582      my ($content_model_flag, $insert) = @_;  
3583      my $parse_rcdata = sub ($) {
3584        my ($content_model_flag) = @_;
3585    
3586      ## Step 1      ## Step 1
3587      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3588      my $el;      my $el;
3589      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3590    
3591      ## Step 2      ## Step 2
3592      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3593    
3594      ## Step 3      ## Step 3
3595      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3596      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3597    
3598      ## Step 4      ## Step 4
3599      my $text = '';      my $text = '';
3600        !!!nack ('t40.1');
3601      !!!next-token;      !!!next-token;
3602      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3603          !!!cp ('t40');
3604        $text .= $token->{data};        $text .= $token->{data};
3605        !!!next-token;        !!!next-token;
3606      }      }
3607    
3608      ## Step 5      ## Step 5
3609      if (length $text) {      if (length $text) {
3610          !!!cp ('t41');
3611        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3612        $el->append_child ($text);        $el->append_child ($text);
3613      }      }
3614    
3615      ## Step 6      ## Step 6
3616      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3617    
3618      ## Step 7      ## Step 7
3619      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3620            $token->{tag_name} eq $start_tag_name) {
3621          !!!cp ('t42');
3622        ## Ignore the token        ## Ignore the token
3623      } else {      } else {
3624        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        ## NOTE: An end-of-file token.
3625          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3626            !!!cp ('t43');
3627            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3628          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3629            !!!cp ('t44');
3630            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3631          } else {
3632            die "$0: $content_model_flag in parse_rcdata";
3633          }
3634      }      }
3635      !!!next-token;      !!!next-token;
3636    }; # $parse_rcdata    }; # $parse_rcdata
3637    
3638    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3639      my $script_el;      my $script_el;
3640      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3641      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3642    
3643      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
3644      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3645            
3646      my $text = '';      my $text = '';
3647        !!!nack ('t45.1');
3648      !!!next-token;      !!!next-token;
3649      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3650          !!!cp ('t45');
3651        $text .= $token->{data};        $text .= $token->{data};
3652        !!!next-token;        !!!next-token;
3653      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3654      if (length $text) {      if (length $text) {
3655          !!!cp ('t46');
3656        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3657      }      }
3658                                
3659      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
3660    
3661      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3662          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3663          !!!cp ('t47');
3664        ## Ignore the token        ## Ignore the token
3665      } else {      } else {
3666        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3667          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3668        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3669        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3670      }      }
3671            
3672      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3673          !!!cp ('t49');
3674        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3675      } else {      } else {
3676          !!!cp ('t50');
3677        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3678        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3679    
# Line 2267  sub _tree_construction_main ($) { Line 3687  sub _tree_construction_main ($) {
3687      !!!next-token;      !!!next-token;
3688    }; # $script_start_tag    }; # $script_start_tag
3689    
3690      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3691      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3692      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3693    
3694    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3695      my $tag_name = shift;      my $end_tag_token = shift;
3696        my $tag_name = $end_tag_token->{tag_name};
3697    
3698        ## NOTE: The adoption agency algorithm (AAA).
3699    
3700      FET: {      FET: {
3701        ## Step 1        ## Step 1
3702        my $formatting_element;        my $formatting_element;
3703        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3704        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3705          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3706              !!!cp ('t52');
3707              last AFE;
3708            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3709                         eq $tag_name) {
3710              !!!cp ('t51');
3711            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3712            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3713            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3714          }          }
3715        } # AFE        } # AFE
3716        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3717          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3718            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3719          ## Ignore the token          ## Ignore the token
3720          !!!next-token;          !!!next-token;
3721          return;          return;
# Line 2296  sub _tree_construction_main ($) { Line 3727  sub _tree_construction_main ($) {
3727          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3728          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3729            if ($in_scope) {            if ($in_scope) {
3730                !!!cp ('t54');
3731              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3732              last INSCOPE;              last INSCOPE;
3733            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3734              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3735                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3736                                token => $end_tag_token);
3737              ## Ignore the token              ## Ignore the token
3738              !!!next-token;              !!!next-token;
3739              return;              return;
3740            }            }
3741          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3742                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3743            $in_scope = 0;            $in_scope = 0;
3744          }          }
3745        } # INSCOPE        } # INSCOPE
3746        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3747          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3748            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3749                            token => $end_tag_token);
3750          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3751          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3752          return;          return;
3753        }        }
3754        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3755          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3756            !!!parse-error (type => 'not closed',
3757                            value => $self->{open_elements}->[-1]->[0]
3758                                ->manakai_local_name,
3759                            token => $end_tag_token);
3760        }        }
3761                
3762        ## Step 2        ## Step 2
# Line 2326  sub _tree_construction_main ($) { Line 3764  sub _tree_construction_main ($) {
3764        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3765        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3766          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3767          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3768              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3769              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3770               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3771              !!!cp ('t59');
3772            $furthest_block = $node;            $furthest_block = $node;
3773            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3774          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3775              !!!cp ('t60');
3776            last OE;            last OE;
3777          }          }
3778        } # OE        } # OE
3779                
3780        ## Step 3        ## Step 3
3781        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3782            !!!cp ('t61');
3783          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3784          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3785          !!!next-token;          !!!next-token;
# Line 2351  sub _tree_construction_main ($) { Line 3792  sub _tree_construction_main ($) {
3792        ## Step 5        ## Step 5
3793        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3794        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3795            !!!cp ('t62');
3796          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3797        }        }
3798                
# Line 2373  sub _tree_construction_main ($) { Line 3815  sub _tree_construction_main ($) {
3815          S7S2: {          S7S2: {
3816            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3817              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3818                  !!!cp ('t63');
3819                $node_i_in_active = $_;                $node_i_in_active = $_;
3820                last S7S2;                last S7S2;
3821              }              }
# Line 2386  sub _tree_construction_main ($) { Line 3829  sub _tree_construction_main ($) {
3829                    
3830          ## Step 4          ## Step 4
3831          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3832              !!!cp ('t64');
3833            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3834          }          }
3835                    
3836          ## Step 5          ## Step 5
3837          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3838              !!!cp ('t65');
3839            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3840            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3841            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2408  sub _tree_construction_main ($) { Line 3853  sub _tree_construction_main ($) {
3853        } # S7          } # S7  
3854                
3855        ## Step 8        ## Step 8
3856        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3857            my $foster_parent_element;
3858            my $next_sibling;
3859            OE: for (reverse 0..$#{$self->{open_elements}}) {
3860              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3861                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3862                                 if (defined $parent and $parent->node_type == 1) {
3863                                   !!!cp ('t65.1');
3864                                   $foster_parent_element = $parent;
3865                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3866                                 } else {
3867                                   !!!cp ('t65.2');
3868                                   $foster_parent_element
3869                                     = $self->{open_elements}->[$_ - 1]->[0];
3870                                 }
3871                                 last OE;
3872                               }
3873                             } # OE
3874                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3875                               unless defined $foster_parent_element;
3876            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3877            $open_tables->[-1]->[1] = 1; # tainted
3878          } else {
3879            !!!cp ('t65.3');
3880            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3881          }
3882                
3883        ## Step 9        ## Step 9
3884        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2425  sub _tree_construction_main ($) { Line 3895  sub _tree_construction_main ($) {
3895        my $i;        my $i;
3896        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3897          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3898              !!!cp ('t66');
3899            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3900            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3901          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3902              !!!cp ('t67');
3903            $i = $_;            $i = $_;
3904          }          }
3905        } # AFE        } # AFE
# Line 2437  sub _tree_construction_main ($) { Line 3909  sub _tree_construction_main ($) {
3909        undef $i;        undef $i;
3910        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3911          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3912              !!!cp ('t68');
3913            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3914            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3915          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3916              !!!cp ('t69');
3917            $i = $_;            $i = $_;
3918          }          }
3919        } # OE        } # OE
# Line 2450  sub _tree_construction_main ($) { Line 3924  sub _tree_construction_main ($) {
3924      } # FET      } # FET
3925    }; # $formatting_end_tag    }; # $formatting_end_tag
3926    
3927    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3928      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3929    }; # $insert_to_current    }; # $insert_to_current
3930    
3931    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3932                         my $child = shift;      my $child = shift;
3933                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3934                              table => 1, tbody => 1, tfoot => 1,        # MUST
3935                              thead => 1, tr => 1,        my $foster_parent_element;
3936                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3937                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3938                           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') {  
3939                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3940                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3941                                   !!!cp ('t70');
3942                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3943                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3944                               } else {                               } else {
3945                                   !!!cp ('t71');
3946                                 $foster_parent_element                                 $foster_parent_element
3947                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3948                               }                               }
# Line 2480  sub _tree_construction_main ($) { Line 3953  sub _tree_construction_main ($) {
3953                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3954                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3955                             ($child, $next_sibling);                             ($child, $next_sibling);
3956                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3957                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3958                         }        !!!cp ('t72');
3959          $self->{open_elements}->[-1]->[0]->append_child ($child);
3960        }
3961    }; # $insert_to_foster    }; # $insert_to_foster
3962    
3963    my $in_body = sub {    B: while (1) {
3964      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3965      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3966        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3967          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
3968          $script_start_tag->($insert);        ## Stay in the phase
3969          return;        !!!next-token;
3970        } elsif ($token->{tag_name} eq 'style') {        next B;
3971          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
3972          $parse_rcdata->('CDATA', $insert);               $token->{tag_name} eq 'html') {
3973          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3974        } elsif ({          !!!cp ('t79');
3975                  base => 1, link => 1,          !!!parse-error (type => 'after html:html', token => $token);
3976                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
3977          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3978          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3979          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html:html', token => $token);
3980          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3981          return;        } else {
3982        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
3983          ## 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  
         }  
3984    
3985          !!!next-token;        !!!cp ('t82');
3986          return;        !!!parse-error (type => 'not first start tag', token => $token);
3987        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3988          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3989          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3990          $parse_rcdata->('RCDATA', sub {            !!!cp ('t84');
3991            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
3992              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
3993            } 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});  
             }  
           }  
3994          }          }
3995          }
3996          !!!nack ('t84.1');
3997          !!!next-token;
3998          next B;
3999        } elsif ($token->{type} == COMMENT_TOKEN) {
4000          my $comment = $self->{document}->create_comment ($token->{data});
4001          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4002            !!!cp ('t85');
4003            $self->{document}->append_child ($comment);
4004          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4005            !!!cp ('t86');
4006            $self->{open_elements}->[0]->[0]->append_child ($comment);
4007          } else {
4008            !!!cp ('t87');
4009            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4010          }
4011          !!!next-token;
4012          next B;
4013        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4014          if ($token->{type} == CHARACTER_TOKEN) {
4015            !!!cp ('t87.1');
4016            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4017          !!!next-token;          !!!next-token;
4018          return;          next B;
4019        } elsif ({        } elsif ($token->{type} == START_TAG_TOKEN) {
4020                  address => 1, blockquote => 1, center => 1, dir => 1,          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4021                  div => 1, dl => 1, fieldset => 1, listing => 1,               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4022                  menu => 1, ol => 1, p => 1, ul => 1,              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4023                  pre => 1,              ($token->{tag_name} eq 'svg' and
4024                 }->{$token->{tag_name}}) {               $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4025          ## has a p element in scope            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4026          INSCOPE: for (reverse @{$self->{open_elements}}) {            !!!cp ('t87.2');
4027            if ($_->[1] eq 'p') {            #
4028              !!!back-token;          } elsif ({
4029              $token = {type => 'end tag', tag_name => 'p'};                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4030              return;                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4031            } elsif ({                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4032                      table => 1, caption => 1, td => 1, th => 1,                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4033                      button => 1, marquee => 1, object => 1, html => 1,                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4034                     }->{$_->[1]}) {                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4035              last INSCOPE;                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4036            }                    var => 1,
4037          } # INSCOPE                   }->{$token->{tag_name}}) {
4038                        !!!cp ('t87.2');
4039          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!parse-error (type => 'not closed',
4040          if ($token->{tag_name} eq 'pre') {                            value => $self->{open_elements}->[-1]->[0]
4041            !!!next-token;                                ->manakai_local_name,
4042            if ($token->{type} eq 'character') {                            token => $token);
4043              $token->{data} =~ s/^\x0A//;  
4044              unless (length $token->{data}) {            pop @{$self->{open_elements}}
4045                !!!next-token;                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4046              }  
4047            }            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4048          } else {            ## Reprocess.
4049            !!!next-token;            next B;
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
4050          } else {          } else {
4051            ## has a p element in scope            my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4052            INSCOPE: for (reverse @{$self->{open_elements}}) {            my $tag_name = $token->{tag_name};
4053              if ($_->[1] eq 'p') {            if ($nsuri eq $SVG_NS) {
4054                !!!back-token;              $tag_name = {
4055                $token = {type => 'end tag', tag_name => 'p'};                 altglyph => 'altGlyph',
4056                return;                 altglyphdef => 'altGlyphDef',
4057              } elsif ({                 altglyphitem => 'altGlyphItem',
4058                        table => 1, caption => 1, td => 1, th => 1,                 animatecolor => 'animateColor',
4059                        button => 1, marquee => 1, object => 1, html => 1,                 animatemotion => 'animateMotion',
4060                       }->{$_->[1]}) {                 animatetransform => 'animateTransform',
4061                last INSCOPE;                 clippath => 'clipPath',
4062              }                 feblend => 'feBlend',
4063            } # INSCOPE                 fecolormatrix => 'feColorMatrix',
4064                               fecomponenttransfer => 'feComponentTransfer',
4065            !!!insert-element-t ($token->{tag_name}, $token->{attributes});                 fecomposite => 'feComposite',
4066            $self->{form_element} = $self->{open_elements}->[-1]->[0];                 feconvolvematrix => 'feConvolveMatrix',
4067            !!!next-token;                 fediffuselighting => 'feDiffuseLighting',
4068            return;                 fedisplacementmap => 'feDisplacementMap',
4069          }                 fedistantlight => 'feDistantLight',
4070        } elsif ($token->{tag_name} eq 'li') {                 feflood => 'feFlood',
4071          ## has a p element in scope                 fefunca => 'feFuncA',
4072          INSCOPE: for (reverse @{$self->{open_elements}}) {                 fefuncb => 'feFuncB',
4073            if ($_->[1] eq 'p') {                 fefuncg => 'feFuncG',
4074              !!!back-token;                 fefuncr => 'feFuncR',
4075              $token = {type => 'end tag', tag_name => 'p'};                 fegaussianblur => 'feGaussianBlur',
4076              return;                 feimage => 'feImage',
4077            } elsif ({                 femerge => 'feMerge',
4078                      table => 1, caption => 1, td => 1, th => 1,                 femergenode => 'feMergeNode',
4079                      button => 1, marquee => 1, object => 1, html => 1,                 femorphology => 'feMorphology',
4080                     }->{$_->[1]}) {                 feoffset => 'feOffset',
4081              last INSCOPE;                 fepointlight => 'fePointLight',
4082            }                 fespecularlighting => 'feSpecularLighting',
4083          } # INSCOPE                 fespotlight => 'feSpotLight',
4084                             fetile => 'feTile',
4085          ## Step 1                 feturbulence => 'feTurbulence',
4086          my $i = -1;                 foreignobject => 'foreignObject',
4087          my $node = $self->{open_elements}->[$i];                 glyphref => 'glyphRef',
4088          LI: {                 lineargradient => 'linearGradient',
4089            ## Step 2                 radialgradient => 'radialGradient',
4090            if ($node->[1] eq 'li') {                 #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4091              if ($i != -1) {                 textpath => 'textPath',  
4092                !!!parse-error (type => 'end tag missing:'.              }->{$tag_name} || $tag_name;
                               $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_flag} = 'PLAINTEXT';  
             
         !!!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;  
4093            }            }
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4094    
4095          !!!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];  
4096    
4097          !!!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);  
4098    
4099          ## 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', $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';  
         }  
4100    
4101          ## NOTE: There is an "as if <br>" code clone.            if ($self->{self_closing}) {
4102          $reconstruct_active_formatting_elements->($insert_to_current);              pop @{$self->{open_elements}};
4103                        !!!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}};  
4104            } else {            } else {
4105              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           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_flag} = 'RCDATA';  
         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;  
4106            }            }
4107          }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4108            !!!next-token;            !!!next-token;
4109              next B;
4110          }          }
4111          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4112            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4113          }          !!!cp ('t87.5');
4114                    #
4115          $self->{content_model_flag} = 'PCDATA';        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4116                    ## NOTE: "using the rules for secondary insertion mode" then "continue"
4117          if ($token->{type} eq 'end tag' and          !!!cp ('t87.6');
4118              $token->{tag_name} eq $tag_name) {          #
4119            ## Ignore the token          ## TODO: ...
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->('CDATA', $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.  
4120        } else {        } else {
4121          $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;  
4122        }        }
4123      } 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]);  
             }  
           }  
4124    
4125            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4126            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4127            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4128          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4129            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4130            ## 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]);  
4131            } else {            } else {
4132              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4133                ## Ignore the token.
4134                !!!next-token;
4135                next B;
4136            }            }
4137          }            unless (length $token->{data}) {
4138                        !!!cp ('t88');
4139          if (defined $i) {              !!!next-token;
4140            splice @{$self->{open_elements}}, $i;              next B;
         } 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;  
4141            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4142          }          }
4143    
4144          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4145          !!!next-token;            !!!cp ('t89');
4146          return;            ## As if <head>
4147        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4148                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4149                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4150          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         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;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         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');  
4151    
4152          ## As if <br>            ## Reprocess in the "in head" insertion mode...
4153          $reconstruct_active_formatting_elements->($insert_to_current);            pop @{$self->{open_elements}};
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## 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];  
4154    
4155          ## Step 2            ## Reprocess in the "after head" insertion mode...
4156          S2: {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4157            if ($node->[1] eq $token->{tag_name}) {            !!!cp ('t90');
4158              ## Step 1            ## As if </noscript>
4159              ## generate implied end tags            pop @{$self->{open_elements}};
4160              if ({            !!!parse-error (type => 'in noscript:#character', token => $token);
4161                   dd => 1, dt => 1, li => 1, p => 1,            
4162                   td => 1, th => 1, tr => 1,            ## Reprocess in the "in head" insertion mode...
4163                   tbody => 1, tfoot=> 1, thead => 1,            ## As if </head>
4164                  }->{$self->{open_elements}->[-1]->[1]}) {            pop @{$self->{open_elements}};
4165                !!!back-token;  
4166                $token = {type => 'end tag',            ## Reprocess in the "after head" insertion mode...
4167                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4168                return;            !!!cp ('t91');
4169              }            pop @{$self->{open_elements}};
4170            
4171              ## Step 2            ## Reprocess in the "after head" insertion mode...
4172              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {          } else {
4173                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t92');
4174              }          }
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4175    
4176            ## "after head" insertion mode
4177            ## As if <body>
4178            !!!insert-element ('body',, $token);
4179            $self->{insertion_mode} = IN_BODY_IM;
4180            ## reprocess
4181            next B;
4182          } elsif ($token->{type} == START_TAG_TOKEN) {
4183            if ($token->{tag_name} eq 'head') {
4184              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4185                !!!cp ('t93');
4186                !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4187                $self->{open_elements}->[-1]->[0]->append_child
4188                    ($self->{head_element});
4189                push @{$self->{open_elements}},
4190                    [$self->{head_element}, $el_category->{head}];
4191                $self->{insertion_mode} = IN_HEAD_IM;
4192                !!!nack ('t93.1');
4193              !!!next-token;              !!!next-token;
4194              last S2;              next B;
4195              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4196                !!!cp ('t93.2');
4197                !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4198                ## Ignore the token
4199                !!!nack ('t93.3');
4200                !!!next-token;
4201                next B;
4202            } else {            } else {
4203              ## Step 3              !!!cp ('t95');
4204              if (not $formatting_category->{$node->[1]} and              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4205                  #not $phrasing_category->{$node->[1]} and              ## Ignore the token
4206                  ($special_category->{$node->[1]} or              !!!nack ('t95.1');
4207                   $scoping_category->{$node->[1]})) {              !!!next-token;
4208                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              next B;
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
4209            }            }
4210                      } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4211            ## Step 4            !!!cp ('t96');
4212            $node_i--;            ## As if <head>
4213            $node = $self->{open_elements}->[$node_i];            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4214                        $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4215            ## Step 5;            push @{$self->{open_elements}},
4216            redo S2;                [$self->{head_element}, $el_category->{head}];
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4217    
4218    B: {            $self->{insertion_mode} = IN_HEAD_IM;
4219      if ($self->{insertion_mode} ne 'trailing end') {            ## Reprocess in the "in head" insertion mode...
4220        if ($token->{type} eq 'DOCTYPE') {          } else {
4221          !!!parse-error (type => 'in html:#DOCTYPE');            !!!cp ('t97');
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!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});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## 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]);  
4222          }          }
4223    
4224          ## Stop parsing              if ($token->{tag_name} eq 'base') {
4225          last B;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4226                    !!!cp ('t98');
4227                    ## As if </noscript>
4228                    pop @{$self->{open_elements}};
4229                    !!!parse-error (type => 'in noscript:base', token => $token);
4230                  
4231                    $self->{insertion_mode} = IN_HEAD_IM;
4232                    ## Reprocess in the "in head" insertion mode...
4233                  } else {
4234                    !!!cp ('t99');
4235                  }
4236    
4237          ## ISSUE: There is an issue in the spec.                ## NOTE: There is a "as if in head" code clone.
4238        } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4239          if ($self->{insertion_mode} eq 'before head') {                  !!!cp ('t100');
4240            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4241              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  push @{$self->{open_elements}},
4242                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                      [$self->{head_element}, $el_category->{head}];
4243                unless (length $token->{data}) {                } else {
4244                  !!!next-token;                  !!!cp ('t101');
                 redo B;  
4245                }                }
4246              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4247              ## As if <head>                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4248              !!!create-element ($self->{head_element}, 'head');                pop @{$self->{open_elements}} # <head>
4249              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4250              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                !!!nack ('t101.1');
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             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.  
4251                !!!next-token;                !!!next-token;
4252                redo B;                next B;
4253              }              } elsif ($token->{tag_name} eq 'link') {
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             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;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } 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}}) {  
4254                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4255                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4256                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4257                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4258                    push @{$self->{open_elements}},
4259                        [$self->{head_element}, $el_category->{head}];
4260                  } else {
4261                    !!!cp ('t103');
4262                }                }
4263                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4264                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4265                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4266                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4267                  !!!ack ('t103.1');
4268                !!!next-token;                !!!next-token;
4269                redo B;                next B;
4270              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4271                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4272                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4273                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4274                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4275                    push @{$self->{open_elements}},
4276                        [$self->{head_element}, $el_category->{head}];
4277                  } else {
4278                    !!!cp ('t105');
4279                }                }
4280                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4281                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4282    
4283                unless ($self->{confident}) {                unless ($self->{confident}) {
4284                  my $charset;                  if ($token->{attributes}->{charset}) {
4285                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4286                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4287                  }                    ## in the {change_encoding} callback.
4288                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4289                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4290                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4291                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4292                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4293                          ->set_user_data (manakai_has_reference =>
4294                                               $token->{attributes}->{charset}
4295                                                   ->{has_reference});
4296                    } elsif ($token->{attributes}->{content}) {
4297                      if ($token->{attributes}->{content}->{value}
4298                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4299                              [\x09-\x0D\x20]*=
4300                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4301                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4302                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4303                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4304                        ## in the {change_encoding} callback.
4305                        $self->{change_encoding}
4306                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4307                               $token);
4308                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4309                            ->set_user_data (manakai_has_reference =>
4310                                                 $token->{attributes}->{content}
4311                                                       ->{has_reference});
4312                      } else {
4313                        !!!cp ('t108');
4314                      }
4315                    }
4316                  } else {
4317                    if ($token->{attributes}->{charset}) {
4318                      !!!cp ('t109');
4319                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4320                          ->set_user_data (manakai_has_reference =>
4321                                               $token->{attributes}->{charset}
4322                                                   ->{has_reference});
4323                    }
4324                    if ($token->{attributes}->{content}) {
4325                      !!!cp ('t110');
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                  }                  }
                 ## TODO: Change the encoding  
4331                }                }
4332    
4333                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4334                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4335                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4336                !!!next-token;                !!!next-token;
4337                redo B;                next B;
4338              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4339                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4340                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4341                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4342                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4343                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
4344                  
4345                    $self->{insertion_mode} = IN_HEAD_IM;
4346                    ## Reprocess in the "in head" insertion mode...
4347                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4348                    !!!cp ('t112');
4349                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4350                    push @{$self->{open_elements}},
4351                        [$self->{head_element}, $el_category->{head}];
4352                  } else {
4353                    !!!cp ('t113');
4354                }                }
4355    
4356                  ## NOTE: There is a "as if in head" code clone.
4357                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4358                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4359                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4360                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4361                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4362                redo B;                next B;
4363              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4364                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4365                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4366                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4367                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4368                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4369                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4370                }                  push @{$self->{open_elements}},
4371                $parse_rcdata->('CDATA', $insert_to_current);                      [$self->{head_element}, $el_category->{head}];
4372                pop @{$self->{open_elements}}                } else {
4373                    if $self->{insertion_mode} eq 'after head';                  !!!cp ('t115');
4374                redo B;                }
4375                  $parse_rcdata->(CDATA_CONTENT_MODEL);
4376                  pop @{$self->{open_elements}} # <head>
4377                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4378                  next B;
4379              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4380                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4381                    !!!cp ('t116');
4382                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4383                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4384                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4385                    !!!nack ('t116.1');
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  next B;
4388                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4389                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4390                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4391                  ## Ignore the token                  ## Ignore the token
4392                  redo B;                  !!!nack ('t117.1');
4393                    !!!next-token;
4394                    next B;
4395                } else {                } else {
4396                    !!!cp ('t118');
4397                  #                  #
4398                }                }
4399              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4400                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4401                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4402                ## Ignore the token                  ## As if </noscript>
4403                !!!next-token;                  pop @{$self->{open_elements}};
4404                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
4405              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
4406                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
4407                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
4408                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4409                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
4410                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4411                    push @{$self->{open_elements}},
4412                        [$self->{head_element}, $el_category->{head}];
4413                  } else {
4414                    !!!cp ('t121');
4415                }                }
4416    
4417                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4418                $script_start_tag->($insert_to_current);                $script_start_tag->();
4419                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4420                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4421                redo B;                next B;
4422              } 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  
4423                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4424                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4425                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4426                    ## As if </noscript>
4427                    pop @{$self->{open_elements}};
4428                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4429                    
4430                    ## Reprocess in the "in head" insertion mode...
4431                    ## As if </head>
4432                    pop @{$self->{open_elements}};
4433                    
4434                    ## Reprocess in the "after head" insertion mode...
4435                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4436                    !!!cp ('t124');
4437                    pop @{$self->{open_elements}};
4438                    
4439                    ## Reprocess in the "after head" insertion mode...
4440                  } else {
4441                    !!!cp ('t125');
4442                  }
4443    
4444                  ## "after head" insertion mode
4445                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4446                  if ($token->{tag_name} eq 'body') {
4447                    !!!cp ('t126');
4448                    $self->{insertion_mode} = IN_BODY_IM;
4449                  } elsif ($token->{tag_name} eq 'frameset') {
4450                    !!!cp ('t127');
4451                    $self->{insertion_mode} = IN_FRAMESET_IM;
4452                  } else {
4453                    die "$0: tag name: $self->{tag_name}";
4454                  }
4455                  !!!nack ('t127.1');
4456                !!!next-token;                !!!next-token;
4457                redo B;                next B;
4458              } else {              } else {
4459                  !!!cp ('t128');
4460                #                #
4461              }              }
4462            } elsif ($token->{type} eq 'end tag') {  
4463              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4464                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4465                  ## As if </noscript>
4466                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4467                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4468                !!!next-token;                
4469                redo B;                ## Reprocess in the "in head" insertion mode...
4470              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
4471                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
4472    
4473            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
4474          } elsif ($self->{insertion_mode} eq 'in body') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4475            if ($token->{type} eq 'character') {                !!!cp ('t130');
4476              ## NOTE: There is a code clone of "character in body".                ## As if </head>
4477              $reconstruct_active_formatting_elements->($insert_to_current);                pop @{$self->{open_elements}};
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4478    
4479              !!!next-token;                ## Reprocess in the "after head" insertion mode...
4480              redo B;              } else {
4481            } elsif ($token->{type} eq 'comment') {                !!!cp ('t131');
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             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;  
               }  
4482              }              }
4483    
4484              !!!parse-error (type => 'in table:#character');              ## "after head" insertion mode
4485                ## As if <body>
4486                !!!insert-element ('body',, $token);
4487                $self->{insertion_mode} = IN_BODY_IM;
4488                ## reprocess
4489                !!!ack-later;
4490                next B;
4491              } elsif ($token->{type} == END_TAG_TOKEN) {
4492                if ($token->{tag_name} eq 'head') {
4493                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4494                    !!!cp ('t132');
4495                    ## As if <head>
4496                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4497                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4498                    push @{$self->{open_elements}},
4499                        [$self->{head_element}, $el_category->{head}];
4500    
4501              ## As if in body, but insert into foster parent element                  ## Reprocess in the "in head" insertion mode...
4502              ## ISSUE: Spec says that "whenever a node would be inserted                  pop @{$self->{open_elements}};
4503              ## into the current node" while characters might not be                  $self->{insertion_mode} = AFTER_HEAD_IM;
4504              ## result in a new Text node.                  !!!next-token;
4505              $reconstruct_active_formatting_elements->($insert_to_foster);                  next B;
4506                              } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4507              if ({                  !!!cp ('t133');
4508                   table => 1, tbody => 1, tfoot => 1,                  ## As if </noscript>
4509                   thead => 1, tr => 1,                  pop @{$self->{open_elements}};
4510                  }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'in noscript:/head', token => $token);
4511                # MUST                  
4512                my $foster_parent_element;                  ## Reprocess in the "in head" insertion mode...
4513                my $next_sibling;                  pop @{$self->{open_elements}};
4514                my $prev_sibling;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4515                OE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!next-token;
4516                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  next B;
4517                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4518                    if (defined $parent and $parent->node_type == 1) {                  !!!cp ('t134');
4519                      $foster_parent_element = $parent;                  pop @{$self->{open_elements}};
4520                      $next_sibling = $self->{open_elements}->[$_]->[0];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4521                      $prev_sibling = $next_sibling->previous_sibling;                  !!!next-token;
4522                    } else {                  next B;
4523                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4524                      $prev_sibling = $foster_parent_element->last_child;                  !!!cp ('t134.1');
4525                    }                  !!!parse-error (type => 'unmatched end tag:head', token => $token);
4526                    last OE;                  ## Ignore the token
4527                  }                  !!!next-token;
4528                } # OE                  next B;
               $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});  
4529                } else {                } else {
4530                  $foster_parent_element->insert_before                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
4531                }                }
4532              } else {              } elsif ($token->{tag_name} eq 'noscript') {
4533                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4534              }                  !!!cp ('t136');
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 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]);  
4535                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4536                    $self->{insertion_mode} = IN_HEAD_IM;
4537                    !!!next-token;
4538                    next B;
4539                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4540                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4541                    !!!cp ('t137');
4542                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4543                    ## Ignore the token ## ISSUE: An issue in the spec.
4544                    !!!next-token;
4545                    next B;
4546                  } else {
4547                    !!!cp ('t138');
4548                    #
4549                }                }
   
               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;  
4550              } elsif ({              } elsif ({
4551                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
4552                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4553                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4554                while ($self->{open_elements}->[-1]->[1] ne 'table' and                    $self->{insertion_mode} == IN_HEAD_IM or
4555                       $self->{open_elements}->[-1]->[1] ne 'html') {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4556                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t140');
4557                  pop @{$self->{open_elements}};                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4558                    ## Ignore the token
4559                    !!!next-token;
4560                    next B;
4561                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4562                    !!!cp ('t140.1');
4563                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4564                    ## Ignore the token
4565                    !!!next-token;
4566                    next B;
4567                  } else {
4568                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4569                }                }
4570                } elsif ($token->{tag_name} eq 'p') {
4571                  !!!cp ('t142');
4572                  !!!parse-error (type => 'unmatched end tag:p', token => $token);
4573                  ## Ignore the token
4574                  !!!next-token;
4575                  next B;
4576                } elsif ($token->{tag_name} eq 'br') {
4577                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4578                    !!!cp ('t142.2');
4579                    ## (before head) as if <head>, (in head) as if </head>
4580                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4581                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4582                    $self->{insertion_mode} = AFTER_HEAD_IM;
4583      
4584                    ## Reprocess in the "after head" insertion mode...
4585                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4586                    !!!cp ('t143.2');
4587                    ## As if </head>
4588                    pop @{$self->{open_elements}};
4589                    $self->{insertion_mode} = AFTER_HEAD_IM;
4590      
4591                    ## Reprocess in the "after head" insertion mode...
4592                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4593                    !!!cp ('t143.3');
4594                    ## ISSUE: Two parse errors for <head><noscript></br>
4595                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4596                    ## As if </noscript>
4597                    pop @{$self->{open_elements}};
4598                    $self->{insertion_mode} = IN_HEAD_IM;
4599    
4600                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
4601                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## As if </head>
4602                  ? 'in column group' : 'in table body';                  pop @{$self->{open_elements}};
4603                ## reprocess                  $self->{insertion_mode} = AFTER_HEAD_IM;
               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]);  
4604    
4605                ## As if </table>                  ## Reprocess in the "after head" insertion mode...
4606                ## have a table element in table scope                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4607                my $i;                  !!!cp ('t143.4');
4608                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  #
4609                  my $node = $self->{open_elements}->[$_];                } else {
4610                  if ($node->[1] eq 'table') {                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!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;  
4611                }                }
4612    
4613                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4614                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'unmatched end tag:br', token => $token);
4615                }                ## Ignore the token
4616                  !!!next-token;
4617                  next B;
4618                } else {
4619                  !!!cp ('t145');
4620                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4621                  ## Ignore the token
4622                  !!!next-token;
4623                  next B;
4624                }
4625    
4626                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4627                  !!!cp ('t146');
4628                  ## As if </noscript>
4629                  pop @{$self->{open_elements}};
4630                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4631                  
4632                  ## Reprocess in the "in head" insertion mode...
4633                  ## As if </head>
4634                  pop @{$self->{open_elements}};
4635    
4636                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
4637                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4638                  !!!cp ('t147');
4639                  ## As if </head>
4640                  pop @{$self->{open_elements}};
4641    
4642                ## reprocess                ## Reprocess in the "after head" insertion mode...
4643                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4644    ## ISSUE: This case cannot be reached?
4645                  !!!cp ('t148');
4646                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4647                  ## Ignore the token ## ISSUE: An issue in the spec.
4648                  !!!next-token;
4649                  next B;
4650              } else {              } else {
4651                #                !!!cp ('t149');
4652              }              }
           } 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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!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;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
4653    
4654                if ($self->{open_elements}->[-1]->[1] ne 'table') {              ## "after head" insertion mode
4655                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## As if <body>
4656                }              !!!insert-element ('body',, $token);
4657                $self->{insertion_mode} = IN_BODY_IM;
4658                ## reprocess
4659                next B;
4660          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4661            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4662              !!!cp ('t149.1');
4663    
4664              ## NOTE: As if <head>
4665              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4666              $self->{open_elements}->[-1]->[0]->append_child
4667                  ($self->{head_element});
4668              #push @{$self->{open_elements}},
4669              #    [$self->{head_element}, $el_category->{head}];
4670              #$self->{insertion_mode} = IN_HEAD_IM;
4671              ## NOTE: Reprocess.
4672    
4673              ## NOTE: As if </head>
4674              #pop @{$self->{open_elements}};
4675              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4676              ## NOTE: Reprocess.
4677              
4678              #
4679            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4680              !!!cp ('t149.2');
4681    
4682                splice @{$self->{open_elements}}, $i;            ## NOTE: As if </head>
4683              pop @{$self->{open_elements}};
4684              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4685              ## NOTE: Reprocess.
4686    
4687                $self->_reset_insertion_mode;            #
4688            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4689              !!!cp ('t149.3');
4690    
4691                !!!next-token;            !!!parse-error (type => 'in noscript:#eof', token => $token);
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
4692    
4693            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## As if </noscript>
4694            $in_body->($insert_to_foster);            pop @{$self->{open_elements}};
4695            redo B;            #$self->{insertion_mode} = IN_HEAD_IM;
4696          } elsif ($self->{insertion_mode} eq 'in caption') {            ## NOTE: Reprocess.
4697            if ($token->{type} eq 'character') {  
4698              ## NOTE: This is a code clone of "character in body".            ## NOTE: As if </head>
4699              pop @{$self->{open_elements}};
4700              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4701              ## NOTE: Reprocess.
4702    
4703              #
4704            } else {
4705              !!!cp ('t149.4');
4706              #
4707            }
4708    
4709            ## NOTE: As if <body>
4710            !!!insert-element ('body',, $token);
4711            $self->{insertion_mode} = IN_BODY_IM;
4712            ## NOTE: Reprocess.
4713            next B;
4714          } else {
4715            die "$0: $token->{type}: Unknown token type";
4716          }
4717    
4718              ## ISSUE: An issue in the spec.
4719        } elsif ($self->{insertion_mode} & BODY_IMS) {
4720              if ($token->{type} == CHARACTER_TOKEN) {
4721                !!!cp ('t150');
4722                ## NOTE: There is a code clone of "character in body".
4723              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4724                            
4725              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4726    
4727              !!!next-token;              !!!next-token;
4728              redo B;              next B;
4729            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
4730              if ({              if ({
4731                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4732                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4733                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4734                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
4735                    ## have an element in table scope
4736                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
4737                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
4738                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
4739                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
4740                  my $node = $self->{open_elements}->[$_];  
4741                  if ($node->[1] eq 'caption') {                      ## Close the cell
4742                    $i = $_;                      !!!back-token; # <x>
4743                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
4744                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
4745                            table => 1, html => 1,                                line => $token->{line},
4746                           }->{$node->[1]}) {                                column => $token->{column}};
4747                    last INSCOPE;                      next B;
4748                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4749                        !!!cp ('t152');
4750                        ## ISSUE: This case can never be reached, maybe.
4751                        last;
4752                      }
4753                  }                  }
4754                } # INSCOPE  
4755                unless (defined $i) {                  !!!cp ('t153');
4756                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4757                        value => $token->{tag_name}, token => $token);
4758                  ## Ignore the token                  ## Ignore the token
4759                    !!!nack ('t153.1');
4760                  !!!next-token;                  !!!next-token;
4761                  redo B;                  next B;
4762                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4763                                  !!!parse-error (type => 'not closed:caption', token => $token);
4764                ## generate implied end tags                  
4765                if ({                  ## NOTE: As if </caption>.
4766                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4767                     td => 1, th => 1, tr => 1,                  my $i;
4768                     tbody => 1, tfoot=> 1, thead => 1,                  INSCOPE: {
4769                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4770                  !!!back-token; # <?>                      my $node = $self->{open_elements}->[$_];
4771                  $token = {type => 'end tag', tag_name => 'caption'};                      if ($node->[1] & CAPTION_EL) {
4772                  !!!back-token;                        !!!cp ('t155');
4773                  $token = {type => 'end tag',                        $i = $_;
4774                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        last INSCOPE;
4775                  redo B;                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4776                }                        !!!cp ('t156');
4777                          last;
4778                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                      }
4779                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    }
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4780    
4781                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
4782                      !!!parse-error (type => 'start tag not allowed',
4783                                      value => $token->{tag_name}, token => $token);
4784                      ## Ignore the token
4785                      !!!nack ('t157.1');
4786                      !!!next-token;
4787                      next B;
4788                    } # INSCOPE
4789                    
4790                    ## generate implied end tags
4791                    while ($self->{open_elements}->[-1]->[1]
4792                               & END_TAG_OPTIONAL_EL) {
4793                      !!!cp ('t158');
4794                      pop @{$self->{open_elements}};
4795                    }
4796    
4797                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4798                redo B;                    !!!cp ('t159');
4799                      !!!parse-error (type => 'not closed',
4800                                      value => $self->{open_elements}->[-1]->[0]
4801                                          ->manakai_local_name,
4802                                      token => $token);
4803                    } else {
4804                      !!!cp ('t160');
4805                    }
4806                    
4807                    splice @{$self->{open_elements}}, $i;
4808                    
4809                    $clear_up_to_marker->();
4810                    
4811                    $self->{insertion_mode} = IN_TABLE_IM;
4812                    
4813                    ## reprocess
4814                    !!!ack-later;
4815                    next B;
4816                  } else {
4817                    !!!cp ('t161');
4818                    #
4819                  }
4820              } else {              } else {
4821                  !!!cp ('t162');
4822                #                #
4823              }              }
4824            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4825              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4826                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
4827                my $i;                  ## have an element in table scope
4828                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
4829                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4830                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4831                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4832                    last INSCOPE;                      !!!cp ('t163');
4833                  } elsif ({                      $i = $_;
4834                            table => 1, html => 1,                      last INSCOPE;
4835                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4836                    last INSCOPE;                      !!!cp ('t164');
4837                        last INSCOPE;
4838                      }
4839                    } # INSCOPE
4840                      unless (defined $i) {
4841                        !!!cp ('t165');
4842                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4843                        ## Ignore the token
4844                        !!!next-token;
4845                        next B;
4846                      }
4847                    
4848                    ## generate implied end tags
4849                    while ($self->{open_elements}->[-1]->[1]
4850                               & END_TAG_OPTIONAL_EL) {
4851                      !!!cp ('t166');
4852                      pop @{$self->{open_elements}};
4853                  }                  }
4854                } # INSCOPE  
4855                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4856                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4857                      !!!cp ('t167');
4858                      !!!parse-error (type => 'not closed',
4859                                      value => $self->{open_elements}->[-1]->[0]
4860                                          ->manakai_local_name,
4861                                      token => $token);
4862                    } else {
4863                      !!!cp ('t168');
4864                    }
4865                    
4866                    splice @{$self->{open_elements}}, $i;
4867                    
4868                    $clear_up_to_marker->();
4869                    
4870                    $self->{insertion_mode} = IN_ROW_IM;
4871                    
4872                    !!!next-token;
4873                    next B;
4874                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4875                    !!!cp ('t169');
4876                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4877                  ## Ignore the token                  ## Ignore the token
4878                  !!!next-token;                  !!!next-token;
4879                  redo B;                  next B;
4880                }                } else {
4881                                  !!!cp ('t170');
4882                ## 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  
                 redo B;  
4883                }                }
4884                } elsif ($token->{tag_name} eq 'caption') {
4885                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4886                    ## have a table element in table scope
4887                    my $i;
4888                    INSCOPE: {
4889                      for (reverse 0..$#{$self->{open_elements}}) {
4890                        my $node = $self->{open_elements}->[$_];
4891                        if ($node->[1] & CAPTION_EL) {
4892                          !!!cp ('t171');
4893                          $i = $_;
4894                          last INSCOPE;
4895                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4896                          !!!cp ('t172');
4897                          last;
4898                        }
4899                      }
4900    
4901                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4902                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4903                                      value => $token->{tag_name}, token => $token);
4904                      ## Ignore the token
4905                      !!!next-token;
4906                      next B;
4907                    } # INSCOPE
4908                    
4909                    ## generate implied end tags
4910                    while ($self->{open_elements}->[-1]->[1]
4911                               & END_TAG_OPTIONAL_EL) {
4912                      !!!cp ('t174');
4913                      pop @{$self->{open_elements}};
4914                    }
4915                    
4916                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4917                      !!!cp ('t175');
4918                      !!!parse-error (type => 'not closed',
4919                                      value => $self->{open_elements}->[-1]->[0]
4920                                          ->manakai_local_name,
4921                                      token => $token);
4922                    } else {
4923                      !!!cp ('t176');
4924                    }
4925                    
4926                    splice @{$self->{open_elements}}, $i;
4927                    
4928                    $clear_up_to_marker->();
4929                    
4930                    $self->{insertion_mode} = IN_TABLE_IM;
4931                    
4932                    !!!next-token;
4933                    next B;
4934                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4935                    !!!cp ('t177');
4936                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4937                    ## Ignore the token
4938                    !!!next-token;
4939                    next B;
4940                  } else {
4941                    !!!cp ('t178');
4942                    #
4943                }                }
4944                } elsif ({
4945                          table => 1, tbody => 1, tfoot => 1,
4946                          thead => 1, tr => 1,
4947                         }->{$token->{tag_name}} and
4948                         $self->{insertion_mode} == IN_CELL_IM) {
4949                  ## have an element in table scope
4950                  my $i;
4951                  my $tn;
4952                  INSCOPE: {
4953                    for (reverse 0..$#{$self->{open_elements}}) {
4954                      my $node = $self->{open_elements}->[$_];
4955                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4956                        !!!cp ('t179');
4957                        $i = $_;
4958    
4959                        ## Close the cell
4960                        !!!back-token; # </x>
4961                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4962                                  line => $token->{line},
4963                                  column => $token->{column}};
4964                        next B;
4965                      } elsif ($node->[1] & TABLE_CELL_EL) {
4966                        !!!cp ('t180');
4967                        $tn = $node->[0]->manakai_local_name;
4968                        ## NOTE: There is exactly one |td| or |th| element
4969                        ## in scope in the stack of open elements by definition.
4970                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4971                        ## ISSUE: Can this be reached?
4972                        !!!cp ('t181');
4973                        last;
4974                      }
4975                    }
4976    
4977                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
4978                    !!!parse-error (type => 'unmatched end tag',
4979                $clear_up_to_marker->();                      value => $token->{tag_name}, token => $token);
4980                    ## Ignore the token
4981                $self->{insertion_mode} = 'in table';                  !!!next-token;
4982                    next B;
4983                !!!next-token;                } # INSCOPE
4984                redo B;              } elsif ($token->{tag_name} eq 'table' and
4985              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4986                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4987    
4988                ## As if </caption>                ## As if </caption>
4989                ## have a table element in table scope                ## have a table element in table scope
4990                my $i;                my $i;
4991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4992                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4993                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4994                      !!!cp ('t184');
4995                    $i = $_;                    $i = $_;
4996                    last INSCOPE;                    last INSCOPE;
4997                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4998                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4999                    last INSCOPE;                    last INSCOPE;
5000                  }                  }
5001                } # INSCOPE                } # INSCOPE
5002                unless (defined $i) {                unless (defined $i) {
5003                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5004                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5005                  ## Ignore the token                  ## Ignore the token
5006                  !!!next-token;                  !!!next-token;
5007                  redo B;                  next B;
5008                }                }
5009                                
5010                ## generate implied end tags                ## generate implied end tags
5011                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5012                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5013                     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;  
5014                }                }
5015    
5016                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5017                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5018                    !!!parse-error (type => 'not closed',
5019                                    value => $self->{open_elements}->[-1]->[0]
5020                                        ->manakai_local_name,
5021                                    token => $token);
5022                  } else {
5023                    !!!cp ('t189');
5024                }                }
5025    
5026                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5027    
5028                $clear_up_to_marker->();                $clear_up_to_marker->();
5029    
5030                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5031    
5032                ## reprocess                ## reprocess
5033                redo B;                next B;
5034              } elsif ({              } elsif ({
5035                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
5036                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5037                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5038                ## Ignore the token                  !!!cp ('t190');
5039                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             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;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
5040                  ## Ignore the token                  ## Ignore the token
5041                  !!!next-token;                  !!!next-token;
5042                  redo B;                  next B;
5043                } else {                } else {
5044                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5045                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5046                }                }
5047              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5048                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5049                          thead => 1, tr => 1,
5050                         }->{$token->{tag_name}} and
5051                         $self->{insertion_mode} == IN_CAPTION_IM) {
5052                  !!!cp ('t192');
5053                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5054                ## Ignore the token                ## Ignore the token
5055                !!!next-token;                !!!next-token;
5056                redo B;                next B;
5057              } else {              } else {
5058                #                !!!cp ('t193');
5059                  #
5060              }              }
5061            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5062              #          for my $entry (@{$self->{open_elements}}) {
5063              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5064                !!!cp ('t75');
5065                !!!parse-error (type => 'in body:#eof', token => $token);
5066                last;
5067            }            }
5068            }
5069    
5070            ## As if </colgroup>          ## Stop parsing.
5071            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5072              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5073              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5074          }
5075    
5076          $insert = $insert_to_current;
5077          #
5078        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5079          if ($token->{type} == CHARACTER_TOKEN) {
5080            if (not $open_tables->[-1]->[1] and # tainted
5081                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5082              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5083                  
5084              unless (length $token->{data}) {
5085                !!!cp ('t194');
5086              !!!next-token;              !!!next-token;
5087              redo B;              next B;
5088            } else {            } else {
5089              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5090            }            }
5091          } 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;  
               }  
             }  
5092    
5093              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5094    
5095              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5096              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5097              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5098              ## result in a new Text node.              ## result in a new Text node.
5099              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5100                
5101              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]}) {  
5102                # MUST                # MUST
5103                my $foster_parent_element;                my $foster_parent_element;
5104                my $next_sibling;                my $next_sibling;
5105                my $prev_sibling;                my $prev_sibling;
5106                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5107                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5108                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5109                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5110                        !!!cp ('t196');
5111                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5112                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5113                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5114                    } else {                    } else {
5115                        !!!cp ('t197');
5116                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5117                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5118                    }                    }
# Line 4160  sub _tree_construction_main ($) { Line 5124  sub _tree_construction_main ($) {
5124                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5125                if (defined $prev_sibling and                if (defined $prev_sibling and
5126                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5127                    !!!cp ('t198');
5128                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5129                } else {                } else {
5130                    !!!cp ('t199');
5131                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5132                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5133                     $next_sibling);                     $next_sibling);
5134                }                }
5135              } else {            $open_tables->[-1]->[1] = 1; # tainted
5136                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5137              }            !!!cp ('t200');
5138              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5139            }
5140                            
5141              !!!next-token;          !!!next-token;
5142              redo B;          next B;
5143            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
5144              if ({              if ({
5145                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5146                   th => 1, td => 1,                   th => 1, td => 1,
5147                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5148                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5149                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
5150                    while (not ($self->{open_elements}->[-1]->[1]
5151                                    & TABLE_SCOPING_EL)) {
5152                      !!!cp ('t201');
5153                      pop @{$self->{open_elements}};
5154                    }
5155                    
5156                    !!!insert-element ('tbody',, $token);
5157                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5158                    ## reprocess in the "in table body" insertion mode...
5159                }                }
5160    
5161                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5162                while (not {                  unless ($token->{tag_name} eq 'tr') {
5163                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
5164                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
5165                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
5166                    
5167                    ## Clear back to table body context
5168                    while (not ($self->{open_elements}->[-1]->[1]
5169                                    & TABLE_ROWS_SCOPING_EL)) {
5170                      !!!cp ('t203');
5171                      ## ISSUE: Can this case be reached?
5172                      pop @{$self->{open_elements}};
5173                    }
5174                    
5175                    $self->{insertion_mode} = IN_ROW_IM;
5176                    if ($token->{tag_name} eq 'tr') {
5177                      !!!cp ('t204');
5178                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5179                      !!!nack ('t204');
5180                      !!!next-token;
5181                      next B;
5182                    } else {
5183                      !!!cp ('t205');
5184                      !!!insert-element ('tr',, $token);
5185                      ## reprocess in the "in row" insertion mode
5186                    }
5187                  } else {
5188                    !!!cp ('t206');
5189                  }
5190    
5191                  ## Clear back to table row context
5192                  while (not ($self->{open_elements}->[-1]->[1]
5193                                  & TABLE_ROW_SCOPING_EL)) {
5194                    !!!cp ('t207');
5195                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5196                }                }
5197                                
5198                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5199                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5200                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5201                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5202                } else {                
5203                  !!!insert-element ('tr');                !!!nack ('t207.1');
5204                  ## reprocess                !!!next-token;
5205                }                next B;
               redo B;  
5206              } elsif ({              } elsif ({
5207                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5208                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5209                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5210                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5211                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5212                my $i;                  ## As if </tr>
5213                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5214                  my $node = $self->{open_elements}->[$_];                  my $i;
5215                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5216                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5217                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5218                    $i = $_;                      !!!cp ('t208');
5219                    last INSCOPE;                      $i = $_;
5220                  } elsif ({                      last INSCOPE;
5221                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5222                           }->{$node->[1]}) {                      !!!cp ('t209');
5223                    last INSCOPE;                      last INSCOPE;
5224                      }
5225                    } # INSCOPE
5226                    unless (defined $i) {
5227                      !!!cp ('t210');
5228    ## TODO: This type is wrong.
5229                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5230                      ## Ignore the token
5231                      !!!nack ('t210.1');
5232                      !!!next-token;
5233                      next B;
5234                    }
5235                    
5236                    ## Clear back to table row context
5237                    while (not ($self->{open_elements}->[-1]->[1]
5238                                    & TABLE_ROW_SCOPING_EL)) {
5239                      !!!cp ('t211');
5240                      ## ISSUE: Can this case be reached?
5241                      pop @{$self->{open_elements}};
5242                    }
5243                    
5244                    pop @{$self->{open_elements}}; # tr
5245                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5246                    if ($token->{tag_name} eq 'tr') {
5247                      !!!cp ('t212');
5248                      ## reprocess
5249                      !!!ack-later;
5250                      next B;
5251                    } else {
5252                      !!!cp ('t213');
5253                      ## reprocess in the "in table body" insertion mode...
5254                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5255                }                }
5256    
5257                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5258                while (not {                  ## have an element in table scope
5259                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5260                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5261                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5262                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5263                        !!!cp ('t214');
5264                        $i = $_;
5265                        last INSCOPE;
5266                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5267                        !!!cp ('t215');
5268                        last INSCOPE;
5269                      }
5270                    } # INSCOPE
5271                    unless (defined $i) {
5272                      !!!cp ('t216');
5273    ## TODO: This erorr type ios wrong.
5274                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5275                      ## Ignore the token
5276                      !!!nack ('t216.1');
5277                      !!!next-token;
5278                      next B;
5279                    }
5280    
5281                    ## Clear back to table body context
5282                    while (not ($self->{open_elements}->[-1]->[1]
5283                                    & TABLE_ROWS_SCOPING_EL)) {
5284                      !!!cp ('t217');
5285                      ## ISSUE: Can this state be reached?
5286                      pop @{$self->{open_elements}};
5287                    }
5288                    
5289                    ## As if <{current node}>
5290                    ## have an element in table scope
5291                    ## true by definition
5292                    
5293                    ## Clear back to table body context
5294                    ## nop by definition
5295                    
5296                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5297                    $self->{insertion_mode} = IN_TABLE_IM;
5298                    ## reprocess in "in table" insertion mode...
5299                  } else {
5300                    !!!cp ('t218');
5301                }                }
5302    
5303                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5304                ## have an element in table scope                  ## Clear back to table context
5305                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5306                                    & TABLE_SCOPING_EL)) {
5307                ## Clear back to table body context                    !!!cp ('t219');
5308                ## nop by definition                    ## ISSUE: Can this state be reached?
5309                      pop @{$self->{open_elements}};
5310                pop @{$self->{open_elements}};                  }
5311                $self->{insertion_mode} = 'in table';                  
5312                ## reprocess                  !!!insert-element ('colgroup',, $token);
5313                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5314                    ## reprocess
5315                    !!!ack-later;
5316                    next B;
5317                  } elsif ({
5318                            caption => 1,
5319                            colgroup => 1,
5320                            tbody => 1, tfoot => 1, thead => 1,
5321                           }->{$token->{tag_name}}) {
5322                    ## Clear back to table context
5323                    while (not ($self->{open_elements}->[-1]->[1]
5324                                    & TABLE_SCOPING_EL)) {
5325                      !!!cp ('t220');
5326                      ## ISSUE: Can this state be reached?
5327                      pop @{$self->{open_elements}};
5328                    }
5329                    
5330                    push @$active_formatting_elements, ['#marker', '']
5331                        if $token->{tag_name} eq 'caption';
5332                    
5333                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5334                    $self->{insertion_mode} = {
5335                                               caption => IN_CAPTION_IM,
5336                                               colgroup => IN_COLUMN_GROUP_IM,
5337                                               tbody => IN_TABLE_BODY_IM,
5338                                               tfoot => IN_TABLE_BODY_IM,
5339                                               thead => IN_TABLE_BODY_IM,
5340                                              }->{$token->{tag_name}};
5341                    !!!next-token;
5342                    !!!nack ('t220.1');
5343                    next B;
5344                  } else {
5345                    die "$0: in table: <>: $token->{tag_name}";
5346                  }
5347              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5348                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5349                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5350                                      ->manakai_local_name,
5351                                  token => $token);
5352    
5353                ## As if </table>                ## As if </table>
5354                ## have a table element in table scope                ## have a table element in table scope
5355                my $i;                my $i;
5356                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5357                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5358                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5359                      !!!cp ('t221');
5360                    $i = $_;                    $i = $_;
5361                    last INSCOPE;                    last INSCOPE;
5362                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5363                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5364                    last INSCOPE;                    last INSCOPE;
5365                  }                  }
5366                } # INSCOPE                } # INSCOPE
5367                unless (defined $i) {                unless (defined $i) {
5368                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5369    ## TODO: The following is wrong, maybe.
5370                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5371                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5372                    !!!nack ('t223.1');
5373                  !!!next-token;                  !!!next-token;
5374                  redo B;                  next B;
5375                }                }
5376                                
5377    ## TODO: Followings are removed from the latest spec.
5378                ## generate implied end tags                ## generate implied end tags
5379                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5380                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5381                     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;  
5382                }                }
5383    
5384                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5385                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5386                    ## NOTE: |<table><tr><table>|
5387                    !!!parse-error (type => 'not closed',
5388                                    value => $self->{open_elements}->[-1]->[0]
5389                                        ->manakai_local_name,
5390                                    token => $token);
5391                  } else {
5392                    !!!cp ('t226');
5393                }                }
5394    
5395                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5396                  pop @{$open_tables};
5397    
5398                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5399    
5400                ## reprocess            ## reprocess
5401                redo B;            !!!ack-later;
5402              } else {            next B;
5403                #          } elsif ($token->{tag_name} eq 'style') {
5404              }            if (not $open_tables->[-1]->[1]) { # tainted
5405            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5406              if ({              ## NOTE: This is a "as if in head" code clone.
5407                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5408                  }->{$token->{tag_name}}) {              next B;
5409                ## have an element in table scope            } else {
5410                my $i;              !!!cp ('t227.7');
5411                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5412                  my $node = $self->{open_elements}->[$_];            }
5413                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5414                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5415                    last INSCOPE;              !!!cp ('t227.6');
5416                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5417                            table => 1, html => 1,              $script_start_tag->();
5418                           }->{$node->[1]}) {              next B;
5419                    last INSCOPE;            } else {
5420                  }              !!!cp ('t227.5');
5421                } # INSCOPE              #
5422                unless (defined $i) {            }
5423                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5424                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5425                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5426                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5427                }                if ($type eq 'hidden') {
5428                    !!!cp ('t227.3');
5429                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5430    
5431                ## 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}};  
               }  
5432    
5433                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;  
               }  
5434    
               ## 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]);  
5435                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
5436    
5437                ## As if <{current node}>                  !!!next-token;
5438                ## have an element in table scope                  !!!ack ('t227.2.1');
5439                ## true by definition                  next B;
5440                  } else {
5441                ## Clear back to table body context                  !!!cp ('t227.2');
5442                ## nop by definition                  #
5443                  }
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       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;  
5444              } else {              } else {
5445                  !!!cp ('t227.1');
5446                #                #
5447              }              }
5448            } else {            } else {
5449                !!!cp ('t227.4');
5450              #              #
5451            }            }
5452                      } else {
5453            ## As if in table            !!!cp ('t227');
5454            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5455            $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;  
               }  
             }  
5456    
5457              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5458    
5459              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5460              ## ISSUE: Spec says that "whenever a node would be inserted          #
5461              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5462              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5463              $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 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!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>  
5464                ## have an element in table scope                ## have an element in table scope
5465                my $i;                my $i;
5466                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5467                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5468                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5469                      !!!cp ('t228');
5470                    $i = $_;                    $i = $_;
5471                    last INSCOPE;                    last INSCOPE;
5472                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5473                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5474                    last INSCOPE;                    last INSCOPE;
5475                  }                  }
5476                } # INSCOPE                } # INSCOPE
5477                unless (defined $i) {                unless (defined $i) {
5478                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5479                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5480                  ## Ignore the token                  ## Ignore the token
5481                    !!!nack ('t230.1');
5482                  !!!next-token;                  !!!next-token;
5483                  redo B;                  next B;
5484                  } else {
5485                    !!!cp ('t232');
5486                }                }
5487    
5488                ## Clear back to table row context                ## Clear back to table row context
5489                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5490                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5491                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5492                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5493                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5494                }                }
5495    
5496                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5497                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5498                ## reprocess                !!!next-token;
5499                redo B;                !!!nack ('t231.1');
5500                  next B;
5501              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5502                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5503                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5504                    ## have an element in table scope
5505                ## As if </table>                  my $i;
5506                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5507                my $i;                    my $node = $self->{open_elements}->[$_];
5508                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5509                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5510                  if ($node->[1] eq 'table') {                      $i = $_;
5511                    $i = $_;                      last INSCOPE;
5512                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5513                  } elsif ({                      !!!cp ('t234');
5514                            table => 1, html => 1,                      last INSCOPE;
5515                           }->{$node->[1]}) {                    }
5516                    last INSCOPE;                  } # INSCOPE
5517                    unless (defined $i) {
5518                      !!!cp ('t235');
5519    ## TODO: The following is wrong.
5520                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5521                      ## Ignore the token
5522                      !!!nack ('t236.1');
5523                      !!!next-token;
5524                      next B;
5525                  }                  }
5526                } # INSCOPE                  
5527                unless (defined $i) {                  ## Clear back to table row context
5528                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5529                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5530                  !!!next-token;                    !!!cp ('t236');
5531                  redo B;  ## ISSUE: Can this state be reached?
5532                }                    pop @{$self->{open_elements}};
                 
               ## 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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## 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;  
5533                  }                  }
5534                } # INSCOPE                  
5535                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5536                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5537                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5538                  !!!next-token;                }
5539                  redo B;  
5540                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5541                    ## have an element in table scope
5542                ## Clear back to table row context                  my $i;
5543                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5544                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5545                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5546                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5547                        $i = $_;
5548                        last INSCOPE;
5549                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5550                        !!!cp ('t238');
5551                        last INSCOPE;
5552                      }
5553                    } # INSCOPE
5554                    unless (defined $i) {
5555                      !!!cp ('t239');
5556                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5557                      ## Ignore the token
5558                      !!!nack ('t239.1');
5559                      !!!next-token;
5560                      next B;
5561                    }
5562                    
5563                    ## Clear back to table body context
5564                    while (not ($self->{open_elements}->[-1]->[1]
5565                                    & TABLE_ROWS_SCOPING_EL)) {
5566                      !!!cp ('t240');
5567                      pop @{$self->{open_elements}};
5568                    }
5569                    
5570                    ## As if <{current node}>
5571                    ## have an element in table scope
5572                    ## true by definition
5573                    
5574                    ## Clear back to table body context
5575                    ## nop by definition
5576                    
5577                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5578                    $self->{insertion_mode} = IN_TABLE_IM;
5579                    ## reprocess in the "in table" insertion mode...
5580                }                }
5581    
5582                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
5583                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
5584                !!!next-token;                ## the code for <table> in the "in table" insertion mode
5585                redo B;                ## is synced with it.
5586              } elsif ($token->{tag_name} eq 'table') {  
5587                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
5588                my $i;                my $i;
5589                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5590                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5591                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
5592                      !!!cp ('t241');
5593                    $i = $_;                    $i = $_;
5594                    last INSCOPE;                    last INSCOPE;
5595                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5596                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5597                    last INSCOPE;                    last INSCOPE;
5598                  }                  }
5599                } # INSCOPE                } # INSCOPE
5600                unless (defined $i) {                unless (defined $i) {
5601                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
5602                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5603                  ## Ignore the token                  ## Ignore the token
5604                    !!!nack ('t243.1');
5605                  !!!next-token;                  !!!next-token;
5606                  redo B;                  next B;
               }  
   
               ## 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}};  
5607                }                }
5608                    
5609                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
5610                $self->{insertion_mode} = 'in table body';                pop @{$open_tables};
5611                ## reprocess                
5612                redo B;                $self->_reset_insertion_mode;
5613                  
5614                  !!!next-token;
5615                  next B;
5616              } elsif ({              } elsif ({
5617                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5618                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5619                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5620                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5621                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5622                  my $node = $self->{open_elements}->[$_];                  my $i;
5623                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5624                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5625                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5626                  } elsif ({                      !!!cp ('t247');
5627                            table => 1, html => 1,                      $i = $_;
5628                           }->{$node->[1]}) {                      last INSCOPE;
5629                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5630                        !!!cp ('t248');
5631                        last INSCOPE;
5632                      }
5633                    } # INSCOPE
5634                      unless (defined $i) {
5635                        !!!cp ('t249');
5636                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5637                        ## Ignore the token
5638                        !!!nack ('t249.1');
5639                        !!!next-token;
5640                        next B;
5641                      }
5642                    
5643                    ## As if </tr>
5644                    ## have an element in table scope
5645                    my $i;
5646                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5647                      my $node = $self->{open_elements}->[$_];
5648                      if ($node->[1] & TABLE_ROW_EL) {
5649                        !!!cp ('t250');
5650                        $i = $_;
5651                        last INSCOPE;
5652                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5653                        !!!cp ('t251');
5654                        last INSCOPE;
5655                      }
5656                    } # INSCOPE
5657                      unless (defined $i) {
5658                        !!!cp ('t252');
5659                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5660                        ## Ignore the token
5661                        !!!nack ('t252.1');
5662                        !!!next-token;
5663                        next B;
5664                      }
5665                    
5666                    ## Clear back to table row context
5667                    while (not ($self->{open_elements}->[-1]->[1]
5668                                    & TABLE_ROW_SCOPING_EL)) {
5669                      !!!cp ('t253');
5670    ## ISSUE: Can this case be reached?
5671                      pop @{$self->{open_elements}};
5672                  }                  }
5673                } # INSCOPE                  
5674                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5675                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5676                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5677                }                }
5678    
               ## As if </tr>  
5679                ## have an element in table scope                ## have an element in table scope
5680                my $i;                my $i;
5681                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5682                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5683                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5684                      !!!cp ('t254');
5685                    $i = $_;                    $i = $_;
5686                    last INSCOPE;                    last INSCOPE;
5687                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5688                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5689                    last INSCOPE;                    last INSCOPE;
5690                  }                  }
5691                } # INSCOPE                } # INSCOPE
5692                unless (defined $i) {                unless (defined $i) {
5693                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5694                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5695                  ## Ignore the token                  ## Ignore the token
5696                    !!!nack ('t256.1');
5697                  !!!next-token;                  !!!next-token;
5698                  redo B;                  next B;
5699                }                }
5700    
5701                ## Clear back to table row context                ## Clear back to table body context
5702                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5703                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5704                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5705                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5707                }                }
5708    
5709                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5710                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5711                ## reprocess                !!!nack ('t257.1');
5712                redo B;                !!!next-token;
5713                  next B;
5714              } elsif ({              } elsif ({
5715                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5716                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5717                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5718                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5719                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5720                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5721                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5722                !!!next-token;            ## Ignore the token
5723                redo B;            !!!nack ('t258.1');
5724              } else {             !!!next-token;
5725                #            next B;
5726              }          } else {
5727            } else {            !!!cp ('t259');
5728              #            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5729    
5730            ## As if in table            $insert = $insert_to_foster;
5731            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5732            $in_body->($insert_to_foster);          }
5733            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5734          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5735            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
5736              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
5737              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
5738                          #
5739              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5740              !!!cp ('t259.2');
5741              #
5742            }
5743    
5744              !!!next-token;          ## Stop parsing
5745              redo B;          last B;
5746            } elsif ($token->{type} eq 'comment') {        } else {
5747              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
5748              my $comment = $self->{document}->create_comment ($token->{data});        }
5749              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5750              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
5751              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5752            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5753              if ({                unless (length $token->{data}) {
5754                   caption => 1, col => 1, colgroup => 1,                  !!!cp ('t260');
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
5755                  !!!next-token;                  !!!next-token;
5756                  redo B;                  next B;
5757                }                }
5758                }
5759                ## Close the cell              
5760                !!!back-token; # <?>              !!!cp ('t261');
5761                $token = {type => 'end tag', tag_name => $tn};              #
5762                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
5763              } else {              if ($token->{tag_name} eq 'col') {
5764                  !!!cp ('t262');
5765                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5766                  pop @{$self->{open_elements}};
5767                  !!!ack ('t262.1');
5768                  !!!next-token;
5769                  next B;
5770                } else {
5771                  !!!cp ('t263');
5772                #                #
5773              }              }
5774            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5775              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5776                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5777                my $i;                  !!!cp ('t264');
5778                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 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});  
5779                  ## Ignore the token                  ## Ignore the token
5780                  !!!next-token;                  !!!next-token;
5781                  redo B;                  next B;
5782                }                } else {
5783                                  !!!cp ('t265');
5784                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
5785                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5786                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
5787                     td => ($token->{tag_name} eq 'th'),                  next B;            
                    th => ($token->{tag_name} eq 'td'),  
                    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;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5788                }                }
5789                } elsif ($token->{tag_name} eq 'col') {
5790                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5791                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5792                ## Ignore the token                ## Ignore the token
5793                !!!next-token;                !!!next-token;
5794                redo B;                next B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } 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;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5795              } else {              } else {
5796                #                !!!cp ('t267');
5797                  #
5798              }              }
5799          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5800            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5801                @{$self->{open_elements}} == 1) { # redundant, maybe
5802              !!!cp ('t270.2');
5803              ## Stop parsing.
5804              last B;
5805            } else {
5806              ## NOTE: As if </colgroup>.
5807              !!!cp ('t270.1');
5808              pop @{$self->{open_elements}}; # colgroup
5809              $self->{insertion_mode} = IN_TABLE_IM;
5810              ## Reprocess.
5811              next B;
5812            }
5813          } else {
5814            die "$0: $token->{type}: Unknown token type";
5815          }
5816    
5817              ## As if </colgroup>
5818              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5819                !!!cp ('t269');
5820    ## TODO: Wrong error type?
5821                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5822                ## Ignore the token
5823                !!!nack ('t269.1');
5824                !!!next-token;
5825                next B;
5826            } else {            } else {
5827              #              !!!cp ('t270');
5828                pop @{$self->{open_elements}}; # colgroup
5829                $self->{insertion_mode} = IN_TABLE_IM;
5830                !!!ack-later;
5831                ## reprocess
5832                next B;
5833              }
5834        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5835          if ($token->{type} == CHARACTER_TOKEN) {
5836            !!!cp ('t271');
5837            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5838            !!!next-token;
5839            next B;
5840          } elsif ($token->{type} == START_TAG_TOKEN) {
5841            if ($token->{tag_name} eq 'option') {
5842              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5843                !!!cp ('t272');
5844                ## As if </option>
5845                pop @{$self->{open_elements}};
5846              } else {
5847                !!!cp ('t273');
5848            }            }
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5849    
5850                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5851                !!!next-token;            !!!nack ('t273.1');
5852                redo B;            !!!next-token;
5853              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5854                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5855                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5856                  pop @{$self->{open_elements}};              !!!cp ('t274');
5857                }              ## As if </option>
5858                pop @{$self->{open_elements}};
5859              } else {
5860                !!!cp ('t275');
5861              }
5862    
5863                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5864                  ## As if </optgroup>              !!!cp ('t276');
5865                  pop @{$self->{open_elements}};              ## As if </optgroup>
5866                }              pop @{$self->{open_elements}};
5867              } else {
5868                !!!cp ('t277');
5869              }
5870    
5871                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5872                !!!next-token;            !!!nack ('t277.1');
5873                redo B;            !!!next-token;
5874              } elsif ($token->{tag_name} eq 'select') {            next B;
5875                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5876                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5877                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5878                my $i;                    {
5879                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5880                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5881                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5882                    $i = $_;                    }->{$token->{tag_name}})) {
5883                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5884                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5885                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5886                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5887                    last INSCOPE;            ## have an element in table scope
5888                  }            my $i;
5889                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5891                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5892                  ## Ignore the token                !!!cp ('t278');
5893                  !!!next-token;                $i = $_;
5894                  redo B;                last INSCOPE;
5895                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5896                  !!!cp ('t279');
5897                  last INSCOPE;
5898                }
5899              } # INSCOPE
5900              unless (defined $i) {
5901                !!!cp ('t280');
5902                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5903                ## Ignore the token
5904                !!!nack ('t280.1');
5905                !!!next-token;
5906                next B;
5907              }
5908                                
5909                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5910              splice @{$self->{open_elements}}, $i;
5911    
5912                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5913    
5914                !!!next-token;            if ($token->{tag_name} eq 'select') {
5915                redo B;              !!!nack ('t281.2');
5916              } else {              !!!next-token;
5917                #              next B;
5918              } else {
5919                !!!cp ('t281.1');
5920                !!!ack-later;
5921                ## Reprocess the token.
5922                next B;
5923              }
5924            } else {
5925              !!!cp ('t282');
5926              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5927              ## Ignore the token
5928              !!!nack ('t282.1');
5929              !!!next-token;
5930              next B;
5931            }
5932          } elsif ($token->{type} == END_TAG_TOKEN) {
5933            if ($token->{tag_name} eq 'optgroup') {
5934              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5935                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5936                !!!cp ('t283');
5937                ## As if </option>
5938                splice @{$self->{open_elements}}, -2;
5939              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5940                !!!cp ('t284');
5941                pop @{$self->{open_elements}};
5942              } else {
5943                !!!cp ('t285');
5944                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5945                ## Ignore the token
5946              }
5947              !!!nack ('t285.1');
5948              !!!next-token;
5949              next B;
5950            } elsif ($token->{tag_name} eq 'option') {
5951              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5952                !!!cp ('t286');
5953                pop @{$self->{open_elements}};
5954              } else {
5955                !!!cp ('t287');
5956                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5957                ## Ignore the token
5958              }
5959              !!!nack ('t287.1');
5960              !!!next-token;
5961              next B;
5962            } elsif ($token->{tag_name} eq 'select') {
5963              ## have an element in table scope
5964              my $i;
5965              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5966                my $node = $self->{open_elements}->[$_];
5967                if ($node->[1] & SELECT_EL) {
5968                  !!!cp ('t288');
5969                  $i = $_;
5970                  last INSCOPE;
5971                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5972                  !!!cp ('t289');
5973                  last INSCOPE;
5974              }              }
5975            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5976              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5977                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5978                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5979                  ## As if </option>              ## Ignore the token
5980                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5981                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5982                  pop @{$self->{open_elements}};              next B;
5983                } 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;  
               }  
5984                                
5985                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5986              splice @{$self->{open_elements}}, $i;
5987    
5988                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5989    
5990                !!!next-token;            !!!nack ('t291.1');
5991                redo B;            !!!next-token;
5992              } elsif ({            next B;
5993                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5994                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5995                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5996                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5997                                   }->{$token->{tag_name}}) {
5998                ## have an element in table scope  ## TODO: The following is wrong?
5999                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;  
               }  
6000                                
6001                ## As if </select>            ## have an element in table scope
6002                ## have an element in table scope            my $i;
6003                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6004                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6005                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6006                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6007                    $i = $_;                $i = $_;
6008                    last INSCOPE;                last INSCOPE;
6009                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6010                            table => 1, html => 1,                !!!cp ('t293');
6011                           }->{$node->[1]}) {                last INSCOPE;
6012                    last INSCOPE;              }
6013                  }            } # INSCOPE
6014                } # INSCOPE            unless (defined $i) {
6015                unless (defined $i) {              !!!cp ('t294');
6016                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6017                  ## Ignore the </select> token              !!!nack ('t294.1');
6018                  !!!next-token; ## TODO: ok?              !!!next-token;
6019                  redo B;              next B;
6020                }            }
6021                                
6022                splice @{$self->{open_elements}}, $i;            ## As if </select>
6023              ## have an element in table scope
6024                $self->_reset_insertion_mode;            undef $i;
6025              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6026                ## reprocess              my $node = $self->{open_elements}->[$_];
6027                redo B;              if ($node->[1] & SELECT_EL) {
6028              } else {                !!!cp ('t295');
6029                #                $i = $_;
6030                  last INSCOPE;
6031                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6032    ## ISSUE: Can this state be reached?
6033                  !!!cp ('t296');
6034                  last INSCOPE;
6035              }              }
6036            } else {            } # INSCOPE
6037              #            unless (defined $i) {
6038                !!!cp ('t297');
6039    ## TODO: The following error type is correct?
6040                !!!parse-error (type => 'unmatched end tag:select', token => $token);
6041                ## Ignore the </select> token
6042                !!!nack ('t297.1');
6043                !!!next-token; ## TODO: ok?
6044                next B;
6045            }            }
6046                  
6047              !!!cp ('t298');
6048              splice @{$self->{open_elements}}, $i;
6049    
6050              $self->_reset_insertion_mode;
6051    
6052            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6053              ## reprocess
6054              next B;
6055            } else {
6056              !!!cp ('t299');
6057              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6058            ## Ignore the token            ## Ignore the token
6059              !!!nack ('t299.3');
6060            !!!next-token;            !!!next-token;
6061            redo B;            next B;
6062          } elsif ($self->{insertion_mode} eq 'after body') {          }
6063            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6064              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6065                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6066                ## As if in body            !!!cp ('t299.1');
6067                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6068            } else {
6069              !!!cp ('t299.2');
6070            }
6071    
6072            ## Stop parsing.
6073            last B;
6074          } else {
6075            die "$0: $token->{type}: Unknown token type";
6076          }
6077        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6078          if ($token->{type} == CHARACTER_TOKEN) {
6079            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6080              my $data = $1;
6081              ## As if in body
6082              $reconstruct_active_formatting_elements->($insert_to_current);
6083                                
6084                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6085              
6086              unless (length $token->{data}) {
6087                !!!cp ('t300');
6088                !!!next-token;
6089                next B;
6090              }
6091            }
6092            
6093            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6094              !!!cp ('t301');
6095              !!!parse-error (type => 'after html:#character', token => $token);
6096    
6097                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6098                  !!!next-token;          } else {
6099                  redo B;            !!!cp ('t302');
6100                }          }
6101              }          
6102                        ## "after body" insertion mode
6103              #          !!!parse-error (type => 'after body:#character', token => $token);
6104              !!!parse-error (type => 'after body:#'.$token->{type});  
6105            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
6106              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
6107              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
6108          } elsif ($token->{type} == START_TAG_TOKEN) {
6109            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6110              !!!cp ('t303');
6111              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6112              
6113              ## Reprocess in the "after body" insertion mode.
6114            } else {
6115              !!!cp ('t304');
6116            }
6117    
6118            ## "after body" insertion mode
6119            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6120    
6121            $self->{insertion_mode} = IN_BODY_IM;
6122            !!!ack-later;
6123            ## reprocess
6124            next B;
6125          } elsif ($token->{type} == END_TAG_TOKEN) {
6126            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6127              !!!cp ('t305');
6128              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6129              
6130              $self->{insertion_mode} = AFTER_BODY_IM;
6131              ## Reprocess in the "after body" insertion mode.
6132            } else {
6133              !!!cp ('t306');
6134            }
6135    
6136            ## "after body" insertion mode
6137            if ($token->{tag_name} eq 'html') {
6138              if (defined $self->{inner_html_node}) {
6139                !!!cp ('t307');
6140                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6141                ## Ignore the token
6142              !!!next-token;              !!!next-token;
6143              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $previous_insertion_mode = $self->{insertion_mode};  
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
6144            } else {            } else {
6145              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
6146                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6147                !!!next-token;
6148                next B;
6149            }            }
6150            } else {
6151              !!!cp ('t309');
6152              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6153    
6154            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6155            ## reprocess            ## reprocess
6156            redo B;            next B;
6157          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6158            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6159              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
6160                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          ## Stop parsing
6161            last B;
6162          } else {
6163            die "$0: $token->{type}: Unknown token type";
6164          }
6165        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6166          if ($token->{type} == CHARACTER_TOKEN) {
6167            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6168              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6169              
6170              unless (length $token->{data}) {
6171                !!!cp ('t310');
6172                !!!next-token;
6173                next B;
6174              }
6175            }
6176            
6177            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6178              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6179                !!!cp ('t311');
6180                !!!parse-error (type => 'in frameset:#character', token => $token);
6181              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6182                !!!cp ('t312');
6183                !!!parse-error (type => 'after frameset:#character', token => $token);
6184              } else { # "after html frameset"
6185                !!!cp ('t313');
6186                !!!parse-error (type => 'after html:#character', token => $token);
6187    
6188                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6189                ## Reprocess in the "after frameset" insertion mode.
6190                !!!parse-error (type => 'after frameset:#character', token => $token);
6191              }
6192              
6193              ## Ignore the token.
6194              if (length $token->{data}) {
6195                !!!cp ('t314');
6196                ## reprocess the rest of characters
6197              } else {
6198                !!!cp ('t315');
6199                !!!next-token;
6200              }
6201              next B;
6202            }
6203            
6204            die qq[$0: Character "$token->{data}"];
6205          } elsif ($token->{type} == START_TAG_TOKEN) {
6206            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6207              !!!cp ('t316');
6208              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6209    
6210                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6211                  !!!next-token;            ## Process in the "after frameset" insertion mode.
6212                  redo B;          } else {
6213                }            !!!cp ('t317');
6214              }          }
6215    
6216              #          if ($token->{tag_name} eq 'frameset' and
6217            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6218              my $comment = $self->{document}->create_comment ($token->{data});            !!!cp ('t318');
6219              $self->{open_elements}->[-1]->[0]->append_child ($comment);            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220              !!!nack ('t318.1');
6221              !!!next-token;
6222              next B;
6223            } elsif ($token->{tag_name} eq 'frame' and
6224                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6225              !!!cp ('t319');
6226              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6227              pop @{$self->{open_elements}};
6228              !!!ack ('t319.1');
6229              !!!next-token;
6230              next B;
6231            } elsif ($token->{tag_name} eq 'noframes') {
6232              !!!cp ('t320');
6233              ## NOTE: As if in body.
6234              $parse_rcdata->(CDATA_CONTENT_MODEL);
6235              next B;
6236            } else {
6237              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6238                !!!cp ('t321');
6239                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6240              } else {
6241                !!!cp ('t322');
6242                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6243              }
6244              ## Ignore the token
6245              !!!nack ('t322.1');
6246              !!!next-token;
6247              next B;
6248            }
6249          } elsif ($token->{type} == END_TAG_TOKEN) {
6250            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6251              !!!cp ('t323');
6252              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6253    
6254              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6255              ## Process in the "after frameset" insertion mode.
6256            } else {
6257              !!!cp ('t324');
6258            }
6259    
6260            if ($token->{tag_name} eq 'frameset' and
6261                $self->{insertion_mode} == IN_FRAMESET_IM) {
6262              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6263                  @{$self->{open_elements}} == 1) {
6264                !!!cp ('t325');
6265                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6266                ## Ignore the token
6267              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6268            } else {            } else {
6269              #              !!!cp ('t326');
6270                pop @{$self->{open_elements}};
6271                !!!next-token;
6272            }            }
6273              
6274            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6275              !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6276                !!!cp ('t327');
6277                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6278              } else {
6279                !!!cp ('t328');
6280              }
6281              next B;
6282            } elsif ($token->{tag_name} eq 'html' and
6283                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6284              !!!cp ('t329');
6285              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6286              !!!next-token;
6287              next B;
6288            } else {
6289              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6290                !!!cp ('t330');
6291                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6292            } else {            } else {
6293              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t331');
6294                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6295            }            }
6296            ## Ignore the token            ## Ignore the token
6297            !!!next-token;            !!!next-token;
6298            redo B;            next B;
6299          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6300            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6301              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6302                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  @{$self->{open_elements}} == 1) { # redundant, maybe
6303              !!!cp ('t331.1');
6304              !!!parse-error (type => 'in body:#eof', token => $token);
6305            } else {
6306              !!!cp ('t331.2');
6307            }
6308            
6309            ## Stop parsing
6310            last B;
6311          } else {
6312            die "$0: $token->{type}: Unknown token type";
6313          }
6314    
6315                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6316                  !!!next-token;      } else {
6317                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6318                }      }
             }  
6319    
6320              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6321                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6322          if ($token->{tag_name} eq 'script') {
6323            !!!cp ('t332');
6324            ## NOTE: This is an "as if in head" code clone
6325            $script_start_tag->();
6326            next B;
6327          } elsif ($token->{tag_name} eq 'style') {
6328            !!!cp ('t333');
6329            ## NOTE: This is an "as if in head" code clone
6330            $parse_rcdata->(CDATA_CONTENT_MODEL);
6331            next B;
6332          } elsif ({
6333                    base => 1, link => 1,
6334                   }->{$token->{tag_name}}) {
6335            !!!cp ('t334');
6336            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6337            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6338            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6339            !!!ack ('t334.1');
6340            !!!next-token;
6341            next B;
6342          } elsif ($token->{tag_name} eq 'meta') {
6343            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6344            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6345            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6346    
6347                ## Ignore the token.          unless ($self->{confident}) {
6348                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6349                  ## reprocess the rest of characters              !!!cp ('t335');
6350                } else {              ## NOTE: Whether the encoding is supported or not is handled
6351                  !!!next-token;              ## in the {change_encoding} callback.
6352                }              $self->{change_encoding}
6353                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6354                
6355                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6356                    ->set_user_data (manakai_has_reference =>
6357                                         $token->{attributes}->{charset}
6358                                             ->{has_reference});
6359              } elsif ($token->{attributes}->{content}) {
6360                if ($token->{attributes}->{content}->{value}
6361                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6362                        [\x09-\x0D\x20]*=
6363                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6364                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6365                  !!!cp ('t336');
6366                  ## NOTE: Whether the encoding is supported or not is handled
6367                  ## in the {change_encoding} callback.
6368                  $self->{change_encoding}
6369                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6370                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6371                      ->set_user_data (manakai_has_reference =>
6372                                           $token->{attributes}->{content}
6373                                                 ->{has_reference});
6374              }              }
6375            } elsif ($token->{type} eq 'comment') {            }
6376              my $comment = $self->{document}->create_comment ($token->{data});          } else {
6377              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($token->{attributes}->{charset}) {
6378              !!!next-token;              !!!cp ('t337');
6379              redo B;              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6380            } elsif ($token->{type} eq 'start tag') {                  ->set_user_data (manakai_has_reference =>
6381              if ($token->{tag_name} eq 'noframes') {                                       $token->{attributes}->{charset}
6382                $in_body->($insert_to_current);                                           ->{has_reference});
6383                redo B;            }
6384              } else {            if ($token->{attributes}->{content}) {
6385                #              !!!cp ('t338');
6386                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6387                    ->set_user_data (manakai_has_reference =>
6388                                         $token->{attributes}->{content}
6389                                             ->{has_reference});
6390              }
6391            }
6392    
6393            !!!ack ('t338.1');
6394            !!!next-token;
6395            next B;
6396          } elsif ($token->{tag_name} eq 'title') {
6397            !!!cp ('t341');
6398            ## NOTE: This is an "as if in head" code clone
6399            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6400            next B;
6401          } elsif ($token->{tag_name} eq 'body') {
6402            !!!parse-error (type => 'in body:body', token => $token);
6403                  
6404            if (@{$self->{open_elements}} == 1 or
6405                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6406              !!!cp ('t342');
6407              ## Ignore the token
6408            } else {
6409              my $body_el = $self->{open_elements}->[1]->[0];
6410              for my $attr_name (keys %{$token->{attributes}}) {
6411                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6412                  !!!cp ('t343');
6413                  $body_el->set_attribute_ns
6414                    (undef, [undef, $attr_name],
6415                     $token->{attributes}->{$attr_name}->{value});
6416              }              }
6417            } elsif ($token->{type} eq 'end tag') {            }
6418              if ($token->{tag_name} eq 'html') {          }
6419                $previous_insertion_mode = $self->{insertion_mode};          !!!nack ('t343.1');
6420                $self->{insertion_mode} = 'trailing end';          !!!next-token;
6421            next B;
6422          } elsif ({
6423                    address => 1, blockquote => 1, center => 1, dir => 1,
6424                    div => 1, dl => 1, fieldset => 1,
6425                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6426                    menu => 1, ol => 1, p => 1, ul => 1,
6427                    pre => 1, listing => 1,
6428                    form => 1,
6429                    table => 1,
6430                    hr => 1,
6431                   }->{$token->{tag_name}}) {
6432            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6433              !!!cp ('t350');
6434              !!!parse-error (type => 'in form:form', token => $token);
6435              ## Ignore the token
6436              !!!nack ('t350.1');
6437              !!!next-token;
6438              next B;
6439            }
6440    
6441            ## has a p element in scope
6442            INSCOPE: for (reverse @{$self->{open_elements}}) {
6443              if ($_->[1] & P_EL) {
6444                !!!cp ('t344');
6445                !!!back-token; # <form>
6446                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6447                          line => $token->{line}, column => $token->{column}};
6448                next B;
6449              } elsif ($_->[1] & SCOPING_EL) {
6450                !!!cp ('t345');
6451                last INSCOPE;
6452              }
6453            } # INSCOPE
6454              
6455            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6456            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6457              !!!nack ('t346.1');
6458              !!!next-token;
6459              if ($token->{type} == CHARACTER_TOKEN) {
6460                $token->{data} =~ s/^\x0A//;
6461                unless (length $token->{data}) {
6462                  !!!cp ('t346');
6463                !!!next-token;                !!!next-token;
               redo B;  
6464              } else {              } else {
6465                #                !!!cp ('t349');
6466                }
6467              } else {
6468                !!!cp ('t348');
6469              }
6470            } elsif ($token->{tag_name} eq 'form') {
6471              !!!cp ('t347.1');
6472              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6473    
6474              !!!nack ('t347.2');
6475              !!!next-token;
6476            } elsif ($token->{tag_name} eq 'table') {
6477              !!!cp ('t382');
6478              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6479              
6480              $self->{insertion_mode} = IN_TABLE_IM;
6481    
6482              !!!nack ('t382.1');
6483              !!!next-token;
6484            } elsif ($token->{tag_name} eq 'hr') {
6485              !!!cp ('t386');
6486              pop @{$self->{open_elements}};
6487            
6488              !!!nack ('t386.1');
6489              !!!next-token;
6490            } else {
6491              !!!nack ('t347.1');
6492              !!!next-token;
6493            }
6494            next B;
6495          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6496            ## has a p element in scope
6497            INSCOPE: for (reverse @{$self->{open_elements}}) {
6498              if ($_->[1] & P_EL) {
6499                !!!cp ('t353');
6500                !!!back-token; # <x>
6501                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6502                          line => $token->{line}, column => $token->{column}};
6503                next B;
6504              } elsif ($_->[1] & SCOPING_EL) {
6505                !!!cp ('t354');
6506                last INSCOPE;
6507              }
6508            } # INSCOPE
6509              
6510            ## Step 1
6511            my $i = -1;
6512            my $node = $self->{open_elements}->[$i];
6513            my $li_or_dtdd = {li => {li => 1},
6514                              dt => {dt => 1, dd => 1},
6515                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6516            LI: {
6517              ## Step 2
6518              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6519                if ($i != -1) {
6520                  !!!cp ('t355');
6521                  !!!parse-error (type => 'not closed',
6522                                  value => $self->{open_elements}->[-1]->[0]
6523                                      ->manakai_local_name,
6524                                  token => $token);
6525                } else {
6526                  !!!cp ('t356');
6527              }              }
6528                splice @{$self->{open_elements}}, $i;
6529                last LI;
6530            } else {            } else {
6531              die "$0: $token->{type}: Unknown token type";              !!!cp ('t357');
6532              }
6533              
6534              ## Step 3
6535              if (not ($node->[1] & FORMATTING_EL) and
6536                  #not $phrasing_category->{$node->[1]} and
6537                  ($node->[1] & SPECIAL_EL or
6538                   $node->[1] & SCOPING_EL) and
6539                  not ($node->[1] & ADDRESS_EL) and
6540                  not ($node->[1] & DIV_EL)) {
6541                !!!cp ('t358');
6542                last LI;
6543              }
6544              
6545              !!!cp ('t359');
6546              ## Step 4
6547              $i--;
6548              $node = $self->{open_elements}->[$i];
6549              redo LI;
6550            } # LI
6551              
6552            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6553            !!!nack ('t359.1');
6554            !!!next-token;
6555            next B;
6556          } elsif ($token->{tag_name} eq 'plaintext') {
6557            ## has a p element in scope
6558            INSCOPE: for (reverse @{$self->{open_elements}}) {
6559              if ($_->[1] & P_EL) {
6560                !!!cp ('t367');
6561                !!!back-token; # <plaintext>
6562                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6563                          line => $token->{line}, column => $token->{column}};
6564                next B;
6565              } elsif ($_->[1] & SCOPING_EL) {
6566                !!!cp ('t368');
6567                last INSCOPE;
6568            }            }
6569            } # INSCOPE
6570              
6571            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6572              
6573            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6574                        
6575            !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});          !!!nack ('t368.1');
6576            !!!next-token;
6577            next B;
6578          } elsif ($token->{tag_name} eq 'a') {
6579            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6580              my $node = $active_formatting_elements->[$i];
6581              if ($node->[1] & A_EL) {
6582                !!!cp ('t371');
6583                !!!parse-error (type => 'in a:a', token => $token);
6584                
6585                !!!back-token; # <a>
6586                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6587                          line => $token->{line}, column => $token->{column}};
6588                $formatting_end_tag->($token);
6589                
6590                AFE2: for (reverse 0..$#$active_formatting_elements) {
6591                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6592                    !!!cp ('t372');
6593                    splice @$active_formatting_elements, $_, 1;
6594                    last AFE2;
6595                  }
6596                } # AFE2
6597                OE: for (reverse 0..$#{$self->{open_elements}}) {
6598                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6599                    !!!cp ('t373');
6600                    splice @{$self->{open_elements}}, $_, 1;
6601                    last OE;
6602                  }
6603                } # OE
6604                last AFE;
6605              } elsif ($node->[0] eq '#marker') {
6606                !!!cp ('t374');
6607                last AFE;
6608              }
6609            } # AFE
6610              
6611            $reconstruct_active_formatting_elements->($insert_to_current);
6612    
6613            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6614            push @$active_formatting_elements, $self->{open_elements}->[-1];
6615    
6616            !!!nack ('t374.1');
6617            !!!next-token;
6618            next B;
6619          } elsif ($token->{tag_name} eq 'nobr') {
6620            $reconstruct_active_formatting_elements->($insert_to_current);
6621    
6622            ## has a |nobr| element in scope
6623            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6624              my $node = $self->{open_elements}->[$_];
6625              if ($node->[1] & NOBR_EL) {
6626                !!!cp ('t376');
6627                !!!parse-error (type => 'in nobr:nobr', token => $token);
6628                !!!back-token; # <nobr>
6629                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6630                          line => $token->{line}, column => $token->{column}};
6631                next B;
6632              } elsif ($node->[1] & SCOPING_EL) {
6633                !!!cp ('t377');
6634                last INSCOPE;
6635              }
6636            } # INSCOPE
6637            
6638            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6639            push @$active_formatting_elements, $self->{open_elements}->[-1];
6640            
6641            !!!nack ('t377.1');
6642            !!!next-token;
6643            next B;
6644          } elsif ($token->{tag_name} eq 'button') {
6645            ## has a button element in scope
6646            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6647              my $node = $self->{open_elements}->[$_];
6648              if ($node->[1] & BUTTON_EL) {
6649                !!!cp ('t378');
6650                !!!parse-error (type => 'in button:button', token => $token);
6651                !!!back-token; # <button>
6652                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6653                          line => $token->{line}, column => $token->{column}};
6654                next B;
6655              } elsif ($node->[1] & SCOPING_EL) {
6656                !!!cp ('t379');
6657                last INSCOPE;
6658              }
6659            } # INSCOPE
6660              
6661            $reconstruct_active_formatting_elements->($insert_to_current);
6662              
6663            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6664    
6665            ## TODO: associate with $self->{form_element} if defined
6666    
6667            push @$active_formatting_elements, ['#marker', ''];
6668    
6669            !!!nack ('t379.1');
6670            !!!next-token;
6671            next B;
6672          } elsif ({
6673                    xmp => 1,
6674                    iframe => 1,
6675                    noembed => 1,
6676                    noframes => 1,
6677                    noscript => 0, ## TODO: 1 if scripting is enabled
6678                   }->{$token->{tag_name}}) {
6679            if ($token->{tag_name} eq 'xmp') {
6680              !!!cp ('t381');
6681              $reconstruct_active_formatting_elements->($insert_to_current);
6682            } else {
6683              !!!cp ('t399');
6684            }
6685            ## NOTE: There is an "as if in body" code clone.
6686            $parse_rcdata->(CDATA_CONTENT_MODEL);
6687            next B;
6688          } elsif ($token->{tag_name} eq 'isindex') {
6689            !!!parse-error (type => 'isindex', token => $token);
6690            
6691            if (defined $self->{form_element}) {
6692              !!!cp ('t389');
6693            ## Ignore the token            ## Ignore the token
6694              !!!nack ('t389'); ## NOTE: Not acknowledged.
6695              !!!next-token;
6696              next B;
6697            } else {
6698              my $at = $token->{attributes};
6699              my $form_attrs;
6700              $form_attrs->{action} = $at->{action} if $at->{action};
6701              my $prompt_attr = $at->{prompt};
6702              $at->{name} = {name => 'name', value => 'isindex'};
6703              delete $at->{action};
6704              delete $at->{prompt};
6705              my @tokens = (
6706                            {type => START_TAG_TOKEN, tag_name => 'form',
6707                             attributes => $form_attrs,
6708                             line => $token->{line}, column => $token->{column}},
6709                            {type => START_TAG_TOKEN, tag_name => 'hr',
6710                             line => $token->{line}, column => $token->{column}},
6711                            {type => START_TAG_TOKEN, tag_name => 'p',
6712                             line => $token->{line}, column => $token->{column}},
6713                            {type => START_TAG_TOKEN, tag_name => 'label',
6714                             line => $token->{line}, column => $token->{column}},
6715                           );
6716              if ($prompt_attr) {
6717                !!!cp ('t390');
6718                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6719                               #line => $token->{line}, column => $token->{column},
6720                              };
6721              } else {
6722                !!!cp ('t391');
6723                push @tokens, {type => CHARACTER_TOKEN,
6724                               data => 'This is a searchable index. Insert your search keywords here: ',
6725                               #line => $token->{line}, column => $token->{column},
6726                              }; # SHOULD
6727                ## TODO: make this configurable
6728              }
6729              push @tokens,
6730                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6731                             line => $token->{line}, column => $token->{column}},
6732                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6733                            {type => END_TAG_TOKEN, tag_name => 'label',
6734                             line => $token->{line}, column => $token->{column}},
6735                            {type => END_TAG_TOKEN, tag_name => 'p',
6736                             line => $token->{line}, column => $token->{column}},
6737                            {type => START_TAG_TOKEN, tag_name => 'hr',
6738                             line => $token->{line}, column => $token->{column}},
6739                            {type => END_TAG_TOKEN, tag_name => 'form',
6740                             line => $token->{line}, column => $token->{column}};
6741              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6742              !!!back-token (@tokens);
6743              !!!next-token;
6744              next B;
6745            }
6746          } elsif ($token->{tag_name} eq 'textarea') {
6747            my $tag_name = $token->{tag_name};
6748            my $el;
6749            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6750            
6751            ## TODO: $self->{form_element} if defined
6752            $self->{content_model} = RCDATA_CONTENT_MODEL;
6753            delete $self->{escape}; # MUST
6754            
6755            $insert->($el);
6756            
6757            my $text = '';
6758            !!!nack ('t392.1');
6759            !!!next-token;
6760            if ($token->{type} == CHARACTER_TOKEN) {
6761              $token->{data} =~ s/^\x0A//;
6762              unless (length $token->{data}) {
6763                !!!cp ('t392');
6764                !!!next-token;
6765              } else {
6766                !!!cp ('t393');
6767              }
6768            } else {
6769              !!!cp ('t394');
6770            }
6771            while ($token->{type} == CHARACTER_TOKEN) {
6772              !!!cp ('t395');
6773              $text .= $token->{data};
6774            !!!next-token;            !!!next-token;
6775            redo B;          }
6776            if (length $text) {
6777              !!!cp ('t396');
6778              $el->manakai_append_text ($text);
6779            }
6780            
6781            $self->{content_model} = PCDATA_CONTENT_MODEL;
6782            
6783            if ($token->{type} == END_TAG_TOKEN and
6784                $token->{tag_name} eq $tag_name) {
6785              !!!cp ('t397');
6786              ## Ignore the token
6787            } else {
6788              !!!cp ('t398');
6789              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6790            }
6791            !!!next-token;
6792            next B;
6793          } elsif ($token->{tag_name} eq 'math' or
6794                   $token->{tag_name} eq 'svg') {
6795            $reconstruct_active_formatting_elements->($insert_to_current);
6796    
6797            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6798    
6799            ## ISSUE: An issue in spec there          ## "adjust foreign attributes" - done in insert-element-f
6800            
6801            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6802            
6803            if ($self->{self_closing}) {
6804              pop @{$self->{open_elements}};
6805              !!!ack ('t398.1');
6806          } else {          } else {
6807            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t398.2');
6808              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6809              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6810              ## mode, "in body" (not "in foreign content") secondary insertion
6811              ## mode, maybe.
6812          }          }
6813        }  
6814      } elsif ($self->{insertion_mode} eq 'trailing end') {          !!!next-token;
6815        ## states in the main stage is preserved yet # MUST          next B;
6816                } elsif ({
6817        if ($token->{type} eq 'DOCTYPE') {                  caption => 1, col => 1, colgroup => 1, frame => 1,
6818          !!!parse-error (type => 'after html:#DOCTYPE');                  frameset => 1, head => 1, option => 1, optgroup => 1,
6819                    tbody => 1, td => 1, tfoot => 1, th => 1,
6820                    thead => 1, tr => 1,
6821                   }->{$token->{tag_name}}) {
6822            !!!cp ('t401');
6823            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6824          ## Ignore the token          ## Ignore the token
6825            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6826          !!!next-token;          !!!next-token;
6827          redo B;          next B;
6828        } elsif ($token->{type} eq 'comment') {          
6829          my $comment = $self->{document}->create_comment ($token->{data});          ## ISSUE: An issue on HTML5 new elements in the spec.
6830          $self->{document}->append_child ($comment);        } else {
6831            if ($token->{tag_name} eq 'image') {
6832              !!!cp ('t384');
6833              !!!parse-error (type => 'image', token => $token);
6834              $token->{tag_name} = 'img';
6835            } else {
6836              !!!cp ('t385');
6837            }
6838    
6839            ## NOTE: There is an "as if <br>" code clone.
6840            $reconstruct_active_formatting_elements->($insert_to_current);
6841            
6842            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6843    
6844            if ({
6845                 applet => 1, marquee => 1, object => 1,
6846                }->{$token->{tag_name}}) {
6847              !!!cp ('t380');
6848              push @$active_formatting_elements, ['#marker', ''];
6849              !!!nack ('t380.1');
6850            } elsif ({
6851                      b => 1, big => 1, em => 1, font => 1, i => 1,
6852                      s => 1, small => 1, strile => 1,
6853                      strong => 1, tt => 1, u => 1,
6854                     }->{$token->{tag_name}}) {
6855              !!!cp ('t375');
6856              push @$active_formatting_elements, $self->{open_elements}->[-1];
6857              !!!nack ('t375.1');
6858            } elsif ($token->{tag_name} eq 'input') {
6859              !!!cp ('t388');
6860              ## TODO: associate with $self->{form_element} if defined
6861              pop @{$self->{open_elements}};
6862              !!!ack ('t388.2');
6863            } elsif ({
6864                      area => 1, basefont => 1, bgsound => 1, br => 1,
6865                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6866                      #image => 1,
6867                     }->{$token->{tag_name}}) {
6868              !!!cp ('t388.1');
6869              pop @{$self->{open_elements}};
6870              !!!ack ('t388.3');
6871            } elsif ($token->{tag_name} eq 'select') {
6872              ## TODO: associate with $self->{form_element} if defined
6873            
6874              if ($self->{insertion_mode} & TABLE_IMS or
6875                  $self->{insertion_mode} & BODY_TABLE_IMS or
6876                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6877                !!!cp ('t400.1');
6878                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6879              } else {
6880                !!!cp ('t400.2');
6881                $self->{insertion_mode} = IN_SELECT_IM;
6882              }
6883              !!!nack ('t400.3');
6884            } else {
6885              !!!nack ('t402');
6886            }
6887            
6888          !!!next-token;          !!!next-token;
6889          redo B;          next B;
6890        } elsif ($token->{type} eq 'character') {        }
6891          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6892            my $data = $1;        if ($token->{tag_name} eq 'body') {
6893            ## As if in the main phase.          ## has a |body| element in scope
6894            ## NOTE: The insertion mode in the main phase          my $i;
6895            ## just before the phase has been changed to the trailing          INSCOPE: {
6896            ## end phase is either "after body" or "after frameset".            for (reverse @{$self->{open_elements}}) {
6897            $reconstruct_active_formatting_elements->($insert_to_current);              if ($_->[1] & BODY_EL) {
6898                  !!!cp ('t405');
6899                  $i = $_;
6900                  last INSCOPE;
6901                } elsif ($_->[1] & SCOPING_EL) {
6902                  !!!cp ('t405.1');
6903                  last;
6904                }
6905              }
6906    
6907              !!!parse-error (type => 'start tag not allowed',
6908                              value => $token->{tag_name}, token => $token);
6909              ## NOTE: Ignore the token.
6910              !!!next-token;
6911              next B;
6912            } # INSCOPE
6913    
6914            for (@{$self->{open_elements}}) {
6915              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6916                !!!cp ('t403');
6917                !!!parse-error (type => 'not closed',
6918                                value => $_->[0]->manakai_local_name,
6919                                token => $token);
6920                last;
6921              } else {
6922                !!!cp ('t404');
6923              }
6924            }
6925    
6926            $self->{insertion_mode} = AFTER_BODY_IM;
6927            !!!next-token;
6928            next B;
6929          } elsif ($token->{tag_name} eq 'html') {
6930            ## TODO: Update this code.  It seems that the code below is not
6931            ## up-to-date, though it has same effect as speced.
6932            if (@{$self->{open_elements}} > 1 and
6933                $self->{open_elements}->[1]->[1] & BODY_EL) {
6934              ## ISSUE: There is an issue in the spec.
6935              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6936                !!!cp ('t406');
6937                !!!parse-error (type => 'not closed',
6938                                value => $self->{open_elements}->[1]->[0]
6939                                    ->manakai_local_name,
6940                                token => $token);
6941              } else {
6942                !!!cp ('t407');
6943              }
6944              $self->{insertion_mode} = AFTER_BODY_IM;
6945              ## reprocess
6946              next B;
6947            } else {
6948              !!!cp ('t408');
6949              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6950              ## Ignore the token
6951              !!!next-token;
6952              next B;
6953            }
6954          } elsif ({
6955                    address => 1, blockquote => 1, center => 1, dir => 1,
6956                    div => 1, dl => 1, fieldset => 1, listing => 1,
6957                    menu => 1, ol => 1, pre => 1, ul => 1,
6958                    dd => 1, dt => 1, li => 1,
6959                    applet => 1, button => 1, marquee => 1, object => 1,
6960                   }->{$token->{tag_name}}) {
6961            ## has an element in scope
6962            my $i;
6963            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6964              my $node = $self->{open_elements}->[$_];
6965              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6966                !!!cp ('t410');
6967                $i = $_;
6968                last INSCOPE;
6969              } elsif ($node->[1] & SCOPING_EL) {
6970                !!!cp ('t411');
6971                last INSCOPE;
6972              }
6973            } # INSCOPE
6974    
6975            unless (defined $i) { # has an element in scope
6976              !!!cp ('t413');
6977              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6978            } else {
6979              ## Step 1. generate implied end tags
6980              while ({
6981                      dd => ($token->{tag_name} ne 'dd'),
6982                      dt => ($token->{tag_name} ne 'dt'),
6983                      li => ($token->{tag_name} ne 'li'),
6984                      p => 1,
6985                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6986                !!!cp ('t409');
6987                pop @{$self->{open_elements}};
6988              }
6989    
6990              ## Step 2.
6991              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6992                      ne $token->{tag_name}) {
6993                !!!cp ('t412');
6994                !!!parse-error (type => 'not closed',
6995                                value => $self->{open_elements}->[-1]->[0]
6996                                    ->manakai_local_name,
6997                                token => $token);
6998              } else {
6999                !!!cp ('t414');
7000              }
7001    
7002              ## Step 3.
7003              splice @{$self->{open_elements}}, $i;
7004    
7005              ## Step 4.
7006              $clear_up_to_marker->()
7007                  if {
7008                    applet => 1, button => 1, marquee => 1, object => 1,
7009                  }->{$token->{tag_name}};
7010            }
7011            !!!next-token;
7012            next B;
7013          } elsif ($token->{tag_name} eq 'form') {
7014            undef $self->{form_element};
7015    
7016            ## has an element in scope
7017            my $i;
7018            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7019              my $node = $self->{open_elements}->[$_];
7020              if ($node->[1] & FORM_EL) {
7021                !!!cp ('t418');
7022                $i = $_;
7023                last INSCOPE;
7024              } elsif ($node->[1] & SCOPING_EL) {
7025                !!!cp ('t419');
7026                last INSCOPE;
7027              }
7028            } # INSCOPE
7029    
7030            unless (defined $i) { # has an element in scope
7031              !!!cp ('t421');
7032              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7033            } else {
7034              ## Step 1. generate implied end tags
7035              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7036                !!!cp ('t417');
7037                pop @{$self->{open_elements}};
7038              }
7039                        
7040            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7041              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7042                      ne $token->{tag_name}) {
7043                !!!cp ('t417.1');
7044                !!!parse-error (type => 'not closed',
7045                                value => $self->{open_elements}->[-1]->[0]
7046                                    ->manakai_local_name,
7047                                token => $token);
7048              } else {
7049                !!!cp ('t420');
7050              }  
7051                        
7052            unless (length $token->{data}) {            ## Step 3.
7053              !!!next-token;            splice @{$self->{open_elements}}, $i;
7054              redo B;          }
7055    
7056            !!!next-token;
7057            next B;
7058          } elsif ({
7059                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7060                   }->{$token->{tag_name}}) {
7061            ## has an element in scope
7062            my $i;
7063            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7064              my $node = $self->{open_elements}->[$_];
7065              if ($node->[1] & HEADING_EL) {
7066                !!!cp ('t423');
7067                $i = $_;
7068                last INSCOPE;
7069              } elsif ($node->[1] & SCOPING_EL) {
7070                !!!cp ('t424');
7071                last INSCOPE;
7072              }
7073            } # INSCOPE
7074    
7075            unless (defined $i) { # has an element in scope
7076              !!!cp ('t425.1');
7077              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7078            } else {
7079              ## Step 1. generate implied end tags
7080              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7081                !!!cp ('t422');
7082                pop @{$self->{open_elements}};
7083            }            }
7084              
7085              ## Step 2.
7086              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7087                      ne $token->{tag_name}) {
7088                !!!cp ('t425');
7089                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7090              } else {
7091                !!!cp ('t426');
7092              }
7093    
7094              ## Step 3.
7095              splice @{$self->{open_elements}}, $i;
7096          }          }
7097            
7098            !!!next-token;
7099            next B;
7100          } elsif ($token->{tag_name} eq 'p') {
7101            ## has an element in scope
7102            my $i;
7103            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7104              my $node = $self->{open_elements}->[$_];
7105              if ($node->[1] & P_EL) {
7106                !!!cp ('t410.1');
7107                $i = $_;
7108                last INSCOPE;
7109              } elsif ($node->[1] & SCOPING_EL) {
7110                !!!cp ('t411.1');
7111                last INSCOPE;
7112              }
7113            } # INSCOPE
7114    
7115          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7116          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7117          ## reprocess                    ne $token->{tag_name}) {
7118          redo B;              !!!cp ('t412.1');
7119        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7120                 $token->{type} eq 'end tag') {                              value => $self->{open_elements}->[-1]->[0]
7121          !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});                                  ->manakai_local_name,
7122          $self->{insertion_mode} = $previous_insertion_mode;                              token => $token);
7123          ## reprocess            } else {
7124          redo B;              !!!cp ('t414.1');
7125        } elsif ($token->{type} eq 'end-of-file') {            }
7126          ## Stop parsing  
7127          last B;            splice @{$self->{open_elements}}, $i;
7128            } else {
7129              !!!cp ('t413.1');
7130              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7131    
7132              !!!cp ('t415.1');
7133              ## As if <p>, then reprocess the current token
7134              my $el;
7135              !!!create-element ($el, $HTML_NS, 'p',, $token);
7136              $insert->($el);
7137              ## NOTE: Not inserted into |$self->{open_elements}|.
7138            }
7139    
7140            !!!next-token;
7141            next B;
7142          } elsif ({
7143                    a => 1,
7144                    b => 1, big => 1, em => 1, font => 1, i => 1,
7145                    nobr => 1, s => 1, small => 1, strile => 1,
7146                    strong => 1, tt => 1, u => 1,
7147                   }->{$token->{tag_name}}) {
7148            !!!cp ('t427');
7149            $formatting_end_tag->($token);
7150            next B;
7151          } elsif ($token->{tag_name} eq 'br') {
7152            !!!cp ('t428');
7153            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7154    
7155            ## As if <br>
7156            $reconstruct_active_formatting_elements->($insert_to_current);
7157            
7158            my $el;
7159            !!!create-element ($el, $HTML_NS, 'br',, $token);
7160            $insert->($el);
7161            
7162            ## Ignore the token.
7163            !!!next-token;
7164            next B;
7165          } elsif ({
7166                    caption => 1, col => 1, colgroup => 1, frame => 1,
7167                    frameset => 1, head => 1, option => 1, optgroup => 1,
7168                    tbody => 1, td => 1, tfoot => 1, th => 1,
7169                    thead => 1, tr => 1,
7170                    area => 1, basefont => 1, bgsound => 1,
7171                    embed => 1, hr => 1, iframe => 1, image => 1,
7172                    img => 1, input => 1, isindex => 1, noembed => 1,
7173                    noframes => 1, param => 1, select => 1, spacer => 1,
7174                    table => 1, textarea => 1, wbr => 1,
7175                    noscript => 0, ## TODO: if scripting is enabled
7176                   }->{$token->{tag_name}}) {
7177            !!!cp ('t429');
7178            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7179            ## Ignore the token
7180            !!!next-token;
7181            next B;
7182            
7183            ## ISSUE: Issue on HTML5 new elements in spec
7184            
7185        } else {        } else {
7186          die "$0: $token->{type}: Unknown token";          ## Step 1
7187            my $node_i = -1;
7188            my $node = $self->{open_elements}->[$node_i];
7189    
7190            ## Step 2
7191            S2: {
7192              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7193                ## Step 1
7194                ## generate implied end tags
7195                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7196                  !!!cp ('t430');
7197                  ## ISSUE: Can this case be reached?
7198                  pop @{$self->{open_elements}};
7199                }
7200            
7201                ## Step 2
7202                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7203                        ne $token->{tag_name}) {
7204                  !!!cp ('t431');
7205                  ## NOTE: <x><y></x>
7206                  !!!parse-error (type => 'not closed',
7207                                  value => $self->{open_elements}->[-1]->[0]
7208                                      ->manakai_local_name,
7209                                  token => $token);
7210                } else {
7211                  !!!cp ('t432');
7212                }
7213                
7214                ## Step 3
7215                splice @{$self->{open_elements}}, $node_i;
7216    
7217                !!!next-token;
7218                last S2;
7219              } else {
7220                ## Step 3
7221                if (not ($node->[1] & FORMATTING_EL) and
7222                    #not $phrasing_category->{$node->[1]} and
7223                    ($node->[1] & SPECIAL_EL or
7224                     $node->[1] & SCOPING_EL)) {
7225                  !!!cp ('t433');
7226                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7227                  ## Ignore the token
7228                  !!!next-token;
7229                  last S2;
7230                }
7231    
7232                !!!cp ('t434');
7233              }
7234              
7235              ## Step 4
7236              $node_i--;
7237              $node = $self->{open_elements}->[$node_i];
7238              
7239              ## Step 5;
7240              redo S2;
7241            } # S2
7242            next B;
7243        }        }
7244      }      }
7245        next B;
7246      } continue { # B
7247        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7248          ## NOTE: The code below is executed in cases where it does not have
7249          ## to be, but it it is harmless even in those cases.
7250          ## has an element in scope
7251          INSCOPE: {
7252            for (reverse 0..$#{$self->{open_elements}}) {
7253              my $node = $self->{open_elements}->[$_];
7254              if ($node->[1] & FOREIGN_EL) {
7255                last INSCOPE;
7256              } elsif ($node->[1] & SCOPING_EL) {
7257                last;
7258              }
7259            }
7260            
7261            ## NOTE: No foreign element in scope.
7262            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7263          } # INSCOPE
7264        }
7265    } # B    } # B
7266    
7267    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5268  sub set_inner_html ($$$) { Line 7275  sub set_inner_html ($$$) {
7275    my $s = \$_[0];    my $s = \$_[0];
7276    my $onerror = $_[1];    my $onerror = $_[1];
7277    
7278      ## ISSUE: Should {confident} be true?
7279    
7280    my $nt = $node->node_type;    my $nt = $node->node_type;
7281    if ($nt == 9) {    if ($nt == 9) {
7282      # MUST      # MUST
# Line 5296  sub set_inner_html ($$$) { Line 7305  sub set_inner_html ($$$) {
7305      my $p = $class->new;      my $p = $class->new;
7306      $p->{document} = $doc;      $p->{document} = $doc;
7307    
7308      ## Step 9 # MUST      ## Step 8 # MUST
7309      my $i = 0;      my $i = 0;
7310      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7311      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7312      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7313        my $self = shift;        my $self = shift;
7314    
7315        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7316        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7317    
7318          $self->{next_char} = -1 and return if $i >= length $$s;
7319          $self->{next_char} = ord substr $$s, $i++, 1;
7320    
7321        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7322        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7323        $column++;  
7324          if ($self->{next_char} == 0x000A) { # LF
7325        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7326          $line++;          $p->{column} = 0;
7327          $column = 0;          !!!cp ('i1');
7328        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7329          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7330          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7331          $line++;          $p->{line}++;
7332          $column = 0;          $p->{column} = 0;
7333        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7334          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7335        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7336            !!!cp ('i3');
7337          } elsif ($self->{next_char} == 0x0000) { # NULL
7338            !!!cp ('i4');
7339          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7340          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7341          } elsif ($self->{next_char} <= 0x0008 or
7342                   (0x000E <= $self->{next_char} and
7343                    $self->{next_char} <= 0x001F) or
7344                   (0x007F <= $self->{next_char} and
7345                    $self->{next_char} <= 0x009F) or
7346                   (0xD800 <= $self->{next_char} and
7347                    $self->{next_char} <= 0xDFFF) or
7348                   (0xFDD0 <= $self->{next_char} and
7349                    $self->{next_char} <= 0xFDDF) or
7350                   {
7351                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7352                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7353                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7354                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7355                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7356                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7357                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7358                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7359                    0x10FFFE => 1, 0x10FFFF => 1,
7360                   }->{$self->{next_char}}) {
7361            !!!cp ('i4.1');
7362            !!!parse-error (type => 'control char', level => $self->{must_level});
7363    ## TODO: error type documentation
7364        }        }
7365      };      };
7366      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7367      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7368            
7369      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7370        my (%opt) = @_;        my (%opt) = @_;
7371        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7372          my $column = $opt{column};
7373          if (defined $opt{token} and defined $opt{token}->{line}) {
7374            $line = $opt{token}->{line};
7375            $column = $opt{token}->{column};
7376          }
7377          warn "Parse error ($opt{type}) at line $line column $column\n";
7378      };      };
7379      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7380        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7381      };      };
7382            
7383      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7384      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7385    
7386      ## Step 2      ## Step 2
7387      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7388      $p->{content_model_flag} = {      $p->{content_model} = {
7389        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7390        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7391        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7392        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7393        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7394        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7395        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7396        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7397        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7398        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7399      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7400         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7401            unless defined $p->{content_model};
7402            ## ISSUE: What is "the name of the element"? local name?
7403    
7404      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7405          ## TODO: Foreign element OK?
7406    
7407      ## Step 4      ## Step 3
7408      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7409        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7410    
7411      ## Step 5 # MUST      ## Step 4 # MUST
7412      $doc->append_child ($root);      $doc->append_child ($root);
7413    
7414      ## Step 6 # MUST      ## Step 5 # MUST
7415      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7416    
7417      undef $p->{head_element};      undef $p->{head_element};
7418    
7419      ## Step 7 # MUST      ## Step 6 # MUST
7420      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7421    
7422      ## Step 8 # MUST      ## Step 7 # MUST
7423      my $anode = $node;      my $anode = $node;
7424      AN: while (defined $anode) {      AN: while (defined $anode) {
7425        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7426          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7427          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7428            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7429                !!!cp ('i5');
7430              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7431              last AN;              last AN;
7432            }            }
# Line 5387  sub set_inner_html ($$$) { Line 7435  sub set_inner_html ($$$) {
7435        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7436      } # AN      } # AN
7437            
7438      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7439      {      {
7440        my $self = $p;        my $self = $p;
7441        !!!next-token;        !!!next-token;
7442      }      }
7443      $p->_tree_construction_main;      $p->_tree_construction_main;
7444    
7445      ## Step 11 # MUST      ## Step 10 # MUST
7446      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7447      for (@cn) {      for (@cn) {
7448        $node->remove_child ($_);        $node->remove_child ($_);
7449      }      }
7450      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7451    
7452      ## Step 12 # MUST      ## Step 11 # MUST
7453      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7454      for (@cn) {      for (@cn) {
7455        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5411  sub set_inner_html ($$$) { Line 7458  sub set_inner_html ($$$) {
7458      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7459    
7460      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7461    
7462        delete $p->{parse_error}; # delete loop
7463    } else {    } else {
7464      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";
7465    }    }
# Line 5418  sub set_inner_html ($$$) { Line 7467  sub set_inner_html ($$$) {
7467    
7468  } # tree construction stage  } # tree construction stage
7469    
7470  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7471    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  
7472    
7473  1;  1;
7474  # $Date$  # $Date$

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.143

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24