/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.43 by wakaba, Sat Jul 21 07:21:44 2007 UTC revision 1.144 by wakaba, Sat May 24 11:07:24 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    
49    sub TABLE_ROWS_EL () {
50      TABLE_EL |
51      TABLE_ROW_EL |
52      TABLE_ROW_GROUP_EL
53    }
54    
55    sub END_TAG_OPTIONAL_EL () {
56      DD_EL |
57      DT_EL |
58      LI_EL |
59      P_EL
60    }
61    
62    sub ALL_END_TAG_OPTIONAL_EL () {
63      END_TAG_OPTIONAL_EL |
64      BODY_EL |
65      HTML_EL |
66      TABLE_CELL_EL |
67      TABLE_ROW_EL |
68      TABLE_ROW_GROUP_EL
69    }
70    
71    sub SCOPING_EL () {
72      BUTTON_EL |
73      CAPTION_EL |
74      HTML_EL |
75      TABLE_EL |
76      TABLE_CELL_EL |
77      MISC_SCOPING_EL
78    }
79    
80    sub TABLE_SCOPING_EL () {
81      HTML_EL |
82      TABLE_EL
83    }
84    
85    sub TABLE_ROWS_SCOPING_EL () {
86      HTML_EL |
87      TABLE_ROW_GROUP_EL
88    }
89    
90    sub TABLE_ROW_SCOPING_EL () {
91      HTML_EL |
92      TABLE_ROW_EL
93    }
94    
95    sub SPECIAL_EL () {
96      ADDRESS_EL |
97      BODY_EL |
98      DIV_EL |
99      END_TAG_OPTIONAL_EL |
100      FORM_EL |
101      FRAMESET_EL |
102      HEADING_EL |
103      OPTION_EL |
104      OPTGROUP_EL |
105      SELECT_EL |
106      TABLE_ROW_EL |
107      TABLE_ROW_GROUP_EL |
108      MISC_SPECIAL_EL
109    }
110    
111    my $el_category = {
112      a => A_EL | FORMATTING_EL,
113      address => ADDRESS_EL,
114      applet => MISC_SCOPING_EL,
115      area => MISC_SPECIAL_EL,
116      b => FORMATTING_EL,
117      base => MISC_SPECIAL_EL,
118      basefont => MISC_SPECIAL_EL,
119      bgsound => MISC_SPECIAL_EL,
120      big => FORMATTING_EL,
121      blockquote => MISC_SPECIAL_EL,
122      body => BODY_EL,
123      br => MISC_SPECIAL_EL,
124      button => BUTTON_EL,
125      caption => CAPTION_EL,
126      center => MISC_SPECIAL_EL,
127      col => MISC_SPECIAL_EL,
128      colgroup => MISC_SPECIAL_EL,
129      dd => DD_EL,
130      dir => MISC_SPECIAL_EL,
131      div => DIV_EL,
132      dl => MISC_SPECIAL_EL,
133      dt => DT_EL,
134      em => FORMATTING_EL,
135      embed => MISC_SPECIAL_EL,
136      fieldset => MISC_SPECIAL_EL,
137      font => FORMATTING_EL,
138      form => FORM_EL,
139      frame => MISC_SPECIAL_EL,
140      frameset => FRAMESET_EL,
141      h1 => HEADING_EL,
142      h2 => HEADING_EL,
143      h3 => HEADING_EL,
144      h4 => HEADING_EL,
145      h5 => HEADING_EL,
146      h6 => HEADING_EL,
147      head => MISC_SPECIAL_EL,
148      hr => MISC_SPECIAL_EL,
149      html => HTML_EL,
150      i => FORMATTING_EL,
151      iframe => MISC_SPECIAL_EL,
152      img => MISC_SPECIAL_EL,
153      input => MISC_SPECIAL_EL,
154      isindex => MISC_SPECIAL_EL,
155      li => LI_EL,
156      link => MISC_SPECIAL_EL,
157      listing => MISC_SPECIAL_EL,
158      marquee => MISC_SCOPING_EL,
159      menu => MISC_SPECIAL_EL,
160      meta => MISC_SPECIAL_EL,
161      nobr => NOBR_EL | FORMATTING_EL,
162      noembed => MISC_SPECIAL_EL,
163      noframes => MISC_SPECIAL_EL,
164      noscript => MISC_SPECIAL_EL,
165      object => MISC_SCOPING_EL,
166      ol => MISC_SPECIAL_EL,
167      optgroup => OPTGROUP_EL,
168      option => OPTION_EL,
169      p => P_EL,
170      param => MISC_SPECIAL_EL,
171      plaintext => MISC_SPECIAL_EL,
172      pre => MISC_SPECIAL_EL,
173      s => FORMATTING_EL,
174      script => MISC_SPECIAL_EL,
175      select => SELECT_EL,
176      small => FORMATTING_EL,
177      spacer => MISC_SPECIAL_EL,
178      strike => FORMATTING_EL,
179      strong => FORMATTING_EL,
180      style => MISC_SPECIAL_EL,
181      table => TABLE_EL,
182      tbody => TABLE_ROW_GROUP_EL,
183      td => TABLE_CELL_EL,
184      textarea => MISC_SPECIAL_EL,
185      tfoot => TABLE_ROW_GROUP_EL,
186      th => TABLE_CELL_EL,
187      thead => TABLE_ROW_GROUP_EL,
188      title => MISC_SPECIAL_EL,
189      tr => TABLE_ROW_EL,
190      tt => FORMATTING_EL,
191      u => FORMATTING_EL,
192      ul => MISC_SPECIAL_EL,
193      wbr => MISC_SPECIAL_EL,
194    };
195    
196    my $el_category_f = {
197      $MML_NS => {
198        'annotation-xml' => MML_AXML_EL,
199        mi => FOREIGN_FLOW_CONTENT_EL,
200        mo => FOREIGN_FLOW_CONTENT_EL,
201        mn => FOREIGN_FLOW_CONTENT_EL,
202        ms => FOREIGN_FLOW_CONTENT_EL,
203        mtext => FOREIGN_FLOW_CONTENT_EL,
204      },
205      $SVG_NS => {
206        foreignObject => FOREIGN_FLOW_CONTENT_EL,
207        desc => FOREIGN_FLOW_CONTENT_EL,
208        title => FOREIGN_FLOW_CONTENT_EL,
209      },
210      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
211    };
212    
213    my $svg_attr_name = {
214      attributetype => 'attributeType',
215      basefrequency => 'baseFrequency',
216      baseprofile => 'baseProfile',
217      calcmode => 'calcMode',
218      clippathunits => 'clipPathUnits',
219      contentscripttype => 'contentScriptType',
220      contentstyletype => 'contentStyleType',
221      diffuseconstant => 'diffuseConstant',
222      edgemode => 'edgeMode',
223      externalresourcesrequired => 'externalResourcesRequired',
224      fecolormatrix => 'feColorMatrix',
225      fecomposite => 'feComposite',
226      fegaussianblur => 'feGaussianBlur',
227      femorphology => 'feMorphology',
228      fetile => 'feTile',
229      filterres => 'filterRes',
230      filterunits => 'filterUnits',
231      glyphref => 'glyphRef',
232      gradienttransform => 'gradientTransform',
233      gradientunits => 'gradientUnits',
234      kernelmatrix => 'kernelMatrix',
235      kernelunitlength => 'kernelUnitLength',
236      keypoints => 'keyPoints',
237      keysplines => 'keySplines',
238      keytimes => 'keyTimes',
239      lengthadjust => 'lengthAdjust',
240      limitingconeangle => 'limitingConeAngle',
241      markerheight => 'markerHeight',
242      markerunits => 'markerUnits',
243      markerwidth => 'markerWidth',
244      maskcontentunits => 'maskContentUnits',
245      maskunits => 'maskUnits',
246      numoctaves => 'numOctaves',
247      pathlength => 'pathLength',
248      patterncontentunits => 'patternContentUnits',
249      patterntransform => 'patternTransform',
250      patternunits => 'patternUnits',
251      pointsatx => 'pointsAtX',
252      pointsaty => 'pointsAtY',
253      pointsatz => 'pointsAtZ',
254      preservealpha => 'preserveAlpha',
255      preserveaspectratio => 'preserveAspectRatio',
256      primitiveunits => 'primitiveUnits',
257      refx => 'refX',
258      refy => 'refY',
259      repeatcount => 'repeatCount',
260      repeatdur => 'repeatDur',
261      requiredextensions => 'requiredExtensions',
262      specularconstant => 'specularConstant',
263      specularexponent => 'specularExponent',
264      spreadmethod => 'spreadMethod',
265      startoffset => 'startOffset',
266      stddeviation => 'stdDeviation',
267      stitchtiles => 'stitchTiles',
268      surfacescale => 'surfaceScale',
269      systemlanguage => 'systemLanguage',
270      tablevalues => 'tableValues',
271      targetx => 'targetX',
272      targety => 'targetY',
273      textlength => 'textLength',
274      viewbox => 'viewBox',
275      viewtarget => 'viewTarget',
276      xchannelselector => 'xChannelSelector',
277      ychannelselector => 'yChannelSelector',
278      zoomandpan => 'zoomAndPan',
279  };  };
280    
281    my $foreign_attr_xname = {
282      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
283      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
284      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
285      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
286      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
287      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
288      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
289      'xml:base' => [$XML_NS, ['xml', 'base']],
290      'xml:lang' => [$XML_NS, ['xml', 'lang']],
291      'xml:space' => [$XML_NS, ['xml', 'space']],
292      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
293      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
294    };
295    
296    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
297    
298  my $c1_entity_char = {  my $c1_entity_char = {
299    0x80 => 0x20AC,    0x80 => 0x20AC,
300    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 330  my $c1_entity_char = {
330    0x9F => 0x0178,    0x9F => 0x0178,
331  }; # $c1_entity_char  }; # $c1_entity_char
332    
333  my $special_category = {  sub parse_byte_string ($$$$;$) {
334    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
335    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
336    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
337    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
338    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
339    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
340    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
341    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
342    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
343    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  
344    
345  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
346    my $self = shift->new;      my (%opt) = @_;
347    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
348      };
349      $self->{parse_error} = $onerror; # updated later by parse_char_string
350    
351      ## HTML5 encoding sniffing algorithm
352      require Message::Charset::Info;
353      my $charset;
354      my $buffer;
355      my ($char_stream, $e_status);
356    
357      SNIFFING: {
358    
359        ## Step 1
360        if (defined $charset_name) {
361          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
362    
363          ## ISSUE: Unsupported encoding is not ignored according to the spec.
364          ($char_stream, $e_status) = $charset->get_decode_handle
365              ($byte_stream, allow_error_reporting => 1,
366               allow_fallback => 1);
367          if ($char_stream) {
368            $self->{confident} = 1;
369            last SNIFFING;
370          } else {
371            ## TODO: unsupported error
372          }
373        }
374    
375        ## Step 2
376        my $byte_buffer = '';
377        for (1..1024) {
378          my $char = $byte_stream->getc;
379          last unless defined $char;
380          $byte_buffer .= $char;
381        } ## TODO: timeout
382    
383        ## Step 3
384        if ($byte_buffer =~ /^\xFE\xFF/) {
385          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
386          ($char_stream, $e_status) = $charset->get_decode_handle
387              ($byte_stream, allow_error_reporting => 1,
388               allow_fallback => 1, byte_buffer => \$byte_buffer);
389          $self->{confident} = 1;
390          last SNIFFING;
391        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
392          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
393          ($char_stream, $e_status) = $charset->get_decode_handle
394              ($byte_stream, allow_error_reporting => 1,
395               allow_fallback => 1, byte_buffer => \$byte_buffer);
396          $self->{confident} = 1;
397          last SNIFFING;
398        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
399          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
400          ($char_stream, $e_status) = $charset->get_decode_handle
401              ($byte_stream, allow_error_reporting => 1,
402               allow_fallback => 1, byte_buffer => \$byte_buffer);
403          $self->{confident} = 1;
404          last SNIFFING;
405        }
406    
407        ## Step 4
408        ## TODO: <meta charset>
409    
410        ## Step 5
411        ## TODO: from history
412    
413        ## Step 6
414        require Whatpm::Charset::UniversalCharDet;
415        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
416            ($byte_buffer);
417        if (defined $charset_name) {
418          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
419    
420          ## ISSUE: Unsupported encoding is not ignored according to the spec.
421          require Whatpm::Charset::DecodeHandle;
422          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
423              ($byte_stream);
424          ($char_stream, $e_status) = $charset->get_decode_handle
425              ($buffer, allow_error_reporting => 1,
426               allow_fallback => 1, byte_buffer => \$byte_buffer);
427          if ($char_stream) {
428            $buffer->{buffer} = $byte_buffer;
429            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
430                            value => $charset_name,
431                            level => $self->{info_level},
432                            line => 1, column => 1);
433            $self->{confident} = 0;
434            last SNIFFING;
435          }
436        }
437    
438        ## Step 7: default
439        ## TODO: Make this configurable.
440        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
441            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
442            ## detectable in the step 6.
443        require Whatpm::Charset::DecodeHandle;
444        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
445            ($byte_stream);
446        ($char_stream, $e_status)
447            = $charset->get_decode_handle ($buffer,
448                                           allow_error_reporting => 1,
449                                           allow_fallback => 1,
450                                           byte_buffer => \$byte_buffer);
451        $buffer->{buffer} = $byte_buffer;
452        !!!parse-error (type => 'sniffing:default', ## TODO: type name
453                        value => 'windows-1252',
454                        level => $self->{info_level},
455                        line => 1, column => 1);
456        $self->{confident} = 0;
457      } # SNIFFING
458    
459      $self->{input_encoding} = $charset->get_iana_name;
460      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
461        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
462                        value => $self->{input_encoding},
463                        level => $self->{unsupported_level},
464                        line => 1, column => 1);
465      } elsif (not ($e_status &
466                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
467        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
468                        value => $self->{input_encoding},
469                        level => $self->{unsupported_level},
470                        line => 1, column => 1);
471      }
472    
473      $self->{change_encoding} = sub {
474        my $self = shift;
475        $charset_name = shift;
476        my $token = shift;
477    
478        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
479        ($char_stream, $e_status) = $charset->get_decode_handle
480            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
481             byte_buffer => \ $buffer->{buffer});
482        
483        if ($char_stream) { # if supported
484          ## "Change the encoding" algorithm:
485    
486          ## Step 1    
487          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
488            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
489            ($char_stream, $e_status) = $charset->get_decode_handle
490                ($byte_stream,
491                 byte_buffer => \ $buffer->{buffer});
492          }
493          $charset_name = $charset->get_iana_name;
494          
495          ## Step 2
496          if (defined $self->{input_encoding} and
497              $self->{input_encoding} eq $charset_name) {
498            !!!parse-error (type => 'charset label:matching', ## TODO: type
499                            value => $charset_name,
500                            level => $self->{info_level});
501            $self->{confident} = 1;
502            return;
503          }
504    
505          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
506              ':'.$charset_name, level => 'w', token => $token);
507          
508          ## Step 3
509          # if (can) {
510            ## change the encoding on the fly.
511            #$self->{confident} = 1;
512            #return;
513          # }
514          
515          ## Step 4
516          throw Whatpm::HTML::RestartParser ();
517        }
518      }; # $self->{change_encoding}
519    
520      my $char_onerror = sub {
521        my (undef, $type, %opt) = @_;
522        !!!parse-error (%opt, type => $type,
523                        line => $self->{line}, column => $self->{column} + 1);
524        if ($opt{octets}) {
525          ${$opt{octets}} = "\x{FFFD}"; # relacement character
526        }
527      };
528      $char_stream->onerror ($char_onerror);
529    
530      my @args = @_; shift @args; # $s
531      my $return;
532      try {
533        $return = $self->parse_char_stream ($char_stream, @args);  
534      } catch Whatpm::HTML::RestartParser with {
535        ## NOTE: Invoked after {change_encoding}.
536    
537        $self->{input_encoding} = $charset->get_iana_name;
538        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
539          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
540                          value => $self->{input_encoding},
541                          level => $self->{unsupported_level},
542                          line => 1, column => 1);
543        } elsif (not ($e_status &
544                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
545          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
546                          value => $self->{input_encoding},
547                          level => $self->{unsupported_level},
548                          line => 1, column => 1);
549        }
550        $self->{confident} = 1;
551        $char_stream->onerror ($char_onerror);
552        $return = $self->parse_char_stream ($char_stream, @args);
553      };
554      return $return;
555    } # parse_byte_stream
556    
557    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
558    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
559    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
560    ## because the core part of our HTML parser expects a string of character,
561    ## not a string of bytes or code units or anything which might contain a BOM.
562    ## Therefore, any parser interface that accepts a string of bytes,
563    ## such as |parse_byte_string| in this module, must ensure that it does
564    ## strip the BOM and never strip any ZWNBSP.
565    
566    sub parse_char_string ($$$;$) {
567      my $self = shift;
568      require utf8;
569      my $s = ref $_[0] ? $_[0] : \($_[0]);
570      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
571      return $self->parse_char_stream ($input, @_[1..$#_]);
572    } # parse_char_string
573    *parse_string = \&parse_char_string;
574    
575    sub parse_char_stream ($$$;$) {
576      my $self = ref $_[0] ? shift : shift->new;
577      my $input = $_[0];
578    $self->{document} = $_[1];    $self->{document} = $_[1];
579      @{$self->{document}->child_nodes} = ();
580    
581    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
582    
583      $self->{confident} = 1 unless exists $self->{confident};
584      $self->{document}->input_encoding ($self->{input_encoding})
585          if defined $self->{input_encoding};
586    
587    my $i = 0;    my $i = 0;
588    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
589    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
590    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
591      my $self = shift;      my $self = shift;
592    
593      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
594      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
595    
596      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
597      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
598      $column++;        $char = $self->{next_next_char};
599          delete $self->{next_next_char};
600        } else {
601          $char = $input->getc;
602        }
603        $self->{next_char} = -1 and return unless defined $char;
604        $self->{next_char} = ord $char;
605    
606        ($self->{line_prev}, $self->{column_prev})
607            = ($self->{line}, $self->{column});
608        $self->{column}++;
609            
610      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
611        $line++;        !!!cp ('j1');
612        $column = 0;        $self->{line}++;
613      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
614        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
615        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
616        $line++;        my $next = $input->getc;
617        $column = 0;        if (defined $next and $next ne "\x0A") {
618      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
619        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
620      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
621          $self->{line}++;
622          $self->{column} = 0;
623        } elsif ($self->{next_char} > 0x10FFFF) {
624          !!!cp ('j3');
625          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
626        } elsif ($self->{next_char} == 0x0000) { # NULL
627          !!!cp ('j4');
628        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
629        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
630        } elsif ($self->{next_char} <= 0x0008 or
631                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
632                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
633                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
634                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
635                 {
636                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
637                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
638                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
639                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
640                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
641                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
642                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
643                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
644                  0x10FFFE => 1, 0x10FFFF => 1,
645                 }->{$self->{next_char}}) {
646          !!!cp ('j5');
647          !!!parse-error (type => 'control char', level => $self->{must_level});
648    ## TODO: error type documentation
649      }      }
650    };    };
651    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
652    $self->{next_input_character} = -1;    $self->{next_char} = -1;
653    
654    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
655      my (%opt) = @_;      my (%opt) = @_;
656      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
657        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
658        warn "Parse error ($opt{type}) at line $line column $column\n";
659    };    };
660    $self->{parse_error} = sub {    $self->{parse_error} = sub {
661      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
662    };    };
663    
664    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 666  sub parse_string ($$$;$) {
666    $self->_construct_tree;    $self->_construct_tree;
667    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
668    
669      delete $self->{parse_error}; # remove loop
670    
671    return $self->{document};    return $self->{document};
672  } # parse_string  } # parse_char_stream
673    
674  sub new ($) {  sub new ($) {
675    my $class = shift;    my $class = shift;
676    my $self = bless {}, $class;    my $self = bless {
677    $self->{set_next_input_character} = sub {      must_level => 'm',
678      $self->{next_input_character} = -1;      should_level => 's',
679        good_level => 'w',
680        warn_level => 'w',
681        info_level => 'i',
682        unsupported_level => 'u',
683      }, $class;
684      $self->{set_next_char} = sub {
685        $self->{next_char} = -1;
686    };    };
687    $self->{parse_error} = sub {    $self->{parse_error} = sub {
688      #      #
689    };    };
690      $self->{change_encoding} = sub {
691        # if ($_[0] is a supported encoding) {
692        #   run "change the encoding" algorithm;
693        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
694        # }
695      };
696      $self->{application_cache_selection} = sub {
697        #
698      };
699    return $self;    return $self;
700  } # new  } # new
701    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 708  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
708  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
709  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
710    
711    sub DATA_STATE () { 0 }
712    sub ENTITY_DATA_STATE () { 1 }
713    sub TAG_OPEN_STATE () { 2 }
714    sub CLOSE_TAG_OPEN_STATE () { 3 }
715    sub TAG_NAME_STATE () { 4 }
716    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
717    sub ATTRIBUTE_NAME_STATE () { 6 }
718    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
719    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
720    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
721    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
722    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
723    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
724    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
725    sub COMMENT_START_STATE () { 14 }
726    sub COMMENT_START_DASH_STATE () { 15 }
727    sub COMMENT_STATE () { 16 }
728    sub COMMENT_END_STATE () { 17 }
729    sub COMMENT_END_DASH_STATE () { 18 }
730    sub BOGUS_COMMENT_STATE () { 19 }
731    sub DOCTYPE_STATE () { 20 }
732    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
733    sub DOCTYPE_NAME_STATE () { 22 }
734    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
735    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
736    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
737    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
738    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
739    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
740    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
741    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
742    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
743    sub BOGUS_DOCTYPE_STATE () { 32 }
744    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
745    sub SELF_CLOSING_START_TAG_STATE () { 34 }
746    sub CDATA_BLOCK_STATE () { 35 }
747    
748    sub DOCTYPE_TOKEN () { 1 }
749    sub COMMENT_TOKEN () { 2 }
750    sub START_TAG_TOKEN () { 3 }
751    sub END_TAG_TOKEN () { 4 }
752    sub END_OF_FILE_TOKEN () { 5 }
753    sub CHARACTER_TOKEN () { 6 }
754    
755    sub AFTER_HTML_IMS () { 0b100 }
756    sub HEAD_IMS ()       { 0b1000 }
757    sub BODY_IMS ()       { 0b10000 }
758    sub BODY_TABLE_IMS () { 0b100000 }
759    sub TABLE_IMS ()      { 0b1000000 }
760    sub ROW_IMS ()        { 0b10000000 }
761    sub BODY_AFTER_IMS () { 0b100000000 }
762    sub FRAME_IMS ()      { 0b1000000000 }
763    sub SELECT_IMS ()     { 0b10000000000 }
764    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
765        ## NOTE: "in foreign content" insertion mode is special; it is combined
766        ## with the secondary insertion mode.  In this parser, they are stored
767        ## together in the bit-or'ed form.
768    
769    ## NOTE: "initial" and "before html" insertion modes have no constants.
770    
771    ## NOTE: "after after body" insertion mode.
772    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
773    
774    ## NOTE: "after after frameset" insertion mode.
775    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
776    
777    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
778    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
779    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
780    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
781    sub IN_BODY_IM () { BODY_IMS }
782    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
783    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
784    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
785    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
786    sub IN_TABLE_IM () { TABLE_IMS }
787    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
788    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
789    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
790    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
791    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
792    sub IN_COLUMN_GROUP_IM () { 0b10 }
793    
794  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
795    
796  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
797    my $self = shift;    my $self = shift;
798    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
799    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
800    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
801    undef $self->{current_attribute};    undef $self->{current_attribute};
802    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
803    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
804      delete $self->{self_closing};
805    $self->{char} = [];    $self->{char} = [];
806    # $self->{next_input_character}    # $self->{next_char}
807    !!!next-input-character;    !!!next-input-character;
808    $self->{token} = [];    $self->{token} = [];
809    # $self->{escape}    # $self->{escape}
810  } # _initialize_tokenizer  } # _initialize_tokenizer
811    
812  ## A token has:  ## A token has:
813  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
814  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
815  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
816  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
817  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
818  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
819  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
820  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
821    ##        ->{name}
822    ##        ->{value}
823    ##        ->{has_reference} == 1 or 0
824    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
825    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
826    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
827    ##     while the token is pushed back to the stack.
828    
829    ## ISSUE: "When a DOCTYPE token is created, its
830    ## <i>self-closing flag</i> must be unset (its other state is that it
831    ## be set), and its attributes list must be empty.": Wrong subject?
832    
833  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
834    
# Line 194  sub _initialize_tokenizer ($) { Line 838  sub _initialize_tokenizer ($) {
838  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
839  ## and removed from the list.  ## and removed from the list.
840    
841    ## NOTE: HTML5 "Writing HTML documents" section, applied to
842    ## documents and not to user agents and conformance checkers,
843    ## contains some requirements that are not detected by the
844    ## parsing algorithm:
845    ## - Some requirements on character encoding declarations. ## TODO
846    ## - "Elements MUST NOT contain content that their content model disallows."
847    ##   ... Some are parse error, some are not (will be reported by c.c.).
848    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
849    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
850    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
851    
852    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
853    ## be detected by the HTML5 parsing algorithm:
854    ## - Text,
855    
856  sub _get_next_token ($) {  sub _get_next_token ($) {
857    my $self = shift;    my $self = shift;
858    
859      if ($self->{self_closing}) {
860        !!!parse-error (type => 'nestc', token => $self->{current_token});
861        ## NOTE: The |self_closing| flag is only set by start tag token.
862        ## In addition, when a start tag token is emitted, it is always set to
863        ## |current_token|.
864        delete $self->{self_closing};
865      }
866    
867    if (@{$self->{token}}) {    if (@{$self->{token}}) {
868        $self->{self_closing} = $self->{token}->[0]->{self_closing};
869      return shift @{$self->{token}};      return shift @{$self->{token}};
870    }    }
871    
872    A: {    A: {
873      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
874        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
875          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
876            $self->{state} = 'entity data';              not $self->{escape}) {
877              !!!cp (1);
878              $self->{state} = ENTITY_DATA_STATE;
879            !!!next-input-character;            !!!next-input-character;
880            redo A;            redo A;
881          } else {          } else {
882              !!!cp (2);
883            #            #
884          }          }
885        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
886          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
887            unless ($self->{escape}) {            unless ($self->{escape}) {
888              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
889                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
890                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
891                  !!!cp (3);
892                $self->{escape} = 1;                $self->{escape} = 1;
893                } else {
894                  !!!cp (4);
895              }              }
896              } else {
897                !!!cp (5);
898            }            }
899          }          }
900                    
901          #          #
902        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
903          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
904              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
905               not $self->{escape})) {               not $self->{escape})) {
906            $self->{state} = 'tag open';            !!!cp (6);
907              $self->{state} = TAG_OPEN_STATE;
908            !!!next-input-character;            !!!next-input-character;
909            redo A;            redo A;
910          } else {          } else {
911              !!!cp (7);
912            #            #
913          }          }
914        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
915          if ($self->{escape} and          if ($self->{escape} and
916              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
917            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
918                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
919                !!!cp (8);
920              delete $self->{escape};              delete $self->{escape};
921              } else {
922                !!!cp (9);
923            }            }
924            } else {
925              !!!cp (10);
926          }          }
927                    
928          #          #
929        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
930          !!!emit ({type => 'end-of-file'});          !!!cp (11);
931            !!!emit ({type => END_OF_FILE_TOKEN,
932                      line => $self->{line}, column => $self->{column}});
933          last A; ## TODO: ok?          last A; ## TODO: ok?
934          } else {
935            !!!cp (12);
936        }        }
937        # Anything else        # Anything else
938        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
939                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
940                       line => $self->{line}, column => $self->{column},
941                      };
942        ## Stay in the data state        ## Stay in the data state
943        !!!next-input-character;        !!!next-input-character;
944    
945        !!!emit ($token);        !!!emit ($token);
946    
947        redo A;        redo A;
948      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
949        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
950    
951          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
952                
953        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
954    
955        $self->{state} = 'data';        $self->{state} = DATA_STATE;
956        # next-input-character is already done        # next-input-character is already done
957    
958        unless (defined $token) {        unless (defined $token) {
959          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
960            !!!emit ({type => CHARACTER_TOKEN, data => '&',
961                      line => $l, column => $c,
962                     });
963        } else {        } else {
964            !!!cp (14);
965          !!!emit ($token);          !!!emit ($token);
966        }        }
967    
968        redo A;        redo A;
969      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
970        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
971          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
972              !!!cp (15);
973            !!!next-input-character;            !!!next-input-character;
974            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
975            redo A;            redo A;
976          } else {          } else {
977              !!!cp (16);
978            ## reconsume            ## reconsume
979            $self->{state} = 'data';            $self->{state} = DATA_STATE;
980    
981            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
982                        line => $self->{line_prev},
983                        column => $self->{column_prev},
984                       });
985    
986            redo A;            redo A;
987          }          }
988        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
989          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
990            $self->{state} = 'markup declaration open';            !!!cp (17);
991              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
992            !!!next-input-character;            !!!next-input-character;
993            redo A;            redo A;
994          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
995            $self->{state} = 'close tag open';            !!!cp (18);
996              $self->{state} = CLOSE_TAG_OPEN_STATE;
997            !!!next-input-character;            !!!next-input-character;
998            redo A;            redo A;
999          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1000                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1001              !!!cp (19);
1002            $self->{current_token}            $self->{current_token}
1003              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1004                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1005            $self->{state} = 'tag name';                 line => $self->{line_prev},
1006                   column => $self->{column_prev}};
1007              $self->{state} = TAG_NAME_STATE;
1008            !!!next-input-character;            !!!next-input-character;
1009            redo A;            redo A;
1010          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1011                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1012            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1013                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1014            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1015                                        line => $self->{line_prev},
1016                                        column => $self->{column_prev}};
1017              $self->{state} = TAG_NAME_STATE;
1018            !!!next-input-character;            !!!next-input-character;
1019            redo A;            redo A;
1020          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1021            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1022            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1023                              line => $self->{line_prev},
1024                              column => $self->{column_prev});
1025              $self->{state} = DATA_STATE;
1026            !!!next-input-character;            !!!next-input-character;
1027    
1028            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1029                        line => $self->{line_prev},
1030                        column => $self->{column_prev},
1031                       });
1032    
1033            redo A;            redo A;
1034          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1035            !!!parse-error (type => 'pio');            !!!cp (22);
1036            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1037            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1038                              column => $self->{column_prev});
1039              $self->{state} = BOGUS_COMMENT_STATE;
1040              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1041                                        line => $self->{line_prev},
1042                                        column => $self->{column_prev},
1043                                       };
1044              ## $self->{next_char} is intentionally left as is
1045            redo A;            redo A;
1046          } else {          } else {
1047            !!!parse-error (type => 'bare stago');            !!!cp (23);
1048            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1049                              line => $self->{line_prev},
1050                              column => $self->{column_prev});
1051              $self->{state} = DATA_STATE;
1052            ## reconsume            ## reconsume
1053    
1054            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1055                        line => $self->{line_prev},
1056                        column => $self->{column_prev},
1057                       });
1058    
1059            redo A;            redo A;
1060          }          }
1061        } else {        } else {
1062          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1063        }        }
1064      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1065          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1066        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1067          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1068    
1069            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1070            my @next_char;            my @next_char;
1071            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1072              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1073              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1074              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1075              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1076                  !!!cp (24);
1077                !!!next-input-character;                !!!next-input-character;
1078                next TAGNAME;                next TAGNAME;
1079              } else {              } else {
1080                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1081                  $self->{next_char} = shift @next_char; # reconsume
1082                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1083                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1084    
1085                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1086                            line => $l, column => $c,
1087                           });
1088        
1089                redo A;                redo A;
1090              }              }
1091            }            }
1092            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1093                
1094            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1095                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1096                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1097                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1098                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1099                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1100                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1101                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1102              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1103                $self->{next_char} = shift @next_char; # reconsume
1104              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1105              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1106              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1107                          line => $l, column => $c,
1108                         });
1109              redo A;              redo A;
1110            } else {            } else {
1111              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1112                $self->{next_char} = shift @next_char;
1113              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1114              # and consume...              # and consume...
1115            }            }
1116          } else {          } else {
1117            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1118              !!!cp (28);
1119            # next-input-character is already done            # next-input-character is already done
1120            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1121            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1122                        line => $l, column => $c,
1123                       });
1124            redo A;            redo A;
1125          }          }
1126        }        }
1127                
1128        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1129            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1130          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1131                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1132          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1133          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1134          redo A;                 line => $l, column => $c};
1135        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1136                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1137          $self->{current_token} = {type => 'end tag',          redo A;
1138                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1139          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1140          !!!next-input-character;          !!!cp (30);
1141          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1142        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1143          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1144          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1145            !!!next-input-character;
1146            redo A;
1147          } elsif ($self->{next_char} == 0x003E) { # >
1148            !!!cp (31);
1149            !!!parse-error (type => 'empty end tag',
1150                            line => $self->{line_prev}, ## "<" in "</>"
1151                            column => $self->{column_prev} - 1);
1152            $self->{state} = DATA_STATE;
1153          !!!next-input-character;          !!!next-input-character;
1154          redo A;          redo A;
1155        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1156            !!!cp (32);
1157          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1158          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1159          # reconsume          # reconsume
1160    
1161          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1162                      line => $l, column => $c,
1163                     });
1164    
1165          redo A;          redo A;
1166        } else {        } else {
1167            !!!cp (33);
1168          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1169          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1170          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1171          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1172        }                                    column => $self->{column_prev} - 1,
1173      } elsif ($self->{state} eq 'tag name') {                                   };
1174        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1175            $self->{next_input_character} == 0x000A or # LF          redo A;
1176            $self->{next_input_character} == 0x000B or # VT        }
1177            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1178            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1179          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1180          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1181          redo A;            $self->{next_char} == 0x000C or # FF
1182        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1183          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1184            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1185                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1186            redo A;
1187          } elsif ($self->{next_char} == 0x003E) { # >
1188            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189              !!!cp (35);
1190            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1191          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1192            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1193            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1194              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1195            }            #  !!! cp (36);
1196              #  !!! parse-error (type => 'end tag attribute');
1197              #} else {
1198                !!!cp (37);
1199              #}
1200          } else {          } else {
1201            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1202          }          }
1203          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1204          !!!next-input-character;          !!!next-input-character;
1205    
1206          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1207    
1208          redo A;          redo A;
1209        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1210                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1211          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1212            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1213            # start tag or end tag            # start tag or end tag
1214          ## Stay in this state          ## Stay in this state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1218          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1219          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1220            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1221            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1222          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1223            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1224            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1225              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1226            }            #  !!! cp (40);
1227              #  !!! parse-error (type => 'end tag attribute');
1228              #} else {
1229                !!!cp (41);
1230              #}
1231          } else {          } else {
1232            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1233          }          }
1234          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1235          # reconsume          # reconsume
1236    
1237          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1238    
1239          redo A;          redo A;
1240        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1241            !!!cp (42);
1242            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1243          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} 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  
1244          redo A;          redo A;
1245        } else {        } else {
1246          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1247            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1248            # start tag or end tag            # start tag or end tag
1249          ## Stay in the state          ## Stay in the state
1250          !!!next-input-character;          !!!next-input-character;
1251          redo A;          redo A;
1252        }        }
1253      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1254        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1255            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1256            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1257            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1258            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1259            !!!cp (45);
1260          ## Stay in the state          ## Stay in the state
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1264          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1265            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1266            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1267          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1268            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1269            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1270                !!!cp (47);
1271              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1272              } else {
1273                !!!cp (48);
1274            }            }
1275          } else {          } else {
1276            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1277          }          }
1278          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1279          !!!next-input-character;          !!!next-input-character;
1280    
1281          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1282    
1283          redo A;          redo A;
1284        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1285                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1286          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1287                                value => ''};          $self->{current_attribute}
1288          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1289                   value => '',
1290                   line => $self->{line}, column => $self->{column}};
1291            $self->{state} = ATTRIBUTE_NAME_STATE;
1292            !!!next-input-character;
1293            redo A;
1294          } elsif ($self->{next_char} == 0x002F) { # /
1295            !!!cp (50);
1296            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1297          !!!next-input-character;          !!!next-input-character;
1298          redo A;          redo A;
1299        } 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) {  
1300          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1301          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1302            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1303            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1304          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1305            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1306            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1307                !!!cp (53);
1308              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1309              } else {
1310                !!!cp (54);
1311            }            }
1312          } else {          } else {
1313            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1314          }          }
1315          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1316          # reconsume          # reconsume
1317    
1318          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1319    
1320          redo A;          redo A;
1321        } else {        } else {
1322          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1323                                value => ''};               0x0022 => 1, # "
1324          $self->{state} = 'attribute name';               0x0027 => 1, # '
1325                 0x003D => 1, # =
1326                }->{$self->{next_char}}) {
1327              !!!cp (55);
1328              !!!parse-error (type => 'bad attribute name');
1329            } else {
1330              !!!cp (56);
1331            }
1332            $self->{current_attribute}
1333                = {name => chr ($self->{next_char}),
1334                   value => '',
1335                   line => $self->{line}, column => $self->{column}};
1336            $self->{state} = ATTRIBUTE_NAME_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        }        }
1340      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1341        my $before_leave = sub {        my $before_leave = sub {
1342          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1343              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1344            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1345              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1346            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1347          } else {          } else {
1348              !!!cp (58);
1349            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1350              = $self->{current_attribute};              = $self->{current_attribute};
1351          }          }
1352        }; # $before_leave        }; # $before_leave
1353    
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1357            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1358            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1359            !!!cp (59);
1360          $before_leave->();          $before_leave->();
1361          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1362          !!!next-input-character;          !!!next-input-character;
1363          redo A;          redo A;
1364        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1365            !!!cp (60);
1366          $before_leave->();          $before_leave->();
1367          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1368          !!!next-input-character;          !!!next-input-character;
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1371          $before_leave->();          $before_leave->();
1372          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1373            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1374            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1375          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1376              !!!cp (62);
1377            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1378            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1379              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1381  sub _get_next_token ($) {
1381          } else {          } else {
1382            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1383          }          }
1384          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386    
1387          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1388    
1389          redo A;          redo A;
1390        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1391                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1392          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1393            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1394          ## Stay in the state          ## Stay in the state
1395          !!!next-input-character;          !!!next-input-character;
1396          redo A;          redo A;
1397        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1398            !!!cp (64);
1399          $before_leave->();          $before_leave->();
1400            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1401          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} 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  
1402          redo A;          redo A;
1403        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1404          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1405          $before_leave->();          $before_leave->();
1406          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1407            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1408            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1409          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1410            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1411            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1412                !!!cp (67);
1413              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1414              } else {
1415                ## NOTE: This state should never be reached.
1416                !!!cp (68);
1417            }            }
1418          } else {          } else {
1419            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1420          }          }
1421          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1422          # reconsume          # reconsume
1423    
1424          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1425    
1426          redo A;          redo A;
1427        } else {        } else {
1428          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1429                $self->{next_char} == 0x0027) { # '
1430              !!!cp (69);
1431              !!!parse-error (type => 'bad attribute name');
1432            } else {
1433              !!!cp (70);
1434            }
1435            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1436          ## Stay in the state          ## Stay in the state
1437          !!!next-input-character;          !!!next-input-character;
1438          redo A;          redo A;
1439        }        }
1440      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1441        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1442            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1443            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1444            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1445            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1446            !!!cp (71);
1447          ## Stay in the state          ## Stay in the state
1448          !!!next-input-character;          !!!next-input-character;
1449          redo A;          redo A;
1450        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1451          $self->{state} = 'before attribute value';          !!!cp (72);
1452            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1453          !!!next-input-character;          !!!next-input-character;
1454          redo A;          redo A;
1455        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1456          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1457            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1458            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1459          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1460            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1461            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1462                !!!cp (74);
1463              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1464              } else {
1465                ## NOTE: This state should never be reached.
1466                !!!cp (75);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1470          }          }
1471          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473    
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1478                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1479          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1480                                value => ''};          $self->{current_attribute}
1481          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484            $self->{state} = ATTRIBUTE_NAME_STATE;
1485            !!!next-input-character;
1486            redo A;
1487          } elsif ($self->{next_char} == 0x002F) { # /
1488            !!!cp (77);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } 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) {  
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1495            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1496            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1497          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1500                !!!cp (80);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502              } else {
1503                ## NOTE: This state should never be reached.
1504                !!!cp (81);
1505            }            }
1506          } else {          } else {
1507            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1508          }          }
1509          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1510          # reconsume          # reconsume
1511    
1512          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1513    
1514          redo A;          redo A;
1515        } else {        } else {
1516          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1517                                value => ''};          $self->{current_attribute}
1518          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1519                   value => '',
1520                   line => $self->{line}, column => $self->{column}};
1521            $self->{state} = ATTRIBUTE_NAME_STATE;
1522          !!!next-input-character;          !!!next-input-character;
1523          redo A;                  redo A;        
1524        }        }
1525      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1526        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1527            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1528            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1529            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1530            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1531            !!!cp (83);
1532          ## Stay in the state          ## Stay in the state
1533          !!!next-input-character;          !!!next-input-character;
1534          redo A;          redo A;
1535        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1536          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1537            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539          redo A;          redo A;
1540        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1541          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1542            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1543          ## reconsume          ## reconsume
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1546          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1547            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1551          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1552            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1553            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1554          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1555            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1556            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1557                !!!cp (88);
1558              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1559              } else {
1560                ## NOTE: This state should never be reached.
1561                !!!cp (89);
1562            }            }
1563          } else {          } else {
1564            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1565          }          }
1566          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1567          !!!next-input-character;          !!!next-input-character;
1568    
1569          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1570    
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1573          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1574          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1575            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1576            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1577          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1578            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1579            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1580                !!!cp (91);
1581              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1582              } else {
1583                ## NOTE: This state should never be reached.
1584                !!!cp (92);
1585            }            }
1586          } else {          } else {
1587            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1588          }          }
1589          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1590          ## reconsume          ## reconsume
1591    
1592          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1593    
1594          redo A;          redo A;
1595        } else {        } else {
1596          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1597          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1598              !!!parse-error (type => 'bad attribute value');
1599            } else {
1600              !!!cp (94);
1601            }
1602            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1603            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605          redo A;          redo A;
1606        }        }
1607      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1608        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1609          $self->{state} = 'before attribute name';          !!!cp (95);
1610            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1611          !!!next-input-character;          !!!next-input-character;
1612          redo A;          redo A;
1613        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1614          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1615          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1616            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;          redo A;
1619        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1620          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1621          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1622            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1623            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1624          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1625            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1626            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1627                !!!cp (98);
1628              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1629              } else {
1630                ## NOTE: This state should never be reached.
1631                !!!cp (99);
1632            }            }
1633          } else {          } else {
1634            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1635          }          }
1636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1637          ## reconsume          ## reconsume
1638    
1639          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1640    
1641          redo A;          redo A;
1642        } else {        } else {
1643          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1644            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1645          ## Stay in the state          ## Stay in the state
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        }        }
1649      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1650        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1651          $self->{state} = 'before attribute name';          !!!cp (101);
1652            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1653          !!!next-input-character;          !!!next-input-character;
1654          redo A;          redo A;
1655        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1656          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1657          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1658            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660          redo A;          redo A;
1661        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1662          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1663          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1664            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1665            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1666          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1667            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1668            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1669                !!!cp (104);
1670              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1671              } else {
1672                ## NOTE: This state should never be reached.
1673                !!!cp (105);
1674            }            }
1675          } else {          } else {
1676            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1677          }          }
1678          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1679          ## reconsume          ## reconsume
1680    
1681          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1682    
1683          redo A;          redo A;
1684        } else {        } else {
1685          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1686            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1687          ## Stay in the state          ## Stay in the state
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        }        }
1691      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1692        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1693            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1694            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1695            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1696            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1697          $self->{state} = 'before attribute name';          !!!cp (107);
1698          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1699          redo A;          !!!next-input-character;
1700        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1701          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1702          $self->{state} = 'entity in attribute value';          !!!cp (108);
1703          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1704          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1705        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1706          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1707            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1708                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1709              !!!cp (109);
1710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1711          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1713            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1714                !!!cp (110);
1715              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1716              } else {
1717                ## NOTE: This state should never be reached.
1718                !!!cp (111);
1719            }            }
1720          } else {          } else {
1721            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1722          }          }
1723          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1724          !!!next-input-character;          !!!next-input-character;
1725    
1726          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1727    
1728          redo A;          redo A;
1729        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1730          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1731          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1732            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1733            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1734          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1735            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1736            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1737                !!!cp (113);
1738              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1739              } else {
1740                ## NOTE: This state should never be reached.
1741                !!!cp (114);
1742            }            }
1743          } else {          } else {
1744            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1745          }          }
1746          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1747          ## reconsume          ## reconsume
1748    
1749          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1750    
1751          redo A;          redo A;
1752        } else {        } else {
1753          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1754                 0x0022 => 1, # "
1755                 0x0027 => 1, # '
1756                 0x003D => 1, # =
1757                }->{$self->{next_char}}) {
1758              !!!cp (115);
1759              !!!parse-error (type => 'bad attribute value');
1760            } else {
1761              !!!cp (116);
1762            }
1763            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1764          ## Stay in the state          ## Stay in the state
1765          !!!next-input-character;          !!!next-input-character;
1766          redo A;          redo A;
1767        }        }
1768      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1769        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1770              (1,
1771               $self->{last_attribute_value_state}
1772                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1773               $self->{last_attribute_value_state}
1774                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1775               -1);
1776    
1777        unless (defined $token) {        unless (defined $token) {
1778            !!!cp (117);
1779          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1780        } else {        } else {
1781            !!!cp (118);
1782          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1783            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1784          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1785        }        }
1786    
1787        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1788        # next-input-character is already done        # next-input-character is already done
1789        redo A;        redo A;
1790      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1791          if ($self->{next_char} == 0x0009 or # HT
1792              $self->{next_char} == 0x000A or # LF
1793              $self->{next_char} == 0x000B or # VT
1794              $self->{next_char} == 0x000C or # FF
1795              $self->{next_char} == 0x0020) { # SP
1796            !!!cp (118);
1797            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1798            !!!next-input-character;
1799            redo A;
1800          } elsif ($self->{next_char} == 0x003E) { # >
1801            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1802              !!!cp (119);
1803              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1804            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1805              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1806              if ($self->{current_token}->{attributes}) {
1807                !!!cp (120);
1808                !!!parse-error (type => 'end tag attribute');
1809              } else {
1810                ## NOTE: This state should never be reached.
1811                !!!cp (121);
1812              }
1813            } else {
1814              die "$0: $self->{current_token}->{type}: Unknown token type";
1815            }
1816            $self->{state} = DATA_STATE;
1817            !!!next-input-character;
1818    
1819            !!!emit ($self->{current_token}); # start tag or end tag
1820    
1821            redo A;
1822          } elsif ($self->{next_char} == 0x002F) { # /
1823            !!!cp (122);
1824            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1825            !!!next-input-character;
1826            redo A;
1827          } elsif ($self->{next_char} == -1) {
1828            !!!parse-error (type => 'unclosed tag');
1829            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1830              !!!cp (122.3);
1831              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1832            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1833              if ($self->{current_token}->{attributes}) {
1834                !!!cp (122.1);
1835                !!!parse-error (type => 'end tag attribute');
1836              } else {
1837                ## NOTE: This state should never be reached.
1838                !!!cp (122.2);
1839              }
1840            } else {
1841              die "$0: $self->{current_token}->{type}: Unknown token type";
1842            }
1843            $self->{state} = DATA_STATE;
1844            ## Reconsume.
1845            !!!emit ($self->{current_token}); # start tag or end tag
1846            redo A;
1847          } else {
1848            !!!cp ('124.1');
1849            !!!parse-error (type => 'no space between attributes');
1850            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1851            ## reconsume
1852            redo A;
1853          }
1854        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1855          if ($self->{next_char} == 0x003E) { # >
1856            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1857              !!!cp ('124.2');
1858              !!!parse-error (type => 'nestc', token => $self->{current_token});
1859              ## TODO: Different type than slash in start tag
1860              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1861              if ($self->{current_token}->{attributes}) {
1862                !!!cp ('124.4');
1863                !!!parse-error (type => 'end tag attribute');
1864              } else {
1865                !!!cp ('124.5');
1866              }
1867              ## TODO: Test |<title></title/>|
1868            } else {
1869              !!!cp ('124.3');
1870              $self->{self_closing} = 1;
1871            }
1872    
1873            $self->{state} = DATA_STATE;
1874            !!!next-input-character;
1875    
1876            !!!emit ($self->{current_token}); # start tag or end tag
1877    
1878            redo A;
1879          } elsif ($self->{next_char} == -1) {
1880            !!!parse-error (type => 'unclosed tag');
1881            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1882              !!!cp (124.7);
1883              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1884            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1885              if ($self->{current_token}->{attributes}) {
1886                !!!cp (124.5);
1887                !!!parse-error (type => 'end tag attribute');
1888              } else {
1889                ## NOTE: This state should never be reached.
1890                !!!cp (124.6);
1891              }
1892            } else {
1893              die "$0: $self->{current_token}->{type}: Unknown token type";
1894            }
1895            $self->{state} = DATA_STATE;
1896            ## Reconsume.
1897            !!!emit ($self->{current_token}); # start tag or end tag
1898            redo A;
1899          } else {
1900            !!!cp ('124.4');
1901            !!!parse-error (type => 'nestc');
1902            ## TODO: This error type is wrong.
1903            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1904            ## Reconsume.
1905            redo A;
1906          }
1907        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1908        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1909                
1910        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1911          #my $token = {type => COMMENT_TOKEN, data => ''};
1912    
1913        BC: {        BC: {
1914          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1915            $self->{state} = 'data';            !!!cp (124);
1916              $self->{state} = DATA_STATE;
1917            !!!next-input-character;            !!!next-input-character;
1918    
1919            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1920    
1921            redo A;            redo A;
1922          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1923            $self->{state} = 'data';            !!!cp (125);
1924              $self->{state} = DATA_STATE;
1925            ## reconsume            ## reconsume
1926    
1927            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1928    
1929            redo A;            redo A;
1930          } else {          } else {
1931            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1932              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1933            !!!next-input-character;            !!!next-input-character;
1934            redo BC;            redo BC;
1935          }          }
1936        } # BC        } # BC
1937      } elsif ($self->{state} eq 'markup declaration open') {  
1938          die "$0: _get_next_token: unexpected case [BC]";
1939        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1940        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1941    
1942          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1943    
1944        my @next_char;        my @next_char;
1945        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1946                
1947        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1948          !!!next-input-character;          !!!next-input-character;
1949          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1950          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1951            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1952            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1953                                        line => $l, column => $c,
1954                                       };
1955              $self->{state} = COMMENT_START_STATE;
1956            !!!next-input-character;            !!!next-input-character;
1957            redo A;            redo A;
1958            } else {
1959              !!!cp (128);
1960          }          }
1961        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1962                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1963          !!!next-input-character;          !!!next-input-character;
1964          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1965          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1966              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1967            !!!next-input-character;            !!!next-input-character;
1968            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1969            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1970                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1971              !!!next-input-character;              !!!next-input-character;
1972              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1973              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1974                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1975                !!!next-input-character;                !!!next-input-character;
1976                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1977                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1978                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1979                  !!!next-input-character;                  !!!next-input-character;
1980                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1981                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1982                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1983                    !!!next-input-character;                    !!!next-input-character;
1984                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1985                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1986                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1987                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1988                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1989                        $self->{state} = DOCTYPE_STATE;
1990                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1991                                                  quirks => 1,
1992                                                  line => $l, column => $c,
1993                                                 };
1994                      !!!next-input-character;                      !!!next-input-character;
1995                      redo A;                      redo A;
1996                      } else {
1997                        !!!cp (130);
1998                    }                    }
1999                    } else {
2000                      !!!cp (131);
2001                  }                  }
2002                  } else {
2003                    !!!cp (132);
2004                }                }
2005                } else {
2006                  !!!cp (133);
2007              }              }
2008              } else {
2009                !!!cp (134);
2010            }            }
2011            } else {
2012              !!!cp (135);
2013            }
2014          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2015                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2016                   $self->{next_char} == 0x005B) { # [
2017            !!!next-input-character;
2018            push @next_char, $self->{next_char};
2019            if ($self->{next_char} == 0x0043) { # C
2020              !!!next-input-character;
2021              push @next_char, $self->{next_char};
2022              if ($self->{next_char} == 0x0044) { # D
2023                !!!next-input-character;
2024                push @next_char, $self->{next_char};
2025                if ($self->{next_char} == 0x0041) { # A
2026                  !!!next-input-character;
2027                  push @next_char, $self->{next_char};
2028                  if ($self->{next_char} == 0x0054) { # T
2029                    !!!next-input-character;
2030                    push @next_char, $self->{next_char};
2031                    if ($self->{next_char} == 0x0041) { # A
2032                      !!!next-input-character;
2033                      push @next_char, $self->{next_char};
2034                      if ($self->{next_char} == 0x005B) { # [
2035                        !!!cp (135.1);
2036                        $self->{state} = CDATA_BLOCK_STATE;
2037                        !!!next-input-character;
2038                        redo A;
2039                      } else {
2040                        !!!cp (135.2);
2041                      }
2042                    } else {
2043                      !!!cp (135.3);
2044                    }
2045                  } else {
2046                    !!!cp (135.4);                
2047                  }
2048                } else {
2049                  !!!cp (135.5);
2050                }
2051              } else {
2052                !!!cp (135.6);
2053              }
2054            } else {
2055              !!!cp (135.7);
2056          }          }
2057          } else {
2058            !!!cp (136);
2059        }        }
2060    
2061        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2062        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2063        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2064        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2065          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2066                                    line => $l, column => $c,
2067                                   };
2068        redo A;        redo A;
2069                
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2071        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
2072      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2073        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2074          $self->{state} = 'comment start dash';          !!!cp (137);
2075            $self->{state} = COMMENT_START_DASH_STATE;
2076          !!!next-input-character;          !!!next-input-character;
2077          redo A;          redo A;
2078        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2079            !!!cp (138);
2080          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2081          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2082          !!!next-input-character;          !!!next-input-character;
2083    
2084          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2085    
2086          redo A;          redo A;
2087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2088            !!!cp (139);
2089          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2090          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2091          ## reconsume          ## reconsume
2092    
2093          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2094    
2095          redo A;          redo A;
2096        } else {        } else {
2097            !!!cp (140);
2098          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2099              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2100          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2101          !!!next-input-character;          !!!next-input-character;
2102          redo A;          redo A;
2103        }        }
2104      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2105        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2106          $self->{state} = 'comment end';          !!!cp (141);
2107            $self->{state} = COMMENT_END_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2111            !!!cp (142);
2112          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2113          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2114          !!!next-input-character;          !!!next-input-character;
2115    
2116          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2117    
2118          redo A;          redo A;
2119        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2120            !!!cp (143);
2121          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2122          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2123          ## reconsume          ## reconsume
2124    
2125          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2126    
2127          redo A;          redo A;
2128        } else {        } else {
2129            !!!cp (144);
2130          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2131              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2132          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2133          !!!next-input-character;          !!!next-input-character;
2134          redo A;          redo A;
2135        }        }
2136      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2137        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2138          $self->{state} = 'comment end dash';          !!!cp (145);
2139            $self->{state} = COMMENT_END_DASH_STATE;
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2143            !!!cp (146);
2144          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2145          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2146          ## reconsume          ## reconsume
2147    
2148          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2149    
2150          redo A;          redo A;
2151        } else {        } else {
2152          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2153            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2154          ## Stay in the state          ## Stay in the state
2155          !!!next-input-character;          !!!next-input-character;
2156          redo A;          redo A;
2157        }        }
2158      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2159        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2160          $self->{state} = 'comment end';          !!!cp (148);
2161            $self->{state} = COMMENT_END_STATE;
2162          !!!next-input-character;          !!!next-input-character;
2163          redo A;          redo A;
2164        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2165            !!!cp (149);
2166          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2167          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2168          ## reconsume          ## reconsume
2169    
2170          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2171    
2172          redo A;          redo A;
2173        } else {        } else {
2174          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2175          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2176            $self->{state} = COMMENT_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178          redo A;          redo A;
2179        }        }
2180      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2181        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2182          $self->{state} = 'data';          !!!cp (151);
2183            $self->{state} = DATA_STATE;
2184          !!!next-input-character;          !!!next-input-character;
2185    
2186          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2187    
2188          redo A;          redo A;
2189        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2190          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2191            !!!parse-error (type => 'dash in comment',
2192                            line => $self->{line_prev},
2193                            column => $self->{column_prev});
2194          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2195          ## Stay in the state          ## Stay in the state
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2199            !!!cp (153);
2200          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2201          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2202          ## reconsume          ## reconsume
2203    
2204          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2205    
2206          redo A;          redo A;
2207        } else {        } else {
2208          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2209          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2210          $self->{state} = 'comment';                          line => $self->{line_prev},
2211                            column => $self->{column_prev});
2212            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2213            $self->{state} = COMMENT_STATE;
2214          !!!next-input-character;          !!!next-input-character;
2215          redo A;          redo A;
2216        }        }
2217      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2218        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2219            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2220            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2221            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2222            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2223          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2224            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } else {        } else {
2228            !!!cp (156);
2229          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2230          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2231          ## reconsume          ## reconsume
2232          redo A;          redo A;
2233        }        }
2234      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2235        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2236            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2237            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2238            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2239            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2240            !!!cp (157);
2241          ## Stay in the state          ## Stay in the state
2242          !!!next-input-character;          !!!next-input-character;
2243          redo A;          redo A;
2244        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2245            !!!cp (158);
2246          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2247          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249    
2250          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2251    
2252          redo A;          redo A;
2253        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2254            !!!cp (159);
2255          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2256          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2257          ## reconsume          ## reconsume
2258    
2259          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2260    
2261          redo A;          redo A;
2262        } else {        } else {
2263          $self->{current_token}          !!!cp (160);
2264              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2265                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2266  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2267          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2268          !!!next-input-character;          !!!next-input-character;
2269          redo A;          redo A;
2270        }        }
2271      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2272  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2273        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2274            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2275            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2276            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2277            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2278          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2279            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2280          !!!next-input-character;          !!!next-input-character;
2281          redo A;          redo A;
2282        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2283          $self->{state} = 'data';          !!!cp (162);
2284            $self->{state} = DATA_STATE;
2285          !!!next-input-character;          !!!next-input-character;
2286    
2287          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2288    
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2291            !!!cp (163);
2292          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2293          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2294          ## reconsume          ## reconsume
2295    
2296          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2297          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2298    
2299          redo A;          redo A;
2300        } else {        } else {
2301            !!!cp (164);
2302          $self->{current_token}->{name}          $self->{current_token}->{name}
2303            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        }        }
2308      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2309        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2310            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2311            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2312            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2313            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2314            !!!cp (165);
2315          ## Stay in the state          ## Stay in the state
2316          !!!next-input-character;          !!!next-input-character;
2317          redo A;          redo A;
2318        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2319          $self->{state} = 'data';          !!!cp (166);
2320            $self->{state} = DATA_STATE;
2321          !!!next-input-character;          !!!next-input-character;
2322    
2323          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2324    
2325          redo A;          redo A;
2326        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2327            !!!cp (167);
2328          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2329          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2330          ## reconsume          ## reconsume
2331    
2332          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2333          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2334    
2335          redo A;          redo A;
2336        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2337                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2338          !!!next-input-character;          !!!next-input-character;
2339          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2340              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2341            !!!next-input-character;            !!!next-input-character;
2342            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2343                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2344              !!!next-input-character;              !!!next-input-character;
2345              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2346                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2347                !!!next-input-character;                !!!next-input-character;
2348                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2349                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2350                  !!!next-input-character;                  !!!next-input-character;
2351                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2352                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2353                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2354                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2355                    !!!next-input-character;                    !!!next-input-character;
2356                    redo A;                    redo A;
2357                    } else {
2358                      !!!cp (169);
2359                  }                  }
2360                  } else {
2361                    !!!cp (170);
2362                }                }
2363                } else {
2364                  !!!cp (171);
2365              }              }
2366              } else {
2367                !!!cp (172);
2368            }            }
2369            } else {
2370              !!!cp (173);
2371          }          }
2372    
2373          #          #
2374        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2375                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2376          !!!next-input-character;          !!!next-input-character;
2377          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2378              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2379            !!!next-input-character;            !!!next-input-character;
2380            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2381                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2382              !!!next-input-character;              !!!next-input-character;
2383              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2384                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2385                !!!next-input-character;                !!!next-input-character;
2386                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2387                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2388                  !!!next-input-character;                  !!!next-input-character;
2389                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2390                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2391                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2392                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2393                    !!!next-input-character;                    !!!next-input-character;
2394                    redo A;                    redo A;
2395                    } else {
2396                      !!!cp (175);
2397                  }                  }
2398                  } else {
2399                    !!!cp (176);
2400                }                }
2401                } else {
2402                  !!!cp (177);
2403              }              }
2404              } else {
2405                !!!cp (178);
2406            }            }
2407            } else {
2408              !!!cp (179);
2409          }          }
2410    
2411          #          #
2412        } else {        } else {
2413            !!!cp (180);
2414          !!!next-input-character;          !!!next-input-character;
2415          #          #
2416        }        }
2417    
2418        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2419        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2420    
2421          $self->{state} = BOGUS_DOCTYPE_STATE;
2422        # next-input-character is already done        # next-input-character is already done
2423        redo A;        redo A;
2424      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2425        if ({        if ({
2426              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2427              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2428            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2429            !!!cp (181);
2430          ## Stay in the state          ## Stay in the state
2431          !!!next-input-character;          !!!next-input-character;
2432          redo A;          redo A;
2433        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2434            !!!cp (182);
2435          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2436          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2440            !!!cp (183);
2441          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2442          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2443          !!!next-input-character;          !!!next-input-character;
2444          redo A;          redo A;
2445        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2446            !!!cp (184);
2447          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2448    
2449          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2450          !!!next-input-character;          !!!next-input-character;
2451    
2452          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2453          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2454    
2455          redo A;          redo A;
2456        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2457            !!!cp (185);
2458          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2459    
2460          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2461          ## reconsume          ## reconsume
2462    
2463          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2464          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2465    
2466          redo A;          redo A;
2467        } else {        } else {
2468            !!!cp (186);
2469          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2470          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2471    
2472            $self->{state} = BOGUS_DOCTYPE_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474          redo A;          redo A;
2475        }        }
2476      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2477        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2478          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2479            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2480          !!!next-input-character;          !!!next-input-character;
2481          redo A;          redo A;
2482        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2483            !!!cp (188);
2484            !!!parse-error (type => 'unclosed PUBLIC literal');
2485    
2486            $self->{state} = DATA_STATE;
2487            !!!next-input-character;
2488    
2489            $self->{current_token}->{quirks} = 1;
2490            !!!emit ($self->{current_token}); # DOCTYPE
2491    
2492            redo A;
2493          } elsif ($self->{next_char} == -1) {
2494            !!!cp (189);
2495          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2496    
2497          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2498          ## reconsume          ## reconsume
2499    
2500          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2501          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2502    
2503          redo A;          redo A;
2504        } else {        } else {
2505            !!!cp (190);
2506          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2507              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2508          ## Stay in the state          ## Stay in the state
2509          !!!next-input-character;          !!!next-input-character;
2510          redo A;          redo A;
2511        }        }
2512      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2513        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2514          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2515            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2516          !!!next-input-character;          !!!next-input-character;
2517          redo A;          redo A;
2518        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2519            !!!cp (192);
2520          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2521    
2522          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2523            !!!next-input-character;
2524    
2525            $self->{current_token}->{quirks} = 1;
2526            !!!emit ($self->{current_token}); # DOCTYPE
2527    
2528            redo A;
2529          } elsif ($self->{next_char} == -1) {
2530            !!!cp (193);
2531            !!!parse-error (type => 'unclosed PUBLIC literal');
2532    
2533            $self->{state} = DATA_STATE;
2534          ## reconsume          ## reconsume
2535    
2536          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2537          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2538    
2539          redo A;          redo A;
2540        } else {        } else {
2541            !!!cp (194);
2542          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2543              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2544          ## Stay in the state          ## Stay in the state
2545          !!!next-input-character;          !!!next-input-character;
2546          redo A;          redo A;
2547        }        }
2548      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2549        if ({        if ({
2550              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2551              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2552            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2553            !!!cp (195);
2554          ## Stay in the state          ## Stay in the state
2555          !!!next-input-character;          !!!next-input-character;
2556          redo A;          redo A;
2557        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2558            !!!cp (196);
2559          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2560          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2561          !!!next-input-character;          !!!next-input-character;
2562          redo A;          redo A;
2563        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2564            !!!cp (197);
2565          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2566          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2567          !!!next-input-character;          !!!next-input-character;
2568          redo A;          redo A;
2569        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2570          $self->{state} = 'data';          !!!cp (198);
2571            $self->{state} = DATA_STATE;
2572          !!!next-input-character;          !!!next-input-character;
2573    
2574          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2575    
2576          redo A;          redo A;
2577        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2578            !!!cp (199);
2579          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2580    
2581          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2582          ## reconsume          ## reconsume
2583    
2584          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2585          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2586    
2587          redo A;          redo A;
2588        } else {        } else {
2589            !!!cp (200);
2590          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2591          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2592    
2593            $self->{state} = BOGUS_DOCTYPE_STATE;
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
2596        }        }
2597      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2598        if ({        if ({
2599              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2600              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2601            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2602            !!!cp (201);
2603          ## Stay in the state          ## Stay in the state
2604          !!!next-input-character;          !!!next-input-character;
2605          redo A;          redo A;
2606        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2607            !!!cp (202);
2608          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2609          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2610          !!!next-input-character;          !!!next-input-character;
2611          redo A;          redo A;
2612        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2613            !!!cp (203);
2614          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2615          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2616          !!!next-input-character;          !!!next-input-character;
2617          redo A;          redo A;
2618        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2619            !!!cp (204);
2620          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2621          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2622          !!!next-input-character;          !!!next-input-character;
2623    
2624          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2625          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2626    
2627          redo A;          redo A;
2628        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2629            !!!cp (205);
2630          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2631    
2632          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2633          ## reconsume          ## reconsume
2634    
2635          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2636          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2637    
2638          redo A;          redo A;
2639        } else {        } else {
2640            !!!cp (206);
2641          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2642          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2643    
2644            $self->{state} = BOGUS_DOCTYPE_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646          redo A;          redo A;
2647        }        }
2648      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2649        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2650          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2651            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2652            !!!next-input-character;
2653            redo A;
2654          } elsif ($self->{next_char} == 0x003E) { # >
2655            !!!cp (208);
2656            !!!parse-error (type => 'unclosed PUBLIC literal');
2657    
2658            $self->{state} = DATA_STATE;
2659          !!!next-input-character;          !!!next-input-character;
2660    
2661            $self->{current_token}->{quirks} = 1;
2662            !!!emit ($self->{current_token}); # DOCTYPE
2663    
2664          redo A;          redo A;
2665        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2666            !!!cp (209);
2667          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2668    
2669          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2670          ## reconsume          ## reconsume
2671    
2672          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2673          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2674    
2675          redo A;          redo A;
2676        } else {        } else {
2677            !!!cp (210);
2678          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2679              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2680          ## Stay in the state          ## Stay in the state
2681          !!!next-input-character;          !!!next-input-character;
2682          redo A;          redo A;
2683        }        }
2684      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2685        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2686          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2687            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2688          !!!next-input-character;          !!!next-input-character;
2689          redo A;          redo A;
2690        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2691            !!!cp (212);
2692            !!!parse-error (type => 'unclosed PUBLIC literal');
2693    
2694            $self->{state} = DATA_STATE;
2695            !!!next-input-character;
2696    
2697            $self->{current_token}->{quirks} = 1;
2698            !!!emit ($self->{current_token}); # DOCTYPE
2699    
2700            redo A;
2701          } elsif ($self->{next_char} == -1) {
2702            !!!cp (213);
2703          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2704    
2705          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2706          ## reconsume          ## reconsume
2707    
2708          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2709          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2710    
2711          redo A;          redo A;
2712        } else {        } else {
2713            !!!cp (214);
2714          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2715              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2716          ## Stay in the state          ## Stay in the state
2717          !!!next-input-character;          !!!next-input-character;
2718          redo A;          redo A;
2719        }        }
2720      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2721        if ({        if ({
2722              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2723              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2724            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2725            !!!cp (215);
2726          ## Stay in the state          ## Stay in the state
2727          !!!next-input-character;          !!!next-input-character;
2728          redo A;          redo A;
2729        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2730          $self->{state} = 'data';          !!!cp (216);
2731            $self->{state} = DATA_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733    
2734          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2735    
2736          redo A;          redo A;
2737        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2738          !!!parse-error (type => 'unclosed DOCTYPE');          !!!cp (217);
2739    
2740          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2741          ## reconsume          ## reconsume
2742    
2743          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2744          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2745    
2746          redo A;          redo A;
2747        } else {        } else {
2748            !!!cp (218);
2749          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2750          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2751    
2752            $self->{state} = BOGUS_DOCTYPE_STATE;
2753          !!!next-input-character;          !!!next-input-character;
2754          redo A;          redo A;
2755        }        }
2756      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2757        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2758          $self->{state} = 'data';          !!!cp (219);
2759            $self->{state} = DATA_STATE;
2760          !!!next-input-character;          !!!next-input-character;
2761    
         delete $self->{current_token}->{correct};  
2762          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2763    
2764          redo A;          redo A;
2765        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2766            !!!cp (220);
2767          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2768          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2769          ## reconsume          ## reconsume
2770    
         delete $self->{current_token}->{correct};  
2771          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2772    
2773          redo A;          redo A;
2774        } else {        } else {
2775            !!!cp (221);
2776          ## Stay in the state          ## Stay in the state
2777          !!!next-input-character;          !!!next-input-character;
2778          redo A;          redo A;
2779        }        }
2780        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2781          my $s = '';
2782          
2783          my ($l, $c) = ($self->{line}, $self->{column});
2784    
2785          CS: while ($self->{next_char} != -1) {
2786            if ($self->{next_char} == 0x005D) { # ]
2787              !!!next-input-character;
2788              if ($self->{next_char} == 0x005D) { # ]
2789                !!!next-input-character;
2790                MDC: {
2791                  if ($self->{next_char} == 0x003E) { # >
2792                    !!!cp (221.1);
2793                    !!!next-input-character;
2794                    last CS;
2795                  } elsif ($self->{next_char} == 0x005D) { # ]
2796                    !!!cp (221.2);
2797                    $s .= ']';
2798                    !!!next-input-character;
2799                    redo MDC;
2800                  } else {
2801                    !!!cp (221.3);
2802                    $s .= ']]';
2803                    #
2804                  }
2805                } # MDC
2806              } else {
2807                !!!cp (221.4);
2808                $s .= ']';
2809                #
2810              }
2811            } else {
2812              !!!cp (221.5);
2813              #
2814            }
2815            $s .= chr $self->{next_char};
2816            !!!next-input-character;
2817          } # CS
2818    
2819          $self->{state} = DATA_STATE;
2820          ## next-input-character done or EOF, which is reconsumed.
2821    
2822          if (length $s) {
2823            !!!cp (221.6);
2824            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2825                      line => $l, column => $c});
2826          } else {
2827            !!!cp (221.7);
2828          }
2829    
2830          redo A;
2831    
2832          ## ISSUE: "text tokens" in spec.
2833          ## TODO: Streaming support
2834      } else {      } else {
2835        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2836      }      }
# Line 1609  sub _get_next_token ($) { Line 2839  sub _get_next_token ($) {
2839    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2840  } # _get_next_token  } # _get_next_token
2841    
2842  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2843    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2844    
2845      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2846    
2847    if ({    if ({
2848         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2849         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2850        }->{$self->{next_input_character}}) {         $additional => 1,
2851          }->{$self->{next_char}}) {
2852        !!!cp (1001);
2853      ## Don't consume      ## Don't consume
2854      ## No error      ## No error
2855      return undef;      return undef;
2856    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2857      !!!next-input-character;      !!!next-input-character;
2858      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2859          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2860        my $code;        my $code;
2861        X: {        X: {
2862          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2863          !!!next-input-character;          !!!next-input-character;
2864          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2865              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2866              !!!cp (1002);
2867            $code ||= 0;            $code ||= 0;
2868            $code *= 0x10;            $code *= 0x10;
2869            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2870            redo X;            redo X;
2871          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2872                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2873              !!!cp (1003);
2874            $code ||= 0;            $code ||= 0;
2875            $code *= 0x10;            $code *= 0x10;
2876            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2877            redo X;            redo X;
2878          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2879                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2880              !!!cp (1004);
2881            $code ||= 0;            $code ||= 0;
2882            $code *= 0x10;            $code *= 0x10;
2883            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2884            redo X;            redo X;
2885          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2886            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2887            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2888            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2889              $self->{next_char} = 0x0023; # #
2890            return undef;            return undef;
2891          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2892              !!!cp (1006);
2893            !!!next-input-character;            !!!next-input-character;
2894          } else {          } else {
2895            !!!parse-error (type => 'no refc');            !!!cp (1007);
2896              !!!parse-error (type => 'no refc', line => $l, column => $c);
2897          }          }
2898    
2899          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2900            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2901              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2902            $code = 0xFFFD;            $code = 0xFFFD;
2903          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2904            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2905              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2906            $code = 0xFFFD;            $code = 0xFFFD;
2907          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2908            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2909              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2910            $code = 0x000A;            $code = 0x000A;
2911          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2912            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2913              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2914            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2915          }          }
2916    
2917          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2918                    has_reference => 1,
2919                    line => $l, column => $c,
2920                   };
2921        } # X        } # X
2922      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2923               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2924        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2925        !!!next-input-character;        !!!next-input-character;
2926                
2927        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2928                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2929            !!!cp (1012);
2930          $code *= 10;          $code *= 10;
2931          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2932                    
2933          !!!next-input-character;          !!!next-input-character;
2934        }        }
2935    
2936        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2937            !!!cp (1013);
2938          !!!next-input-character;          !!!next-input-character;
2939        } else {        } else {
2940          !!!parse-error (type => 'no refc');          !!!cp (1014);
2941            !!!parse-error (type => 'no refc', line => $l, column => $c);
2942        }        }
2943    
2944        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2945          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2946            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2947          $code = 0xFFFD;          $code = 0xFFFD;
2948        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2949          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2950            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2951          $code = 0xFFFD;          $code = 0xFFFD;
2952        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2953          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2954            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2955          $code = 0x000A;          $code = 0x000A;
2956        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2957          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2958            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2959          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2960        }        }
2961                
2962        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2963                  line => $l, column => $c,
2964                 };
2965      } else {      } else {
2966        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2967        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2968        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2969          $self->{next_char} = 0x0023; # #
2970        return undef;        return undef;
2971      }      }
2972    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2973              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2974             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2975              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2976      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2977      !!!next-input-character;      !!!next-input-character;
2978    
2979      my $value = $entity_name;      my $value = $entity_name;
# Line 1724  sub _tokenize_attempt_to_consume_an_enti Line 2981  sub _tokenize_attempt_to_consume_an_enti
2981      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2982      our $EntityChar;      our $EntityChar;
2983    
2984      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2985             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2986             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2987               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2988              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2989               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2990              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2991               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2992              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2993        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2994        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2995          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2996              !!!cp (1020);
2997            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2998            $match = 1;            $match = 1;
2999            !!!next-input-character;            !!!next-input-character;
3000            last;            last;
3001          } else {          } else {
3002              !!!cp (1021);
3003            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3004            $match = -1;            $match = -1;
3005            !!!next-input-character;            !!!next-input-character;
3006          }          }
3007        } else {        } else {
3008          $value .= chr $self->{next_input_character};          !!!cp (1022);
3009            $value .= chr $self->{next_char};
3010          $match *= 2;          $match *= 2;
3011          !!!next-input-character;          !!!next-input-character;
3012        }        }
3013      }      }
3014            
3015      if ($match > 0) {      if ($match > 0) {
3016        return {type => 'character', data => $value};        !!!cp (1023);
3017          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3018                  line => $l, column => $c,
3019                 };
3020      } elsif ($match < 0) {      } elsif ($match < 0) {
3021        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3022        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3023          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
3024        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3025          return {type => 'character', data => $value};                  line => $l, column => $c,
3026                   };
3027          } else {
3028            !!!cp (1025);
3029            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3030                    line => $l, column => $c,
3031                   };
3032        }        }
3033      } else {      } else {
3034        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3035        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3036        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3037          return {type => CHARACTER_TOKEN, data => '&'.$value,
3038                  line => $l, column => $c,
3039                 };
3040      }      }
3041    } else {    } else {
3042        !!!cp (1027);
3043      ## no characters are consumed      ## no characters are consumed
3044      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3045      return undef;      return undef;
3046    }    }
3047  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1806  sub _construct_tree ($) { Line 3079  sub _construct_tree ($) {
3079        
3080    !!!next-token;    !!!next-token;
3081    
   $self->{insertion_mode} = 'before head';  
3082    undef $self->{form_element};    undef $self->{form_element};
3083    undef $self->{head_element};    undef $self->{head_element};
3084    $self->{open_elements} = [];    $self->{open_elements} = [];
3085    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3086    
3087      ## NOTE: The "initial" insertion mode.
3088    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3089    
3090      ## NOTE: The "before html" insertion mode.
3091    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3092      $self->{insertion_mode} = BEFORE_HEAD_IM;
3093    
3094      ## NOTE: The "before head" insertion mode and so on.
3095    $self->_tree_construction_main;    $self->_tree_construction_main;
3096  } # _construct_tree  } # _construct_tree
3097    
3098  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3099    my $self = shift;    my $self = shift;
3100    
3101      ## NOTE: "initial" insertion mode
3102    
3103    INITIAL: {    INITIAL: {
3104      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3105        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3106        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3107        ## language.        ## language.
# Line 1830  sub _tree_construction_initial ($) { Line 3111  sub _tree_construction_initial ($) {
3111        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3112            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3113            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3114          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3115            !!!parse-error (type => 'not HTML5', token => $token);
3116        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3117            !!!cp ('t2');
3118          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3119          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3120          } else {
3121            !!!cp ('t3');
3122        }        }
3123                
3124        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3125          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3126          ## NOTE: Default value for both |public_id| and |system_id| attributes
3127          ## are empty strings, so that we don't set any value in missing cases.
3128        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3129            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3130        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 3133  sub _tree_construction_initial ($) {
3133        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3134        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3135                
3136        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3137            !!!cp ('t4');
3138          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3139        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3140          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3141          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3142          if ({          my $prefix = [
3143            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3144            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3145            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3146            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3147            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3148            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3149            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3150            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3151            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3152            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3153            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3154            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3155            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3156            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3157            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3158            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3159            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3160            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3161            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3162            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3163            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3164            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3165            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3166            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3167            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3168            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3169            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3170            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3171            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3172            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3173            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3174            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3175            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3176            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3177            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3178            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3179            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3180            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3181            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3182            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3183            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3184            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3185            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3186            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3187            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3188            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3189            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3190            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3191            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3192            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3193            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3194            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3195            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3196            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3197            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3198            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3199            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3200            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3201            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3202            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3203            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3204            "-//W3C//DTD W3 HTML//EN" => 1,            }
3205            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3206            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3207            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3208            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3209            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3210            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3211            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3212          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3213                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3214            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3215                !!!cp ('t6');
3216              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3217            } else {            } else {
3218                !!!cp ('t7');
3219              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3220            }            }
3221          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3222                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3223              !!!cp ('t8');
3224            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3225            } else {
3226              !!!cp ('t9');
3227          }          }
3228          } else {
3229            !!!cp ('t10');
3230        }        }
3231        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3232          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3233          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3234          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3235              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3236              ## marked as quirks.
3237            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3238              !!!cp ('t11');
3239            } else {
3240              !!!cp ('t12');
3241          }          }
3242          } else {
3243            !!!cp ('t13');
3244        }        }
3245                
3246        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3247        !!!next-token;        !!!next-token;
3248        return;        return;
3249      } elsif ({      } elsif ({
3250                'start tag' => 1,                START_TAG_TOKEN, 1,
3251                'end tag' => 1,                END_TAG_TOKEN, 1,
3252                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3253               }->{$token->{type}}) {               }->{$token->{type}}) {
3254        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3255          !!!parse-error (type => 'no DOCTYPE', token => $token);
3256        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3257        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3258        ## reprocess        ## reprocess
3259          !!!ack-later;
3260        return;        return;
3261      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3262        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3263          ## Ignore the token          ## Ignore the token
3264    
3265          unless (length $token->{data}) {          unless (length $token->{data}) {
3266            ## Stay in the phase            !!!cp ('t15');
3267              ## Stay in the insertion mode.
3268            !!!next-token;            !!!next-token;
3269            redo INITIAL;            redo INITIAL;
3270            } else {
3271              !!!cp ('t16');
3272          }          }
3273          } else {
3274            !!!cp ('t17');
3275        }        }
3276    
3277        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3278        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3279        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3280        ## reprocess        ## reprocess
3281        return;        return;
3282      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3283          !!!cp ('t18');
3284        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3285        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3286                
3287        ## Stay in the phase.        ## Stay in the insertion mode.
3288        !!!next-token;        !!!next-token;
3289        redo INITIAL;        redo INITIAL;
3290      } else {      } else {
3291        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3292      }      }
3293    } # INITIAL    } # INITIAL
3294    
3295      die "$0: _tree_construction_initial: This should be never reached";
3296  } # _tree_construction_initial  } # _tree_construction_initial
3297    
3298  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3299    my $self = shift;    my $self = shift;
3300    
3301      ## NOTE: "before html" insertion mode.
3302        
3303    B: {    B: {
3304        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3305          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3306            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3307          ## Ignore the token          ## Ignore the token
3308          ## Stay in the phase          ## Stay in the insertion mode.
3309          !!!next-token;          !!!next-token;
3310          redo B;          redo B;
3311        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3312            !!!cp ('t20');
3313          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3314          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3315          ## Stay in the phase          ## Stay in the insertion mode.
3316          !!!next-token;          !!!next-token;
3317          redo B;          redo B;
3318        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3319          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3320            ## Ignore the token.            ## Ignore the token.
3321    
3322            unless (length $token->{data}) {            unless (length $token->{data}) {
3323              ## Stay in the phase              !!!cp ('t21');
3324                ## Stay in the insertion mode.
3325              !!!next-token;              !!!next-token;
3326              redo B;              redo B;
3327              } else {
3328                !!!cp ('t22');
3329            }            }
3330            } else {
3331              !!!cp ('t23');
3332          }          }
3333    
3334            $self->{application_cache_selection}->(undef);
3335    
3336          #          #
3337          } elsif ($token->{type} == START_TAG_TOKEN) {
3338            if ($token->{tag_name} eq 'html') {
3339              my $root_element;
3340              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3341              $self->{document}->append_child ($root_element);
3342              push @{$self->{open_elements}},
3343                  [$root_element, $el_category->{html}];
3344    
3345              if ($token->{attributes}->{manifest}) {
3346                !!!cp ('t24');
3347                $self->{application_cache_selection}
3348                    ->($token->{attributes}->{manifest}->{value});
3349                ## ISSUE: Spec is unclear on relative references.
3350                ## According to Hixie (#whatwg 2008-03-19), it should be
3351                ## resolved against the base URI of the document in HTML
3352                ## or xml:base of the element in XHTML.
3353              } else {
3354                !!!cp ('t25');
3355                $self->{application_cache_selection}->(undef);
3356              }
3357    
3358              !!!nack ('t25c');
3359    
3360              !!!next-token;
3361              return; ## Go to the "before head" insertion mode.
3362            } else {
3363              !!!cp ('t25.1');
3364              #
3365            }
3366        } elsif ({        } elsif ({
3367                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3368                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3369                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3370          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3371          #          #
3372        } else {        } else {
3373          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3374        }        }
3375        my $root_element; !!!create-element ($root_element, 'html');  
3376        $self->{document}->append_child ($root_element);      my $root_element;
3377        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3378        ## reprocess      $self->{document}->append_child ($root_element);
3379        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3380        return; ## Go to the main phase.  
3381        $self->{application_cache_selection}->(undef);
3382    
3383        ## NOTE: Reprocess the token.
3384        !!!ack-later;
3385        return; ## Go to the "before head" insertion mode.
3386    
3387        ## ISSUE: There is an issue in the spec
3388    } # B    } # B
3389    
3390      die "$0: _tree_construction_root_element: This should never be reached";
3391  } # _tree_construction_root_element  } # _tree_construction_root_element
3392    
3393  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3402  sub _reset_insertion_mode ($) {
3402            
3403      ## Step 3      ## Step 3
3404      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3405        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3406          $last = 1;          $last = 1;
3407          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3408            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3409                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3410              #          } else {
3411            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3412          }          }
3413        }        }
3414              
3415        ## Step 4..13        ## Step 4..14
3416        my $new_mode = {        my $new_mode;
3417                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3418                        td => 'in cell',          !!!cp ('t28.1');
3419                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3420                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3421                        tbody => 'in table body',          ## SVG elements as foreigners.
3422                        thead => 'in table head',          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3423                        tfoot => 'in table foot',          ## ISSUE: What is set as the secondary insertion mode?
3424                        caption => 'in caption',        } elsif ($node->[1] & TABLE_CELL_EL) {
3425                        colgroup => 'in column group',          if ($last) {
3426                        table => 'in table',            !!!cp ('t28.2');
3427                        head => 'in body', # not in head!            #
3428                        body => 'in body',          } else {
3429                        frameset => 'in frameset',            !!!cp ('t28.3');
3430                       }->{$node->[1]};            $new_mode = IN_CELL_IM;
3431            }
3432          } else {
3433            !!!cp ('t28.4');
3434            $new_mode = {
3435                          select => IN_SELECT_IM,
3436                          ## NOTE: |option| and |optgroup| do not set
3437                          ## insertion mode to "in select" by themselves.
3438                          tr => IN_ROW_IM,
3439                          tbody => IN_TABLE_BODY_IM,
3440                          thead => IN_TABLE_BODY_IM,
3441                          tfoot => IN_TABLE_BODY_IM,
3442                          caption => IN_CAPTION_IM,
3443                          colgroup => IN_COLUMN_GROUP_IM,
3444                          table => IN_TABLE_IM,
3445                          head => IN_BODY_IM, # not in head!
3446                          body => IN_BODY_IM,
3447                          frameset => IN_FRAMESET_IM,
3448                         }->{$node->[0]->manakai_local_name};
3449          }
3450        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3451                
3452        ## Step 14        ## Step 15
3453        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3454          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3455            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3456              $self->{insertion_mode} = BEFORE_HEAD_IM;
3457          } else {          } else {
3458            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3459              !!!cp ('t30');
3460              $self->{insertion_mode} = AFTER_HEAD_IM;
3461          }          }
3462          return;          return;
3463          } else {
3464            !!!cp ('t31');
3465        }        }
3466                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3467        ## Step 16        ## Step 16
3468          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3469          
3470          ## Step 17
3471        $i--;        $i--;
3472        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3473                
3474        ## Step 17        ## Step 18
3475        redo S3;        redo S3;
3476      } # S3      } # S3
3477    
3478      die "$0: _reset_insertion_mode: This line should never be reached";
3479  } # _reset_insertion_mode  } # _reset_insertion_mode
3480    
3481  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3482    my $self = shift;    my $self = shift;
3483    
   my $previous_insertion_mode;  
   
3484    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3485    
3486    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3497  sub _tree_construction_main ($) {
3497      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3498      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3499        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3500            !!!cp ('t32');
3501          return;          return;
3502        }        }
3503      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3512  sub _tree_construction_main ($) {
3512    
3513        ## Step 6        ## Step 6
3514        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3515            !!!cp ('t33_1');
3516          #          #
3517        } else {        } else {
3518          my $in_open_elements;          my $in_open_elements;
3519          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3520            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3521                !!!cp ('t33');
3522              $in_open_elements = 1;              $in_open_elements = 1;
3523              last OE;              last OE;
3524            }            }
3525          }          }
3526          if ($in_open_elements) {          if ($in_open_elements) {
3527              !!!cp ('t34');
3528            #            #
3529          } else {          } else {
3530              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3531              !!!cp ('t35');
3532            redo S4;            redo S4;
3533          }          }
3534        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3551  sub _tree_construction_main ($) {
3551    
3552        ## Step 11        ## Step 11
3553        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3554            !!!cp ('t36');
3555          ## Step 7'          ## Step 7'
3556          $i++;          $i++;
3557          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3558                    
3559          redo S7;          redo S7;
3560        }        }
3561    
3562          !!!cp ('t37');
3563      } # S7      } # S7
3564    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3565    
3566    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3567      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3568        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3569            !!!cp ('t38');
3570          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3571          return;          return;
3572        }        }
3573      }      }
3574    
3575        !!!cp ('t39');
3576    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3577    
3578    my $parse_rcdata = sub ($$) {    my $insert;
3579      my ($content_model_flag, $insert) = @_;  
3580      my $parse_rcdata = sub ($) {
3581        my ($content_model_flag) = @_;
3582    
3583      ## Step 1      ## Step 1
3584      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3585      my $el;      my $el;
3586      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3587    
3588      ## Step 2      ## Step 2
3589      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3590    
3591      ## Step 3      ## Step 3
3592      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2204  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595      ## Step 4      ## Step 4
3596      my $text = '';      my $text = '';
3597        !!!nack ('t40.1');
3598      !!!next-token;      !!!next-token;
3599      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3600          !!!cp ('t40');
3601        $text .= $token->{data};        $text .= $token->{data};
3602        !!!next-token;        !!!next-token;
3603      }      }
3604    
3605      ## Step 5      ## Step 5
3606      if (length $text) {      if (length $text) {
3607          !!!cp ('t41');
3608        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3609        $el->append_child ($text);        $el->append_child ($text);
3610      }      }
# Line 2220  sub _tree_construction_main ($) { Line 3613  sub _tree_construction_main ($) {
3613      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3614    
3615      ## Step 7      ## Step 7
3616      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3617            $token->{tag_name} eq $start_tag_name) {
3618          !!!cp ('t42');
3619        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3620      } else {      } else {
3621        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3622          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3623            !!!cp ('t43');
3624            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3625          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3626            !!!cp ('t44');
3627            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3628          } else {
3629            die "$0: $content_model_flag in parse_rcdata";
3630          }
3631      }      }
3632      !!!next-token;      !!!next-token;
3633    }; # $parse_rcdata    }; # $parse_rcdata
3634    
3635    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3636      my $script_el;      my $script_el;
3637      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3638      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3639    
3640      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3641      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3642            
3643      my $text = '';      my $text = '';
3644        !!!nack ('t45.1');
3645      !!!next-token;      !!!next-token;
3646      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3647          !!!cp ('t45');
3648        $text .= $token->{data};        $text .= $token->{data};
3649        !!!next-token;        !!!next-token;
3650      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3651      if (length $text) {      if (length $text) {
3652          !!!cp ('t46');
3653        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3654      }      }
3655                                
3656      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3657    
3658      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3659          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3660          !!!cp ('t47');
3661        ## Ignore the token        ## Ignore the token
3662      } else {      } else {
3663        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3664          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3665        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3666        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3667      }      }
3668            
3669      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3670          !!!cp ('t49');
3671        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3672      } else {      } else {
3673          !!!cp ('t50');
3674        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3675        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3676    
# Line 2278  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684      !!!next-token;      !!!next-token;
3685    }; # $script_start_tag    }; # $script_start_tag
3686    
3687      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3688      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3689      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3690    
3691    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3692      my $tag_name = shift;      my $end_tag_token = shift;
3693        my $tag_name = $end_tag_token->{tag_name};
3694    
3695        ## NOTE: The adoption agency algorithm (AAA).
3696    
3697      FET: {      FET: {
3698        ## Step 1        ## Step 1
3699        my $formatting_element;        my $formatting_element;
3700        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3701        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3702          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3703              !!!cp ('t52');
3704              last AFE;
3705            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3706                         eq $tag_name) {
3707              !!!cp ('t51');
3708            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3709            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3710            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3711          }          }
3712        } # AFE        } # AFE
3713        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3714          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3715            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3716          ## Ignore the token          ## Ignore the token
3717          !!!next-token;          !!!next-token;
3718          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3724  sub _tree_construction_main ($) {
3724          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3725          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3726            if ($in_scope) {            if ($in_scope) {
3727                !!!cp ('t54');
3728              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3729              last INSCOPE;              last INSCOPE;
3730            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3731              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3732                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3733                                token => $end_tag_token);
3734              ## Ignore the token              ## Ignore the token
3735              !!!next-token;              !!!next-token;
3736              return;              return;
3737            }            }
3738          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3739                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3740            $in_scope = 0;            $in_scope = 0;
3741          }          }
3742        } # INSCOPE        } # INSCOPE
3743        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3744          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3745            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3746                            token => $end_tag_token);
3747          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3748          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3749          return;          return;
3750        }        }
3751        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3752          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3753            !!!parse-error (type => 'not closed',
3754                            value => $self->{open_elements}->[-1]->[0]
3755                                ->manakai_local_name,
3756                            token => $end_tag_token);
3757        }        }
3758                
3759        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 3761  sub _tree_construction_main ($) {
3761        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3762        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3763          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3764          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3765              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3766              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3767               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3768              !!!cp ('t59');
3769            $furthest_block = $node;            $furthest_block = $node;
3770            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3771          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3772              !!!cp ('t60');
3773            last OE;            last OE;
3774          }          }
3775        } # OE        } # OE
3776                
3777        ## Step 3        ## Step 3
3778        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3779            !!!cp ('t61');
3780          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3781          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3782          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789        ## Step 5        ## Step 5
3790        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3791        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3792            !!!cp ('t62');
3793          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3794        }        }
3795                
# Line 2384  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812          S7S2: {          S7S2: {
3813            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3814              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3815                  !!!cp ('t63');
3816                $node_i_in_active = $_;                $node_i_in_active = $_;
3817                last S7S2;                last S7S2;
3818              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3826  sub _tree_construction_main ($) {
3826                    
3827          ## Step 4          ## Step 4
3828          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3829              !!!cp ('t64');
3830            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3831          }          }
3832                    
3833          ## Step 5          ## Step 5
3834          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3835              !!!cp ('t65');
3836            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3837            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3838            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850        } # S7          } # S7  
3851                
3852        ## Step 8        ## Step 8
3853        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3854            my $foster_parent_element;
3855            my $next_sibling;
3856            OE: for (reverse 0..$#{$self->{open_elements}}) {
3857              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3858                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3859                                 if (defined $parent and $parent->node_type == 1) {
3860                                   !!!cp ('t65.1');
3861                                   $foster_parent_element = $parent;
3862                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3863                                 } else {
3864                                   !!!cp ('t65.2');
3865                                   $foster_parent_element
3866                                     = $self->{open_elements}->[$_ - 1]->[0];
3867                                 }
3868                                 last OE;
3869                               }
3870                             } # OE
3871                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3872                               unless defined $foster_parent_element;
3873            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3874            $open_tables->[-1]->[1] = 1; # tainted
3875          } else {
3876            !!!cp ('t65.3');
3877            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3878          }
3879                
3880        ## Step 9        ## Step 9
3881        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3892  sub _tree_construction_main ($) {
3892        my $i;        my $i;
3893        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3894          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3895              !!!cp ('t66');
3896            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3897            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3898          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3899              !!!cp ('t67');
3900            $i = $_;            $i = $_;
3901          }          }
3902        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906        undef $i;        undef $i;
3907        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3908          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3909              !!!cp ('t68');
3910            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3911            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3912          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3913              !!!cp ('t69');
3914            $i = $_;            $i = $_;
3915          }          }
3916        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921      } # FET      } # FET
3922    }; # $formatting_end_tag    }; # $formatting_end_tag
3923    
3924    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3925      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3926    }; # $insert_to_current    }; # $insert_to_current
3927    
3928    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3929                         my $child = shift;      my $child = shift;
3930                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3931                              table => 1, tbody => 1, tfoot => 1,        # MUST
3932                              thead => 1, tr => 1,        my $foster_parent_element;
3933                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3934                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3935                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3936                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3937                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3938                                   !!!cp ('t70');
3939                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3940                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3941                               } else {                               } else {
3942                                   !!!cp ('t71');
3943                                 $foster_parent_element                                 $foster_parent_element
3944                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3945                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 3950  sub _tree_construction_main ($) {
3950                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3951                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3952                             ($child, $next_sibling);                             ($child, $next_sibling);
3953                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3954                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3955                         }        !!!cp ('t72');
3956          $self->{open_elements}->[-1]->[0]->append_child ($child);
3957        }
3958    }; # $insert_to_foster    }; # $insert_to_foster
3959    
3960    my $in_body = sub {    B: while (1) {
3961      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
3962      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
3963        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3964          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
3965          $script_start_tag->($insert);        ## Stay in the phase
3966          return;        !!!next-token;
3967        } elsif ($token->{tag_name} eq 'style') {        next B;
3968          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
3969          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);               $token->{tag_name} eq 'html') {
3970          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3971        } elsif ({          !!!cp ('t79');
3972                  base => 1, link => 1,          !!!parse-error (type => 'after html:html', token => $token);
3973                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
3974          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3975          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
3976          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html:html', token => $token);
3977          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3978          return;        } else {
3979        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
3980          ## 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  
         }  
3981    
3982          !!!next-token;        !!!cp ('t82');
3983          return;        !!!parse-error (type => 'not first start tag', token => $token);
3984        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
3985          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
3986          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3987          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {            !!!cp ('t84');
3988            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
3989              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
3990            } else {               $token->{attributes}->{$attr_name}->{value});
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
3991          }          }
3992        } elsif ($token->{tag_name} eq 'li') {        }
3993          ## has a p element in scope        !!!nack ('t84.1');
3994          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
3995            if ($_->[1] eq 'p') {        next B;
3996              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
3997              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
3998              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3999            } elsif ({          !!!cp ('t85');
4000                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
4001                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4002                     }->{$_->[1]}) {          !!!cp ('t86');
4003              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
4004            }        } else {
4005          } # INSCOPE          !!!cp ('t87');
4006                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
4007          ## Step 1        }
4008          my $i = -1;        !!!next-token;
4009          my $node = $self->{open_elements}->[$i];        next B;
4010          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4011            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
4012            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
4013              if ($i != -1) {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
4014          !!!next-token;          !!!next-token;
4015          return;          next B;
4016        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4017          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4018            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4019            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4020              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4021                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4022              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4023              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4024              $formatting_end_tag->($token->{tag_name});            #
4025                        } elsif ({
4026              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4027                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
4028                  splice @$active_formatting_elements, $_, 1;                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
4029                  last AFE2;                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
4030                }                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
4031              } # AFE2                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,
4032              OE: for (reverse 0..$#{$self->{open_elements}}) {                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
4033                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    var => 1,
4034                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4035                  last OE;            !!!cp ('t87.2');
4036                }            !!!parse-error (type => 'not closed',
4037              } # OE                            value => $self->{open_elements}->[-1]->[0]
4038              last AFE;                                ->manakai_local_name,
4039            } elsif ($node->[0] eq '#marker') {                            token => $token);
4040              last AFE;  
4041              pop @{$self->{open_elements}}
4042                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4043    
4044              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4045              ## Reprocess.
4046              next B;
4047            } else {
4048              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4049              my $tag_name = $token->{tag_name};
4050              if ($nsuri eq $SVG_NS) {
4051                $tag_name = {
4052                   altglyph => 'altGlyph',
4053                   altglyphdef => 'altGlyphDef',
4054                   altglyphitem => 'altGlyphItem',
4055                   animatecolor => 'animateColor',
4056                   animatemotion => 'animateMotion',
4057                   animatetransform => 'animateTransform',
4058                   clippath => 'clipPath',
4059                   feblend => 'feBlend',
4060                   fecolormatrix => 'feColorMatrix',
4061                   fecomponenttransfer => 'feComponentTransfer',
4062                   fecomposite => 'feComposite',
4063                   feconvolvematrix => 'feConvolveMatrix',
4064                   fediffuselighting => 'feDiffuseLighting',
4065                   fedisplacementmap => 'feDisplacementMap',
4066                   fedistantlight => 'feDistantLight',
4067                   feflood => 'feFlood',
4068                   fefunca => 'feFuncA',
4069                   fefuncb => 'feFuncB',
4070                   fefuncg => 'feFuncG',
4071                   fefuncr => 'feFuncR',
4072                   fegaussianblur => 'feGaussianBlur',
4073                   feimage => 'feImage',
4074                   femerge => 'feMerge',
4075                   femergenode => 'feMergeNode',
4076                   femorphology => 'feMorphology',
4077                   feoffset => 'feOffset',
4078                   fepointlight => 'fePointLight',
4079                   fespecularlighting => 'feSpecularLighting',
4080                   fespotlight => 'feSpotLight',
4081                   fetile => 'feTile',
4082                   feturbulence => 'feTurbulence',
4083                   foreignobject => 'foreignObject',
4084                   glyphref => 'glyphRef',
4085                   lineargradient => 'linearGradient',
4086                   radialgradient => 'radialGradient',
4087                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4088                   textpath => 'textPath',  
4089                }->{$tag_name} || $tag_name;
4090            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4091    
4092          !!!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];  
4093    
4094          !!!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);  
4095    
4096          ## 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', ''];  
4097    
4098          !!!next-token;            if ($self->{self_closing}) {
4099          return;              pop @{$self->{open_elements}};
4100        } elsif ($token->{tag_name} eq 'marquee' or              !!!ack ('t87.3');
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!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}};  
4101            } else {            } else {
4102              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
4103            }            }
4104            push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4105            !!!next-token;            !!!next-token;
4106              next B;
4107          }          }
4108          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4109            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4110          }          !!!cp ('t87.5');
4111                    #
4112          $self->{content_model} = PCDATA_CONTENT_MODEL;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4113                    ## NOTE: "using the rules for secondary insertion mode" then "continue"
4114          if ($token->{type} eq 'end tag' and          !!!cp ('t87.6');
4115              $token->{tag_name} eq $tag_name) {          #
4116            ## 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_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4117        } else {        } else {
4118          $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;  
4119        }        }
4120      } 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]);  
             }  
           }  
4121    
4122            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4123            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4124            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4125          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4126            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4127            ## 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]);  
4128            } else {            } else {
4129              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4130                ## Ignore the token.
4131                !!!next-token;
4132                next B;
4133            }            }
4134          }            unless (length $token->{data}) {
4135                        !!!cp ('t88');
4136          if (defined $i) {              !!!next-token;
4137            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;  
4138            }            }
         } # 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]);  
4139          }          }
4140    
4141          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4142          !!!next-token;            !!!cp ('t89');
4143          return;            ## As if <head>
4144        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4145                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4146                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4147          ## 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');  
4148    
4149          ## As if <br>            ## Reprocess in the "in head" insertion mode...
4150          $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];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4151    
4152              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4153              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4154            } else {            !!!cp ('t90');
4155              ## Step 3            ## As if </noscript>
4156              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4157                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#character', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4158                        
4159            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4160            redo S2;            ## As if </head>
4161          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($token->{type} eq 'DOCTYPE') {  
       !!!parse-error (type => 'DOCTYPE in the middle');  
       ## Ignore the token  
       ## Stay in the phase  
       !!!next-token;  
       redo B;  
     } elsif ($token->{type} eq 'end-of-file') {  
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
4162    
4163        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4164        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4165      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4166               $token->{tag_name} eq 'html') {            pop @{$self->{open_elements}};
       if ($self->{insertion_mode} eq 'trailing end') {  
         ## Turn into the main phase  
         !!!parse-error (type => 'after html:html');  
         $self->{insertion_mode} = $previous_insertion_mode;  
       }  
4167    
4168  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4169  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4170        unless ($token->{first_start_tag}) {            !!!cp ('t92');
         !!!parse-error (type => 'not first start tag');  
       }  
       my $top_el = $self->{open_elements}->[0]->[0];  
       for my $attr_name (keys %{$token->{attributes}}) {  
         unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
           $top_el->set_attribute_ns  
             (undef, [undef, $attr_name],  
              $token->{attributes}->{$attr_name}->{value});  
4171          }          }
4172        }  
4173        !!!next-token;          ## "after head" insertion mode
4174        redo B;          ## As if <body>
4175      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4176        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4177        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4178          $self->{document}->append_child ($comment);          next B;
4179        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4180          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4181        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4182          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4183        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4184        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4185        redo B;                  ($self->{head_element});
4186      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4187            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4188              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4189                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4190                unless (length $token->{data}) {              !!!next-token;
4191                  !!!next-token;              next B;
4192                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4193                }              !!!cp ('t93.2');
4194              }              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type
4195              ## As if <head>              ## Ignore the token
4196              !!!create-element ($self->{head_element}, 'head');              !!!nack ('t93.3');
4197              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!next-token;
4198              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              next B;
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
4199            } else {            } else {
4200              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4201                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4202                ## Ignore the token
4203                !!!nack ('t95.1');
4204                !!!next-token;
4205                next B;
4206            }            }
4207          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4208                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4209                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4210            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4211              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4212                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4213                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4214                  !!!next-token;  
4215                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4216              ## Reprocess in the "in head" insertion mode...
4217            } else {
4218              !!!cp ('t97');
4219            }
4220    
4221                if ($token->{tag_name} eq 'base') {
4222                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4223                    !!!cp ('t98');
4224                    ## As if </noscript>
4225                    pop @{$self->{open_elements}};
4226                    !!!parse-error (type => 'in noscript:base', token => $token);
4227                  
4228                    $self->{insertion_mode} = IN_HEAD_IM;
4229                    ## Reprocess in the "in head" insertion mode...
4230                  } else {
4231                    !!!cp ('t99');
4232                }                }
4233              }  
               
             #  
           } 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}}) {  
4234                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4235                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4236                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4237                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4238                    push @{$self->{open_elements}},
4239                        [$self->{head_element}, $el_category->{head}];
4240                  } else {
4241                    !!!cp ('t101');
4242                }                }
4243                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4244                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4245                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4246                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4247                  !!!nack ('t101.1');
4248                !!!next-token;                !!!next-token;
4249                redo B;                next B;
4250              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4251                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4252                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4253                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4254                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4255                    push @{$self->{open_elements}},
4256                        [$self->{head_element}, $el_category->{head}];
4257                  } else {
4258                    !!!cp ('t103');
4259                }                }
4260                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4261                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4262                  pop @{$self->{open_elements}} # <head>
4263                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4264                  !!!ack ('t103.1');
4265                  !!!next-token;
4266                  next B;
4267                } elsif ($token->{tag_name} eq 'meta') {
4268                  ## NOTE: There is a "as if in head" code clone.
4269                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4270                    !!!cp ('t104');
4271                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4272                    push @{$self->{open_elements}},
4273                        [$self->{head_element}, $el_category->{head}];
4274                  } else {
4275                    !!!cp ('t105');
4276                  }
4277                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4278                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4279    
4280                unless ($self->{confident}) {                unless ($self->{confident}) {
4281                  my $charset;                  if ($token->{attributes}->{charset}) {
4282                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4283                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4284                  }                    ## in the {change_encoding} callback.
4285                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4286                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4287                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4288                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4289                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4290                          ->set_user_data (manakai_has_reference =>
4291                                               $token->{attributes}->{charset}
4292                                                   ->{has_reference});
4293                    } elsif ($token->{attributes}->{content}) {
4294                      if ($token->{attributes}->{content}->{value}
4295                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4296                              [\x09-\x0D\x20]*=
4297                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4298                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4299                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4300                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4301                        ## in the {change_encoding} callback.
4302                        $self->{change_encoding}
4303                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4304                               $token);
4305                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4306                            ->set_user_data (manakai_has_reference =>
4307                                                 $token->{attributes}->{content}
4308                                                       ->{has_reference});
4309                      } else {
4310                        !!!cp ('t108');
4311                      }
4312                    }
4313                  } else {
4314                    if ($token->{attributes}->{charset}) {
4315                      !!!cp ('t109');
4316                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4317                          ->set_user_data (manakai_has_reference =>
4318                                               $token->{attributes}->{charset}
4319                                                   ->{has_reference});
4320                    }
4321                    if ($token->{attributes}->{content}) {
4322                      !!!cp ('t110');
4323                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4324                          ->set_user_data (manakai_has_reference =>
4325                                               $token->{attributes}->{content}
4326                                                   ->{has_reference});
4327                  }                  }
                 ## TODO: Change the encoding  
4328                }                }
4329    
4330                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4331                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4332                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4333                !!!next-token;                !!!next-token;
4334                redo B;                next B;
4335              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4336                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4337                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4338                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4339                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4340                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
4341                  
4342                    $self->{insertion_mode} = IN_HEAD_IM;
4343                    ## Reprocess in the "in head" insertion mode...
4344                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4345                    !!!cp ('t112');
4346                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4347                    push @{$self->{open_elements}},
4348                        [$self->{head_element}, $el_category->{head}];
4349                  } else {
4350                    !!!cp ('t113');
4351                }                }
4352    
4353                  ## NOTE: There is a "as if in head" code clone.
4354                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4355                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4356                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4357                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4358                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4359                    if $self->{insertion_mode} eq 'after head';                next B;
               redo B;  
4360              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4361                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4362                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4363                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4364                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4365                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4366                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4367                }                  push @{$self->{open_elements}},
4368                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                      [$self->{head_element}, $el_category->{head}];
4369                pop @{$self->{open_elements}}                } else {
4370                    if $self->{insertion_mode} eq 'after head';                  !!!cp ('t115');
4371                redo B;                }
4372                  $parse_rcdata->(CDATA_CONTENT_MODEL);
4373                  pop @{$self->{open_elements}} # <head>
4374                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4375                  next B;
4376              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4377                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4378                    !!!cp ('t116');
4379                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4380                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4381                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4382                    !!!nack ('t116.1');
4383                  !!!next-token;                  !!!next-token;
4384                  redo B;                  next B;
4385                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4387                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4388                  ## Ignore the token                  ## Ignore the token
4389                    !!!nack ('t117.1');
4390                  !!!next-token;                  !!!next-token;
4391                  redo B;                  next B;
4392                } else {                } else {
4393                    !!!cp ('t118');
4394                  #                  #
4395                }                }
4396              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4397                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4398                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4399                ## Ignore the token                  ## As if </noscript>
4400                !!!next-token;                  pop @{$self->{open_elements}};
4401                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
4402              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
4403                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
4404                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
4405                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4406                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
4407                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4408                    push @{$self->{open_elements}},
4409                        [$self->{head_element}, $el_category->{head}];
4410                  } else {
4411                    !!!cp ('t121');
4412                }                }
4413    
4414                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4415                $script_start_tag->($insert_to_current);                $script_start_tag->();
4416                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4417                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4418                redo B;                next B;
4419              } 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  
4420                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4421                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4422                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4423                    ## As if </noscript>
4424                    pop @{$self->{open_elements}};
4425                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4426                    
4427                    ## Reprocess in the "in head" insertion mode...
4428                    ## As if </head>
4429                    pop @{$self->{open_elements}};
4430                    
4431                    ## Reprocess in the "after head" insertion mode...
4432                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4433                    !!!cp ('t124');
4434                    pop @{$self->{open_elements}};
4435                    
4436                    ## Reprocess in the "after head" insertion mode...
4437                  } else {
4438                    !!!cp ('t125');
4439                  }
4440    
4441                  ## "after head" insertion mode
4442                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4443                  if ($token->{tag_name} eq 'body') {
4444                    !!!cp ('t126');
4445                    $self->{insertion_mode} = IN_BODY_IM;
4446                  } elsif ($token->{tag_name} eq 'frameset') {
4447                    !!!cp ('t127');
4448                    $self->{insertion_mode} = IN_FRAMESET_IM;
4449                  } else {
4450                    die "$0: tag name: $self->{tag_name}";
4451                  }
4452                  !!!nack ('t127.1');
4453                !!!next-token;                !!!next-token;
4454                redo B;                next B;
4455              } else {              } else {
4456                  !!!cp ('t128');
4457                #                #
4458              }              }
4459            } elsif ($token->{type} eq 'end tag') {  
4460              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4461                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4462                  ## As if </noscript>
4463                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4464                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4465                !!!next-token;                
4466                redo B;                ## Reprocess in the "in head" insertion mode...
4467              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
4468                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4469                $self->{insertion_mode} = 'in head';  
4470                !!!next-token;                ## Reprocess in the "after head" insertion mode...
4471                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4472              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
4473                       {                ## As if </head>
4474                  pop @{$self->{open_elements}};
4475    
4476                  ## Reprocess in the "after head" insertion mode...
4477                } else {
4478                  !!!cp ('t131');
4479                }
4480    
4481                ## "after head" insertion mode
4482                ## As if <body>
4483                !!!insert-element ('body',, $token);
4484                $self->{insertion_mode} = IN_BODY_IM;
4485                ## reprocess
4486                !!!ack-later;
4487                next B;
4488              } elsif ($token->{type} == END_TAG_TOKEN) {
4489                if ($token->{tag_name} eq 'head') {
4490                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4491                    !!!cp ('t132');
4492                    ## As if <head>
4493                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4494                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4495                    push @{$self->{open_elements}},
4496                        [$self->{head_element}, $el_category->{head}];
4497    
4498                    ## Reprocess in the "in head" insertion mode...
4499                    pop @{$self->{open_elements}};
4500                    $self->{insertion_mode} = AFTER_HEAD_IM;
4501                    !!!next-token;
4502                    next B;
4503                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504                    !!!cp ('t133');
4505                    ## As if </noscript>
4506                    pop @{$self->{open_elements}};
4507                    !!!parse-error (type => 'in noscript:/head', token => $token);
4508                    
4509                    ## Reprocess in the "in head" insertion mode...
4510                    pop @{$self->{open_elements}};
4511                    $self->{insertion_mode} = AFTER_HEAD_IM;
4512                    !!!next-token;
4513                    next B;
4514                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4515                    !!!cp ('t134');
4516                    pop @{$self->{open_elements}};
4517                    $self->{insertion_mode} = AFTER_HEAD_IM;
4518                    !!!next-token;
4519                    next B;
4520                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4521                    !!!cp ('t134.1');
4522                    !!!parse-error (type => 'unmatched end tag:head', token => $token);
4523                    ## Ignore the token
4524                    !!!next-token;
4525                    next B;
4526                  } else {
4527                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4528                  }
4529                } elsif ($token->{tag_name} eq 'noscript') {
4530                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4531                    !!!cp ('t136');
4532                    pop @{$self->{open_elements}};
4533                    $self->{insertion_mode} = IN_HEAD_IM;
4534                    !!!next-token;
4535                    next B;
4536                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4537                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4538                    !!!cp ('t137');
4539                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4540                    ## Ignore the token ## ISSUE: An issue in the spec.
4541                    !!!next-token;
4542                    next B;
4543                  } else {
4544                    !!!cp ('t138');
4545                    #
4546                  }
4547                } elsif ({
4548                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
4549                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4550                #                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4551              } elsif ($self->{insertion_mode} eq 'in head noscript' and                    $self->{insertion_mode} == IN_HEAD_IM or
4552                       {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4553                        p => 1, br => 1,                  !!!cp ('t140');
4554                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4555                #                  ## Ignore the token
4556              } elsif ($self->{insertion_mode} ne 'after head') {                  !!!next-token;
4557                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  next B;
4558                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4559                    !!!cp ('t140.1');
4560                    !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);
4561                    ## Ignore the token
4562                    !!!next-token;
4563                    next B;
4564                  } else {
4565                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4566                  }
4567                } elsif ($token->{tag_name} eq 'p') {
4568                  !!!cp ('t142');
4569                  !!!parse-error (type => 'unmatched end tag:p', token => $token);
4570                  ## Ignore the token
4571                  !!!next-token;
4572                  next B;
4573                } elsif ($token->{tag_name} eq 'br') {
4574                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4575                    !!!cp ('t142.2');
4576                    ## (before head) as if <head>, (in head) as if </head>
4577                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4578                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4579                    $self->{insertion_mode} = AFTER_HEAD_IM;
4580      
4581                    ## Reprocess in the "after head" insertion mode...
4582                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4583                    !!!cp ('t143.2');
4584                    ## As if </head>
4585                    pop @{$self->{open_elements}};
4586                    $self->{insertion_mode} = AFTER_HEAD_IM;
4587      
4588                    ## Reprocess in the "after head" insertion mode...
4589                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4590                    !!!cp ('t143.3');
4591                    ## ISSUE: Two parse errors for <head><noscript></br>
4592                    !!!parse-error (type => 'unmatched end tag:br', token => $token);
4593                    ## As if </noscript>
4594                    pop @{$self->{open_elements}};
4595                    $self->{insertion_mode} = IN_HEAD_IM;
4596    
4597                    ## Reprocess in the "in head" insertion mode...
4598                    ## As if </head>
4599                    pop @{$self->{open_elements}};
4600                    $self->{insertion_mode} = AFTER_HEAD_IM;
4601    
4602                    ## Reprocess in the "after head" insertion mode...
4603                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4604                    !!!cp ('t143.4');
4605                    #
4606                  } else {
4607                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4608                  }
4609    
4610                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4611                  !!!parse-error (type => 'unmatched end tag:br', token => $token);
4612                ## Ignore the token                ## Ignore the token
4613                !!!next-token;                !!!next-token;
4614                redo B;                next B;
4615              } else {              } else {
4616                #                !!!cp ('t145');
4617                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4618                  ## Ignore the token
4619                  !!!next-token;
4620                  next B;
4621              }              }
           } else {  
             #  
           }  
4622    
4623            ## As if </head> or </noscript> or <body>              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4624            if ($self->{insertion_mode} eq 'in head') {                !!!cp ('t146');
4625              pop @{$self->{open_elements}};                ## As if </noscript>
4626              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
4627            } elsif ($self->{insertion_mode} eq 'in head noscript') {                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4628              pop @{$self->{open_elements}};                
4629              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));                ## Reprocess in the "in head" insertion mode...
4630              $self->{insertion_mode} = 'in head';                ## As if </head>
4631            } else { # 'after head'                pop @{$self->{open_elements}};
4632              !!!insert-element ('body');  
4633              $self->{insertion_mode} = 'in body';                ## Reprocess in the "after head" insertion mode...
4634            }              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4635            ## reprocess                !!!cp ('t147');
4636            redo B;                ## As if </head>
4637                  pop @{$self->{open_elements}};
4638    
4639                  ## Reprocess in the "after head" insertion mode...
4640                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4641    ## ISSUE: This case cannot be reached?
4642                  !!!cp ('t148');
4643                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4644                  ## Ignore the token ## ISSUE: An issue in the spec.
4645                  !!!next-token;
4646                  next B;
4647                } else {
4648                  !!!cp ('t149');
4649                }
4650    
4651                ## "after head" insertion mode
4652                ## As if <body>
4653                !!!insert-element ('body',, $token);
4654                $self->{insertion_mode} = IN_BODY_IM;
4655                ## reprocess
4656                next B;
4657          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4658            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4659              !!!cp ('t149.1');
4660    
4661              ## NOTE: As if <head>
4662              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663              $self->{open_elements}->[-1]->[0]->append_child
4664                  ($self->{head_element});
4665              #push @{$self->{open_elements}},
4666              #    [$self->{head_element}, $el_category->{head}];
4667              #$self->{insertion_mode} = IN_HEAD_IM;
4668              ## NOTE: Reprocess.
4669    
4670              ## NOTE: As if </head>
4671              #pop @{$self->{open_elements}};
4672              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4673              ## NOTE: Reprocess.
4674              
4675              #
4676            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4677              !!!cp ('t149.2');
4678    
4679              ## NOTE: As if </head>
4680              pop @{$self->{open_elements}};
4681              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4682              ## NOTE: Reprocess.
4683    
4684              #
4685            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4686              !!!cp ('t149.3');
4687    
4688              !!!parse-error (type => 'in noscript:#eof', token => $token);
4689    
4690              ## As if </noscript>
4691              pop @{$self->{open_elements}};
4692              #$self->{insertion_mode} = IN_HEAD_IM;
4693              ## NOTE: Reprocess.
4694    
4695              ## NOTE: As if </head>
4696              pop @{$self->{open_elements}};
4697              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4698              ## NOTE: Reprocess.
4699    
4700              #
4701            } else {
4702              !!!cp ('t149.4');
4703              #
4704            }
4705    
4706            ## NOTE: As if <body>
4707            !!!insert-element ('body',, $token);
4708            $self->{insertion_mode} = IN_BODY_IM;
4709            ## NOTE: Reprocess.
4710            next B;
4711          } else {
4712            die "$0: $token->{type}: Unknown token type";
4713          }
4714    
4715            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4716          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4717                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
4718                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4719              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4720              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4721                            
4722              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4723    
4724              !!!next-token;              !!!next-token;
4725              redo B;              next B;
4726            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4727              if ({              if ({
4728                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4729                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4730                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4731                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4732                  ## have an element in table scope                  ## have an element in table scope
4733                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4734                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4735                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4736                      $tn = $node->[1];                      !!!cp ('t151');
4737                      last INSCOPE;  
4738                    } elsif ({                      ## Close the cell
4739                              table => 1, html => 1,                      !!!back-token; # <x>
4740                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4741                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4742                    }                                line => $token->{line},
4743                  } # INSCOPE                                column => $token->{column}};
4744                    unless (defined $tn) {                      next B;
4745                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4746                      ## Ignore the token                      !!!cp ('t152');
4747                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4748                      redo B;                      last;
4749                    }                    }
4750                    }
4751    
4752                    !!!cp ('t153');
4753                    !!!parse-error (type => 'start tag not allowed',
4754                        value => $token->{tag_name}, token => $token);
4755                    ## Ignore the token
4756                    !!!nack ('t153.1');
4757                    !!!next-token;
4758                    next B;
4759                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4760                    !!!parse-error (type => 'not closed:caption', token => $token);
4761                                    
4762                  ## Close the cell                  ## NOTE: As if </caption>.
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in caption') {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
4763                  ## have a table element in table scope                  ## have a table element in table scope
4764                  my $i;                  my $i;
4765                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4766                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4767                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4768                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4769                      last INSCOPE;                        !!!cp ('t155');
4770                    } elsif ({                        $i = $_;
4771                              table => 1, html => 1,                        last INSCOPE;
4772                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4773                      last INSCOPE;                        !!!cp ('t156');
4774                          last;
4775                        }
4776                    }                    }
4777    
4778                      !!!cp ('t157');
4779                      !!!parse-error (type => 'start tag not allowed',
4780                                      value => $token->{tag_name}, token => $token);
4781                      ## Ignore the token
4782                      !!!nack ('t157.1');
4783                      !!!next-token;
4784                      next B;
4785                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4786                                    
4787                  ## generate implied end tags                  ## generate implied end tags
4788                  if ({                  while ($self->{open_elements}->[-1]->[1]
4789                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4790                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4791                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4792                  }                  }
4793    
4794                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4795                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4796                      !!!parse-error (type => 'not closed',
4797                                      value => $self->{open_elements}->[-1]->[0]
4798                                          ->manakai_local_name,
4799                                      token => $token);
4800                    } else {
4801                      !!!cp ('t160');
4802                  }                  }
4803                                    
4804                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4805                                    
4806                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4807                                    
4808                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4809                                    
4810                  ## reprocess                  ## reprocess
4811                  redo B;                  !!!ack-later;
4812                    next B;
4813                } else {                } else {
4814                    !!!cp ('t161');
4815                  #                  #
4816                }                }
4817              } else {              } else {
4818                  !!!cp ('t162');
4819                #                #
4820              }              }
4821            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4822              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4823                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4824                  ## have an element in table scope                  ## have an element in table scope
4825                  my $i;                  my $i;
4826                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4827                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4828                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4829                        !!!cp ('t163');
4830                      $i = $_;                      $i = $_;
4831                      last INSCOPE;                      last INSCOPE;
4832                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4833                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4834                      last INSCOPE;                      last INSCOPE;
4835                    }                    }
4836                  } # INSCOPE                  } # INSCOPE
4837                    unless (defined $i) {                    unless (defined $i) {
4838                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4839                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4840                      ## Ignore the token                      ## Ignore the token
4841                      !!!next-token;                      !!!next-token;
4842                      redo B;                      next B;
4843                    }                    }
4844                                    
4845                  ## generate implied end tags                  ## generate implied end tags
4846                  if ({                  while ($self->{open_elements}->[-1]->[1]
4847                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4848                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4849                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4850                  }                  }
4851                    
4852                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4853                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4854                      !!!cp ('t167');
4855                      !!!parse-error (type => 'not closed',
4856                                      value => $self->{open_elements}->[-1]->[0]
4857                                          ->manakai_local_name,
4858                                      token => $token);
4859                    } else {
4860                      !!!cp ('t168');
4861                  }                  }
4862                                    
4863                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4864                                    
4865                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4866                                    
4867                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
4868                                    
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4872                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4873                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4874                  ## Ignore the token                  ## Ignore the token
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } else {                } else {
4878                    !!!cp ('t170');
4879                  #                  #
4880                }                }
4881              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4882                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4883                  ## have a table element in table scope                  ## have a table element in table scope
4884                  my $i;                  my $i;
4885                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4886                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4887                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4888                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4889                      last INSCOPE;                        !!!cp ('t171');
4890                    } elsif ({                        $i = $_;
4891                              table => 1, html => 1,                        last INSCOPE;
4892                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4893                      last INSCOPE;                        !!!cp ('t172');
4894                          last;
4895                        }
4896                    }                    }
4897    
4898                      !!!cp ('t173');
4899                      !!!parse-error (type => 'unmatched end tag',
4900                                      value => $token->{tag_name}, token => $token);
4901                      ## Ignore the token
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4909                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4910                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4911                  }                  }
4912                                    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4915                      !!!parse-error (type => 'not closed',
4916                                      value => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                    } else {
4920                      !!!cp ('t176');
4921                  }                  }
4922                                    
4923                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4924                                    
4925                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4926                                    
4927                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  !!!next-token;                  !!!next-token;
4930                  redo B;                  next B;
4931                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4932                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4933                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4934                  ## Ignore the token                  ## Ignore the token
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                    !!!cp ('t178');
4939                  #                  #
4940                }                }
4941              } elsif ({              } elsif ({
4942                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
4943                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4944                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4945                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
4946                ## have an element in table scope                ## have an element in table scope
4947                my $i;                my $i;
4948                my $tn;                my $tn;
4949                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4950                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4951                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4952                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4953                    last INSCOPE;                      !!!cp ('t179');
4954                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4955                    $tn = $node->[1];  
4956                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4957                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4958                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4959                            table => 1, html => 1,                                line => $token->{line},
4960                           }->{$node->[1]}) {                                column => $token->{column}};
4961                    last INSCOPE;                      next B;
4962                      } elsif ($node->[1] & TABLE_CELL_EL) {
4963                        !!!cp ('t180');
4964                        $tn = $node->[0]->manakai_local_name;
4965                        ## NOTE: There is exactly one |td| or |th| element
4966                        ## in scope in the stack of open elements by definition.
4967                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4968                        ## ISSUE: Can this be reached?
4969                        !!!cp ('t181');
4970                        last;
4971                      }
4972                  }                  }
4973                } # INSCOPE  
4974                unless (defined $i) {                  !!!cp ('t182');
4975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4976                        value => $token->{tag_name}, token => $token);
4977                  ## Ignore the token                  ## Ignore the token
4978                  !!!next-token;                  !!!next-token;
4979                  redo B;                  next B;
4980                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4981              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4982                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4983                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4984    
4985                ## As if </caption>                ## As if </caption>
4986                ## have a table element in table scope                ## have a table element in table scope
4987                my $i;                my $i;
4988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4990                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4991                      !!!cp ('t184');
4992                    $i = $_;                    $i = $_;
4993                    last INSCOPE;                    last INSCOPE;
4994                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4995                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4996                    last INSCOPE;                    last INSCOPE;
4997                  }                  }
4998                } # INSCOPE                } # INSCOPE
4999                unless (defined $i) {                unless (defined $i) {
5000                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5001                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
5002                  ## Ignore the token                  ## Ignore the token
5003                  !!!next-token;                  !!!next-token;
5004                  redo B;                  next B;
5005                }                }
5006                                
5007                ## generate implied end tags                ## generate implied end tags
5008                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5009                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5010                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5011                }                }
5012    
5013                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5014                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5015                    !!!parse-error (type => 'not closed',
5016                                    value => $self->{open_elements}->[-1]->[0]
5017                                        ->manakai_local_name,
5018                                    token => $token);
5019                  } else {
5020                    !!!cp ('t189');
5021                }                }
5022    
5023                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5024    
5025                $clear_up_to_marker->();                $clear_up_to_marker->();
5026    
5027                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5028    
5029                ## reprocess                ## reprocess
5030                redo B;                next B;
5031              } elsif ({              } elsif ({
5032                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5033                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5034                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5035                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5036                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5037                  ## Ignore the token                  ## Ignore the token
5038                  !!!next-token;                  !!!next-token;
5039                  redo B;                  next B;
5040                } else {                } else {
5041                    !!!cp ('t191');
5042                  #                  #
5043                }                }
5044              } elsif ({              } elsif ({
5045                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5046                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5047                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5048                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5049                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5050                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5051                ## Ignore the token                ## Ignore the token
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054              } else {              } else {
5055                  !!!cp ('t193');
5056                #                #
5057              }              }
5058            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5059              #          for my $entry (@{$self->{open_elements}}) {
5060              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5061                !!!cp ('t75');
5062                !!!parse-error (type => 'in body:#eof', token => $token);
5063                last;
5064            }            }
5065                      }
5066            $in_body->($insert_to_current);  
5067            redo B;          ## Stop parsing.
5068          } elsif ($self->{insertion_mode} eq 'in table') {          last B;
5069            if ($token->{type} eq 'character') {        } else {
5070              ## NOTE: There are "character in table" code clones.          die "$0: $token->{type}: Unknown token type";
5071              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        }
5072                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5073          $insert = $insert_to_current;
5074          #
5075        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5076          if ($token->{type} == CHARACTER_TOKEN) {
5077            if (not $open_tables->[-1]->[1] and # tainted
5078                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5079              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5080                                
5081                unless (length $token->{data}) {            unless (length $token->{data}) {
5082                  !!!next-token;              !!!cp ('t194');
5083                  redo B;              !!!next-token;
5084                }              next B;
5085              }            } else {
5086                !!!cp ('t195');
5087              }
5088            }
5089    
5090              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5091    
5092              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5093              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3989  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095              ## result in a new Text node.              ## result in a new Text node.
5096              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5097                            
5098              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5099                # MUST                # MUST
5100                my $foster_parent_element;                my $foster_parent_element;
5101                my $next_sibling;                my $next_sibling;
5102                my $prev_sibling;                my $prev_sibling;
5103                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5104                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5105                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5106                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5107                        !!!cp ('t196');
5108                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5109                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5110                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5111                    } else {                    } else {
5112                        !!!cp ('t197');
5113                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5114                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5115                    }                    }
# Line 4016  sub _tree_construction_main ($) { Line 5121  sub _tree_construction_main ($) {
5121                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5122                if (defined $prev_sibling and                if (defined $prev_sibling and
5123                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5124                    !!!cp ('t198');
5125                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5126                } else {                } else {
5127                    !!!cp ('t199');
5128                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5129                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5130                     $next_sibling);                     $next_sibling);
5131                }                }
5132              } else {            $open_tables->[-1]->[1] = 1; # tainted
5133                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5134              }            !!!cp ('t200');
5135              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5136            }
5137                            
5138              !!!next-token;          !!!next-token;
5139              redo B;          next B;
5140            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5141              if ({              if ({
5142                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
5143                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
5144                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5145                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
5146                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
5147                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while (not ($self->{open_elements}->[-1]->[1]
5148                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                  & TABLE_SCOPING_EL)) {
5149                  pop @{$self->{open_elements}};                    !!!cp ('t201');
5150                }                    pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5151                  }                  }
5152                } # INSCOPE                  
5153                unless (defined $i) {                  !!!insert-element ('tbody',, $token);
5154                  !!!parse-error (type => 'unmatched end tag:table');                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5155                  ## Ignore tokens </table><table>                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5156                }                }
5157    
5158                splice @{$self->{open_elements}}, $i;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5159                    unless ($token->{tag_name} eq 'tr') {
5160                $self->_reset_insertion_mode;                    !!!cp ('t202');
5161                      !!!parse-error (type => 'missing start tag:tr', token => $token);
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5162                  }                  }
5163                } # INSCOPE                  
5164                unless (defined $i) {                  ## Clear back to table body context
5165                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not ($self->{open_elements}->[-1]->[1]
5166                  ## Ignore the token                                  & TABLE_ROWS_SCOPING_EL)) {
5167                  !!!next-token;                    !!!cp ('t203');
5168                  redo B;                    ## ISSUE: Can this case be reached?
5169                }                    pop @{$self->{open_elements}};
5170                                  }
5171                ## generate implied end tags                  
5172                if ({                  $self->{insertion_mode} = IN_ROW_IM;
5173                     dd => 1, dt => 1, li => 1, p => 1,                  if ($token->{tag_name} eq 'tr') {
5174                     td => 1, th => 1, tr => 1,                    !!!cp ('t204');
5175                     tbody => 1, tfoot=> 1, thead => 1,                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5176                    }->{$self->{open_elements}->[-1]->[1]}) {                    !!!nack ('t204');
5177                  !!!back-token;                    !!!next-token;
5178                  $token = {type => 'end tag',                    next B;
5179                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  } else {
5180                  redo B;                    !!!cp ('t205');
5181                      !!!insert-element ('tr',, $token);
5182                      ## reprocess in the "in row" insertion mode
5183                    }
5184                  } else {
5185                    !!!cp ('t206');
5186                }                }
5187    
5188                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
5189                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                while (not ($self->{open_elements}->[-1]->[1]
5190                                  & TABLE_ROW_SCOPING_EL)) {
5191                    !!!cp ('t207');
5192                    pop @{$self->{open_elements}};
5193                }                }
5194                  
5195                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5196                  $self->{insertion_mode} = IN_CELL_IM;
5197    
5198                splice @{$self->{open_elements}}, $i;                push @$active_formatting_elements, ['#marker', ''];
5199                  
5200                $self->_reset_insertion_mode;                !!!nack ('t207.1');
   
5201                !!!next-token;                !!!next-token;
5202                redo B;                next B;
5203              } elsif ({              } elsif ({
5204                        body => 1, caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5205                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                        tbody => 1, tfoot => 1, thead => 1,
5206                        thead => 1, tr => 1,                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5207                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5208                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == IN_ROW_IM) {
5209                ## Ignore the token                  ## As if </tr>
5210                !!!next-token;                  ## have an element in table scope
5211                redo B;                  my $i;
5212              } else {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                #                    my $node = $self->{open_elements}->[$_];
5214              }                    if ($node->[1] & TABLE_ROW_EL) {
5215            } else {                      !!!cp ('t208');
5216              #                      $i = $_;
5217            }                      last INSCOPE;
5218                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5219            !!!parse-error (type => 'in table:'.$token->{tag_name});                      !!!cp ('t209');
5220            $in_body->($insert_to_foster);                      last INSCOPE;
5221            redo B;                    }
5222          } elsif ($self->{insertion_mode} eq 'in column group') {                  } # INSCOPE
5223            if ($token->{type} eq 'character') {                  unless (defined $i) {
5224              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                    !!!cp ('t210');
5225                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  ## TODO: This type is wrong.
5226                unless (length $token->{data}) {                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5227                  !!!next-token;                    ## Ignore the token
5228                  redo B;                    !!!nack ('t210.1');
5229                }                    !!!next-token;
5230              }                    next B;
5231                                }
5232              #                  
5233            } elsif ($token->{type} eq 'start tag') {                  ## Clear back to table row context
5234              if ($token->{tag_name} eq 'col') {                  while (not ($self->{open_elements}->[-1]->[1]
5235                !!!insert-element ($token->{tag_name}, $token->{attributes});                                  & TABLE_ROW_SCOPING_EL)) {
5236                pop @{$self->{open_elements}};                    !!!cp ('t211');
5237                !!!next-token;                    ## ISSUE: Can this case be reached?
5238                redo B;                    pop @{$self->{open_elements}};
5239              } else {                  }
5240                #                  
5241              }                  pop @{$self->{open_elements}}; # tr
5242            } elsif ($token->{type} eq 'end tag') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5243              if ($token->{tag_name} eq 'colgroup') {                  if ($token->{tag_name} eq 'tr') {
5244                if ($self->{open_elements}->[-1]->[1] eq 'html') {                    !!!cp ('t212');
5245                  !!!parse-error (type => 'unmatched end tag:colgroup');                    ## reprocess
5246                  ## Ignore the token                    !!!ack-later;
5247                  !!!next-token;                    next B;
5248                  redo B;                  } else {
5249                } else {                    !!!cp ('t213');
5250                  pop @{$self->{open_elements}}; # colgroup                    ## reprocess in the "in table body" insertion mode...
5251                  $self->{insertion_mode} = 'in table';                  }
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
5252                }                }
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
5253    
5254              if ({                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5255                   table => 1, tbody => 1, tfoot => 1,                  ## have an element in table scope
5256                   thead => 1, tr => 1,                  my $i;
5257                  }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5258                # MUST                    my $node = $self->{open_elements}->[$_];
5259                my $foster_parent_element;                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5260                my $next_sibling;                      !!!cp ('t214');
5261                my $prev_sibling;                      $i = $_;
5262                OE: for (reverse 0..$#{$self->{open_elements}}) {                      last INSCOPE;
5263                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5264                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                      !!!cp ('t215');
5265                    if (defined $parent and $parent->node_type == 1) {                      last INSCOPE;
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
5266                    }                    }
5267                    last OE;                  } # INSCOPE
5268                    unless (defined $i) {
5269                      !!!cp ('t216');
5270    ## TODO: This erorr type ios wrong.
5271                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5272                      ## Ignore the token
5273                      !!!nack ('t216.1');
5274                      !!!next-token;
5275                      next B;
5276                  }                  }
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
5277    
5278                ## Clear back to table body context                  ## Clear back to table body context
5279                while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5280                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5281                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5282                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5283                      pop @{$self->{open_elements}};
5284                    }
5285                    
5286                    ## As if <{current node}>
5287                    ## have an element in table scope
5288                    ## true by definition
5289                    
5290                    ## Clear back to table body context
5291                    ## nop by definition
5292                    
5293                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5294                }                  $self->{insertion_mode} = IN_TABLE_IM;
5295                                  ## reprocess in "in table" insertion mode...
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
5296                } else {                } else {
5297                  !!!insert-element ('tr');                  !!!cp ('t218');
                 ## reprocess  
5298                }                }
5299                redo B;  
5300              } elsif ({                if ($token->{tag_name} eq 'col') {
5301                        caption => 1, col => 1, colgroup => 1,                  ## Clear back to table context
5302                        tbody => 1, tfoot => 1, thead => 1,                  while (not ($self->{open_elements}->[-1]->[1]
5303                       }->{$token->{tag_name}}) {                                  & TABLE_SCOPING_EL)) {
5304                ## have an element in table scope                    !!!cp ('t219');
5305                my $i;                    ## ISSUE: Can this state be reached?
5306                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    pop @{$self->{open_elements}};
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5307                  }                  }
5308                } # INSCOPE                  
5309                unless (defined $i) {                  !!!insert-element ('colgroup',, $token);
5310                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5311                  ## Ignore the token                  ## reprocess
5312                    !!!ack-later;
5313                    next B;
5314                  } elsif ({
5315                            caption => 1,
5316                            colgroup => 1,
5317                            tbody => 1, tfoot => 1, thead => 1,
5318                           }->{$token->{tag_name}}) {
5319                    ## Clear back to table context
5320                    while (not ($self->{open_elements}->[-1]->[1]
5321                                    & TABLE_SCOPING_EL)) {
5322                      !!!cp ('t220');
5323                      ## ISSUE: Can this state be reached?
5324                      pop @{$self->{open_elements}};
5325                    }
5326                    
5327                    push @$active_formatting_elements, ['#marker', '']
5328                        if $token->{tag_name} eq 'caption';
5329                    
5330                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5331                    $self->{insertion_mode} = {
5332                                               caption => IN_CAPTION_IM,
5333                                               colgroup => IN_COLUMN_GROUP_IM,
5334                                               tbody => IN_TABLE_BODY_IM,
5335                                               tfoot => IN_TABLE_BODY_IM,
5336                                               thead => IN_TABLE_BODY_IM,
5337                                              }->{$token->{tag_name}};
5338                  !!!next-token;                  !!!next-token;
5339                  redo B;                  !!!nack ('t220.1');
5340                }                  next B;
5341                  } else {
5342                ## Clear back to table body context                  die "$0: in table: <>: $token->{tag_name}";
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5343                }                }
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
5344              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5345                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5346                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
5347                                      ->manakai_local_name,
5348                                  token => $token);
5349    
5350                ## As if </table>                ## As if </table>
5351                ## have a table element in table scope                ## have a table element in table scope
5352                my $i;                my $i;
5353                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5354                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5355                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5356                      !!!cp ('t221');
5357                    $i = $_;                    $i = $_;
5358                    last INSCOPE;                    last INSCOPE;
5359                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5360                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5361                    last INSCOPE;                    last INSCOPE;
5362                  }                  }
5363                } # INSCOPE                } # INSCOPE
5364                unless (defined $i) {                unless (defined $i) {
5365                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5366    ## TODO: The following is wrong, maybe.
5367                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5368                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5369                    !!!nack ('t223.1');
5370                  !!!next-token;                  !!!next-token;
5371                  redo B;                  next B;
5372                }                }
5373                                
5374    ## TODO: Followings are removed from the latest spec.
5375                ## generate implied end tags                ## generate implied end tags
5376                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5377                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5378                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5379                }                }
5380    
5381                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5382                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5383                    ## NOTE: |<table><tr><table>|
5384                    !!!parse-error (type => 'not closed',
5385                                    value => $self->{open_elements}->[-1]->[0]
5386                                        ->manakai_local_name,
5387                                    token => $token);
5388                  } else {
5389                    !!!cp ('t226');
5390                }                }
5391    
5392                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5393                  pop @{$open_tables};
5394    
5395                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5396    
5397                ## reprocess            ## reprocess
5398                redo B;            !!!ack-later;
5399              } else {            next B;
5400                #          } elsif ($token->{tag_name} eq 'style') {
5401              }            if (not $open_tables->[-1]->[1]) { # tainted
5402            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5403              if ({              ## NOTE: This is a "as if in head" code clone.
5404                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5405                  }->{$token->{tag_name}}) {              next B;
5406                ## have an element in table scope            } else {
5407                my $i;              !!!cp ('t227.7');
5408                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5409                  my $node = $self->{open_elements}->[$_];            }
5410                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5411                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5412                    last INSCOPE;              !!!cp ('t227.6');
5413                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5414                            table => 1, html => 1,              $script_start_tag->();
5415                           }->{$node->[1]}) {              next B;
5416                    last INSCOPE;            } else {
5417                  }              !!!cp ('t227.5');
5418                } # INSCOPE              #
5419                unless (defined $i) {            }
5420                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5421                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5422                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5423                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5424                }                if ($type eq 'hidden') {
5425                    !!!cp ('t227.3');
5426                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5427    
5428                ## 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}};  
               }  
5429    
5430                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;  
               }  
5431    
               ## 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]);  
5432                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
5433    
5434                ## Clear back to table body context                  !!!next-token;
5435                ## nop by definition                  !!!ack ('t227.2.1');
5436                    next B;
5437                pop @{$self->{open_elements}};                } else {
5438                $self->{insertion_mode} = 'in table';                  !!!cp ('t227.2');
5439                ## reprocess                  #
5440                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;  
5441              } else {              } else {
5442                  !!!cp ('t227.1');
5443                #                #
5444              }              }
5445            } else {            } else {
5446                !!!cp ('t227.4');
5447              #              #
5448            }            }
5449                      } else {
5450            ## As if in table            !!!cp ('t227');
5451            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5452            $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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
5453    
5454              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
5455    
5456                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
5457                          #
5458                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
5459                redo B;              if ($token->{tag_name} eq 'tr' and
5460              } elsif ({                  $self->{insertion_mode} == IN_ROW_IM) {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5461                ## have an element in table scope                ## have an element in table scope
5462                my $i;                my $i;
5463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5464                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5465                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5466                      !!!cp ('t228');
5467                    $i = $_;                    $i = $_;
5468                    last INSCOPE;                    last INSCOPE;
5469                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5470                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5471                    last INSCOPE;                    last INSCOPE;
5472                  }                  }
5473                } # INSCOPE                } # INSCOPE
5474                unless (defined $i) {                unless (defined $i) {
5475                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5476                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                  ## Ignore the token                  ## Ignore the token
5478                    !!!nack ('t230.1');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                  } else {
5482                    !!!cp ('t232');
5483                }                }
5484    
5485                ## Clear back to table row context                ## Clear back to table row context
5486                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5487                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5488                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5489                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5490                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5491                }                }
5492    
5493                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5494                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5495                ## reprocess                !!!next-token;
5496                redo B;                !!!nack ('t231.1');
5497                  next B;
5498              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5499                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5500                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5501                    ## have an element in table scope
5502                ## As if </table>                  my $i;
5503                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5504                my $i;                    my $node = $self->{open_elements}->[$_];
5505                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5506                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5507                  if ($node->[1] eq 'table') {                      $i = $_;
5508                    $i = $_;                      last INSCOPE;
5509                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5510                  } elsif ({                      !!!cp ('t234');
5511                            table => 1, html => 1,                      last INSCOPE;
5512                           }->{$node->[1]}) {                    }
5513                    last INSCOPE;                  } # INSCOPE
5514                    unless (defined $i) {
5515                      !!!cp ('t235');
5516    ## TODO: The following is wrong.
5517                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5518                      ## Ignore the token
5519                      !!!nack ('t236.1');
5520                      !!!next-token;
5521                      next B;
5522                  }                  }
5523                } # INSCOPE                  
5524                unless (defined $i) {                  ## Clear back to table row context
5525                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5526                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5527                  !!!next-token;                    !!!cp ('t236');
5528                  redo B;  ## ISSUE: Can this state be reached?
5529                }                    pop @{$self->{open_elements}};
5530                                  }
5531                ## generate implied end tags                  
5532                if ({                  pop @{$self->{open_elements}}; # tr
5533                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5534                     td => 1, th => 1, tr => 1,                  ## reprocess in the "in table body" insertion mode...
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5535                }                }
5536    
5537                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5538                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## have an element in table scope
5539                    my $i;
5540                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5541                      my $node = $self->{open_elements}->[$_];
5542                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5543                        !!!cp ('t237');
5544                        $i = $_;
5545                        last INSCOPE;
5546                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5547                        !!!cp ('t238');
5548                        last INSCOPE;
5549                      }
5550                    } # INSCOPE
5551                    unless (defined $i) {
5552                      !!!cp ('t239');
5553                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5554                      ## Ignore the token
5555                      !!!nack ('t239.1');
5556                      !!!next-token;
5557                      next B;
5558                    }
5559                    
5560                    ## Clear back to table body context
5561                    while (not ($self->{open_elements}->[-1]->[1]
5562                                    & TABLE_ROWS_SCOPING_EL)) {
5563                      !!!cp ('t240');
5564                      pop @{$self->{open_elements}};
5565                    }
5566                    
5567                    ## As if <{current node}>
5568                    ## have an element in table scope
5569                    ## true by definition
5570                    
5571                    ## Clear back to table body context
5572                    ## nop by definition
5573                    
5574                    pop @{$self->{open_elements}};
5575                    $self->{insertion_mode} = IN_TABLE_IM;
5576                    ## reprocess in the "in table" insertion mode...
5577                }                }
5578    
5579                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
5580                  ## When you edit the code fragment below, please ensure that
5581                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
5582                  ## is synced with it.
5583    
5584                ## reprocess                ## have a table element in table scope
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
5585                my $i;                my $i;
5586                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5587                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5588                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5589                      !!!cp ('t241');
5590                    $i = $_;                    $i = $_;
5591                    last INSCOPE;                    last INSCOPE;
5592                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5593                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5594                    last INSCOPE;                    last INSCOPE;
5595                  }                  }
5596                } # INSCOPE                } # INSCOPE
5597                unless (defined $i) {                unless (defined $i) {
5598                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5599                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5600                  ## Ignore the token                  ## Ignore the token
5601                    !!!nack ('t243.1');
5602                  !!!next-token;                  !!!next-token;
5603                  redo B;                  next B;
5604                }                }
5605                    
5606                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5607                while (not {                pop @{$open_tables};
5608                  tr => 1, html => 1,                
5609                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5610                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5611                  pop @{$self->{open_elements}};                !!!next-token;
5612                  next B;
5613                } elsif ({
5614                          tbody => 1, tfoot => 1, thead => 1,
5615                         }->{$token->{tag_name}} and
5616                         $self->{insertion_mode} & ROW_IMS) {
5617                  if ($self->{insertion_mode} == IN_ROW_IM) {
5618                    ## have an element in table scope
5619                    my $i;
5620                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5621                      my $node = $self->{open_elements}->[$_];
5622                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5623                        !!!cp ('t247');
5624                        $i = $_;
5625                        last INSCOPE;
5626                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5627                        !!!cp ('t248');
5628                        last INSCOPE;
5629                      }
5630                    } # INSCOPE
5631                      unless (defined $i) {
5632                        !!!cp ('t249');
5633                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5634                        ## Ignore the token
5635                        !!!nack ('t249.1');
5636                        !!!next-token;
5637                        next B;
5638                      }
5639                    
5640                    ## As if </tr>
5641                    ## have an element in table scope
5642                    my $i;
5643                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5644                      my $node = $self->{open_elements}->[$_];
5645                      if ($node->[1] & TABLE_ROW_EL) {
5646                        !!!cp ('t250');
5647                        $i = $_;
5648                        last INSCOPE;
5649                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5650                        !!!cp ('t251');
5651                        last INSCOPE;
5652                      }
5653                    } # INSCOPE
5654                      unless (defined $i) {
5655                        !!!cp ('t252');
5656                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5657                        ## Ignore the token
5658                        !!!nack ('t252.1');
5659                        !!!next-token;
5660                        next B;
5661                      }
5662                    
5663                    ## Clear back to table row context
5664                    while (not ($self->{open_elements}->[-1]->[1]
5665                                    & TABLE_ROW_SCOPING_EL)) {
5666                      !!!cp ('t253');
5667    ## ISSUE: Can this case be reached?
5668                      pop @{$self->{open_elements}};
5669                    }
5670                    
5671                    pop @{$self->{open_elements}}; # tr
5672                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5673                    ## reprocess in the "in table body" insertion mode...
5674                }                }
5675    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
5676                ## have an element in table scope                ## have an element in table scope
5677                my $i;                my $i;
5678                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5679                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5680                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5681                      !!!cp ('t254');
5682                    $i = $_;                    $i = $_;
5683                    last INSCOPE;                    last INSCOPE;
5684                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5685                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5686                    last INSCOPE;                    last INSCOPE;
5687                  }                  }
5688                } # INSCOPE                } # INSCOPE
5689                unless (defined $i) {                unless (defined $i) {
5690                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t256');
5691                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5692                  ## Ignore the token                  ## Ignore the token
5693                    !!!nack ('t256.1');
5694                  !!!next-token;                  !!!next-token;
5695                  redo B;                  next B;
5696                }                }
5697    
5698                ## Clear back to table row context                ## Clear back to table body context
5699                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5700                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5701                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5702                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5704                }                }
5705    
5706                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5707                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5708                ## reprocess                !!!nack ('t257.1');
5709                redo B;                !!!next-token;
5710                  next B;
5711              } elsif ({              } elsif ({
5712                        tbody => 1, tfoot => 1, thead => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5713                          html => 1, td => 1, th => 1,
5714                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5715                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5716                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5717                ## have an element in table scope            !!!cp ('t258');
5718                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5719                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## Ignore the token
5720                  my $node = $self->{open_elements}->[$_];            !!!nack ('t258.1');
5721                  if ($node->[1] eq $token->{tag_name}) {             !!!next-token;
5722                    $i = $_;            next B;
5723                    last INSCOPE;          } else {
5724                  } elsif ({            !!!cp ('t259');
5725                            table => 1, html => 1,            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5726                           }->{$node->[1]}) {  
5727                    last INSCOPE;            $insert = $insert_to_foster;
5728                  }            #
5729                } # INSCOPE          }
5730                unless (defined $i) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5731                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5732                  ## Ignore the token                  @{$self->{open_elements}} == 1) { # redundant, maybe
5733              !!!parse-error (type => 'in body:#eof', token => $token);
5734              !!!cp ('t259.1');
5735              #
5736            } else {
5737              !!!cp ('t259.2');
5738              #
5739            }
5740    
5741            ## Stop parsing
5742            last B;
5743          } else {
5744            die "$0: $token->{type}: Unknown token type";
5745          }
5746        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5747              if ($token->{type} == CHARACTER_TOKEN) {
5748                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5749                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5750                  unless (length $token->{data}) {
5751                    !!!cp ('t260');
5752                  !!!next-token;                  !!!next-token;
5753                  redo B;                  next B;
5754                }                }
5755                }
5756                ## As if </tr>              
5757                ## have an element in table scope              !!!cp ('t261');
5758                my $i;              #
5759                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5760                  my $node = $self->{open_elements}->[$_];              if ($token->{tag_name} eq 'col') {
5761                  if ($node->[1] eq 'tr') {                !!!cp ('t262');
5762                    $i = $_;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5763                    last INSCOPE;                pop @{$self->{open_elements}};
5764                  } elsif ({                !!!ack ('t262.1');
5765                            table => 1, html => 1,                !!!next-token;
5766                           }->{$node->[1]}) {                next B;
5767                    last INSCOPE;              } else {
5768                  }                !!!cp ('t263');
5769                } # INSCOPE                #
5770                unless (defined $i) {              }
5771                  !!!parse-error (type => 'unmatched end tag:tr');            } elsif ($token->{type} == END_TAG_TOKEN) {
5772                if ($token->{tag_name} eq 'colgroup') {
5773                  if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5774                    !!!cp ('t264');
5775                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5776                  ## Ignore the token                  ## Ignore the token
5777                  !!!next-token;                  !!!next-token;
5778                  redo B;                  next B;
5779                }                } else {
5780                    !!!cp ('t265');
5781                ## Clear back to table row context                  pop @{$self->{open_elements}}; # colgroup
5782                while (not {                  $self->{insertion_mode} = IN_TABLE_IM;
5783                  tr => 1, html => 1,                  !!!next-token;
5784                }->{$self->{open_elements}->[-1]->[1]}) {                  next B;            
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
5785                }                }
5786                } elsif ($token->{tag_name} eq 'col') {
5787                pop @{$self->{open_elements}}; # tr                !!!cp ('t266');
5788                $self->{insertion_mode} = 'in table body';                !!!parse-error (type => 'unmatched end tag:col', token => $token);
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5789                ## Ignore the token                ## Ignore the token
5790                !!!next-token;                !!!next-token;
5791                redo B;                next B;
5792              } else {              } else {
5793                #                !!!cp ('t267');
5794                  #
5795              }              }
5796            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5797              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5798            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5799              !!!cp ('t270.2');
5800              ## Stop parsing.
5801              last B;
5802            } else {
5803              ## NOTE: As if </colgroup>.
5804              !!!cp ('t270.1');
5805              pop @{$self->{open_elements}}; # colgroup
5806              $self->{insertion_mode} = IN_TABLE_IM;
5807              ## Reprocess.
5808              next B;
5809            }
5810          } else {
5811            die "$0: $token->{type}: Unknown token type";
5812          }
5813    
5814            ## As if in table            ## As if </colgroup>
5815            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5816            $in_body->($insert_to_foster);              !!!cp ('t269');
5817            redo B;  ## TODO: Wrong error type?
5818          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5819            if ($token->{type} eq 'character') {              ## Ignore the token
5820              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              !!!nack ('t269.1');
5821              !!!next-token;              !!!next-token;
5822              redo B;              next B;
5823            } elsif ($token->{type} eq 'start tag') {            } else {
5824              if ($token->{tag_name} eq 'option') {              !!!cp ('t270');
5825                if ($self->{open_elements}->[-1]->[1] eq 'option') {              pop @{$self->{open_elements}}; # colgroup
5826                  ## As if </option>              $self->{insertion_mode} = IN_TABLE_IM;
5827                  pop @{$self->{open_elements}};              !!!ack-later;
5828                }              ## reprocess
5829                next B;
5830              }
5831        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5832          if ($token->{type} == CHARACTER_TOKEN) {
5833            !!!cp ('t271');
5834            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5835            !!!next-token;
5836            next B;
5837          } elsif ($token->{type} == START_TAG_TOKEN) {
5838            if ($token->{tag_name} eq 'option') {
5839              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5840                !!!cp ('t272');
5841                ## As if </option>
5842                pop @{$self->{open_elements}};
5843              } else {
5844                !!!cp ('t273');
5845              }
5846    
5847                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5848                !!!next-token;            !!!nack ('t273.1');
5849                redo B;            !!!next-token;
5850              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5851                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5852                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5853                  pop @{$self->{open_elements}};              !!!cp ('t274');
5854                }              ## As if </option>
5855                pop @{$self->{open_elements}};
5856              } else {
5857                !!!cp ('t275');
5858              }
5859    
5860                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5861                  ## As if </optgroup>              !!!cp ('t276');
5862                  pop @{$self->{open_elements}};              ## As if </optgroup>
5863                }              pop @{$self->{open_elements}};
5864              } else {
5865                !!!cp ('t277');
5866              }
5867    
5868                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5869                !!!next-token;            !!!nack ('t277.1');
5870                redo B;            !!!next-token;
5871              } elsif ($token->{tag_name} eq 'select') {            next B;
5872                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5873                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5874                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5875                my $i;                    {
5876                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5877                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5878                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5879                    $i = $_;                    }->{$token->{tag_name}})) {
5880                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5881                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5882                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5883                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5884                    last INSCOPE;            ## have an element in table scope
5885                  }            my $i;
5886                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5887                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5888                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5889                  ## Ignore the token                !!!cp ('t278');
5890                  !!!next-token;                $i = $_;
5891                  redo B;                last INSCOPE;
5892                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5893                  !!!cp ('t279');
5894                  last INSCOPE;
5895                }
5896              } # INSCOPE
5897              unless (defined $i) {
5898                !!!cp ('t280');
5899                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5900                ## Ignore the token
5901                !!!nack ('t280.1');
5902                !!!next-token;
5903                next B;
5904              }
5905                                
5906                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5907              splice @{$self->{open_elements}}, $i;
5908    
5909                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5910    
5911                !!!next-token;            if ($token->{tag_name} eq 'select') {
5912                redo B;              !!!nack ('t281.2');
5913              } else {              !!!next-token;
5914                #              next B;
5915              } else {
5916                !!!cp ('t281.1');
5917                !!!ack-later;
5918                ## Reprocess the token.
5919                next B;
5920              }
5921            } else {
5922              !!!cp ('t282');
5923              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5924              ## Ignore the token
5925              !!!nack ('t282.1');
5926              !!!next-token;
5927              next B;
5928            }
5929          } elsif ($token->{type} == END_TAG_TOKEN) {
5930            if ($token->{tag_name} eq 'optgroup') {
5931              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5932                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5933                !!!cp ('t283');
5934                ## As if </option>
5935                splice @{$self->{open_elements}}, -2;
5936              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5937                !!!cp ('t284');
5938                pop @{$self->{open_elements}};
5939              } else {
5940                !!!cp ('t285');
5941                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5942                ## Ignore the token
5943              }
5944              !!!nack ('t285.1');
5945              !!!next-token;
5946              next B;
5947            } elsif ($token->{tag_name} eq 'option') {
5948              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5949                !!!cp ('t286');
5950                pop @{$self->{open_elements}};
5951              } else {
5952                !!!cp ('t287');
5953                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5954                ## Ignore the token
5955              }
5956              !!!nack ('t287.1');
5957              !!!next-token;
5958              next B;
5959            } elsif ($token->{tag_name} eq 'select') {
5960              ## have an element in table scope
5961              my $i;
5962              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5963                my $node = $self->{open_elements}->[$_];
5964                if ($node->[1] & SELECT_EL) {
5965                  !!!cp ('t288');
5966                  $i = $_;
5967                  last INSCOPE;
5968                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5969                  !!!cp ('t289');
5970                  last INSCOPE;
5971              }              }
5972            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5973              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5974                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5975                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5976                  ## As if </option>              ## Ignore the token
5977                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5978                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5979                  pop @{$self->{open_elements}};              next B;
5980                } 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;  
               }  
5981                                
5982                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5983              splice @{$self->{open_elements}}, $i;
5984    
5985                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5986    
5987                !!!next-token;            !!!nack ('t291.1');
5988                redo B;            !!!next-token;
5989              } elsif ({            next B;
5990                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5991                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5992                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5993                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5994                                   }->{$token->{tag_name}}) {
5995                ## have an element in table scope  ## TODO: The following is wrong?
5996                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;  
               }  
5997                                
5998                ## As if </select>            ## have an element in table scope
5999                ## have an element in table scope            my $i;
6000                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6001                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6002                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6003                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6004                    $i = $_;                $i = $_;
6005                    last INSCOPE;                last INSCOPE;
6006                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6007                            table => 1, html => 1,                !!!cp ('t293');
6008                           }->{$node->[1]}) {                last INSCOPE;
6009                    last INSCOPE;              }
6010                  }            } # INSCOPE
6011                } # INSCOPE            unless (defined $i) {
6012                unless (defined $i) {              !!!cp ('t294');
6013                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6014                  ## Ignore the </select> token              !!!nack ('t294.1');
6015                  !!!next-token; ## TODO: ok?              !!!next-token;
6016                  redo B;              next B;
6017                }            }
6018                                
6019                splice @{$self->{open_elements}}, $i;            ## As if </select>
6020              ## have an element in table scope
6021                $self->_reset_insertion_mode;            undef $i;
6022              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6023                ## reprocess              my $node = $self->{open_elements}->[$_];
6024                redo B;              if ($node->[1] & SELECT_EL) {
6025              } else {                !!!cp ('t295');
6026                #                $i = $_;
6027                  last INSCOPE;
6028                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6029    ## ISSUE: Can this state be reached?
6030                  !!!cp ('t296');
6031                  last INSCOPE;
6032              }              }
6033            } else {            } # INSCOPE
6034              #            unless (defined $i) {
6035                !!!cp ('t297');
6036    ## TODO: The following error type is correct?
6037                !!!parse-error (type => 'unmatched end tag:select', token => $token);
6038                ## Ignore the </select> token
6039                !!!nack ('t297.1');
6040                !!!next-token; ## TODO: ok?
6041                next B;
6042            }            }
6043                  
6044              !!!cp ('t298');
6045              splice @{$self->{open_elements}}, $i;
6046    
6047              $self->_reset_insertion_mode;
6048    
6049            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6050              ## reprocess
6051              next B;
6052            } else {
6053              !!!cp ('t299');
6054              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
6055            ## Ignore the token            ## Ignore the token
6056              !!!nack ('t299.3');
6057            !!!next-token;            !!!next-token;
6058            redo B;            next B;
6059          } elsif ($self->{insertion_mode} eq 'after body') {          }
6060            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6061              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6062                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6063                ## As if in body            !!!cp ('t299.1');
6064                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6065            } else {
6066              !!!cp ('t299.2');
6067            }
6068    
6069            ## Stop parsing.
6070            last B;
6071          } else {
6072            die "$0: $token->{type}: Unknown token type";
6073          }
6074        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6075          if ($token->{type} == CHARACTER_TOKEN) {
6076            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6077              my $data = $1;
6078              ## As if in body
6079              $reconstruct_active_formatting_elements->($insert_to_current);
6080                                
6081                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6082              
6083              unless (length $token->{data}) {
6084                !!!cp ('t300');
6085                !!!next-token;
6086                next B;
6087              }
6088            }
6089            
6090            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6091              !!!cp ('t301');
6092              !!!parse-error (type => 'after html:#character', token => $token);
6093    
6094                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6095                  !!!next-token;          } else {
6096                  redo B;            !!!cp ('t302');
6097                }          }
6098              }          
6099                        ## "after body" insertion mode
6100              #          !!!parse-error (type => 'after body:#character', token => $token);
6101              !!!parse-error (type => 'after body:#character');  
6102            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6103              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6104              #          next B;
6105            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6106              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6107                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6108                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6109                  ## Ignore the token            
6110                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
6111                  redo B;          } else {
6112                } else {            !!!cp ('t304');
6113                  $previous_insertion_mode = $self->{insertion_mode};          }
6114                  $self->{insertion_mode} = 'trailing end';  
6115                  !!!next-token;          ## "after body" insertion mode
6116                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6117                }  
6118              } else {          $self->{insertion_mode} = IN_BODY_IM;
6119                !!!parse-error (type => 'after body:/'.$token->{tag_name});          !!!ack-later;
6120              }          ## reprocess
6121            next B;
6122          } elsif ($token->{type} == END_TAG_TOKEN) {
6123            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6124              !!!cp ('t305');
6125              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6126              
6127              $self->{insertion_mode} = AFTER_BODY_IM;
6128              ## Reprocess in the "after body" insertion mode.
6129            } else {
6130              !!!cp ('t306');
6131            }
6132    
6133            ## "after body" insertion mode
6134            if ($token->{tag_name} eq 'html') {
6135              if (defined $self->{inner_html_node}) {
6136                !!!cp ('t307');
6137                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6138                ## Ignore the token
6139                !!!next-token;
6140                next B;
6141            } else {            } else {
6142              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6143                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6144                !!!next-token;
6145                next B;
6146            }            }
6147            } else {
6148              !!!cp ('t309');
6149              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6150    
6151            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6152            ## reprocess            ## reprocess
6153            redo B;            next B;
6154      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6155        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156            !!!cp ('t309.2');
6157            ## Stop parsing
6158            last B;
6159          } else {
6160            die "$0: $token->{type}: Unknown token type";
6161          }
6162        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6163          if ($token->{type} == CHARACTER_TOKEN) {
6164          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6165            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6166              
6167            unless (length $token->{data}) {            unless (length $token->{data}) {
6168                !!!cp ('t310');
6169              !!!next-token;              !!!next-token;
6170              redo B;              next B;
6171            }            }
6172          }          }
6173            
6174          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6175          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6176          !!!next-token;              !!!cp ('t311');
6177          redo B;              !!!parse-error (type => 'in frameset:#character', token => $token);
6178        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6179          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6180            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#character', token => $token);
6181              } else { # "after html frameset"
6182                !!!cp ('t313');
6183                !!!parse-error (type => 'after html:#character', token => $token);
6184    
6185                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6186                ## Reprocess in the "after frameset" insertion mode.
6187                !!!parse-error (type => 'after frameset:#character', token => $token);
6188              }
6189              
6190              ## Ignore the token.
6191              if (length $token->{data}) {
6192                !!!cp ('t314');
6193                ## reprocess the rest of characters
6194              } else {
6195                !!!cp ('t315');
6196                !!!next-token;
6197              }
6198              next B;
6199            }
6200            
6201            die qq[$0: Character "$token->{data}"];
6202          } elsif ($token->{type} == START_TAG_TOKEN) {
6203            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6204              !!!cp ('t316');
6205              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6206    
6207              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6208              ## Process in the "after frameset" insertion mode.
6209            } else {
6210              !!!cp ('t317');
6211            }
6212    
6213            if ($token->{tag_name} eq 'frameset' and
6214                $self->{insertion_mode} == IN_FRAMESET_IM) {
6215              !!!cp ('t318');
6216              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217              !!!nack ('t318.1');
6218            !!!next-token;            !!!next-token;
6219            redo B;            next B;
6220          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6221            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6222              !!!cp ('t319');
6223              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6224            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6225              !!!ack ('t319.1');
6226            !!!next-token;            !!!next-token;
6227            redo B;            next B;
6228          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6229            $in_body->($insert_to_current);            !!!cp ('t320');
6230            redo B;            ## NOTE: As if in body.
6231          } else {            $parse_rcdata->(CDATA_CONTENT_MODEL);
6232            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            next B;
6233            } else {
6234              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6235                !!!cp ('t321');
6236                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6237              } else {
6238                !!!cp ('t322');
6239                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6240              }
6241            ## Ignore the token            ## Ignore the token
6242              !!!nack ('t322.1');
6243            !!!next-token;            !!!next-token;
6244            redo B;            next B;
6245            }
6246          } elsif ($token->{type} == END_TAG_TOKEN) {
6247            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6248              !!!cp ('t323');
6249              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6250    
6251              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6252              ## Process in the "after frameset" insertion mode.
6253            } else {
6254              !!!cp ('t324');
6255          }          }
6256        } elsif ($token->{type} eq 'end tag') {  
6257          if ($token->{tag_name} eq 'frameset') {          if ($token->{tag_name} eq 'frameset' and
6258            if ($self->{open_elements}->[-1]->[1] eq 'html' and              $self->{insertion_mode} == IN_FRAMESET_IM) {
6259              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6260                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6261              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6262                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6263              ## Ignore the token              ## Ignore the token
6264              !!!next-token;              !!!next-token;
6265            } else {            } else {
6266                !!!cp ('t326');
6267              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6268              !!!next-token;              !!!next-token;
6269            }            }
6270    
6271            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6272                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6273              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6274                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6275              } else {
6276                !!!cp ('t328');
6277            }            }
6278            redo B;            next B;
6279            } elsif ($token->{tag_name} eq 'html' and
6280                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6281              !!!cp ('t329');
6282              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6283              !!!next-token;
6284              next B;
6285          } else {          } else {
6286            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6287                !!!cp ('t330');
6288                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6289              } else {
6290                !!!cp ('t331');
6291                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6292              }
6293            ## Ignore the token            ## Ignore the token
6294            !!!next-token;            !!!next-token;
6295            redo B;            next B;
6296            }
6297          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6298            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6299                    @{$self->{open_elements}} == 1) { # redundant, maybe
6300              !!!cp ('t331.1');
6301              !!!parse-error (type => 'in body:#eof', token => $token);
6302            } else {
6303              !!!cp ('t331.2');
6304          }          }
6305            
6306            ## Stop parsing
6307            last B;
6308        } else {        } else {
6309          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6310        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
6311    
6312                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6313                  !!!next-token;      } else {
6314                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6315                }      }
             }  
6316    
6317              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6318                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6319          if ($token->{tag_name} eq 'script') {
6320            !!!cp ('t332');
6321            ## NOTE: This is an "as if in head" code clone
6322            $script_start_tag->();
6323            next B;
6324          } elsif ($token->{tag_name} eq 'style') {
6325            !!!cp ('t333');
6326            ## NOTE: This is an "as if in head" code clone
6327            $parse_rcdata->(CDATA_CONTENT_MODEL);
6328            next B;
6329          } elsif ({
6330                    base => 1, link => 1,
6331                   }->{$token->{tag_name}}) {
6332            !!!cp ('t334');
6333            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6334            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6336            !!!ack ('t334.1');
6337            !!!next-token;
6338            next B;
6339          } elsif ($token->{tag_name} eq 'meta') {
6340            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6341            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6342            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6343    
6344                ## Ignore the token.          unless ($self->{confident}) {
6345                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6346                  ## reprocess the rest of characters              !!!cp ('t335');
6347                } else {              ## NOTE: Whether the encoding is supported or not is handled
6348                  !!!next-token;              ## in the {change_encoding} callback.
6349                }              $self->{change_encoding}
6350                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6351                
6352                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6353                    ->set_user_data (manakai_has_reference =>
6354                                         $token->{attributes}->{charset}
6355                                             ->{has_reference});
6356              } elsif ($token->{attributes}->{content}) {
6357                if ($token->{attributes}->{content}->{value}
6358                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6359                        [\x09-\x0D\x20]*=
6360                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6361                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6362                  !!!cp ('t336');
6363                  ## NOTE: Whether the encoding is supported or not is handled
6364                  ## in the {change_encoding} callback.
6365                  $self->{change_encoding}
6366                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6367                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6368                      ->set_user_data (manakai_has_reference =>
6369                                           $token->{attributes}->{content}
6370                                                 ->{has_reference});
6371              }              }
6372              }
6373            } else {
6374              if ($token->{attributes}->{charset}) {
6375                !!!cp ('t337');
6376                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6377                    ->set_user_data (manakai_has_reference =>
6378                                         $token->{attributes}->{charset}
6379                                             ->{has_reference});
6380              }
6381              if ($token->{attributes}->{content}) {
6382                !!!cp ('t338');
6383                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6384                    ->set_user_data (manakai_has_reference =>
6385                                         $token->{attributes}->{content}
6386                                             ->{has_reference});
6387              }
6388            }
6389    
6390          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6391        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6392          if ($token->{tag_name} eq 'noframes') {          next B;
6393            $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'title') {
6394            redo B;          !!!cp ('t341');
6395            ## NOTE: This is an "as if in head" code clone
6396            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6397            next B;
6398          } elsif ($token->{tag_name} eq 'body') {
6399            !!!parse-error (type => 'in body:body', token => $token);
6400                  
6401            if (@{$self->{open_elements}} == 1 or
6402                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6403              !!!cp ('t342');
6404              ## Ignore the token
6405          } else {          } else {
6406            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6407              for my $attr_name (keys %{$token->{attributes}}) {
6408                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6409                  !!!cp ('t343');
6410                  $body_el->set_attribute_ns
6411                    (undef, [undef, $attr_name],
6412                     $token->{attributes}->{$attr_name}->{value});
6413                }
6414              }
6415            }
6416            !!!nack ('t343.1');
6417            !!!next-token;
6418            next B;
6419          } elsif ({
6420                    address => 1, blockquote => 1, center => 1, dir => 1,
6421                    div => 1, dl => 1, fieldset => 1,
6422                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6423                    menu => 1, ol => 1, p => 1, ul => 1,
6424                    pre => 1, listing => 1,
6425                    form => 1,
6426                    table => 1,
6427                    hr => 1,
6428                   }->{$token->{tag_name}}) {
6429            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6430              !!!cp ('t350');
6431              !!!parse-error (type => 'in form:form', token => $token);
6432            ## Ignore the token            ## Ignore the token
6433              !!!nack ('t350.1');
6434            !!!next-token;            !!!next-token;
6435            redo B;            next B;
6436          }          }
6437        } elsif ($token->{type} eq 'end tag') {  
6438          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6439            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6440            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6441                !!!cp ('t344');
6442                !!!back-token; # <form>
6443                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6444                          line => $token->{line}, column => $token->{column}};
6445                next B;
6446              } elsif ($_->[1] & SCOPING_EL) {
6447                !!!cp ('t345');
6448                last INSCOPE;
6449              }
6450            } # INSCOPE
6451              
6452            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6453            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6454              !!!nack ('t346.1');
6455              !!!next-token;
6456              if ($token->{type} == CHARACTER_TOKEN) {
6457                $token->{data} =~ s/^\x0A//;
6458                unless (length $token->{data}) {
6459                  !!!cp ('t346');
6460                  !!!next-token;
6461                } else {
6462                  !!!cp ('t349');
6463                }
6464              } else {
6465                !!!cp ('t348');
6466              }
6467            } elsif ($token->{tag_name} eq 'form') {
6468              !!!cp ('t347.1');
6469              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6470    
6471              !!!nack ('t347.2');
6472              !!!next-token;
6473            } elsif ($token->{tag_name} eq 'table') {
6474              !!!cp ('t382');
6475              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6476              
6477              $self->{insertion_mode} = IN_TABLE_IM;
6478    
6479              !!!nack ('t382.1');
6480              !!!next-token;
6481            } elsif ($token->{tag_name} eq 'hr') {
6482              !!!cp ('t386');
6483              pop @{$self->{open_elements}};
6484            
6485              !!!nack ('t386.1');
6486            !!!next-token;            !!!next-token;
           redo B;  
6487          } else {          } else {
6488            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
6489              !!!next-token;
6490            }
6491            next B;
6492          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6493            ## has a p element in scope
6494            INSCOPE: for (reverse @{$self->{open_elements}}) {
6495              if ($_->[1] & P_EL) {
6496                !!!cp ('t353');
6497                !!!back-token; # <x>
6498                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6499                          line => $token->{line}, column => $token->{column}};
6500                next B;
6501              } elsif ($_->[1] & SCOPING_EL) {
6502                !!!cp ('t354');
6503                last INSCOPE;
6504              }
6505            } # INSCOPE
6506              
6507            ## Step 1
6508            my $i = -1;
6509            my $node = $self->{open_elements}->[$i];
6510            my $li_or_dtdd = {li => {li => 1},
6511                              dt => {dt => 1, dd => 1},
6512                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6513            LI: {
6514              ## Step 2
6515              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6516                if ($i != -1) {
6517                  !!!cp ('t355');
6518                  !!!parse-error (type => 'not closed',
6519                                  value => $self->{open_elements}->[-1]->[0]
6520                                      ->manakai_local_name,
6521                                  token => $token);
6522                } else {
6523                  !!!cp ('t356');
6524                }
6525                splice @{$self->{open_elements}}, $i;
6526                last LI;
6527              } else {
6528                !!!cp ('t357');
6529              }
6530              
6531              ## Step 3
6532              if (not ($node->[1] & FORMATTING_EL) and
6533                  #not $phrasing_category->{$node->[1]} and
6534                  ($node->[1] & SPECIAL_EL or
6535                   $node->[1] & SCOPING_EL) and
6536                  not ($node->[1] & ADDRESS_EL) and
6537                  not ($node->[1] & DIV_EL)) {
6538                !!!cp ('t358');
6539                last LI;
6540              }
6541              
6542              !!!cp ('t359');
6543              ## Step 4
6544              $i--;
6545              $node = $self->{open_elements}->[$i];
6546              redo LI;
6547            } # LI
6548              
6549            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6550            !!!nack ('t359.1');
6551            !!!next-token;
6552            next B;
6553          } elsif ($token->{tag_name} eq 'plaintext') {
6554            ## has a p element in scope
6555            INSCOPE: for (reverse @{$self->{open_elements}}) {
6556              if ($_->[1] & P_EL) {
6557                !!!cp ('t367');
6558                !!!back-token; # <plaintext>
6559                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6560                          line => $token->{line}, column => $token->{column}};
6561                next B;
6562              } elsif ($_->[1] & SCOPING_EL) {
6563                !!!cp ('t368');
6564                last INSCOPE;
6565              }
6566            } # INSCOPE
6567              
6568            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6569              
6570            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6571              
6572            !!!nack ('t368.1');
6573            !!!next-token;
6574            next B;
6575          } elsif ($token->{tag_name} eq 'a') {
6576            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6577              my $node = $active_formatting_elements->[$i];
6578              if ($node->[1] & A_EL) {
6579                !!!cp ('t371');
6580                !!!parse-error (type => 'in a:a', token => $token);
6581                
6582                !!!back-token; # <a>
6583                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6584                          line => $token->{line}, column => $token->{column}};
6585                $formatting_end_tag->($token);
6586                
6587                AFE2: for (reverse 0..$#$active_formatting_elements) {
6588                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6589                    !!!cp ('t372');
6590                    splice @$active_formatting_elements, $_, 1;
6591                    last AFE2;
6592                  }
6593                } # AFE2
6594                OE: for (reverse 0..$#{$self->{open_elements}}) {
6595                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6596                    !!!cp ('t373');
6597                    splice @{$self->{open_elements}}, $_, 1;
6598                    last OE;
6599                  }
6600                } # OE
6601                last AFE;
6602              } elsif ($node->[0] eq '#marker') {
6603                !!!cp ('t374');
6604                last AFE;
6605              }
6606            } # AFE
6607              
6608            $reconstruct_active_formatting_elements->($insert_to_current);
6609    
6610            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6611            push @$active_formatting_elements, $self->{open_elements}->[-1];
6612    
6613            !!!nack ('t374.1');
6614            !!!next-token;
6615            next B;
6616          } elsif ($token->{tag_name} eq 'nobr') {
6617            $reconstruct_active_formatting_elements->($insert_to_current);
6618    
6619            ## has a |nobr| element in scope
6620            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6621              my $node = $self->{open_elements}->[$_];
6622              if ($node->[1] & NOBR_EL) {
6623                !!!cp ('t376');
6624                !!!parse-error (type => 'in nobr:nobr', token => $token);
6625                !!!back-token; # <nobr>
6626                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6627                          line => $token->{line}, column => $token->{column}};
6628                next B;
6629              } elsif ($node->[1] & SCOPING_EL) {
6630                !!!cp ('t377');
6631                last INSCOPE;
6632              }
6633            } # INSCOPE
6634            
6635            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6636            push @$active_formatting_elements, $self->{open_elements}->[-1];
6637            
6638            !!!nack ('t377.1');
6639            !!!next-token;
6640            next B;
6641          } elsif ($token->{tag_name} eq 'button') {
6642            ## has a button element in scope
6643            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6644              my $node = $self->{open_elements}->[$_];
6645              if ($node->[1] & BUTTON_EL) {
6646                !!!cp ('t378');
6647                !!!parse-error (type => 'in button:button', token => $token);
6648                !!!back-token; # <button>
6649                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6650                          line => $token->{line}, column => $token->{column}};
6651                next B;
6652              } elsif ($node->[1] & SCOPING_EL) {
6653                !!!cp ('t379');
6654                last INSCOPE;
6655              }
6656            } # INSCOPE
6657              
6658            $reconstruct_active_formatting_elements->($insert_to_current);
6659              
6660            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6661    
6662            ## TODO: associate with $self->{form_element} if defined
6663    
6664            push @$active_formatting_elements, ['#marker', ''];
6665    
6666            !!!nack ('t379.1');
6667            !!!next-token;
6668            next B;
6669          } elsif ({
6670                    xmp => 1,
6671                    iframe => 1,
6672                    noembed => 1,
6673                    noframes => 1,
6674                    noscript => 0, ## TODO: 1 if scripting is enabled
6675                   }->{$token->{tag_name}}) {
6676            if ($token->{tag_name} eq 'xmp') {
6677              !!!cp ('t381');
6678              $reconstruct_active_formatting_elements->($insert_to_current);
6679            } else {
6680              !!!cp ('t399');
6681            }
6682            ## NOTE: There is an "as if in body" code clone.
6683            $parse_rcdata->(CDATA_CONTENT_MODEL);
6684            next B;
6685          } elsif ($token->{tag_name} eq 'isindex') {
6686            !!!parse-error (type => 'isindex', token => $token);
6687            
6688            if (defined $self->{form_element}) {
6689              !!!cp ('t389');
6690            ## Ignore the token            ## Ignore the token
6691              !!!nack ('t389'); ## NOTE: Not acknowledged.
6692              !!!next-token;
6693              next B;
6694            } else {
6695              my $at = $token->{attributes};
6696              my $form_attrs;
6697              $form_attrs->{action} = $at->{action} if $at->{action};
6698              my $prompt_attr = $at->{prompt};
6699              $at->{name} = {name => 'name', value => 'isindex'};
6700              delete $at->{action};
6701              delete $at->{prompt};
6702              my @tokens = (
6703                            {type => START_TAG_TOKEN, tag_name => 'form',
6704                             attributes => $form_attrs,
6705                             line => $token->{line}, column => $token->{column}},
6706                            {type => START_TAG_TOKEN, tag_name => 'hr',
6707                             line => $token->{line}, column => $token->{column}},
6708                            {type => START_TAG_TOKEN, tag_name => 'p',
6709                             line => $token->{line}, column => $token->{column}},
6710                            {type => START_TAG_TOKEN, tag_name => 'label',
6711                             line => $token->{line}, column => $token->{column}},
6712                           );
6713              if ($prompt_attr) {
6714                !!!cp ('t390');
6715                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6716                               #line => $token->{line}, column => $token->{column},
6717                              };
6718              } else {
6719                !!!cp ('t391');
6720                push @tokens, {type => CHARACTER_TOKEN,
6721                               data => 'This is a searchable index. Insert your search keywords here: ',
6722                               #line => $token->{line}, column => $token->{column},
6723                              }; # SHOULD
6724                ## TODO: make this configurable
6725              }
6726              push @tokens,
6727                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6728                             line => $token->{line}, column => $token->{column}},
6729                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6730                            {type => END_TAG_TOKEN, tag_name => 'label',
6731                             line => $token->{line}, column => $token->{column}},
6732                            {type => END_TAG_TOKEN, tag_name => 'p',
6733                             line => $token->{line}, column => $token->{column}},
6734                            {type => START_TAG_TOKEN, tag_name => 'hr',
6735                             line => $token->{line}, column => $token->{column}},
6736                            {type => END_TAG_TOKEN, tag_name => 'form',
6737                             line => $token->{line}, column => $token->{column}};
6738              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6739              !!!back-token (@tokens);
6740              !!!next-token;
6741              next B;
6742            }
6743          } elsif ($token->{tag_name} eq 'textarea') {
6744            my $tag_name = $token->{tag_name};
6745            my $el;
6746            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6747            
6748            ## TODO: $self->{form_element} if defined
6749            $self->{content_model} = RCDATA_CONTENT_MODEL;
6750            delete $self->{escape}; # MUST
6751            
6752            $insert->($el);
6753            
6754            my $text = '';
6755            !!!nack ('t392.1');
6756            !!!next-token;
6757            if ($token->{type} == CHARACTER_TOKEN) {
6758              $token->{data} =~ s/^\x0A//;
6759              unless (length $token->{data}) {
6760                !!!cp ('t392');
6761                !!!next-token;
6762              } else {
6763                !!!cp ('t393');
6764              }
6765            } else {
6766              !!!cp ('t394');
6767            }
6768            while ($token->{type} == CHARACTER_TOKEN) {
6769              !!!cp ('t395');
6770              $text .= $token->{data};
6771            !!!next-token;            !!!next-token;
           redo B;  
6772          }          }
6773            if (length $text) {
6774              !!!cp ('t396');
6775              $el->manakai_append_text ($text);
6776            }
6777            
6778            $self->{content_model} = PCDATA_CONTENT_MODEL;
6779            
6780            if ($token->{type} == END_TAG_TOKEN and
6781                $token->{tag_name} eq $tag_name) {
6782              !!!cp ('t397');
6783              ## Ignore the token
6784            } else {
6785              !!!cp ('t398');
6786              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6787            }
6788            !!!next-token;
6789            next B;
6790          } elsif ($token->{tag_name} eq 'math' or
6791                   $token->{tag_name} eq 'svg') {
6792            $reconstruct_active_formatting_elements->($insert_to_current);
6793    
6794            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6795    
6796            ## "adjust foreign attributes" - done in insert-element-f
6797            
6798            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6799            
6800            if ($self->{self_closing}) {
6801              pop @{$self->{open_elements}};
6802              !!!ack ('t398.1');
6803            } else {
6804              !!!cp ('t398.2');
6805              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6806              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6807              ## mode, "in body" (not "in foreign content") secondary insertion
6808              ## mode, maybe.
6809            }
6810    
6811            !!!next-token;
6812            next B;
6813          } elsif ({
6814                    caption => 1, col => 1, colgroup => 1, frame => 1,
6815                    frameset => 1, head => 1, option => 1, optgroup => 1,
6816                    tbody => 1, td => 1, tfoot => 1, th => 1,
6817                    thead => 1, tr => 1,
6818                   }->{$token->{tag_name}}) {
6819            !!!cp ('t401');
6820            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6821            ## Ignore the token
6822            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6823            !!!next-token;
6824            next B;
6825            
6826            ## ISSUE: An issue on HTML5 new elements in the spec.
6827        } else {        } else {
6828          die "$0: $token->{type}: Unknown token type";          if ($token->{tag_name} eq 'image') {
6829              !!!cp ('t384');
6830              !!!parse-error (type => 'image', token => $token);
6831              $token->{tag_name} = 'img';
6832            } else {
6833              !!!cp ('t385');
6834            }
6835    
6836            ## NOTE: There is an "as if <br>" code clone.
6837            $reconstruct_active_formatting_elements->($insert_to_current);
6838            
6839            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6840    
6841            if ({
6842                 applet => 1, marquee => 1, object => 1,
6843                }->{$token->{tag_name}}) {
6844              !!!cp ('t380');
6845              push @$active_formatting_elements, ['#marker', ''];
6846              !!!nack ('t380.1');
6847            } elsif ({
6848                      b => 1, big => 1, em => 1, font => 1, i => 1,
6849                      s => 1, small => 1, strile => 1,
6850                      strong => 1, tt => 1, u => 1,
6851                     }->{$token->{tag_name}}) {
6852              !!!cp ('t375');
6853              push @$active_formatting_elements, $self->{open_elements}->[-1];
6854              !!!nack ('t375.1');
6855            } elsif ($token->{tag_name} eq 'input') {
6856              !!!cp ('t388');
6857              ## TODO: associate with $self->{form_element} if defined
6858              pop @{$self->{open_elements}};
6859              !!!ack ('t388.2');
6860            } elsif ({
6861                      area => 1, basefont => 1, bgsound => 1, br => 1,
6862                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6863                      #image => 1,
6864                     }->{$token->{tag_name}}) {
6865              !!!cp ('t388.1');
6866              pop @{$self->{open_elements}};
6867              !!!ack ('t388.3');
6868            } elsif ($token->{tag_name} eq 'select') {
6869              ## TODO: associate with $self->{form_element} if defined
6870            
6871              if ($self->{insertion_mode} & TABLE_IMS or
6872                  $self->{insertion_mode} & BODY_TABLE_IMS or
6873                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6874                !!!cp ('t400.1');
6875                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6876              } else {
6877                !!!cp ('t400.2');
6878                $self->{insertion_mode} = IN_SELECT_IM;
6879              }
6880              !!!nack ('t400.3');
6881            } else {
6882              !!!nack ('t402');
6883            }
6884            
6885            !!!next-token;
6886            next B;
6887        }        }
6888        } elsif ($token->{type} == END_TAG_TOKEN) {
6889          if ($token->{tag_name} eq 'body') {
6890            ## has a |body| element in scope
6891            my $i;
6892            INSCOPE: {
6893              for (reverse @{$self->{open_elements}}) {
6894                if ($_->[1] & BODY_EL) {
6895                  !!!cp ('t405');
6896                  $i = $_;
6897                  last INSCOPE;
6898                } elsif ($_->[1] & SCOPING_EL) {
6899                  !!!cp ('t405.1');
6900                  last;
6901                }
6902              }
6903    
6904        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6905      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name}, token => $token);
6906        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6907                    !!!next-token;
6908        if ($token->{type} eq 'character') {            next B;
6909          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6910            my $data = $1;  
6911            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6912            ## NOTE: The insertion mode in the main phase            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6913            ## just before the phase has been changed to the trailing              !!!cp ('t403');
6914            ## end phase is either "after body" or "after frameset".              !!!parse-error (type => 'not closed',
6915            $reconstruct_active_formatting_elements->($insert_to_current);                              value => $_->[0]->manakai_local_name,
6916                                token => $token);
6917                last;
6918              } else {
6919                !!!cp ('t404');
6920              }
6921            }
6922    
6923            $self->{insertion_mode} = AFTER_BODY_IM;
6924            !!!next-token;
6925            next B;
6926          } elsif ($token->{tag_name} eq 'html') {
6927            ## TODO: Update this code.  It seems that the code below is not
6928            ## up-to-date, though it has same effect as speced.
6929            if (@{$self->{open_elements}} > 1 and
6930                $self->{open_elements}->[1]->[1] & BODY_EL) {
6931              ## ISSUE: There is an issue in the spec.
6932              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6933                !!!cp ('t406');
6934                !!!parse-error (type => 'not closed',
6935                                value => $self->{open_elements}->[1]->[0]
6936                                    ->manakai_local_name,
6937                                token => $token);
6938              } else {
6939                !!!cp ('t407');
6940              }
6941              $self->{insertion_mode} = AFTER_BODY_IM;
6942              ## reprocess
6943              next B;
6944            } else {
6945              !!!cp ('t408');
6946              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6947              ## Ignore the token
6948              !!!next-token;
6949              next B;
6950            }
6951          } elsif ({
6952                    address => 1, blockquote => 1, center => 1, dir => 1,
6953                    div => 1, dl => 1, fieldset => 1, listing => 1,
6954                    menu => 1, ol => 1, pre => 1, ul => 1,
6955                    dd => 1, dt => 1, li => 1,
6956                    applet => 1, button => 1, marquee => 1, object => 1,
6957                   }->{$token->{tag_name}}) {
6958            ## has an element in scope
6959            my $i;
6960            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6961              my $node = $self->{open_elements}->[$_];
6962              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6963                !!!cp ('t410');
6964                $i = $_;
6965                last INSCOPE;
6966              } elsif ($node->[1] & SCOPING_EL) {
6967                !!!cp ('t411');
6968                last INSCOPE;
6969              }
6970            } # INSCOPE
6971    
6972            unless (defined $i) { # has an element in scope
6973              !!!cp ('t413');
6974              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6975            } else {
6976              ## Step 1. generate implied end tags
6977              while ({
6978                      dd => ($token->{tag_name} ne 'dd'),
6979                      dt => ($token->{tag_name} ne 'dt'),
6980                      li => ($token->{tag_name} ne 'li'),
6981                      p => 1,
6982                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6983                !!!cp ('t409');
6984                pop @{$self->{open_elements}};
6985              }
6986    
6987              ## Step 2.
6988              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6989                      ne $token->{tag_name}) {
6990                !!!cp ('t412');
6991                !!!parse-error (type => 'not closed',
6992                                value => $self->{open_elements}->[-1]->[0]
6993                                    ->manakai_local_name,
6994                                token => $token);
6995              } else {
6996                !!!cp ('t414');
6997              }
6998    
6999              ## Step 3.
7000              splice @{$self->{open_elements}}, $i;
7001    
7002              ## Step 4.
7003              $clear_up_to_marker->()
7004                  if {
7005                    applet => 1, button => 1, marquee => 1, object => 1,
7006                  }->{$token->{tag_name}};
7007            }
7008            !!!next-token;
7009            next B;
7010          } elsif ($token->{tag_name} eq 'form') {
7011            undef $self->{form_element};
7012    
7013            ## has an element in scope
7014            my $i;
7015            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7016              my $node = $self->{open_elements}->[$_];
7017              if ($node->[1] & FORM_EL) {
7018                !!!cp ('t418');
7019                $i = $_;
7020                last INSCOPE;
7021              } elsif ($node->[1] & SCOPING_EL) {
7022                !!!cp ('t419');
7023                last INSCOPE;
7024              }
7025            } # INSCOPE
7026    
7027            unless (defined $i) { # has an element in scope
7028              !!!cp ('t421');
7029              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7030            } else {
7031              ## Step 1. generate implied end tags
7032              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7033                !!!cp ('t417');
7034                pop @{$self->{open_elements}};
7035              }
7036                        
7037            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7038              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7039                      ne $token->{tag_name}) {
7040                !!!cp ('t417.1');
7041                !!!parse-error (type => 'not closed',
7042                                value => $self->{open_elements}->[-1]->[0]
7043                                    ->manakai_local_name,
7044                                token => $token);
7045              } else {
7046                !!!cp ('t420');
7047              }  
7048                        
7049            unless (length $token->{data}) {            ## Step 3.
7050              !!!next-token;            splice @{$self->{open_elements}}, $i;
7051              redo B;          }
7052    
7053            !!!next-token;
7054            next B;
7055          } elsif ({
7056                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7057                   }->{$token->{tag_name}}) {
7058            ## has an element in scope
7059            my $i;
7060            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061              my $node = $self->{open_elements}->[$_];
7062              if ($node->[1] & HEADING_EL) {
7063                !!!cp ('t423');
7064                $i = $_;
7065                last INSCOPE;
7066              } elsif ($node->[1] & SCOPING_EL) {
7067                !!!cp ('t424');
7068                last INSCOPE;
7069              }
7070            } # INSCOPE
7071    
7072            unless (defined $i) { # has an element in scope
7073              !!!cp ('t425.1');
7074              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7075            } else {
7076              ## Step 1. generate implied end tags
7077              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7078                !!!cp ('t422');
7079                pop @{$self->{open_elements}};
7080              }
7081              
7082              ## Step 2.
7083              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7084                      ne $token->{tag_name}) {
7085                !!!cp ('t425');
7086                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7087              } else {
7088                !!!cp ('t426');
7089            }            }
7090    
7091              ## Step 3.
7092              splice @{$self->{open_elements}}, $i;
7093          }          }
7094            
7095            !!!next-token;
7096            next B;
7097          } elsif ($token->{tag_name} eq 'p') {
7098            ## has an element in scope
7099            my $i;
7100            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7101              my $node = $self->{open_elements}->[$_];
7102              if ($node->[1] & P_EL) {
7103                !!!cp ('t410.1');
7104                $i = $_;
7105                last INSCOPE;
7106              } elsif ($node->[1] & SCOPING_EL) {
7107                !!!cp ('t411.1');
7108                last INSCOPE;
7109              }
7110            } # INSCOPE
7111    
7112          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7113          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7114          ## reprocess                    ne $token->{tag_name}) {
7115          redo B;              !!!cp ('t412.1');
7116        } elsif ($token->{type} eq 'start tag') {              !!!parse-error (type => 'not closed',
7117          !!!parse-error (type => 'after html:'.$token->{tag_name});                              value => $self->{open_elements}->[-1]->[0]
7118          $self->{insertion_mode} = $previous_insertion_mode;                                  ->manakai_local_name,
7119          ## reprocess                              token => $token);
7120          redo B;            } else {
7121        } elsif ($token->{type} eq 'end tag') {              !!!cp ('t414.1');
7122          !!!parse-error (type => 'after html:/'.$token->{tag_name});            }
7123          $self->{insertion_mode} = $previous_insertion_mode;  
7124          ## reprocess            splice @{$self->{open_elements}}, $i;
7125          redo B;          } else {
7126              !!!cp ('t413.1');
7127              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7128    
7129              !!!cp ('t415.1');
7130              ## As if <p>, then reprocess the current token
7131              my $el;
7132              !!!create-element ($el, $HTML_NS, 'p',, $token);
7133              $insert->($el);
7134              ## NOTE: Not inserted into |$self->{open_elements}|.
7135            }
7136    
7137            !!!next-token;
7138            next B;
7139          } elsif ({
7140                    a => 1,
7141                    b => 1, big => 1, em => 1, font => 1, i => 1,
7142                    nobr => 1, s => 1, small => 1, strile => 1,
7143                    strong => 1, tt => 1, u => 1,
7144                   }->{$token->{tag_name}}) {
7145            !!!cp ('t427');
7146            $formatting_end_tag->($token);
7147            next B;
7148          } elsif ($token->{tag_name} eq 'br') {
7149            !!!cp ('t428');
7150            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7151    
7152            ## As if <br>
7153            $reconstruct_active_formatting_elements->($insert_to_current);
7154            
7155            my $el;
7156            !!!create-element ($el, $HTML_NS, 'br',, $token);
7157            $insert->($el);
7158            
7159            ## Ignore the token.
7160            !!!next-token;
7161            next B;
7162          } elsif ({
7163                    caption => 1, col => 1, colgroup => 1, frame => 1,
7164                    frameset => 1, head => 1, option => 1, optgroup => 1,
7165                    tbody => 1, td => 1, tfoot => 1, th => 1,
7166                    thead => 1, tr => 1,
7167                    area => 1, basefont => 1, bgsound => 1,
7168                    embed => 1, hr => 1, iframe => 1, image => 1,
7169                    img => 1, input => 1, isindex => 1, noembed => 1,
7170                    noframes => 1, param => 1, select => 1, spacer => 1,
7171                    table => 1, textarea => 1, wbr => 1,
7172                    noscript => 0, ## TODO: if scripting is enabled
7173                   }->{$token->{tag_name}}) {
7174            !!!cp ('t429');
7175            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7176            ## Ignore the token
7177            !!!next-token;
7178            next B;
7179            
7180            ## ISSUE: Issue on HTML5 new elements in spec
7181            
7182        } else {        } else {
7183          die "$0: $token->{type}: Unknown token";          ## Step 1
7184            my $node_i = -1;
7185            my $node = $self->{open_elements}->[$node_i];
7186    
7187            ## Step 2
7188            S2: {
7189              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7190                ## Step 1
7191                ## generate implied end tags
7192                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7193                  !!!cp ('t430');
7194                  ## ISSUE: Can this case be reached?
7195                  pop @{$self->{open_elements}};
7196                }
7197            
7198                ## Step 2
7199                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7200                        ne $token->{tag_name}) {
7201                  !!!cp ('t431');
7202                  ## NOTE: <x><y></x>
7203                  !!!parse-error (type => 'not closed',
7204                                  value => $self->{open_elements}->[-1]->[0]
7205                                      ->manakai_local_name,
7206                                  token => $token);
7207                } else {
7208                  !!!cp ('t432');
7209                }
7210                
7211                ## Step 3
7212                splice @{$self->{open_elements}}, $node_i;
7213    
7214                !!!next-token;
7215                last S2;
7216              } else {
7217                ## Step 3
7218                if (not ($node->[1] & FORMATTING_EL) and
7219                    #not $phrasing_category->{$node->[1]} and
7220                    ($node->[1] & SPECIAL_EL or
7221                     $node->[1] & SCOPING_EL)) {
7222                  !!!cp ('t433');
7223                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7224                  ## Ignore the token
7225                  !!!next-token;
7226                  last S2;
7227                }
7228    
7229                !!!cp ('t434');
7230              }
7231              
7232              ## Step 4
7233              $node_i--;
7234              $node = $self->{open_elements}->[$node_i];
7235              
7236              ## Step 5;
7237              redo S2;
7238            } # S2
7239            next B;
7240        }        }
7241      } else {      }
7242        die "$0: $self->{insertion_mode}: Unknown insertion mode";      next B;
7243      } continue { # B
7244        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7245          ## NOTE: The code below is executed in cases where it does not have
7246          ## to be, but it it is harmless even in those cases.
7247          ## has an element in scope
7248          INSCOPE: {
7249            for (reverse 0..$#{$self->{open_elements}}) {
7250              my $node = $self->{open_elements}->[$_];
7251              if ($node->[1] & FOREIGN_EL) {
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                last;
7255              }
7256            }
7257            
7258            ## NOTE: No foreign element in scope.
7259            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7260          } # INSCOPE
7261      }      }
7262    } # B    } # B
7263    
# Line 5211  sub set_inner_html ($$$) { Line 7272  sub set_inner_html ($$$) {
7272    my $s = \$_[0];    my $s = \$_[0];
7273    my $onerror = $_[1];    my $onerror = $_[1];
7274    
7275      ## ISSUE: Should {confident} be true?
7276    
7277    my $nt = $node->node_type;    my $nt = $node->node_type;
7278    if ($nt == 9) {    if ($nt == 9) {
7279      # MUST      # MUST
# Line 5239  sub set_inner_html ($$$) { Line 7302  sub set_inner_html ($$$) {
7302      my $p = $class->new;      my $p = $class->new;
7303      $p->{document} = $doc;      $p->{document} = $doc;
7304    
7305      ## Step 9 # MUST      ## Step 8 # MUST
7306      my $i = 0;      my $i = 0;
7307      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7308      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7309      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7310        my $self = shift;        my $self = shift;
7311    
7312        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7313        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7314    
7315          $self->{next_char} = -1 and return if $i >= length $$s;
7316          $self->{next_char} = ord substr $$s, $i++, 1;
7317    
7318        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7319        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7320        $column++;  
7321          if ($self->{next_char} == 0x000A) { # LF
7322        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7323          $line++;          $p->{column} = 0;
7324          $column = 0;          !!!cp ('i1');
7325        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7326          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7327          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7328          $line++;          $p->{line}++;
7329          $column = 0;          $p->{column} = 0;
7330        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7331          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7332        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7333            !!!cp ('i3');
7334          } elsif ($self->{next_char} == 0x0000) { # NULL
7335            !!!cp ('i4');
7336          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7337          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7338          } elsif ($self->{next_char} <= 0x0008 or
7339                   (0x000E <= $self->{next_char} and
7340                    $self->{next_char} <= 0x001F) or
7341                   (0x007F <= $self->{next_char} and
7342                    $self->{next_char} <= 0x009F) or
7343                   (0xD800 <= $self->{next_char} and
7344                    $self->{next_char} <= 0xDFFF) or
7345                   (0xFDD0 <= $self->{next_char} and
7346                    $self->{next_char} <= 0xFDDF) or
7347                   {
7348                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7349                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7350                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7351                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7352                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7353                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7354                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7355                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7356                    0x10FFFE => 1, 0x10FFFF => 1,
7357                   }->{$self->{next_char}}) {
7358            !!!cp ('i4.1');
7359            !!!parse-error (type => 'control char', level => $self->{must_level});
7360    ## TODO: error type documentation
7361        }        }
7362      };      };
7363      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7364      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7365            
7366      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7367        my (%opt) = @_;        my (%opt) = @_;
7368        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7369          my $column = $opt{column};
7370          if (defined $opt{token} and defined $opt{token}->{line}) {
7371            $line = $opt{token}->{line};
7372            $column = $opt{token}->{column};
7373          }
7374          warn "Parse error ($opt{type}) at line $line column $column\n";
7375      };      };
7376      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7377        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7378      };      };
7379            
7380      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7381      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7382    
7383      ## Step 2      ## Step 2
7384      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7385      $p->{content_model} = {      $p->{content_model} = {
7386        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7387        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5300  sub set_inner_html ($$$) { Line 7398  sub set_inner_html ($$$) {
7398          unless defined $p->{content_model};          unless defined $p->{content_model};
7399          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7400    
7401      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7402          ## TODO: Foreign element OK?
7403    
7404      ## Step 4      ## Step 3
7405      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7406        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7407    
7408      ## Step 5 # MUST      ## Step 4 # MUST
7409      $doc->append_child ($root);      $doc->append_child ($root);
7410    
7411      ## Step 6 # MUST      ## Step 5 # MUST
7412      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7413    
7414      undef $p->{head_element};      undef $p->{head_element};
7415    
7416      ## Step 7 # MUST      ## Step 6 # MUST
7417      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7418    
7419      ## Step 8 # MUST      ## Step 7 # MUST
7420      my $anode = $node;      my $anode = $node;
7421      AN: while (defined $anode) {      AN: while (defined $anode) {
7422        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7423          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7424          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7425            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7426                !!!cp ('i5');
7427              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7428              last AN;              last AN;
7429            }            }
# Line 5332  sub set_inner_html ($$$) { Line 7432  sub set_inner_html ($$$) {
7432        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7433      } # AN      } # AN
7434            
7435      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7436      {      {
7437        my $self = $p;        my $self = $p;
7438        !!!next-token;        !!!next-token;
7439      }      }
7440      $p->_tree_construction_main;      $p->_tree_construction_main;
7441    
7442      ## Step 11 # MUST      ## Step 10 # MUST
7443      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7444      for (@cn) {      for (@cn) {
7445        $node->remove_child ($_);        $node->remove_child ($_);
7446      }      }
7447      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7448    
7449      ## Step 12 # MUST      ## Step 11 # MUST
7450      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7451      for (@cn) {      for (@cn) {
7452        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5356  sub set_inner_html ($$$) { Line 7455  sub set_inner_html ($$$) {
7455      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7456    
7457      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7458    
7459        delete $p->{parse_error}; # delete loop
7460    } else {    } else {
7461      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
7462    }    }
# Line 5363  sub set_inner_html ($$$) { Line 7464  sub set_inner_html ($$$) {
7464    
7465  } # tree construction stage  } # tree construction stage
7466    
7467  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7468    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  
7469    
7470  1;  1;
7471  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24