/[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.44 by wakaba, Sat Jul 21 07:34:32 2007 UTC revision 1.160 by wakaba, Wed Sep 10 10:27:08 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $byte_stream = $_[0];
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
361    
362  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
363    my $self = shift->new;      my (%opt) = @_;
364    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
365      };
366      $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375        ## NOTE: By setting |allow_fallback| option true when the
376        ## |get_decode_handle| method is invoked, we ignore what the HTML5
377        ## spec requires, i.e. unsupported encoding should be ignored.
378          ## TODO: We should not do this unless the parser is invoked
379          ## in the conformance checking mode, in which this behavior
380          ## would be useful.
381    
382        ## Step 1
383        if (defined $charset_name) {
384          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
385    
386          ## ISSUE: Unsupported encoding is not ignored according to the spec.
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1);
390          if ($char_stream) {
391            $self->{confident} = 1;
392            last SNIFFING;
393          } else {
394            ## TODO: unsupported error
395          }
396        }
397    
398        ## Step 2
399        my $byte_buffer = '';
400        for (1..1024) {
401          my $char = $byte_stream->getc;
402          last unless defined $char;
403          $byte_buffer .= $char;
404        } ## TODO: timeout
405    
406        ## Step 3
407        if ($byte_buffer =~ /^\xFE\xFF/) {
408          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
409          ($char_stream, $e_status) = $charset->get_decode_handle
410              ($byte_stream, allow_error_reporting => 1,
411               allow_fallback => 1, byte_buffer => \$byte_buffer);
412          $self->{confident} = 1;
413          last SNIFFING;
414        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
415          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
422          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        }
429    
430        ## Step 4
431        ## TODO: <meta charset>
432    
433        ## Step 5
434        ## TODO: from history
435    
436        ## Step 6
437        require Whatpm::Charset::UniversalCharDet;
438        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
439            ($byte_buffer);
440        if (defined $charset_name) {
441          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
442    
443          ## ISSUE: Unsupported encoding is not ignored according to the spec.
444          require Whatpm::Charset::DecodeHandle;
445          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
446              ($byte_stream);
447          ($char_stream, $e_status) = $charset->get_decode_handle
448              ($buffer, allow_error_reporting => 1,
449               allow_fallback => 1, byte_buffer => \$byte_buffer);
450          if ($char_stream) {
451            $buffer->{buffer} = $byte_buffer;
452            !!!parse-error (type => 'sniffing:chardet',
453                            text => $charset_name,
454                            level => $self->{level}->{info},
455                            layer => 'encode',
456                            line => 1, column => 1);
457            $self->{confident} = 0;
458            last SNIFFING;
459          }
460        }
461    
462        ## Step 7: default
463        ## TODO: Make this configurable.
464        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
465            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
466            ## detectable in the step 6.
467        require Whatpm::Charset::DecodeHandle;
468        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
469            ($byte_stream);
470        ($char_stream, $e_status)
471            = $charset->get_decode_handle ($buffer,
472                                           allow_error_reporting => 1,
473                                           allow_fallback => 1,
474                                           byte_buffer => \$byte_buffer);
475        $buffer->{buffer} = $byte_buffer;
476        !!!parse-error (type => 'sniffing:default',
477                        text => 'windows-1252',
478                        level => $self->{level}->{info},
479                        line => 1, column => 1,
480                        layer => 'encode');
481        $self->{confident} = 0;
482      } # SNIFFING
483    
484      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
485        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
486        !!!parse-error (type => 'chardecode:fallback',
487                        #text => $self->{input_encoding},
488                        level => $self->{level}->{uncertain},
489                        line => 1, column => 1,
490                        layer => 'encode');
491      } elsif (not ($e_status &
492                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
493        $self->{input_encoding} = $charset->get_iana_name;
494        !!!parse-error (type => 'chardecode:no error',
495                        text => $self->{input_encoding},
496                        level => $self->{level}->{uncertain},
497                        line => 1, column => 1,
498                        layer => 'encode');
499      } else {
500        $self->{input_encoding} = $charset->get_iana_name;
501      }
502    
503      $self->{change_encoding} = sub {
504        my $self = shift;
505        $charset_name = shift;
506        my $token = shift;
507    
508        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
509        ($char_stream, $e_status) = $charset->get_decode_handle
510            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
511             byte_buffer => \ $buffer->{buffer});
512        
513        if ($char_stream) { # if supported
514          ## "Change the encoding" algorithm:
515    
516          ## Step 1    
517          if ($charset->{category} &
518              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
519            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
520            ($char_stream, $e_status) = $charset->get_decode_handle
521                ($byte_stream,
522                 byte_buffer => \ $buffer->{buffer});
523          }
524          $charset_name = $charset->get_iana_name;
525          
526          ## Step 2
527          if (defined $self->{input_encoding} and
528              $self->{input_encoding} eq $charset_name) {
529            !!!parse-error (type => 'charset label:matching',
530                            text => $charset_name,
531                            level => $self->{level}->{info});
532            $self->{confident} = 1;
533            return;
534          }
535    
536          !!!parse-error (type => 'charset label detected',
537                          text => $self->{input_encoding},
538                          value => $charset_name,
539                          level => $self->{level}->{warn},
540                          token => $token);
541          
542          ## Step 3
543          # if (can) {
544            ## change the encoding on the fly.
545            #$self->{confident} = 1;
546            #return;
547          # }
548          
549          ## Step 4
550          throw Whatpm::HTML::RestartParser ();
551        }
552      }; # $self->{change_encoding}
553    
554      my $char_onerror = sub {
555        my (undef, $type, %opt) = @_;
556        !!!parse-error (layer => 'encode',
557                        %opt, type => $type,
558                        line => $self->{line}, column => $self->{column} + 1);
559        if ($opt{octets}) {
560          ${$opt{octets}} = "\x{FFFD}"; # relacement character
561        }
562      };
563      $char_stream->onerror ($char_onerror);
564    
565      my @args = @_; shift @args; # $s
566      my $return;
567      try {
568        $return = $self->parse_char_stream ($char_stream, @args);  
569      } catch Whatpm::HTML::RestartParser with {
570        ## NOTE: Invoked after {change_encoding}.
571    
572        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
573          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
574          !!!parse-error (type => 'chardecode:fallback',
575                          level => $self->{level}->{uncertain},
576                          #text => $self->{input_encoding},
577                          line => 1, column => 1,
578                          layer => 'encode');
579        } elsif (not ($e_status &
580                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
581          $self->{input_encoding} = $charset->get_iana_name;
582          !!!parse-error (type => 'chardecode:no error',
583                          text => $self->{input_encoding},
584                          level => $self->{level}->{uncertain},
585                          line => 1, column => 1,
586                          layer => 'encode');
587        } else {
588          $self->{input_encoding} = $charset->get_iana_name;
589        }
590        $self->{confident} = 1;
591        $char_stream->onerror ($char_onerror);
592        $return = $self->parse_char_stream ($char_stream, @args);
593      };
594      return $return;
595    } # parse_byte_stream
596    
597    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
598    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
599    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
600    ## because the core part of our HTML parser expects a string of character,
601    ## not a string of bytes or code units or anything which might contain a BOM.
602    ## Therefore, any parser interface that accepts a string of bytes,
603    ## such as |parse_byte_string| in this module, must ensure that it does
604    ## strip the BOM and never strip any ZWNBSP.
605    
606    sub parse_char_string ($$$;$) {
607      my $self = shift;
608      require utf8;
609      my $s = ref $_[0] ? $_[0] : \($_[0]);
610      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
611      return $self->parse_char_stream ($input, @_[1..$#_]);
612    } # parse_char_string
613    *parse_string = \&parse_char_string;
614    
615    sub parse_char_stream ($$$;$) {
616      my $self = ref $_[0] ? shift : shift->new;
617      my $input = $_[0];
618    $self->{document} = $_[1];    $self->{document} = $_[1];
619      @{$self->{document}->child_nodes} = ();
620    
621    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
622    
623      $self->{confident} = 1 unless exists $self->{confident};
624      $self->{document}->input_encoding ($self->{input_encoding})
625          if defined $self->{input_encoding};
626    
627    my $i = 0;    my $i = 0;
628    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
629    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
630    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
631      my $self = shift;      my $self = shift;
632    
633      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
634      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
635    
636      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
637      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
638      $column++;        $char = $self->{next_next_char};
639          delete $self->{next_next_char};
640        } else {
641          $char = $input->getc;
642        }
643        $self->{next_char} = -1 and return unless defined $char;
644        $self->{next_char} = ord $char;
645    
646        ($self->{line_prev}, $self->{column_prev})
647            = ($self->{line}, $self->{column});
648        $self->{column}++;
649            
650      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
651        $line++;        !!!cp ('j1');
652        $column = 0;        $self->{line}++;
653      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
654        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
655        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
656        $line++;        my $next = $input->getc;
657        $column = 0;        if (defined $next and $next ne "\x0A") {
658      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
659        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
660      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
661          $self->{line}++;
662          $self->{column} = 0;
663        } elsif ($self->{next_char} > 0x10FFFF) {
664          !!!cp ('j3');
665          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
666        } elsif ($self->{next_char} == 0x0000) { # NULL
667          !!!cp ('j4');
668        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
669        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
670        } elsif ($self->{next_char} <= 0x0008 or
671                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
672                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
673                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
674                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
675                 {
676                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
677                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
678                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
679                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
680                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
681                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
682                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
683                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
684                  0x10FFFE => 1, 0x10FFFF => 1,
685                 }->{$self->{next_char}}) {
686          !!!cp ('j5');
687          if ($self->{next_char} < 0x10000) {
688            !!!parse-error (type => 'control char',
689                            text => (sprintf 'U+%04X', $self->{next_char}));
690          } else {
691            !!!parse-error (type => 'control char',
692                            text => (sprintf 'U-%08X', $self->{next_char}));
693          }
694      }      }
695    };    };
696    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
697    $self->{next_input_character} = -1;    $self->{next_char} = -1;
698    
699    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
700      my (%opt) = @_;      my (%opt) = @_;
701      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
702        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
703        warn "Parse error ($opt{type}) at line $line column $column\n";
704    };    };
705    $self->{parse_error} = sub {    $self->{parse_error} = sub {
706      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
707    };    };
708    
709    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 711  sub parse_string ($$$;$) {
711    $self->_construct_tree;    $self->_construct_tree;
712    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
713    
714      delete $self->{parse_error}; # remove loop
715    
716    return $self->{document};    return $self->{document};
717  } # parse_string  } # parse_char_stream
718    
719  sub new ($) {  sub new ($) {
720    my $class = shift;    my $class = shift;
721    my $self = bless {}, $class;    my $self = bless {
722    $self->{set_next_input_character} = sub {      level => {must => 'm',
723      $self->{next_input_character} = -1;                should => 's',
724                  warn => 'w',
725                  info => 'i',
726                  uncertain => 'u'},
727      }, $class;
728      $self->{set_next_char} = sub {
729        $self->{next_char} = -1;
730    };    };
731    $self->{parse_error} = sub {    $self->{parse_error} = sub {
732      #      #
733    };    };
734      $self->{change_encoding} = sub {
735        # if ($_[0] is a supported encoding) {
736        #   run "change the encoding" algorithm;
737        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
738        # }
739      };
740      $self->{application_cache_selection} = sub {
741        #
742      };
743    return $self;    return $self;
744  } # new  } # new
745    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 752  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
752  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
753  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
754    
755    sub DATA_STATE () { 0 }
756    sub ENTITY_DATA_STATE () { 1 }
757    sub TAG_OPEN_STATE () { 2 }
758    sub CLOSE_TAG_OPEN_STATE () { 3 }
759    sub TAG_NAME_STATE () { 4 }
760    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
761    sub ATTRIBUTE_NAME_STATE () { 6 }
762    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
763    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
764    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
765    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
766    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
767    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
768    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
769    sub COMMENT_START_STATE () { 14 }
770    sub COMMENT_START_DASH_STATE () { 15 }
771    sub COMMENT_STATE () { 16 }
772    sub COMMENT_END_STATE () { 17 }
773    sub COMMENT_END_DASH_STATE () { 18 }
774    sub BOGUS_COMMENT_STATE () { 19 }
775    sub DOCTYPE_STATE () { 20 }
776    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
777    sub DOCTYPE_NAME_STATE () { 22 }
778    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
779    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
780    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
781    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
782    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
783    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
784    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
785    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
786    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
787    sub BOGUS_DOCTYPE_STATE () { 32 }
788    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
789    sub SELF_CLOSING_START_TAG_STATE () { 34 }
790    sub CDATA_BLOCK_STATE () { 35 }
791    
792    sub DOCTYPE_TOKEN () { 1 }
793    sub COMMENT_TOKEN () { 2 }
794    sub START_TAG_TOKEN () { 3 }
795    sub END_TAG_TOKEN () { 4 }
796    sub END_OF_FILE_TOKEN () { 5 }
797    sub CHARACTER_TOKEN () { 6 }
798    
799    sub AFTER_HTML_IMS () { 0b100 }
800    sub HEAD_IMS ()       { 0b1000 }
801    sub BODY_IMS ()       { 0b10000 }
802    sub BODY_TABLE_IMS () { 0b100000 }
803    sub TABLE_IMS ()      { 0b1000000 }
804    sub ROW_IMS ()        { 0b10000000 }
805    sub BODY_AFTER_IMS () { 0b100000000 }
806    sub FRAME_IMS ()      { 0b1000000000 }
807    sub SELECT_IMS ()     { 0b10000000000 }
808    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
809        ## NOTE: "in foreign content" insertion mode is special; it is combined
810        ## with the secondary insertion mode.  In this parser, they are stored
811        ## together in the bit-or'ed form.
812    
813    ## NOTE: "initial" and "before html" insertion modes have no constants.
814    
815    ## NOTE: "after after body" insertion mode.
816    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
817    
818    ## NOTE: "after after frameset" insertion mode.
819    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
820    
821    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
822    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
823    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
824    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
825    sub IN_BODY_IM () { BODY_IMS }
826    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
827    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
828    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
829    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
830    sub IN_TABLE_IM () { TABLE_IMS }
831    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
832    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
833    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
834    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
835    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
836    sub IN_COLUMN_GROUP_IM () { 0b10 }
837    
838  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
839    
840  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
841    my $self = shift;    my $self = shift;
842    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
843    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
844    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
845    undef $self->{current_attribute};    undef $self->{current_attribute};
846    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
847    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
848      delete $self->{self_closing};
849    $self->{char} = [];    $self->{char} = [];
850    # $self->{next_input_character}    # $self->{next_char}
851    !!!next-input-character;    !!!next-input-character;
852    $self->{token} = [];    $self->{token} = [];
853    # $self->{escape}    # $self->{escape}
854  } # _initialize_tokenizer  } # _initialize_tokenizer
855    
856  ## A token has:  ## A token has:
857  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
858  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
859  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
860  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
861  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
862  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
863  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
864  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
865    ##        ->{name}
866    ##        ->{value}
867    ##        ->{has_reference} == 1 or 0
868    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
869    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
870    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
871    ##     while the token is pushed back to the stack.
872    
873  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
874    
# Line 194  sub _initialize_tokenizer ($) { Line 878  sub _initialize_tokenizer ($) {
878  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
879  ## and removed from the list.  ## and removed from the list.
880    
881    ## NOTE: HTML5 "Writing HTML documents" section, applied to
882    ## documents and not to user agents and conformance checkers,
883    ## contains some requirements that are not detected by the
884    ## parsing algorithm:
885    ## - Some requirements on character encoding declarations. ## TODO
886    ## - "Elements MUST NOT contain content that their content model disallows."
887    ##   ... Some are parse error, some are not (will be reported by c.c.).
888    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
889    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
890    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
891    
892    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
893    ## be detected by the HTML5 parsing algorithm:
894    ## - Text,
895    
896  sub _get_next_token ($) {  sub _get_next_token ($) {
897    my $self = shift;    my $self = shift;
898    
899      if ($self->{self_closing}) {
900        !!!parse-error (type => 'nestc', token => $self->{current_token});
901        ## NOTE: The |self_closing| flag is only set by start tag token.
902        ## In addition, when a start tag token is emitted, it is always set to
903        ## |current_token|.
904        delete $self->{self_closing};
905      }
906    
907    if (@{$self->{token}}) {    if (@{$self->{token}}) {
908        $self->{self_closing} = $self->{token}->[0]->{self_closing};
909      return shift @{$self->{token}};      return shift @{$self->{token}};
910    }    }
911    
912    A: {    A: {
913      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
914        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
915          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
916            $self->{state} = 'entity data';              not $self->{escape}) {
917              !!!cp (1);
918              $self->{state} = ENTITY_DATA_STATE;
919            !!!next-input-character;            !!!next-input-character;
920            redo A;            redo A;
921          } else {          } else {
922              !!!cp (2);
923            #            #
924          }          }
925        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
926          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
927            unless ($self->{escape}) {            unless ($self->{escape}) {
928              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
929                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
930                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
931                  !!!cp (3);
932                $self->{escape} = 1;                $self->{escape} = 1;
933                } else {
934                  !!!cp (4);
935              }              }
936              } else {
937                !!!cp (5);
938            }            }
939          }          }
940                    
941          #          #
942        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
943          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
944              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
945               not $self->{escape})) {               not $self->{escape})) {
946            $self->{state} = 'tag open';            !!!cp (6);
947              $self->{state} = TAG_OPEN_STATE;
948            !!!next-input-character;            !!!next-input-character;
949            redo A;            redo A;
950          } else {          } else {
951              !!!cp (7);
952            #            #
953          }          }
954        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
955          if ($self->{escape} and          if ($self->{escape} and
956              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
957            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
958                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
959                !!!cp (8);
960              delete $self->{escape};              delete $self->{escape};
961              } else {
962                !!!cp (9);
963            }            }
964            } else {
965              !!!cp (10);
966          }          }
967                    
968          #          #
969        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
970          !!!emit ({type => 'end-of-file'});          !!!cp (11);
971            !!!emit ({type => END_OF_FILE_TOKEN,
972                      line => $self->{line}, column => $self->{column}});
973          last A; ## TODO: ok?          last A; ## TODO: ok?
974          } else {
975            !!!cp (12);
976        }        }
977        # Anything else        # Anything else
978        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
979                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
980                       line => $self->{line}, column => $self->{column},
981                      };
982        ## Stay in the data state        ## Stay in the data state
983        !!!next-input-character;        !!!next-input-character;
984    
985        !!!emit ($token);        !!!emit ($token);
986    
987        redo A;        redo A;
988      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
989        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
990    
991          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
992                
993        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
994    
995        $self->{state} = 'data';        $self->{state} = DATA_STATE;
996        # next-input-character is already done        # next-input-character is already done
997    
998        unless (defined $token) {        unless (defined $token) {
999          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1000            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1001                      line => $l, column => $c,
1002                     });
1003        } else {        } else {
1004            !!!cp (14);
1005          !!!emit ($token);          !!!emit ($token);
1006        }        }
1007    
1008        redo A;        redo A;
1009      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1010        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1011          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1012              !!!cp (15);
1013            !!!next-input-character;            !!!next-input-character;
1014            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1015            redo A;            redo A;
1016          } else {          } else {
1017              !!!cp (16);
1018            ## reconsume            ## reconsume
1019            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1020    
1021            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1022                        line => $self->{line_prev},
1023                        column => $self->{column_prev},
1024                       });
1025    
1026            redo A;            redo A;
1027          }          }
1028        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1029          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1030            $self->{state} = 'markup declaration open';            !!!cp (17);
1031              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1032            !!!next-input-character;            !!!next-input-character;
1033            redo A;            redo A;
1034          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1035            $self->{state} = 'close tag open';            !!!cp (18);
1036              $self->{state} = CLOSE_TAG_OPEN_STATE;
1037            !!!next-input-character;            !!!next-input-character;
1038            redo A;            redo A;
1039          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1040                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1041              !!!cp (19);
1042            $self->{current_token}            $self->{current_token}
1043              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1044                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1045            $self->{state} = 'tag name';                 line => $self->{line_prev},
1046                   column => $self->{column_prev}};
1047              $self->{state} = TAG_NAME_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049            redo A;            redo A;
1050          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1051                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1052            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1053                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1054            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1055                                        line => $self->{line_prev},
1056                                        column => $self->{column_prev}};
1057              $self->{state} = TAG_NAME_STATE;
1058            !!!next-input-character;            !!!next-input-character;
1059            redo A;            redo A;
1060          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1061            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1062            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1063                              line => $self->{line_prev},
1064                              column => $self->{column_prev});
1065              $self->{state} = DATA_STATE;
1066            !!!next-input-character;            !!!next-input-character;
1067    
1068            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1069                        line => $self->{line_prev},
1070                        column => $self->{column_prev},
1071                       });
1072    
1073            redo A;            redo A;
1074          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1075            !!!parse-error (type => 'pio');            !!!cp (22);
1076            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1077            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1078                              column => $self->{column_prev});
1079              $self->{state} = BOGUS_COMMENT_STATE;
1080              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1081                                        line => $self->{line_prev},
1082                                        column => $self->{column_prev},
1083                                       };
1084              ## $self->{next_char} is intentionally left as is
1085            redo A;            redo A;
1086          } else {          } else {
1087            !!!parse-error (type => 'bare stago');            !!!cp (23);
1088            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1089                              line => $self->{line_prev},
1090                              column => $self->{column_prev});
1091              $self->{state} = DATA_STATE;
1092            ## reconsume            ## reconsume
1093    
1094            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1095                        line => $self->{line_prev},
1096                        column => $self->{column_prev},
1097                       });
1098    
1099            redo A;            redo A;
1100          }          }
1101        } else {        } else {
1102          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1103        }        }
1104      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1105          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1106        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1107          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1108    
1109            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1110            my @next_char;            my @next_char;
1111            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++) {
1112              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1113              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1114              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1115              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1116                  !!!cp (24);
1117                !!!next-input-character;                !!!next-input-character;
1118                next TAGNAME;                next TAGNAME;
1119              } else {              } else {
1120                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1121                  $self->{next_char} = shift @next_char; # reconsume
1122                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1123                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1124    
1125                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1126                            line => $l, column => $c,
1127                           });
1128        
1129                redo A;                redo A;
1130              }              }
1131            }            }
1132            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1133                
1134            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1135                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1136                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1137                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1138                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1139                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1140                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1141                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1142              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1143                $self->{next_char} = shift @next_char; # reconsume
1144              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1145              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1146              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                          line => $l, column => $c,
1148                         });
1149              redo A;              redo A;
1150            } else {            } else {
1151              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1152                $self->{next_char} = shift @next_char;
1153              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1154              # and consume...              # and consume...
1155            }            }
1156          } else {          } else {
1157            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1158              !!!cp (28);
1159            # next-input-character is already done            # next-input-character is already done
1160            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1161            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1162                        line => $l, column => $c,
1163                       });
1164            redo A;            redo A;
1165          }          }
1166        }        }
1167                
1168        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1169            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1170          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1171                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1172          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1173          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1174          redo A;                 line => $l, column => $c};
1175        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1176                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1177          $self->{current_token} = {type => 'end tag',          redo A;
1178                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1179          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1180          !!!next-input-character;          !!!cp (30);
1181          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1182        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1183          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1184          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1185            !!!next-input-character;
1186            redo A;
1187          } elsif ($self->{next_char} == 0x003E) { # >
1188            !!!cp (31);
1189            !!!parse-error (type => 'empty end tag',
1190                            line => $self->{line_prev}, ## "<" in "</>"
1191                            column => $self->{column_prev} - 1);
1192            $self->{state} = DATA_STATE;
1193          !!!next-input-character;          !!!next-input-character;
1194          redo A;          redo A;
1195        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1196            !!!cp (32);
1197          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1198          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1199          # reconsume          # reconsume
1200    
1201          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1202                      line => $l, column => $c,
1203                     });
1204    
1205          redo A;          redo A;
1206        } else {        } else {
1207            !!!cp (33);
1208          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1209          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1210          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1211          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1212        }                                    column => $self->{column_prev} - 1,
1213      } elsif ($self->{state} eq 'tag name') {                                   };
1214        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1215            $self->{next_input_character} == 0x000A or # LF          redo A;
1216            $self->{next_input_character} == 0x000B or # VT        }
1217            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1218            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1219          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1220          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1221          redo A;            $self->{next_char} == 0x000C or # FF
1222        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1223          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1224            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1225                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1226            redo A;
1227          } elsif ($self->{next_char} == 0x003E) { # >
1228            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1229              !!!cp (35);
1230            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1231          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1232            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1233            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1234              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1235            }            #  !!! cp (36);
1236              #  !!! parse-error (type => 'end tag attribute');
1237              #} else {
1238                !!!cp (37);
1239              #}
1240          } else {          } else {
1241            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1242          }          }
1243          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1244          !!!next-input-character;          !!!next-input-character;
1245    
1246          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1247    
1248          redo A;          redo A;
1249        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1250                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1251          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1252            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1253            # start tag or end tag            # start tag or end tag
1254          ## Stay in this state          ## Stay in this state
1255          !!!next-input-character;          !!!next-input-character;
1256          redo A;          redo A;
1257        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1258          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1259          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1260            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1261            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1262          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1263            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1264            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1265              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1266            }            #  !!! cp (40);
1267              #  !!! parse-error (type => 'end tag attribute');
1268              #} else {
1269                !!!cp (41);
1270              #}
1271          } else {          } else {
1272            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1273          }          }
1274          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1275          # reconsume          # reconsume
1276    
1277          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1278    
1279          redo A;          redo A;
1280        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1281            !!!cp (42);
1282            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1283          !!!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  
1284          redo A;          redo A;
1285        } else {        } else {
1286          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1287            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1288            # start tag or end tag            # start tag or end tag
1289          ## Stay in the state          ## Stay in the state
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        }        }
1293      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1294        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1295            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1296            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1297            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1298            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1299            !!!cp (45);
1300          ## Stay in the state          ## Stay in the state
1301          !!!next-input-character;          !!!next-input-character;
1302          redo A;          redo A;
1303        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1304          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1310                !!!cp (47);
1311              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1312              } else {
1313                !!!cp (48);
1314            }            }
1315          } else {          } else {
1316            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1317          }          }
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320    
1321          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1322    
1323          redo A;          redo A;
1324        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1325                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1326          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1327                                value => ''};          $self->{current_attribute}
1328          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1329                   value => '',
1330                   line => $self->{line}, column => $self->{column}};
1331            $self->{state} = ATTRIBUTE_NAME_STATE;
1332            !!!next-input-character;
1333            redo A;
1334          } elsif ($self->{next_char} == 0x002F) { # /
1335            !!!cp (50);
1336            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } 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) {  
1340          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1341          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1342            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1343            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1344          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1345            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1346            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1347                !!!cp (53);
1348              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1349              } else {
1350                !!!cp (54);
1351            }            }
1352          } else {          } else {
1353            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1354          }          }
1355          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1356          # reconsume          # reconsume
1357    
1358          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1359    
1360          redo A;          redo A;
1361        } else {        } else {
1362          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1363                                value => ''};               0x0022 => 1, # "
1364          $self->{state} = 'attribute name';               0x0027 => 1, # '
1365                 0x003D => 1, # =
1366                }->{$self->{next_char}}) {
1367              !!!cp (55);
1368              !!!parse-error (type => 'bad attribute name');
1369            } else {
1370              !!!cp (56);
1371            }
1372            $self->{current_attribute}
1373                = {name => chr ($self->{next_char}),
1374                   value => '',
1375                   line => $self->{line}, column => $self->{column}};
1376            $self->{state} = ATTRIBUTE_NAME_STATE;
1377          !!!next-input-character;          !!!next-input-character;
1378          redo A;          redo A;
1379        }        }
1380      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1381        my $before_leave = sub {        my $before_leave = sub {
1382          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1383              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1384            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1385              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1386            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1387          } else {          } else {
1388              !!!cp (58);
1389            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1390              = $self->{current_attribute};              = $self->{current_attribute};
1391          }          }
1392        }; # $before_leave        }; # $before_leave
1393    
1394        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1395            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1396            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1397            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1398            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1399            !!!cp (59);
1400          $before_leave->();          $before_leave->();
1401          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1402          !!!next-input-character;          !!!next-input-character;
1403          redo A;          redo A;
1404        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1405            !!!cp (60);
1406          $before_leave->();          $before_leave->();
1407          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1408          !!!next-input-character;          !!!next-input-character;
1409          redo A;          redo A;
1410        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1411          $before_leave->();          $before_leave->();
1412          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1413            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1414            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1415          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1416              !!!cp (62);
1417            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1418            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1419              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1421  sub _get_next_token ($) {
1421          } else {          } else {
1422            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1423          }          }
1424          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1425          !!!next-input-character;          !!!next-input-character;
1426    
1427          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1428    
1429          redo A;          redo A;
1430        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1431                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1432          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1433            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1434          ## Stay in the state          ## Stay in the state
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1438            !!!cp (64);
1439          $before_leave->();          $before_leave->();
1440            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1441          !!!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  
1442          redo A;          redo A;
1443        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1444          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1445          $before_leave->();          $before_leave->();
1446          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1447            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1448            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1449          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1450            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1451            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1452                !!!cp (67);
1453              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1454              } else {
1455                ## NOTE: This state should never be reached.
1456                !!!cp (68);
1457            }            }
1458          } else {          } else {
1459            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1460          }          }
1461          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1462          # reconsume          # reconsume
1463    
1464          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1465    
1466          redo A;          redo A;
1467        } else {        } else {
1468          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1469                $self->{next_char} == 0x0027) { # '
1470              !!!cp (69);
1471              !!!parse-error (type => 'bad attribute name');
1472            } else {
1473              !!!cp (70);
1474            }
1475            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1476          ## Stay in the state          ## Stay in the state
1477          !!!next-input-character;          !!!next-input-character;
1478          redo A;          redo A;
1479        }        }
1480      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1481        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1482            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1483            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1484            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1485            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1486            !!!cp (71);
1487          ## Stay in the state          ## Stay in the state
1488          !!!next-input-character;          !!!next-input-character;
1489          redo A;          redo A;
1490        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1491          $self->{state} = 'before attribute value';          !!!cp (72);
1492            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1496          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1497            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1498            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1499          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1500            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1501            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1502                !!!cp (74);
1503              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1504              } else {
1505                ## NOTE: This state should never be reached.
1506                !!!cp (75);
1507            }            }
1508          } else {          } else {
1509            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1510          }          }
1511          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513    
1514          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1515    
1516          redo A;          redo A;
1517        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1518                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1519          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1520                                value => ''};          $self->{current_attribute}
1521          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1522                   value => '',
1523                   line => $self->{line}, column => $self->{column}};
1524            $self->{state} = ATTRIBUTE_NAME_STATE;
1525            !!!next-input-character;
1526            redo A;
1527          } elsif ($self->{next_char} == 0x002F) { # /
1528            !!!cp (77);
1529            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        } 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) {  
1533          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1534          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1535            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1536            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1537          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1538            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1539            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1540                !!!cp (80);
1541              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1542              } else {
1543                ## NOTE: This state should never be reached.
1544                !!!cp (81);
1545            }            }
1546          } else {          } else {
1547            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1548          }          }
1549          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1550          # reconsume          # reconsume
1551    
1552          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1553    
1554          redo A;          redo A;
1555        } else {        } else {
1556          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1557                                value => ''};              $self->{next_char} == 0x0027) { # '
1558          $self->{state} = 'attribute name';            !!!cp (78);
1559              !!!parse-error (type => 'bad attribute name');
1560            } else {
1561              !!!cp (82);
1562            }
1563            $self->{current_attribute}
1564                = {name => chr ($self->{next_char}),
1565                   value => '',
1566                   line => $self->{line}, column => $self->{column}};
1567            $self->{state} = ATTRIBUTE_NAME_STATE;
1568          !!!next-input-character;          !!!next-input-character;
1569          redo A;                  redo A;        
1570        }        }
1571      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1572        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1573            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1574            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1575            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1576            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1577            !!!cp (83);
1578          ## Stay in the state          ## Stay in the state
1579          !!!next-input-character;          !!!next-input-character;
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1582          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1583            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1587          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1588            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1589          ## reconsume          ## reconsume
1590          redo A;          redo A;
1591        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1592          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1593            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1597          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1598            $self->{current_token}->{first_start_tag}          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1599                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1600            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1601          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1602            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1603            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1604                !!!cp (88);
1605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1606              } else {
1607                ## NOTE: This state should never be reached.
1608                !!!cp (89);
1609            }            }
1610          } else {          } else {
1611            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1612          }          }
1613          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1614          !!!next-input-character;          !!!next-input-character;
1615    
1616          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1617    
1618          redo A;          redo A;
1619        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1620          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
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 (90);
               = 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 (91);
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 (92);
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});          if ($self->{next_char} == 0x003D) { # =
1644          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1645              !!!parse-error (type => 'bad attribute value');
1646            } else {
1647              !!!cp (94);
1648            }
1649            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1650            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1651          !!!next-input-character;          !!!next-input-character;
1652          redo A;          redo A;
1653        }        }
1654      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1655        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1656          $self->{state} = 'before attribute name';          !!!cp (95);
1657            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1658          !!!next-input-character;          !!!next-input-character;
1659          redo A;          redo A;
1660        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1661          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1662          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1663            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1664          !!!next-input-character;          !!!next-input-character;
1665          redo A;          redo A;
1666        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1667          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1668          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1669            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1672            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1674                !!!cp (98);
1675              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1676              } else {
1677                ## NOTE: This state should never be reached.
1678                !!!cp (99);
1679            }            }
1680          } else {          } else {
1681            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1682          }          }
1683          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1684          ## reconsume          ## reconsume
1685    
1686          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1687    
1688          redo A;          redo A;
1689        } else {        } else {
1690          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1691            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1692          ## Stay in the state          ## Stay in the state
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        }        }
1696      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1697        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1698          $self->{state} = 'before attribute name';          !!!cp (101);
1699            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1700          !!!next-input-character;          !!!next-input-character;
1701          redo A;          redo A;
1702        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1703          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1704          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1705            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1706          !!!next-input-character;          !!!next-input-character;
1707          redo A;          redo A;
1708        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1709          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1710          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1711            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1712            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1713          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1714            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1715            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1716                !!!cp (104);
1717              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1718              } else {
1719                ## NOTE: This state should never be reached.
1720                !!!cp (105);
1721            }            }
1722          } else {          } else {
1723            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1724          }          }
1725          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1726          ## reconsume          ## reconsume
1727    
1728          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1729    
1730          redo A;          redo A;
1731        } else {        } else {
1732          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1733            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1734          ## Stay in the state          ## Stay in the state
1735          !!!next-input-character;          !!!next-input-character;
1736          redo A;          redo A;
1737        }        }
1738      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1739        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1740            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1741            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1742            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1743            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1744          $self->{state} = 'before attribute name';          !!!cp (107);
1745          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1746          redo A;          !!!next-input-character;
1747        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1748          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1749          $self->{state} = 'entity in attribute value';          !!!cp (108);
1750          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1751          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1752        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1753          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1754            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1755                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1756              !!!cp (109);
1757            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1758          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1759            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1761                !!!cp (110);
1762              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1763              } else {
1764                ## NOTE: This state should never be reached.
1765                !!!cp (111);
1766            }            }
1767          } else {          } else {
1768            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1769          }          }
1770          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1771          !!!next-input-character;          !!!next-input-character;
1772    
1773          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1774    
1775          redo A;          redo A;
1776        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1777          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1778          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1779            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1780            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1781          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1782            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1783            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1784                !!!cp (113);
1785              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1786              } else {
1787                ## NOTE: This state should never be reached.
1788                !!!cp (114);
1789            }            }
1790          } else {          } else {
1791            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1792          }          }
1793          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1794          ## reconsume          ## reconsume
1795    
1796          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1797    
1798          redo A;          redo A;
1799        } else {        } else {
1800          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1801                 0x0022 => 1, # "
1802                 0x0027 => 1, # '
1803                 0x003D => 1, # =
1804                }->{$self->{next_char}}) {
1805              !!!cp (115);
1806              !!!parse-error (type => 'bad attribute value');
1807            } else {
1808              !!!cp (116);
1809            }
1810            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1811          ## Stay in the state          ## Stay in the state
1812          !!!next-input-character;          !!!next-input-character;
1813          redo A;          redo A;
1814        }        }
1815      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1816        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1817              (1,
1818               $self->{last_attribute_value_state}
1819                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1820               $self->{last_attribute_value_state}
1821                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1822               -1);
1823    
1824        unless (defined $token) {        unless (defined $token) {
1825            !!!cp (117);
1826          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1827        } else {        } else {
1828            !!!cp (118);
1829          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1830            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1831          ## 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"
1832        }        }
1833    
1834        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1835        # next-input-character is already done        # next-input-character is already done
1836        redo A;        redo A;
1837      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1838          if ($self->{next_char} == 0x0009 or # HT
1839              $self->{next_char} == 0x000A or # LF
1840              $self->{next_char} == 0x000B or # VT
1841              $self->{next_char} == 0x000C or # FF
1842              $self->{next_char} == 0x0020) { # SP
1843            !!!cp (118);
1844            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1845            !!!next-input-character;
1846            redo A;
1847          } elsif ($self->{next_char} == 0x003E) { # >
1848            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1849              !!!cp (119);
1850              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1851            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1852              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1853              if ($self->{current_token}->{attributes}) {
1854                !!!cp (120);
1855                !!!parse-error (type => 'end tag attribute');
1856              } else {
1857                ## NOTE: This state should never be reached.
1858                !!!cp (121);
1859              }
1860            } else {
1861              die "$0: $self->{current_token}->{type}: Unknown token type";
1862            }
1863            $self->{state} = DATA_STATE;
1864            !!!next-input-character;
1865    
1866            !!!emit ($self->{current_token}); # start tag or end tag
1867    
1868            redo A;
1869          } elsif ($self->{next_char} == 0x002F) { # /
1870            !!!cp (122);
1871            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1872            !!!next-input-character;
1873            redo A;
1874          } elsif ($self->{next_char} == -1) {
1875            !!!parse-error (type => 'unclosed tag');
1876            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1877              !!!cp (122.3);
1878              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1879            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1880              if ($self->{current_token}->{attributes}) {
1881                !!!cp (122.1);
1882                !!!parse-error (type => 'end tag attribute');
1883              } else {
1884                ## NOTE: This state should never be reached.
1885                !!!cp (122.2);
1886              }
1887            } else {
1888              die "$0: $self->{current_token}->{type}: Unknown token type";
1889            }
1890            $self->{state} = DATA_STATE;
1891            ## Reconsume.
1892            !!!emit ($self->{current_token}); # start tag or end tag
1893            redo A;
1894          } else {
1895            !!!cp ('124.1');
1896            !!!parse-error (type => 'no space between attributes');
1897            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1898            ## reconsume
1899            redo A;
1900          }
1901        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1902          if ($self->{next_char} == 0x003E) { # >
1903            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1904              !!!cp ('124.2');
1905              !!!parse-error (type => 'nestc', token => $self->{current_token});
1906              ## TODO: Different type than slash in start tag
1907              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1908              if ($self->{current_token}->{attributes}) {
1909                !!!cp ('124.4');
1910                !!!parse-error (type => 'end tag attribute');
1911              } else {
1912                !!!cp ('124.5');
1913              }
1914              ## TODO: Test |<title></title/>|
1915            } else {
1916              !!!cp ('124.3');
1917              $self->{self_closing} = 1;
1918            }
1919    
1920            $self->{state} = DATA_STATE;
1921            !!!next-input-character;
1922    
1923            !!!emit ($self->{current_token}); # start tag or end tag
1924    
1925            redo A;
1926          } elsif ($self->{next_char} == -1) {
1927            !!!parse-error (type => 'unclosed tag');
1928            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1929              !!!cp (124.7);
1930              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1931            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1932              if ($self->{current_token}->{attributes}) {
1933                !!!cp (124.5);
1934                !!!parse-error (type => 'end tag attribute');
1935              } else {
1936                ## NOTE: This state should never be reached.
1937                !!!cp (124.6);
1938              }
1939            } else {
1940              die "$0: $self->{current_token}->{type}: Unknown token type";
1941            }
1942            $self->{state} = DATA_STATE;
1943            ## Reconsume.
1944            !!!emit ($self->{current_token}); # start tag or end tag
1945            redo A;
1946          } else {
1947            !!!cp ('124.4');
1948            !!!parse-error (type => 'nestc');
1949            ## TODO: This error type is wrong.
1950            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1951            ## Reconsume.
1952            redo A;
1953          }
1954        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1955        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1956                
1957        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1958          #my $token = {type => COMMENT_TOKEN, data => ''};
1959    
1960        BC: {        BC: {
1961          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1962            $self->{state} = 'data';            !!!cp (124);
1963              $self->{state} = DATA_STATE;
1964            !!!next-input-character;            !!!next-input-character;
1965    
1966            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1967    
1968            redo A;            redo A;
1969          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1970            $self->{state} = 'data';            !!!cp (125);
1971              $self->{state} = DATA_STATE;
1972            ## reconsume            ## reconsume
1973    
1974            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1975    
1976            redo A;            redo A;
1977          } else {          } else {
1978            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1979              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1980            !!!next-input-character;            !!!next-input-character;
1981            redo BC;            redo BC;
1982          }          }
1983        } # BC        } # BC
1984      } elsif ($self->{state} eq 'markup declaration open') {  
1985          die "$0: _get_next_token: unexpected case [BC]";
1986        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1987        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1988    
1989          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1990    
1991        my @next_char;        my @next_char;
1992        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1993                
1994        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1995          !!!next-input-character;          !!!next-input-character;
1996          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1997          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1998            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1999            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2000                                        line => $l, column => $c,
2001                                       };
2002              $self->{state} = COMMENT_START_STATE;
2003            !!!next-input-character;            !!!next-input-character;
2004            redo A;            redo A;
2005            } else {
2006              !!!cp (128);
2007          }          }
2008        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2009                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2010          !!!next-input-character;          !!!next-input-character;
2011          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2012          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2013              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2014            !!!next-input-character;            !!!next-input-character;
2015            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2016            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2017                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2018              !!!next-input-character;              !!!next-input-character;
2019              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2020              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2021                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2022                !!!next-input-character;                !!!next-input-character;
2023                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2024                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2025                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2026                  !!!next-input-character;                  !!!next-input-character;
2027                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2028                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2029                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2030                    !!!next-input-character;                    !!!next-input-character;
2031                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2032                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2033                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2034                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2035                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
2036                        $self->{state} = DOCTYPE_STATE;
2037                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2038                                                  quirks => 1,
2039                                                  line => $l, column => $c,
2040                                                 };
2041                      !!!next-input-character;                      !!!next-input-character;
2042                      redo A;                      redo A;
2043                      } else {
2044                        !!!cp (130);
2045                    }                    }
2046                    } else {
2047                      !!!cp (131);
2048                  }                  }
2049                  } else {
2050                    !!!cp (132);
2051                }                }
2052                } else {
2053                  !!!cp (133);
2054              }              }
2055              } else {
2056                !!!cp (134);
2057            }            }
2058            } else {
2059              !!!cp (135);
2060          }          }
2061          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2062                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2063                   $self->{next_char} == 0x005B) { # [
2064            !!!next-input-character;
2065            push @next_char, $self->{next_char};
2066            if ($self->{next_char} == 0x0043) { # C
2067              !!!next-input-character;
2068              push @next_char, $self->{next_char};
2069              if ($self->{next_char} == 0x0044) { # D
2070                !!!next-input-character;
2071                push @next_char, $self->{next_char};
2072                if ($self->{next_char} == 0x0041) { # A
2073                  !!!next-input-character;
2074                  push @next_char, $self->{next_char};
2075                  if ($self->{next_char} == 0x0054) { # T
2076                    !!!next-input-character;
2077                    push @next_char, $self->{next_char};
2078                    if ($self->{next_char} == 0x0041) { # A
2079                      !!!next-input-character;
2080                      push @next_char, $self->{next_char};
2081                      if ($self->{next_char} == 0x005B) { # [
2082                        !!!cp (135.1);
2083                        $self->{state} = CDATA_BLOCK_STATE;
2084                        !!!next-input-character;
2085                        redo A;
2086                      } else {
2087                        !!!cp (135.2);
2088                      }
2089                    } else {
2090                      !!!cp (135.3);
2091                    }
2092                  } else {
2093                    !!!cp (135.4);                
2094                  }
2095                } else {
2096                  !!!cp (135.5);
2097                }
2098              } else {
2099                !!!cp (135.6);
2100              }
2101            } else {
2102              !!!cp (135.7);
2103            }
2104          } else {
2105            !!!cp (136);
2106        }        }
2107    
2108        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2109        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2110        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2111        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2112          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2113                                    line => $l, column => $c,
2114                                   };
2115        redo A;        redo A;
2116                
2117        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2118        ## 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?
2119      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2120        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2121          $self->{state} = 'comment start dash';          !!!cp (137);
2122            $self->{state} = COMMENT_START_DASH_STATE;
2123          !!!next-input-character;          !!!next-input-character;
2124          redo A;          redo A;
2125        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2126            !!!cp (138);
2127          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2128          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2129          !!!next-input-character;          !!!next-input-character;
2130    
2131          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2132    
2133          redo A;          redo A;
2134        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2135            !!!cp (139);
2136          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2137          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2138          ## reconsume          ## reconsume
2139    
2140          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2141    
2142          redo A;          redo A;
2143        } else {        } else {
2144            !!!cp (140);
2145          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2146              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2147          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2148          !!!next-input-character;          !!!next-input-character;
2149          redo A;          redo A;
2150        }        }
2151      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2152        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2153          $self->{state} = 'comment end';          !!!cp (141);
2154            $self->{state} = COMMENT_END_STATE;
2155          !!!next-input-character;          !!!next-input-character;
2156          redo A;          redo A;
2157        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2158            !!!cp (142);
2159          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2160          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2161          !!!next-input-character;          !!!next-input-character;
2162    
2163          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2164    
2165          redo A;          redo A;
2166        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2167            !!!cp (143);
2168          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2169          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2170          ## reconsume          ## reconsume
2171    
2172          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2173    
2174          redo A;          redo A;
2175        } else {        } else {
2176            !!!cp (144);
2177          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2178              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2179          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2180          !!!next-input-character;          !!!next-input-character;
2181          redo A;          redo A;
2182        }        }
2183      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2184        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2185          $self->{state} = 'comment end dash';          !!!cp (145);
2186            $self->{state} = COMMENT_END_DASH_STATE;
2187          !!!next-input-character;          !!!next-input-character;
2188          redo A;          redo A;
2189        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2190            !!!cp (146);
2191          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2192          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2193          ## reconsume          ## reconsume
2194    
2195          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2196    
2197          redo A;          redo A;
2198        } else {        } else {
2199          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2200            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2201          ## Stay in the state          ## Stay in the state
2202          !!!next-input-character;          !!!next-input-character;
2203          redo A;          redo A;
2204        }        }
2205      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2206        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2207          $self->{state} = 'comment end';          !!!cp (148);
2208            $self->{state} = COMMENT_END_STATE;
2209          !!!next-input-character;          !!!next-input-character;
2210          redo A;          redo A;
2211        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2212            !!!cp (149);
2213          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2214          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2215          ## reconsume          ## reconsume
2216    
2217          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2218    
2219          redo A;          redo A;
2220        } else {        } else {
2221          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2222          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2223            $self->{state} = COMMENT_STATE;
2224          !!!next-input-character;          !!!next-input-character;
2225          redo A;          redo A;
2226        }        }
2227      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2228        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2229          $self->{state} = 'data';          !!!cp (151);
2230            $self->{state} = DATA_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232    
2233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2234    
2235          redo A;          redo A;
2236        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2237          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2238            !!!parse-error (type => 'dash in comment',
2239                            line => $self->{line_prev},
2240                            column => $self->{column_prev});
2241          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2242          ## Stay in the state          ## Stay in the state
2243          !!!next-input-character;          !!!next-input-character;
2244          redo A;          redo A;
2245        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2246            !!!cp (153);
2247          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2248          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2249          ## reconsume          ## reconsume
2250    
2251          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2252    
2253          redo A;          redo A;
2254        } else {        } else {
2255          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2256          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2257          $self->{state} = 'comment';                          line => $self->{line_prev},
2258                            column => $self->{column_prev});
2259            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2260            $self->{state} = COMMENT_STATE;
2261          !!!next-input-character;          !!!next-input-character;
2262          redo A;          redo A;
2263        }        }
2264      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2265        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2266            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2267            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2268            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2269            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2270          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2271            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2272          !!!next-input-character;          !!!next-input-character;
2273          redo A;          redo A;
2274        } else {        } else {
2275            !!!cp (156);
2276          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2277          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2278          ## reconsume          ## reconsume
2279          redo A;          redo A;
2280        }        }
2281      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2282        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2283            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2284            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2285            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2286            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2287            !!!cp (157);
2288          ## Stay in the state          ## Stay in the state
2289          !!!next-input-character;          !!!next-input-character;
2290          redo A;          redo A;
2291        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2292            !!!cp (158);
2293          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2294          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2295          !!!next-input-character;          !!!next-input-character;
2296    
2297          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2298    
2299          redo A;          redo A;
2300        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2301            !!!cp (159);
2302          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2303          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2304          ## reconsume          ## reconsume
2305    
2306          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2307    
2308          redo A;          redo A;
2309        } else {        } else {
2310          $self->{current_token}          !!!cp (160);
2311              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2312                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2313  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2314          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        }        }
2318      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2319  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2320        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2321            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2322            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2323            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2324            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2325          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2326            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2327          !!!next-input-character;          !!!next-input-character;
2328          redo A;          redo A;
2329        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2330          $self->{state} = 'data';          !!!cp (162);
2331            $self->{state} = DATA_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333    
2334          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2335    
2336          redo A;          redo A;
2337        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2338            !!!cp (163);
2339          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2340          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2341          ## reconsume          ## reconsume
2342    
2343          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2344          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348            !!!cp (164);
2349          $self->{current_token}->{name}          $self->{current_token}->{name}
2350            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2351          ## Stay in the state          ## Stay in the state
2352          !!!next-input-character;          !!!next-input-character;
2353          redo A;          redo A;
2354        }        }
2355      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2356        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2357            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2358            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2359            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2360            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2361            !!!cp (165);
2362          ## Stay in the state          ## Stay in the state
2363          !!!next-input-character;          !!!next-input-character;
2364          redo A;          redo A;
2365        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2366          $self->{state} = 'data';          !!!cp (166);
2367            $self->{state} = DATA_STATE;
2368          !!!next-input-character;          !!!next-input-character;
2369    
2370          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2371    
2372          redo A;          redo A;
2373        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2374            !!!cp (167);
2375          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2376          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2377          ## reconsume          ## reconsume
2378    
2379          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2380          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2381    
2382          redo A;          redo A;
2383        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2384                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2385          !!!next-input-character;          !!!next-input-character;
2386          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2387              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2388            !!!next-input-character;            !!!next-input-character;
2389            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2390                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2391              !!!next-input-character;              !!!next-input-character;
2392              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2393                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2394                !!!next-input-character;                !!!next-input-character;
2395                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2396                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2397                  !!!next-input-character;                  !!!next-input-character;
2398                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2399                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2400                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2401                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2402                    !!!next-input-character;                    !!!next-input-character;
2403                    redo A;                    redo A;
2404                    } else {
2405                      !!!cp (169);
2406                  }                  }
2407                  } else {
2408                    !!!cp (170);
2409                }                }
2410                } else {
2411                  !!!cp (171);
2412              }              }
2413              } else {
2414                !!!cp (172);
2415            }            }
2416            } else {
2417              !!!cp (173);
2418          }          }
2419    
2420          #          #
2421        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2422                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2423          !!!next-input-character;          !!!next-input-character;
2424          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2425              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2426            !!!next-input-character;            !!!next-input-character;
2427            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2428                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2429              !!!next-input-character;              !!!next-input-character;
2430              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2431                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2432                !!!next-input-character;                !!!next-input-character;
2433                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2434                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2435                  !!!next-input-character;                  !!!next-input-character;
2436                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2437                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2438                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2439                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2440                    !!!next-input-character;                    !!!next-input-character;
2441                    redo A;                    redo A;
2442                    } else {
2443                      !!!cp (175);
2444                  }                  }
2445                  } else {
2446                    !!!cp (176);
2447                }                }
2448                } else {
2449                  !!!cp (177);
2450              }              }
2451              } else {
2452                !!!cp (178);
2453            }            }
2454            } else {
2455              !!!cp (179);
2456          }          }
2457    
2458          #          #
2459        } else {        } else {
2460            !!!cp (180);
2461          !!!next-input-character;          !!!next-input-character;
2462          #          #
2463        }        }
2464    
2465        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2466        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2467    
2468          $self->{state} = BOGUS_DOCTYPE_STATE;
2469        # next-input-character is already done        # next-input-character is already done
2470        redo A;        redo A;
2471      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2472        if ({        if ({
2473              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2474              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2475            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2476            !!!cp (181);
2477          ## Stay in the state          ## Stay in the state
2478          !!!next-input-character;          !!!next-input-character;
2479          redo A;          redo A;
2480        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2481            !!!cp (182);
2482          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2483          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2484          !!!next-input-character;          !!!next-input-character;
2485          redo A;          redo A;
2486        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2487            !!!cp (183);
2488          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2489          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2490          !!!next-input-character;          !!!next-input-character;
2491          redo A;          redo A;
2492        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2493            !!!cp (184);
2494          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2495    
2496          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2497          !!!next-input-character;          !!!next-input-character;
2498    
2499          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2500          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2501    
2502          redo A;          redo A;
2503        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2504            !!!cp (185);
2505          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2506    
2507          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2508          ## reconsume          ## reconsume
2509    
2510          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2511          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2512    
2513          redo A;          redo A;
2514        } else {        } else {
2515            !!!cp (186);
2516          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2517          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2518    
2519            $self->{state} = BOGUS_DOCTYPE_STATE;
2520          !!!next-input-character;          !!!next-input-character;
2521          redo A;          redo A;
2522        }        }
2523      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2524        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2525          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2526            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2527          !!!next-input-character;          !!!next-input-character;
2528          redo A;          redo A;
2529        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2530            !!!cp (188);
2531            !!!parse-error (type => 'unclosed PUBLIC literal');
2532    
2533            $self->{state} = DATA_STATE;
2534            !!!next-input-character;
2535    
2536            $self->{current_token}->{quirks} = 1;
2537            !!!emit ($self->{current_token}); # DOCTYPE
2538    
2539            redo A;
2540          } elsif ($self->{next_char} == -1) {
2541            !!!cp (189);
2542          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2543    
2544          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2545          ## reconsume          ## reconsume
2546    
2547          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2548          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2549    
2550          redo A;          redo A;
2551        } else {        } else {
2552            !!!cp (190);
2553          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2554              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2555          ## Stay in the state          ## Stay in the state
2556          !!!next-input-character;          !!!next-input-character;
2557          redo A;          redo A;
2558        }        }
2559      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2560        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2561          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2562            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2563            !!!next-input-character;
2564            redo A;
2565          } elsif ($self->{next_char} == 0x003E) { # >
2566            !!!cp (192);
2567            !!!parse-error (type => 'unclosed PUBLIC literal');
2568    
2569            $self->{state} = DATA_STATE;
2570          !!!next-input-character;          !!!next-input-character;
2571    
2572            $self->{current_token}->{quirks} = 1;
2573            !!!emit ($self->{current_token}); # DOCTYPE
2574    
2575          redo A;          redo A;
2576        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2577            !!!cp (193);
2578          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2579    
2580          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2581          ## reconsume          ## reconsume
2582    
2583          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2584          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2585    
2586          redo A;          redo A;
2587        } else {        } else {
2588            !!!cp (194);
2589          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2590              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2591          ## Stay in the state          ## Stay in the state
2592          !!!next-input-character;          !!!next-input-character;
2593          redo A;          redo A;
2594        }        }
2595      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2596        if ({        if ({
2597              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2598              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2599            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2600            !!!cp (195);
2601          ## Stay in the state          ## Stay in the state
2602          !!!next-input-character;          !!!next-input-character;
2603          redo A;          redo A;
2604        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2605            !!!cp (196);
2606          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2607          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2611            !!!cp (197);
2612          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2613          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2614          !!!next-input-character;          !!!next-input-character;
2615          redo A;          redo A;
2616        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2617          $self->{state} = 'data';          !!!cp (198);
2618            $self->{state} = DATA_STATE;
2619          !!!next-input-character;          !!!next-input-character;
2620    
2621          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2622    
2623          redo A;          redo A;
2624        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2625            !!!cp (199);
2626          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2627    
2628          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2629          ## reconsume          ## reconsume
2630    
2631          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2632          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2633    
2634          redo A;          redo A;
2635        } else {        } else {
2636            !!!cp (200);
2637          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2638          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2639    
2640            $self->{state} = BOGUS_DOCTYPE_STATE;
2641          !!!next-input-character;          !!!next-input-character;
2642          redo A;          redo A;
2643        }        }
2644      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2645        if ({        if ({
2646              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2647              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2648            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2649            !!!cp (201);
2650          ## Stay in the state          ## Stay in the state
2651          !!!next-input-character;          !!!next-input-character;
2652          redo A;          redo A;
2653        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2654            !!!cp (202);
2655          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2656          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658          redo A;          redo A;
2659        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2660            !!!cp (203);
2661          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2662          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2663          !!!next-input-character;          !!!next-input-character;
2664          redo A;          redo A;
2665        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2666            !!!cp (204);
2667          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2668          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2669          !!!next-input-character;          !!!next-input-character;
2670    
2671          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2672          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2673    
2674          redo A;          redo A;
2675        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2676            !!!cp (205);
2677          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2678    
2679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2680          ## reconsume          ## reconsume
2681    
2682          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2683          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2684    
2685          redo A;          redo A;
2686        } else {        } else {
2687            !!!cp (206);
2688          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2689          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2690    
2691            $self->{state} = BOGUS_DOCTYPE_STATE;
2692          !!!next-input-character;          !!!next-input-character;
2693          redo A;          redo A;
2694        }        }
2695      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2696        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2697          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2698            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2699            !!!next-input-character;
2700            redo A;
2701          } elsif ($self->{next_char} == 0x003E) { # >
2702            !!!cp (208);
2703            !!!parse-error (type => 'unclosed SYSTEM literal');
2704    
2705            $self->{state} = DATA_STATE;
2706          !!!next-input-character;          !!!next-input-character;
2707    
2708            $self->{current_token}->{quirks} = 1;
2709            !!!emit ($self->{current_token}); # DOCTYPE
2710    
2711          redo A;          redo A;
2712        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2713            !!!cp (209);
2714          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2715    
2716          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2717          ## reconsume          ## reconsume
2718    
2719          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2720          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2721    
2722          redo A;          redo A;
2723        } else {        } else {
2724            !!!cp (210);
2725          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2726              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2727          ## Stay in the state          ## Stay in the state
2728          !!!next-input-character;          !!!next-input-character;
2729          redo A;          redo A;
2730        }        }
2731      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2732        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2733          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2734            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2735            !!!next-input-character;
2736            redo A;
2737          } elsif ($self->{next_char} == 0x003E) { # >
2738            !!!cp (212);
2739            !!!parse-error (type => 'unclosed SYSTEM literal');
2740    
2741            $self->{state} = DATA_STATE;
2742          !!!next-input-character;          !!!next-input-character;
2743    
2744            $self->{current_token}->{quirks} = 1;
2745            !!!emit ($self->{current_token}); # DOCTYPE
2746    
2747          redo A;          redo A;
2748        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2749            !!!cp (213);
2750          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2751    
2752          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2753          ## reconsume          ## reconsume
2754    
2755          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2756          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2757    
2758          redo A;          redo A;
2759        } else {        } else {
2760            !!!cp (214);
2761          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2762              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2763          ## Stay in the state          ## Stay in the state
2764          !!!next-input-character;          !!!next-input-character;
2765          redo A;          redo A;
2766        }        }
2767      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2768        if ({        if ({
2769              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2770              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2771            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2772            !!!cp (215);
2773          ## Stay in the state          ## Stay in the state
2774          !!!next-input-character;          !!!next-input-character;
2775          redo A;          redo A;
2776        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2777          $self->{state} = 'data';          !!!cp (216);
2778            $self->{state} = DATA_STATE;
2779          !!!next-input-character;          !!!next-input-character;
2780    
2781          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2782    
2783          redo A;          redo A;
2784        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2785            !!!cp (217);
2786          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2787            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2788          ## reconsume          ## reconsume
2789    
2790          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2791          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2792    
2793          redo A;          redo A;
2794        } else {        } else {
2795            !!!cp (218);
2796          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2797          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2798    
2799            $self->{state} = BOGUS_DOCTYPE_STATE;
2800          !!!next-input-character;          !!!next-input-character;
2801          redo A;          redo A;
2802        }        }
2803      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2804        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2805          $self->{state} = 'data';          !!!cp (219);
2806            $self->{state} = DATA_STATE;
2807          !!!next-input-character;          !!!next-input-character;
2808    
         delete $self->{current_token}->{correct};  
2809          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2810    
2811          redo A;          redo A;
2812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2813            !!!cp (220);
2814          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2815          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2816          ## reconsume          ## reconsume
2817    
         delete $self->{current_token}->{correct};  
2818          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2819    
2820          redo A;          redo A;
2821        } else {        } else {
2822            !!!cp (221);
2823          ## Stay in the state          ## Stay in the state
2824          !!!next-input-character;          !!!next-input-character;
2825          redo A;          redo A;
2826        }        }
2827        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2828          my $s = '';
2829          
2830          my ($l, $c) = ($self->{line}, $self->{column});
2831    
2832          CS: while ($self->{next_char} != -1) {
2833            if ($self->{next_char} == 0x005D) { # ]
2834              !!!next-input-character;
2835              if ($self->{next_char} == 0x005D) { # ]
2836                !!!next-input-character;
2837                MDC: {
2838                  if ($self->{next_char} == 0x003E) { # >
2839                    !!!cp (221.1);
2840                    !!!next-input-character;
2841                    last CS;
2842                  } elsif ($self->{next_char} == 0x005D) { # ]
2843                    !!!cp (221.2);
2844                    $s .= ']';
2845                    !!!next-input-character;
2846                    redo MDC;
2847                  } else {
2848                    !!!cp (221.3);
2849                    $s .= ']]';
2850                    #
2851                  }
2852                } # MDC
2853              } else {
2854                !!!cp (221.4);
2855                $s .= ']';
2856                #
2857              }
2858            } else {
2859              !!!cp (221.5);
2860              #
2861            }
2862            $s .= chr $self->{next_char};
2863            !!!next-input-character;
2864          } # CS
2865    
2866          $self->{state} = DATA_STATE;
2867          ## next-input-character done or EOF, which is reconsumed.
2868    
2869          if (length $s) {
2870            !!!cp (221.6);
2871            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2872                      line => $l, column => $c});
2873          } else {
2874            !!!cp (221.7);
2875          }
2876    
2877          redo A;
2878    
2879          ## ISSUE: "text tokens" in spec.
2880          ## TODO: Streaming support
2881      } else {      } else {
2882        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2883      }      }
# Line 1609  sub _get_next_token ($) { Line 2886  sub _get_next_token ($) {
2886    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2887  } # _get_next_token  } # _get_next_token
2888    
2889  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2890    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2891    
2892      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2893    
2894    if ({    if ({
2895         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2896         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2897        }->{$self->{next_input_character}}) {         $additional => 1,
2898          }->{$self->{next_char}}) {
2899        !!!cp (1001);
2900      ## Don't consume      ## Don't consume
2901      ## No error      ## No error
2902      return undef;      return undef;
2903    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2904      !!!next-input-character;      !!!next-input-character;
2905      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2906          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2907        my $code;        my $code;
2908        X: {        X: {
2909          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2910          !!!next-input-character;          !!!next-input-character;
2911          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2912              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2913              !!!cp (1002);
2914            $code ||= 0;            $code ||= 0;
2915            $code *= 0x10;            $code *= 0x10;
2916            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2917            redo X;            redo X;
2918          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2919                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2920              !!!cp (1003);
2921            $code ||= 0;            $code ||= 0;
2922            $code *= 0x10;            $code *= 0x10;
2923            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2924            redo X;            redo X;
2925          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2926                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2927              !!!cp (1004);
2928            $code ||= 0;            $code ||= 0;
2929            $code *= 0x10;            $code *= 0x10;
2930            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2931            redo X;            redo X;
2932          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2933            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2934            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2935            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2936              $self->{next_char} = 0x0023; # #
2937            return undef;            return undef;
2938          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2939              !!!cp (1006);
2940            !!!next-input-character;            !!!next-input-character;
2941          } else {          } else {
2942            !!!parse-error (type => 'no refc');            !!!cp (1007);
2943              !!!parse-error (type => 'no refc', line => $l, column => $c);
2944          }          }
2945    
2946          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2947            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2948              !!!parse-error (type => 'invalid character reference',
2949                              text => (sprintf 'U+%04X', $code),
2950                              line => $l, column => $c);
2951            $code = 0xFFFD;            $code = 0xFFFD;
2952          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2953            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2954              !!!parse-error (type => 'invalid character reference',
2955                              text => (sprintf 'U-%08X', $code),
2956                              line => $l, column => $c);
2957            $code = 0xFFFD;            $code = 0xFFFD;
2958          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2959            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2960              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2961            $code = 0x000A;            $code = 0x000A;
2962          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2963            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2964              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2965            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2966          }          }
2967    
2968          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2969                    has_reference => 1,
2970                    line => $l, column => $c,
2971                   };
2972        } # X        } # X
2973      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2974               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2975        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2976        !!!next-input-character;        !!!next-input-character;
2977                
2978        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2979                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2980            !!!cp (1012);
2981          $code *= 10;          $code *= 10;
2982          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2983                    
2984          !!!next-input-character;          !!!next-input-character;
2985        }        }
2986    
2987        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2988            !!!cp (1013);
2989          !!!next-input-character;          !!!next-input-character;
2990        } else {        } else {
2991          !!!parse-error (type => 'no refc');          !!!cp (1014);
2992            !!!parse-error (type => 'no refc', line => $l, column => $c);
2993        }        }
2994    
2995        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2996          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2997            !!!parse-error (type => 'invalid character reference',
2998                            text => (sprintf 'U+%04X', $code),
2999                            line => $l, column => $c);
3000          $code = 0xFFFD;          $code = 0xFFFD;
3001        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3002          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
3003            !!!parse-error (type => 'invalid character reference',
3004                            text => (sprintf 'U-%08X', $code),
3005                            line => $l, column => $c);
3006          $code = 0xFFFD;          $code = 0xFFFD;
3007        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3008          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
3009            !!!parse-error (type => 'CR character reference',
3010                            line => $l, column => $c);
3011          $code = 0x000A;          $code = 0x000A;
3012        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3013          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
3014            !!!parse-error (type => 'C1 character reference',
3015                            text => (sprintf 'U+%04X', $code),
3016                            line => $l, column => $c);
3017          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3018        }        }
3019                
3020        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3021                  line => $l, column => $c,
3022                 };
3023      } else {      } else {
3024        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3025        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3026        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3027          $self->{next_char} = 0x0023; # #
3028        return undef;        return undef;
3029      }      }
3030    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3031              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3032             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3033              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3034      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3035      !!!next-input-character;      !!!next-input-character;
3036    
3037      my $value = $entity_name;      my $value = $entity_name;
# Line 1724  sub _tokenize_attempt_to_consume_an_enti Line 3039  sub _tokenize_attempt_to_consume_an_enti
3039      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3040      our $EntityChar;      our $EntityChar;
3041    
3042      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3043             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3044             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3045               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3046              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3047               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3048              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3049               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3050              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3051        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3052        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3053          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3054              !!!cp (1020);
3055            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3056            $match = 1;            $match = 1;
3057            !!!next-input-character;            !!!next-input-character;
3058            last;            last;
3059          } else {          } else {
3060              !!!cp (1021);
3061            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3062            $match = -1;            $match = -1;
3063            !!!next-input-character;            !!!next-input-character;
3064          }          }
3065        } else {        } else {
3066          $value .= chr $self->{next_input_character};          !!!cp (1022);
3067            $value .= chr $self->{next_char};
3068          $match *= 2;          $match *= 2;
3069          !!!next-input-character;          !!!next-input-character;
3070        }        }
3071      }      }
3072            
3073      if ($match > 0) {      if ($match > 0) {
3074        return {type => 'character', data => $value};        !!!cp (1023);
3075          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3076                  line => $l, column => $c,
3077                 };
3078      } elsif ($match < 0) {      } elsif ($match < 0) {
3079        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3080        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3081          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
3082        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3083          return {type => 'character', data => $value};                  line => $l, column => $c,
3084                   };
3085          } else {
3086            !!!cp (1025);
3087            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3088                    line => $l, column => $c,
3089                   };
3090        }        }
3091      } else {      } else {
3092        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3093        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3094        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3095          return {type => CHARACTER_TOKEN, data => '&'.$value,
3096                  line => $l, column => $c,
3097                 };
3098      }      }
3099    } else {    } else {
3100        !!!cp (1027);
3101      ## no characters are consumed      ## no characters are consumed
3102      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3103      return undef;      return undef;
3104    }    }
3105  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1780  sub _initialize_tree_constructor ($) { Line 3111  sub _initialize_tree_constructor ($) {
3111    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3112    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3113    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3114      $self->{document}->set_user_data (manakai_source_line => 1);
3115      $self->{document}->set_user_data (manakai_source_column => 1);
3116  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3117    
3118  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1806  sub _construct_tree ($) { Line 3139  sub _construct_tree ($) {
3139        
3140    !!!next-token;    !!!next-token;
3141    
   $self->{insertion_mode} = 'before head';  
3142    undef $self->{form_element};    undef $self->{form_element};
3143    undef $self->{head_element};    undef $self->{head_element};
3144    $self->{open_elements} = [];    $self->{open_elements} = [];
3145    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3146    
3147      ## NOTE: The "initial" insertion mode.
3148    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3149    
3150      ## NOTE: The "before html" insertion mode.
3151    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3152      $self->{insertion_mode} = BEFORE_HEAD_IM;
3153    
3154      ## NOTE: The "before head" insertion mode and so on.
3155    $self->_tree_construction_main;    $self->_tree_construction_main;
3156  } # _construct_tree  } # _construct_tree
3157    
3158  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3159    my $self = shift;    my $self = shift;
3160    
3161      ## NOTE: "initial" insertion mode
3162    
3163    INITIAL: {    INITIAL: {
3164      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3165        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3166        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3167        ## language.        ## language.
3168        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3169        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3170        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3171        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3172            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3173          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3174            !!!parse-error (type => 'not HTML5', token => $token);
3175        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3176          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3177          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3178          } elsif (defined $token->{public_identifier}) {
3179            if ($token->{public_identifier} eq 'XSLT-compat') {
3180              !!!cp ('t1.2');
3181              !!!parse-error (type => 'XSLT-compat', token => $token,
3182                              level => $self->{level}->{should});
3183            } else {
3184              !!!parse-error (type => 'not HTML5', token => $token);
3185            }
3186          } else {
3187            !!!cp ('t3');
3188            #
3189        }        }
3190                
3191        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3192          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3193          ## NOTE: Default value for both |public_id| and |system_id| attributes
3194          ## are empty strings, so that we don't set any value in missing cases.
3195        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3196            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3197        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 3200  sub _tree_construction_initial ($) {
3200        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3201        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3202                
3203        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3204            !!!cp ('t4');
3205          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3206        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3207          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3208          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3209          if ({          my $prefix = [
3210            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3211            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3212            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3213            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3214            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3215            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3216            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3217            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3218            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3219            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3220            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3221            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3222            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3223            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3224            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3225            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3226            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3227            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3228            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3229            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3230            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3231            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3232            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3233            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3234            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3235            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3236            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3237            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3238            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3239            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3240            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3241            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3242            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3243            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3244            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3245            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3246            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3247            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3248            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3249            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3250            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3251            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3252            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3253            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3254            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3255            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3256            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3257            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3258            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3259            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3260            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3261            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3262            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3263            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3264            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3265            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3266            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3267            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3268            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3269            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3270            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3271            "-//W3C//DTD W3 HTML//EN" => 1,            }
3272            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3273            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3274            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3275            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3276            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3277            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3278            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3279          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3280                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3281            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3282                !!!cp ('t6');
3283              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3284            } else {            } else {
3285                !!!cp ('t7');
3286              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3287            }            }
3288          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3289                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3290              !!!cp ('t8');
3291            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3292            } else {
3293              !!!cp ('t9');
3294          }          }
3295          } else {
3296            !!!cp ('t10');
3297        }        }
3298        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3299          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3300          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3301          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") {
3302              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3303              ## marked as quirks.
3304            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3305              !!!cp ('t11');
3306            } else {
3307              !!!cp ('t12');
3308          }          }
3309          } else {
3310            !!!cp ('t13');
3311        }        }
3312                
3313        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3314        !!!next-token;        !!!next-token;
3315        return;        return;
3316      } elsif ({      } elsif ({
3317                'start tag' => 1,                START_TAG_TOKEN, 1,
3318                'end tag' => 1,                END_TAG_TOKEN, 1,
3319                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3320               }->{$token->{type}}) {               }->{$token->{type}}) {
3321        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3322          !!!parse-error (type => 'no DOCTYPE', token => $token);
3323        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3324        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3325        ## reprocess        ## reprocess
3326          !!!ack-later;
3327        return;        return;
3328      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3329        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3330          ## Ignore the token          ## Ignore the token
3331    
3332          unless (length $token->{data}) {          unless (length $token->{data}) {
3333            ## Stay in the phase            !!!cp ('t15');
3334              ## Stay in the insertion mode.
3335            !!!next-token;            !!!next-token;
3336            redo INITIAL;            redo INITIAL;
3337            } else {
3338              !!!cp ('t16');
3339          }          }
3340          } else {
3341            !!!cp ('t17');
3342        }        }
3343    
3344        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3345        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3346        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3347        ## reprocess        ## reprocess
3348        return;        return;
3349      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3350          !!!cp ('t18');
3351        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3352        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3353                
3354        ## Stay in the phase.        ## Stay in the insertion mode.
3355        !!!next-token;        !!!next-token;
3356        redo INITIAL;        redo INITIAL;
3357      } else {      } else {
3358        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3359      }      }
3360    } # INITIAL    } # INITIAL
3361    
3362      die "$0: _tree_construction_initial: This should be never reached";
3363  } # _tree_construction_initial  } # _tree_construction_initial
3364    
3365  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3366    my $self = shift;    my $self = shift;
3367    
3368      ## NOTE: "before html" insertion mode.
3369        
3370    B: {    B: {
3371        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3372          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3373            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3374          ## Ignore the token          ## Ignore the token
3375          ## Stay in the phase          ## Stay in the insertion mode.
3376          !!!next-token;          !!!next-token;
3377          redo B;          redo B;
3378        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3379            !!!cp ('t20');
3380          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3381          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3382          ## Stay in the phase          ## Stay in the insertion mode.
3383          !!!next-token;          !!!next-token;
3384          redo B;          redo B;
3385        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3386          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3387            ## Ignore the token.            ## Ignore the token.
3388    
3389            unless (length $token->{data}) {            unless (length $token->{data}) {
3390              ## Stay in the phase              !!!cp ('t21');
3391                ## Stay in the insertion mode.
3392              !!!next-token;              !!!next-token;
3393              redo B;              redo B;
3394              } else {
3395                !!!cp ('t22');
3396            }            }
3397            } else {
3398              !!!cp ('t23');
3399          }          }
3400    
3401            $self->{application_cache_selection}->(undef);
3402    
3403          #          #
3404          } elsif ($token->{type} == START_TAG_TOKEN) {
3405            if ($token->{tag_name} eq 'html') {
3406              my $root_element;
3407              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3408              $self->{document}->append_child ($root_element);
3409              push @{$self->{open_elements}},
3410                  [$root_element, $el_category->{html}];
3411    
3412              if ($token->{attributes}->{manifest}) {
3413                !!!cp ('t24');
3414                $self->{application_cache_selection}
3415                    ->($token->{attributes}->{manifest}->{value});
3416                ## ISSUE: Spec is unclear on relative references.
3417                ## According to Hixie (#whatwg 2008-03-19), it should be
3418                ## resolved against the base URI of the document in HTML
3419                ## or xml:base of the element in XHTML.
3420              } else {
3421                !!!cp ('t25');
3422                $self->{application_cache_selection}->(undef);
3423              }
3424    
3425              !!!nack ('t25c');
3426    
3427              !!!next-token;
3428              return; ## Go to the "before head" insertion mode.
3429            } else {
3430              !!!cp ('t25.1');
3431              #
3432            }
3433        } elsif ({        } elsif ({
3434                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3435                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3436                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3437          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3438          #          #
3439        } else {        } else {
3440          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3441        }        }
3442        my $root_element; !!!create-element ($root_element, 'html');  
3443        $self->{document}->append_child ($root_element);      my $root_element;
3444        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3445        ## reprocess      $self->{document}->append_child ($root_element);
3446        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3447        return; ## Go to the main phase.  
3448        $self->{application_cache_selection}->(undef);
3449    
3450        ## NOTE: Reprocess the token.
3451        !!!ack-later;
3452        return; ## Go to the "before head" insertion mode.
3453    
3454        ## ISSUE: There is an issue in the spec
3455    } # B    } # B
3456    
3457      die "$0: _tree_construction_root_element: This should never be reached";
3458  } # _tree_construction_root_element  } # _tree_construction_root_element
3459    
3460  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3469  sub _reset_insertion_mode ($) {
3469            
3470      ## Step 3      ## Step 3
3471      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"!?  
3472        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3473          $last = 1;          $last = 1;
3474          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3475            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3476                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3477              #          } else {
3478            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3479          }          }
3480        }        }
3481              
3482        ## Step 4..13        ## Step 4..14
3483        my $new_mode = {        my $new_mode;
3484                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3485                        td => 'in cell',          !!!cp ('t28.1');
3486                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3487                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3488                        tbody => 'in table body',          ## SVG elements as foreigners.
3489                        thead => 'in table head',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3490                        tfoot => 'in table foot',        } elsif ($node->[1] & TABLE_CELL_EL) {
3491                        caption => 'in caption',          if ($last) {
3492                        colgroup => 'in column group',            !!!cp ('t28.2');
3493                        table => 'in table',            #
3494                        head => 'in body', # not in head!          } else {
3495                        body => 'in body',            !!!cp ('t28.3');
3496                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3497                       }->{$node->[1]};          }
3498          } else {
3499            !!!cp ('t28.4');
3500            $new_mode = {
3501                          select => IN_SELECT_IM,
3502                          ## NOTE: |option| and |optgroup| do not set
3503                          ## insertion mode to "in select" by themselves.
3504                          tr => IN_ROW_IM,
3505                          tbody => IN_TABLE_BODY_IM,
3506                          thead => IN_TABLE_BODY_IM,
3507                          tfoot => IN_TABLE_BODY_IM,
3508                          caption => IN_CAPTION_IM,
3509                          colgroup => IN_COLUMN_GROUP_IM,
3510                          table => IN_TABLE_IM,
3511                          head => IN_BODY_IM, # not in head!
3512                          body => IN_BODY_IM,
3513                          frameset => IN_FRAMESET_IM,
3514                         }->{$node->[0]->manakai_local_name};
3515          }
3516        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3517                
3518        ## Step 14        ## Step 15
3519        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3520          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3521            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3522              $self->{insertion_mode} = BEFORE_HEAD_IM;
3523          } else {          } else {
3524            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3525              !!!cp ('t30');
3526              $self->{insertion_mode} = AFTER_HEAD_IM;
3527          }          }
3528          return;          return;
3529          } else {
3530            !!!cp ('t31');
3531        }        }
3532                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3533        ## Step 16        ## Step 16
3534          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3535          
3536          ## Step 17
3537        $i--;        $i--;
3538        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3539                
3540        ## Step 17        ## Step 18
3541        redo S3;        redo S3;
3542      } # S3      } # S3
3543    
3544      die "$0: _reset_insertion_mode: This line should never be reached";
3545  } # _reset_insertion_mode  } # _reset_insertion_mode
3546    
3547  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3548    my $self = shift;    my $self = shift;
3549    
   my $previous_insertion_mode;  
   
3550    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3551    
3552    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3563  sub _tree_construction_main ($) {
3563      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3564      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3565        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3566            !!!cp ('t32');
3567          return;          return;
3568        }        }
3569      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3578  sub _tree_construction_main ($) {
3578    
3579        ## Step 6        ## Step 6
3580        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3581            !!!cp ('t33_1');
3582          #          #
3583        } else {        } else {
3584          my $in_open_elements;          my $in_open_elements;
3585          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3586            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3587                !!!cp ('t33');
3588              $in_open_elements = 1;              $in_open_elements = 1;
3589              last OE;              last OE;
3590            }            }
3591          }          }
3592          if ($in_open_elements) {          if ($in_open_elements) {
3593              !!!cp ('t34');
3594            #            #
3595          } else {          } else {
3596              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3597              !!!cp ('t35');
3598            redo S4;            redo S4;
3599          }          }
3600        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3617  sub _tree_construction_main ($) {
3617    
3618        ## Step 11        ## Step 11
3619        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3620            !!!cp ('t36');
3621          ## Step 7'          ## Step 7'
3622          $i++;          $i++;
3623          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3624                    
3625          redo S7;          redo S7;
3626        }        }
3627    
3628          !!!cp ('t37');
3629      } # S7      } # S7
3630    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3631    
3632    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3633      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3634        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3635            !!!cp ('t38');
3636          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3637          return;          return;
3638        }        }
3639      }      }
3640    
3641        !!!cp ('t39');
3642    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3643    
3644    my $parse_rcdata = sub ($$) {    my $insert;
3645      my ($content_model_flag, $insert) = @_;  
3646      my $parse_rcdata = sub ($) {
3647        my ($content_model_flag) = @_;
3648    
3649      ## Step 1      ## Step 1
3650      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3651      my $el;      my $el;
3652      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3653    
3654      ## Step 2      ## Step 2
3655      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3656    
3657      ## Step 3      ## Step 3
3658      $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 3660  sub _tree_construction_main ($) {
3660    
3661      ## Step 4      ## Step 4
3662      my $text = '';      my $text = '';
3663        !!!nack ('t40.1');
3664      !!!next-token;      !!!next-token;
3665      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3666          !!!cp ('t40');
3667        $text .= $token->{data};        $text .= $token->{data};
3668        !!!next-token;        !!!next-token;
3669      }      }
3670    
3671      ## Step 5      ## Step 5
3672      if (length $text) {      if (length $text) {
3673          !!!cp ('t41');
3674        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3675        $el->append_child ($text);        $el->append_child ($text);
3676      }      }
# Line 2220  sub _tree_construction_main ($) { Line 3679  sub _tree_construction_main ($) {
3679      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3680    
3681      ## Step 7      ## Step 7
3682      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3683            $token->{tag_name} eq $start_tag_name) {
3684          !!!cp ('t42');
3685        ## 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});  
3686      } else {      } else {
3687        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3688          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3689            !!!cp ('t43');
3690            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3691          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3692            !!!cp ('t44');
3693            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3694          } else {
3695            die "$0: $content_model_flag in parse_rcdata";
3696          }
3697      }      }
3698      !!!next-token;      !!!next-token;
3699    }; # $parse_rcdata    }; # $parse_rcdata
3700    
3701    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3702      my $script_el;      my $script_el;
3703      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3704      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3705    
3706      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3707      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3708            
3709      my $text = '';      my $text = '';
3710        !!!nack ('t45.1');
3711      !!!next-token;      !!!next-token;
3712      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3713          !!!cp ('t45');
3714        $text .= $token->{data};        $text .= $token->{data};
3715        !!!next-token;        !!!next-token;
3716      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3717      if (length $text) {      if (length $text) {
3718          !!!cp ('t46');
3719        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3720      }      }
3721                                
3722      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3723    
3724      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3725          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3726          !!!cp ('t47');
3727        ## Ignore the token        ## Ignore the token
3728      } else {      } else {
3729        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3730          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3731        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3732        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3733      }      }
3734            
3735      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3736          !!!cp ('t49');
3737        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3738      } else {      } else {
3739          !!!cp ('t50');
3740        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3741        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3742    
# Line 2278  sub _tree_construction_main ($) { Line 3750  sub _tree_construction_main ($) {
3750      !!!next-token;      !!!next-token;
3751    }; # $script_start_tag    }; # $script_start_tag
3752    
3753      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3754      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3755      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3756    
3757    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3758      my $tag_name = shift;      my $end_tag_token = shift;
3759        my $tag_name = $end_tag_token->{tag_name};
3760    
3761        ## NOTE: The adoption agency algorithm (AAA).
3762    
3763      FET: {      FET: {
3764        ## Step 1        ## Step 1
3765        my $formatting_element;        my $formatting_element;
3766        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3767        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3768          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3769              !!!cp ('t52');
3770              last AFE;
3771            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3772                         eq $tag_name) {
3773              !!!cp ('t51');
3774            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3775            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3776            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3777          }          }
3778        } # AFE        } # AFE
3779        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3780          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3781            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3782          ## Ignore the token          ## Ignore the token
3783          !!!next-token;          !!!next-token;
3784          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3790  sub _tree_construction_main ($) {
3790          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3791          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3792            if ($in_scope) {            if ($in_scope) {
3793                !!!cp ('t54');
3794              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3795              last INSCOPE;              last INSCOPE;
3796            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3797              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3798                !!!parse-error (type => 'unmatched end tag',
3799                                text => $token->{tag_name},
3800                                token => $end_tag_token);
3801              ## Ignore the token              ## Ignore the token
3802              !!!next-token;              !!!next-token;
3803              return;              return;
3804            }            }
3805          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3806                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3807            $in_scope = 0;            $in_scope = 0;
3808          }          }
3809        } # INSCOPE        } # INSCOPE
3810        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3811          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3812            !!!parse-error (type => 'unmatched end tag',
3813                            text => $token->{tag_name},
3814                            token => $end_tag_token);
3815          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3816          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3817          return;          return;
3818        }        }
3819        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3820          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3821            !!!parse-error (type => 'not closed',
3822                            text => $self->{open_elements}->[-1]->[0]
3823                                ->manakai_local_name,
3824                            token => $end_tag_token);
3825        }        }
3826                
3827        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 3829  sub _tree_construction_main ($) {
3829        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3830        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3831          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3832          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3833              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3834              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3835               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3836              !!!cp ('t59');
3837            $furthest_block = $node;            $furthest_block = $node;
3838            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3839          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3840              !!!cp ('t60');
3841            last OE;            last OE;
3842          }          }
3843        } # OE        } # OE
3844                
3845        ## Step 3        ## Step 3
3846        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3847            !!!cp ('t61');
3848          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3849          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3850          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3857  sub _tree_construction_main ($) {
3857        ## Step 5        ## Step 5
3858        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3859        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3860            !!!cp ('t62');
3861          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3862        }        }
3863                
# Line 2384  sub _tree_construction_main ($) { Line 3880  sub _tree_construction_main ($) {
3880          S7S2: {          S7S2: {
3881            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3882              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3883                  !!!cp ('t63');
3884                $node_i_in_active = $_;                $node_i_in_active = $_;
3885                last S7S2;                last S7S2;
3886              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894                    
3895          ## Step 4          ## Step 4
3896          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3897              !!!cp ('t64');
3898            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3899          }          }
3900                    
3901          ## Step 5          ## Step 5
3902          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3903              !!!cp ('t65');
3904            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3905            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3906            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3918  sub _tree_construction_main ($) {
3918        } # S7          } # S7  
3919                
3920        ## Step 8        ## Step 8
3921        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3922            my $foster_parent_element;
3923            my $next_sibling;
3924            OE: for (reverse 0..$#{$self->{open_elements}}) {
3925              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3926                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3927                                 if (defined $parent and $parent->node_type == 1) {
3928                                   !!!cp ('t65.1');
3929                                   $foster_parent_element = $parent;
3930                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3931                                 } else {
3932                                   !!!cp ('t65.2');
3933                                   $foster_parent_element
3934                                     = $self->{open_elements}->[$_ - 1]->[0];
3935                                 }
3936                                 last OE;
3937                               }
3938                             } # OE
3939                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3940                               unless defined $foster_parent_element;
3941            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3942            $open_tables->[-1]->[1] = 1; # tainted
3943          } else {
3944            !!!cp ('t65.3');
3945            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3946          }
3947                
3948        ## Step 9        ## Step 9
3949        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3960  sub _tree_construction_main ($) {
3960        my $i;        my $i;
3961        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3962          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3963              !!!cp ('t66');
3964            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3965            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3966          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3967              !!!cp ('t67');
3968            $i = $_;            $i = $_;
3969          }          }
3970        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3974  sub _tree_construction_main ($) {
3974        undef $i;        undef $i;
3975        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3976          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3977              !!!cp ('t68');
3978            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3979            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3980          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3981              !!!cp ('t69');
3982            $i = $_;            $i = $_;
3983          }          }
3984        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 3989  sub _tree_construction_main ($) {
3989      } # FET      } # FET
3990    }; # $formatting_end_tag    }; # $formatting_end_tag
3991    
3992    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3993      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3994    }; # $insert_to_current    }; # $insert_to_current
3995    
3996    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3997                         my $child = shift;      my $child = shift;
3998                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3999                              table => 1, tbody => 1, tfoot => 1,        # MUST
4000                              thead => 1, tr => 1,        my $foster_parent_element;
4001                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4002                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4003                           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') {  
4004                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4005                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4006                                   !!!cp ('t70');
4007                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4008                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4009                               } else {                               } else {
4010                                   !!!cp ('t71');
4011                                 $foster_parent_element                                 $foster_parent_element
4012                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4013                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 4018  sub _tree_construction_main ($) {
4018                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4019                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4020                             ($child, $next_sibling);                             ($child, $next_sibling);
4021                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4022                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4023                         }        !!!cp ('t72');
4024          $self->{open_elements}->[-1]->[0]->append_child ($child);
4025        }
4026    }; # $insert_to_foster    }; # $insert_to_foster
4027    
4028    my $in_body = sub {    B: while (1) {
4029      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4030      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4031        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4032          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
4033          $script_start_tag->($insert);        ## Stay in the phase
4034          return;        !!!next-token;
4035        } elsif ($token->{tag_name} eq 'style') {        next B;
4036          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
4037          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);               $token->{tag_name} eq 'html') {
4038          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4039        } elsif ({          !!!cp ('t79');
4040                  base => 1, link => 1,          !!!parse-error (type => 'after html', text => 'html', token => $token);
4041                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
4042          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4043          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4044          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html', text => 'html', token => $token);
4045          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4046          return;        } else {
4047        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
4048          ## 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  
         }  
4049    
4050          !!!next-token;        !!!cp ('t82');
4051          return;        !!!parse-error (type => 'not first start tag', token => $token);
4052        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4053          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4054          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4055          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {            !!!cp ('t84');
4056            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
4057              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
4058            } 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;  
4059          }          }
4060        } elsif ($token->{tag_name} eq 'li') {        }
4061          ## has a p element in scope        !!!nack ('t84.1');
4062          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
4063            if ($_->[1] eq 'p') {        next B;
4064              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
4065              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
4066              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4067            } elsif ({          !!!cp ('t85');
4068                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
4069                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4070                     }->{$_->[1]}) {          !!!cp ('t86');
4071              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
4072            }        } else {
4073          } # INSCOPE          !!!cp ('t87');
4074                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
4075          ## Step 1        }
4076          my $i = -1;        !!!next-token;
4077          my $node = $self->{open_elements}->[$i];        next B;
4078          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4079            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
4080            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
4081              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});  
             
4082          !!!next-token;          !!!next-token;
4083          return;          next B;
4084        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4085          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4086            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4087            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4088              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4089                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4090              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4091              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4092              $formatting_end_tag->($token->{tag_name});            #
4093                        } elsif ({
4094              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4095                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4096                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4097                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4098                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4099              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4100              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4101                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4102                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4103                  last OE;            !!!cp ('t87.2');
4104                }            !!!parse-error (type => 'not closed',
4105              } # OE                            text => $self->{open_elements}->[-1]->[0]
4106              last AFE;                                ->manakai_local_name,
4107            } elsif ($node->[0] eq '#marker') {                            token => $token);
4108              last AFE;  
4109              pop @{$self->{open_elements}}
4110                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4111    
4112              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4113              ## Reprocess.
4114              next B;
4115            } else {
4116              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4117              my $tag_name = $token->{tag_name};
4118              if ($nsuri eq $SVG_NS) {
4119                $tag_name = {
4120                   altglyph => 'altGlyph',
4121                   altglyphdef => 'altGlyphDef',
4122                   altglyphitem => 'altGlyphItem',
4123                   animatecolor => 'animateColor',
4124                   animatemotion => 'animateMotion',
4125                   animatetransform => 'animateTransform',
4126                   clippath => 'clipPath',
4127                   feblend => 'feBlend',
4128                   fecolormatrix => 'feColorMatrix',
4129                   fecomponenttransfer => 'feComponentTransfer',
4130                   fecomposite => 'feComposite',
4131                   feconvolvematrix => 'feConvolveMatrix',
4132                   fediffuselighting => 'feDiffuseLighting',
4133                   fedisplacementmap => 'feDisplacementMap',
4134                   fedistantlight => 'feDistantLight',
4135                   feflood => 'feFlood',
4136                   fefunca => 'feFuncA',
4137                   fefuncb => 'feFuncB',
4138                   fefuncg => 'feFuncG',
4139                   fefuncr => 'feFuncR',
4140                   fegaussianblur => 'feGaussianBlur',
4141                   feimage => 'feImage',
4142                   femerge => 'feMerge',
4143                   femergenode => 'feMergeNode',
4144                   femorphology => 'feMorphology',
4145                   feoffset => 'feOffset',
4146                   fepointlight => 'fePointLight',
4147                   fespecularlighting => 'feSpecularLighting',
4148                   fespotlight => 'feSpotLight',
4149                   fetile => 'feTile',
4150                   feturbulence => 'feTurbulence',
4151                   foreignobject => 'foreignObject',
4152                   glyphref => 'glyphRef',
4153                   lineargradient => 'linearGradient',
4154                   radialgradient => 'radialGradient',
4155                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4156                   textpath => 'textPath',  
4157                }->{$tag_name} || $tag_name;
4158            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4159    
4160          !!!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];  
4161    
4162          !!!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);  
4163    
4164          ## has a |nobr| element in scope            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
4165    
4166          ## NOTE: There is an "as if <br>" code clone.            if ($self->{self_closing}) {
4167          $reconstruct_active_formatting_elements->($insert_to_current);              pop @{$self->{open_elements}};
4168                        !!!ack ('t87.3');
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4169            } else {            } else {
4170              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
4171            }            }
4172            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};  
4173            !!!next-token;            !!!next-token;
4174              next B;
4175          }          }
4176          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4177            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4178          }          !!!cp ('t87.5');
4179                    #
4180          $self->{content_model} = PCDATA_CONTENT_MODEL;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4181                    !!!cp ('t87.6');
4182          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
4183              $token->{tag_name} eq $tag_name) {                          text => $self->{open_elements}->[-1]->[0]
4184            ## Ignore the token                              ->manakai_local_name,
4185          } else {                          token => $token);
4186            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4187          }          pop @{$self->{open_elements}}
4188          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4189          return;  
4190        } elsif ({          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4191                  iframe => 1,          ## Reprocess.
4192                  noembed => 1,          next B;
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $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.  
4193        } else {        } else {
4194          $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;  
4195        }        }
4196      } 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]);  
             }  
           }  
4197    
4198            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4199            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4200            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4201          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4202            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4203            ## 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]);  
4204            } else {            } else {
4205              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4206            }              ## Ignore the token.
4207          }              !!!next-token;
4208                        next B;
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4209            }            }
4210          } # INSCOPE            unless (length $token->{data}) {
4211                        !!!cp ('t88');
4212          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {              !!!next-token;
4213            pop @{$self->{open_elements}};              next B;
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4214            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4215          }          }
           
         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');  
4216    
4217          ## As if <br>          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4218          $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t89');
4219                      ## As if <head>
4220          my $el;            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4221          !!!create-element ($el, 'br');            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4222          $insert->($el);            push @{$self->{open_elements}},
4223                          [$self->{head_element}, $el_category->{head}];
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4224    
4225          ## Step 2            ## Reprocess in the "in head" insertion mode...
4226          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4227    
4228              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4229              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4230            } else {            !!!cp ('t90');
4231              ## Step 3            ## As if </noscript>
4232              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4233                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#text', 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];  
4234                        
4235            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4236            redo S2;            ## As if </head>
4237          } # 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.  
       }  
4238    
4239        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4240        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4241      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4242               $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;  
       }  
4243    
4244  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4245  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4246        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});  
4247          }          }
4248        }  
4249        !!!next-token;          ## "after head" insertion mode
4250        redo B;          ## As if <body>
4251      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4252        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4253        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4254          $self->{document}->append_child ($comment);          next B;
4255        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4256          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4257        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4258          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4259        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4260        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4261        redo B;                  ($self->{head_element});
4262      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4263            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4264              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4265                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4266                unless (length $token->{data}) {              !!!next-token;
4267                  !!!next-token;              next B;
4268                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4269                }              !!!cp ('t93.2');
4270              }              !!!parse-error (type => 'after head', text => 'head',
4271              ## As if <head>                              token => $token);
4272              !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4273              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t93.3');
4274              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4275              $self->{insertion_mode} = 'in head';              next B;
             ## 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;  
             }  
4276            } else {            } else {
4277              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4278                !!!parse-error (type => 'in head:head',
4279                                token => $token); # or in head noscript
4280                ## Ignore the token
4281                !!!nack ('t95.1');
4282                !!!next-token;
4283                next B;
4284            }            }
4285          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4286                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4287                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4288            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4289              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4290                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4291                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4292                  !!!next-token;  
4293                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4294              ## Reprocess in the "in head" insertion mode...
4295            } else {
4296              !!!cp ('t97');
4297            }
4298    
4299                if ($token->{tag_name} eq 'base') {
4300                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4301                    !!!cp ('t98');
4302                    ## As if </noscript>
4303                    pop @{$self->{open_elements}};
4304                    !!!parse-error (type => 'in noscript', text => 'base',
4305                                    token => $token);
4306                  
4307                    $self->{insertion_mode} = IN_HEAD_IM;
4308                    ## Reprocess in the "in head" insertion mode...
4309                  } else {
4310                    !!!cp ('t99');
4311                }                }
4312              }  
               
             #  
           } 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}}) {  
4313                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4314                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4315                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4316                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4317                                    text => $token->{tag_name}, token => $token);
4318                    push @{$self->{open_elements}},
4319                        [$self->{head_element}, $el_category->{head}];
4320                  } else {
4321                    !!!cp ('t101');
4322                }                }
4323                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4324                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4325                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4326                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4327                  !!!nack ('t101.1');
4328                !!!next-token;                !!!next-token;
4329                redo B;                next B;
4330              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4331                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4332                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4333                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4334                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4335                                    text => $token->{tag_name}, token => $token);
4336                    push @{$self->{open_elements}},
4337                        [$self->{head_element}, $el_category->{head}];
4338                  } else {
4339                    !!!cp ('t103');
4340                }                }
4341                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4342                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4343                  pop @{$self->{open_elements}} # <head>
4344                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4345                  !!!ack ('t103.1');
4346                  !!!next-token;
4347                  next B;
4348                } elsif ($token->{tag_name} eq 'meta') {
4349                  ## NOTE: There is a "as if in head" code clone.
4350                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4351                    !!!cp ('t104');
4352                    !!!parse-error (type => 'after head',
4353                                    text => $token->{tag_name}, token => $token);
4354                    push @{$self->{open_elements}},
4355                        [$self->{head_element}, $el_category->{head}];
4356                  } else {
4357                    !!!cp ('t105');
4358                  }
4359                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4360                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4361    
4362                unless ($self->{confident}) {                unless ($self->{confident}) {
4363                  my $charset;                  if ($token->{attributes}->{charset}) {
4364                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4365                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4366                  }                    ## in the {change_encoding} callback.
4367                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4368                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4369                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4370                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4371                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4372                          ->set_user_data (manakai_has_reference =>
4373                                               $token->{attributes}->{charset}
4374                                                   ->{has_reference});
4375                    } elsif ($token->{attributes}->{content}) {
4376                      if ($token->{attributes}->{content}->{value}
4377                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4378                              [\x09-\x0D\x20]*=
4379                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4380                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4381                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4382                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4383                        ## in the {change_encoding} callback.
4384                        $self->{change_encoding}
4385                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4386                               $token);
4387                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4388                            ->set_user_data (manakai_has_reference =>
4389                                                 $token->{attributes}->{content}
4390                                                       ->{has_reference});
4391                      } else {
4392                        !!!cp ('t108');
4393                      }
4394                    }
4395                  } else {
4396                    if ($token->{attributes}->{charset}) {
4397                      !!!cp ('t109');
4398                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4399                          ->set_user_data (manakai_has_reference =>
4400                                               $token->{attributes}->{charset}
4401                                                   ->{has_reference});
4402                    }
4403                    if ($token->{attributes}->{content}) {
4404                      !!!cp ('t110');
4405                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4406                          ->set_user_data (manakai_has_reference =>
4407                                               $token->{attributes}->{content}
4408                                                   ->{has_reference});
4409                  }                  }
                 ## TODO: Change the encoding  
4410                }                }
4411    
4412                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4413                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4414                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4415                !!!next-token;                !!!next-token;
4416                redo B;                next B;
4417              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4418                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4419                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4420                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4421                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4422                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript', text => 'title',
4423                                    token => $token);
4424                  
4425                    $self->{insertion_mode} = IN_HEAD_IM;
4426                    ## Reprocess in the "in head" insertion mode...
4427                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4428                    !!!cp ('t112');
4429                    !!!parse-error (type => 'after head',
4430                                    text => $token->{tag_name}, token => $token);
4431                    push @{$self->{open_elements}},
4432                        [$self->{head_element}, $el_category->{head}];
4433                  } else {
4434                    !!!cp ('t113');
4435                }                }
4436    
4437                  ## NOTE: There is a "as if in head" code clone.
4438                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4439                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4440                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4441                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4442                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4443                    if $self->{insertion_mode} eq 'after head';                next B;
4444                redo B;              } elsif ($token->{tag_name} eq 'style' or
4445              } elsif ($token->{tag_name} eq 'style') {                       $token->{tag_name} eq 'noframes') {
4446                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4447                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4448                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4449                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4450                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4451                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4452                                    text => $token->{tag_name}, token => $token);
4453                    push @{$self->{open_elements}},
4454                        [$self->{head_element}, $el_category->{head}];
4455                  } else {
4456                    !!!cp ('t115');
4457                }                }
4458                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4459                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4460                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4461                redo B;                next B;
4462              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4463                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4464                    !!!cp ('t116');
4465                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4466                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4467                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4468                    !!!nack ('t116.1');
4469                  !!!next-token;                  !!!next-token;
4470                  redo B;                  next B;
4471                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4472                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4473                    !!!parse-error (type => 'in noscript', text => 'noscript',
4474                                    token => $token);
4475                  ## Ignore the token                  ## Ignore the token
4476                    !!!nack ('t117.1');
4477                  !!!next-token;                  !!!next-token;
4478                  redo B;                  next B;
4479                } else {                } else {
4480                    !!!cp ('t118');
4481                  #                  #
4482                }                }
4483              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4484                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4485                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4486                ## Ignore the token                  ## As if </noscript>
4487                !!!next-token;                  pop @{$self->{open_elements}};
4488                redo B;                  !!!parse-error (type => 'in noscript', text => 'script',
4489              } elsif ($self->{insertion_mode} ne 'in head noscript' and                                  token => $token);
4490                       $token->{tag_name} eq 'script') {                
4491                if ($self->{insertion_mode} eq 'after head') {                  $self->{insertion_mode} = IN_HEAD_IM;
4492                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  ## Reprocess in the "in head" insertion mode...
4493                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4494                    !!!cp ('t120');
4495                    !!!parse-error (type => 'after head',
4496                                    text => $token->{tag_name}, token => $token);
4497                    push @{$self->{open_elements}},
4498                        [$self->{head_element}, $el_category->{head}];
4499                  } else {
4500                    !!!cp ('t121');
4501                }                }
4502    
4503                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4504                $script_start_tag->($insert_to_current);                $script_start_tag->();
4505                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4506                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4507                redo B;                next B;
4508              } 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  
4509                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4510                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4511                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4512                    ## As if </noscript>
4513                    pop @{$self->{open_elements}};
4514                    !!!parse-error (type => 'in noscript',
4515                                    text => $token->{tag_name}, token => $token);
4516                    
4517                    ## Reprocess in the "in head" insertion mode...
4518                    ## As if </head>
4519                    pop @{$self->{open_elements}};
4520                    
4521                    ## Reprocess in the "after head" insertion mode...
4522                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4523                    !!!cp ('t124');
4524                    pop @{$self->{open_elements}};
4525                    
4526                    ## Reprocess in the "after head" insertion mode...
4527                  } else {
4528                    !!!cp ('t125');
4529                  }
4530    
4531                  ## "after head" insertion mode
4532                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4533                  if ($token->{tag_name} eq 'body') {
4534                    !!!cp ('t126');
4535                    $self->{insertion_mode} = IN_BODY_IM;
4536                  } elsif ($token->{tag_name} eq 'frameset') {
4537                    !!!cp ('t127');
4538                    $self->{insertion_mode} = IN_FRAMESET_IM;
4539                  } else {
4540                    die "$0: tag name: $self->{tag_name}";
4541                  }
4542                  !!!nack ('t127.1');
4543                !!!next-token;                !!!next-token;
4544                redo B;                next B;
4545              } else {              } else {
4546                  !!!cp ('t128');
4547                #                #
4548              }              }
4549            } elsif ($token->{type} eq 'end tag') {  
4550              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4551                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4552                  ## As if </noscript>
4553                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4554                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/',
4555                !!!next-token;                                text => $token->{tag_name}, token => $token);
4556                redo B;                
4557              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## Reprocess in the "in head" insertion mode...
4558                  $token->{tag_name} eq 'noscript') {                ## As if </head>
4559                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4560                $self->{insertion_mode} = 'in head';  
4561                !!!next-token;                ## Reprocess in the "after head" insertion mode...
4562                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4563              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
4564                       {                ## As if </head>
4565                  pop @{$self->{open_elements}};
4566    
4567                  ## Reprocess in the "after head" insertion mode...
4568                } else {
4569                  !!!cp ('t131');
4570                }
4571    
4572                ## "after head" insertion mode
4573                ## As if <body>
4574                !!!insert-element ('body',, $token);
4575                $self->{insertion_mode} = IN_BODY_IM;
4576                ## reprocess
4577                !!!ack-later;
4578                next B;
4579              } elsif ($token->{type} == END_TAG_TOKEN) {
4580                if ($token->{tag_name} eq 'head') {
4581                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4582                    !!!cp ('t132');
4583                    ## As if <head>
4584                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4585                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4586                    push @{$self->{open_elements}},
4587                        [$self->{head_element}, $el_category->{head}];
4588    
4589                    ## Reprocess in the "in head" insertion mode...
4590                    pop @{$self->{open_elements}};
4591                    $self->{insertion_mode} = AFTER_HEAD_IM;
4592                    !!!next-token;
4593                    next B;
4594                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4595                    !!!cp ('t133');
4596                    ## As if </noscript>
4597                    pop @{$self->{open_elements}};
4598                    !!!parse-error (type => 'in noscript:/',
4599                                    text => 'head', token => $token);
4600                    
4601                    ## Reprocess in the "in head" insertion mode...
4602                    pop @{$self->{open_elements}};
4603                    $self->{insertion_mode} = AFTER_HEAD_IM;
4604                    !!!next-token;
4605                    next B;
4606                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4607                    !!!cp ('t134');
4608                    pop @{$self->{open_elements}};
4609                    $self->{insertion_mode} = AFTER_HEAD_IM;
4610                    !!!next-token;
4611                    next B;
4612                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4613                    !!!cp ('t134.1');
4614                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4615                                    token => $token);
4616                    ## Ignore the token
4617                    !!!next-token;
4618                    next B;
4619                  } else {
4620                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4621                  }
4622                } elsif ($token->{tag_name} eq 'noscript') {
4623                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4624                    !!!cp ('t136');
4625                    pop @{$self->{open_elements}};
4626                    $self->{insertion_mode} = IN_HEAD_IM;
4627                    !!!next-token;
4628                    next B;
4629                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4630                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4631                    !!!cp ('t137');
4632                    !!!parse-error (type => 'unmatched end tag',
4633                                    text => 'noscript', token => $token);
4634                    ## Ignore the token ## ISSUE: An issue in the spec.
4635                    !!!next-token;
4636                    next B;
4637                  } else {
4638                    !!!cp ('t138');
4639                    #
4640                  }
4641                } elsif ({
4642                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
4643                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4644                #                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4645              } elsif ($self->{insertion_mode} eq 'in head noscript' and                    $self->{insertion_mode} == IN_HEAD_IM or
4646                       {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4647                        p => 1, br => 1,                  !!!cp ('t140');
4648                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'unmatched end tag',
4649                #                                  text => $token->{tag_name}, token => $token);
4650              } elsif ($self->{insertion_mode} ne 'after head') {                  ## Ignore the token
4651                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!next-token;
4652                    next B;
4653                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4654                    !!!cp ('t140.1');
4655                    !!!parse-error (type => 'unmatched end tag',
4656                                    text => $token->{tag_name}, token => $token);
4657                    ## Ignore the token
4658                    !!!next-token;
4659                    next B;
4660                  } else {
4661                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4662                  }
4663                } elsif ($token->{tag_name} eq 'p') {
4664                  !!!cp ('t142');
4665                  !!!parse-error (type => 'unmatched end tag',
4666                                  text => $token->{tag_name}, token => $token);
4667                  ## Ignore the token
4668                  !!!next-token;
4669                  next B;
4670                } elsif ($token->{tag_name} eq 'br') {
4671                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4672                    !!!cp ('t142.2');
4673                    ## (before head) as if <head>, (in head) as if </head>
4674                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4675                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4676                    $self->{insertion_mode} = AFTER_HEAD_IM;
4677      
4678                    ## Reprocess in the "after head" insertion mode...
4679                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4680                    !!!cp ('t143.2');
4681                    ## As if </head>
4682                    pop @{$self->{open_elements}};
4683                    $self->{insertion_mode} = AFTER_HEAD_IM;
4684      
4685                    ## Reprocess in the "after head" insertion mode...
4686                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4687                    !!!cp ('t143.3');
4688                    ## ISSUE: Two parse errors for <head><noscript></br>
4689                    !!!parse-error (type => 'unmatched end tag',
4690                                    text => 'br', token => $token);
4691                    ## As if </noscript>
4692                    pop @{$self->{open_elements}};
4693                    $self->{insertion_mode} = IN_HEAD_IM;
4694    
4695                    ## Reprocess in the "in head" insertion mode...
4696                    ## As if </head>
4697                    pop @{$self->{open_elements}};
4698                    $self->{insertion_mode} = AFTER_HEAD_IM;
4699    
4700                    ## Reprocess in the "after head" insertion mode...
4701                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4702                    !!!cp ('t143.4');
4703                    #
4704                  } else {
4705                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4706                  }
4707    
4708                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4709                  !!!parse-error (type => 'unmatched end tag',
4710                                  text => 'br', token => $token);
4711                ## Ignore the token                ## Ignore the token
4712                !!!next-token;                !!!next-token;
4713                redo B;                next B;
4714              } else {              } else {
4715                #                !!!cp ('t145');
4716                  !!!parse-error (type => 'unmatched end tag',
4717                                  text => $token->{tag_name}, token => $token);
4718                  ## Ignore the token
4719                  !!!next-token;
4720                  next B;
4721              }              }
           } else {  
             #  
           }  
4722    
4723            ## As if </head> or </noscript> or <body>              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4724            if ($self->{insertion_mode} eq 'in head') {                !!!cp ('t146');
4725              pop @{$self->{open_elements}};                ## As if </noscript>
4726              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
4727            } elsif ($self->{insertion_mode} eq 'in head noscript') {                !!!parse-error (type => 'in noscript:/',
4728              pop @{$self->{open_elements}};                                text => $token->{tag_name}, token => $token);
4729              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));                
4730              $self->{insertion_mode} = 'in head';                ## Reprocess in the "in head" insertion mode...
4731            } else { # 'after head'                ## As if </head>
4732              !!!insert-element ('body');                pop @{$self->{open_elements}};
4733              $self->{insertion_mode} = 'in body';  
4734            }                ## Reprocess in the "after head" insertion mode...
4735            ## reprocess              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4736            redo B;                !!!cp ('t147');
4737                  ## As if </head>
4738                  pop @{$self->{open_elements}};
4739    
4740                  ## Reprocess in the "after head" insertion mode...
4741                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4742    ## ISSUE: This case cannot be reached?
4743                  !!!cp ('t148');
4744                  !!!parse-error (type => 'unmatched end tag',
4745                                  text => $token->{tag_name}, token => $token);
4746                  ## Ignore the token ## ISSUE: An issue in the spec.
4747                  !!!next-token;
4748                  next B;
4749                } else {
4750                  !!!cp ('t149');
4751                }
4752    
4753                ## "after head" insertion mode
4754                ## As if <body>
4755                !!!insert-element ('body',, $token);
4756                $self->{insertion_mode} = IN_BODY_IM;
4757                ## reprocess
4758                next B;
4759          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4760            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4761              !!!cp ('t149.1');
4762    
4763              ## NOTE: As if <head>
4764              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4765              $self->{open_elements}->[-1]->[0]->append_child
4766                  ($self->{head_element});
4767              #push @{$self->{open_elements}},
4768              #    [$self->{head_element}, $el_category->{head}];
4769              #$self->{insertion_mode} = IN_HEAD_IM;
4770              ## NOTE: Reprocess.
4771    
4772              ## NOTE: As if </head>
4773              #pop @{$self->{open_elements}};
4774              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4775              ## NOTE: Reprocess.
4776              
4777              #
4778            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4779              !!!cp ('t149.2');
4780    
4781              ## NOTE: As if </head>
4782              pop @{$self->{open_elements}};
4783              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4784              ## NOTE: Reprocess.
4785    
4786              #
4787            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4788              !!!cp ('t149.3');
4789    
4790              !!!parse-error (type => 'in noscript:#eof', token => $token);
4791    
4792              ## As if </noscript>
4793              pop @{$self->{open_elements}};
4794              #$self->{insertion_mode} = IN_HEAD_IM;
4795              ## NOTE: Reprocess.
4796    
4797              ## NOTE: As if </head>
4798              pop @{$self->{open_elements}};
4799              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4800              ## NOTE: Reprocess.
4801    
4802              #
4803            } else {
4804              !!!cp ('t149.4');
4805              #
4806            }
4807    
4808            ## NOTE: As if <body>
4809            !!!insert-element ('body',, $token);
4810            $self->{insertion_mode} = IN_BODY_IM;
4811            ## NOTE: Reprocess.
4812            next B;
4813          } else {
4814            die "$0: $token->{type}: Unknown token type";
4815          }
4816    
4817            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4818          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4819                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
4820                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4821              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4822              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4823                            
4824              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4825    
4826              !!!next-token;              !!!next-token;
4827              redo B;              next B;
4828            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4829              if ({              if ({
4830                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4831                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4832                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4833                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4834                  ## have an element in table scope                  ## have an element in table scope
4835                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4836                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4837                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4838                      $tn = $node->[1];                      !!!cp ('t151');
4839                      last INSCOPE;  
4840                    } elsif ({                      ## Close the cell
4841                              table => 1, html => 1,                      !!!back-token; # <x>
4842                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4843                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4844                    }                                line => $token->{line},
4845                  } # INSCOPE                                column => $token->{column}};
4846                    unless (defined $tn) {                      next B;
4847                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4848                      ## Ignore the token                      !!!cp ('t152');
4849                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4850                      redo B;                      last;
4851                    }                    }
4852                    }
4853    
4854                    !!!cp ('t153');
4855                    !!!parse-error (type => 'start tag not allowed',
4856                        text => $token->{tag_name}, token => $token);
4857                    ## Ignore the token
4858                    !!!nack ('t153.1');
4859                    !!!next-token;
4860                    next B;
4861                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4862                    !!!parse-error (type => 'not closed', text => 'caption',
4863                                    token => $token);
4864                                    
4865                  ## 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>  
4866                  ## have a table element in table scope                  ## have a table element in table scope
4867                  my $i;                  my $i;
4868                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4869                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4870                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4871                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4872                      last INSCOPE;                        !!!cp ('t155');
4873                    } elsif ({                        $i = $_;
4874                              table => 1, html => 1,                        last INSCOPE;
4875                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4876                      last INSCOPE;                        !!!cp ('t156');
4877                          last;
4878                        }
4879                    }                    }
4880    
4881                      !!!cp ('t157');
4882                      !!!parse-error (type => 'start tag not allowed',
4883                                      text => $token->{tag_name}, token => $token);
4884                      ## Ignore the token
4885                      !!!nack ('t157.1');
4886                      !!!next-token;
4887                      next B;
4888                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4889                                    
4890                  ## generate implied end tags                  ## generate implied end tags
4891                  if ({                  while ($self->{open_elements}->[-1]->[1]
4892                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4893                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4894                       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;  
4895                  }                  }
4896    
4897                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4898                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4899                      !!!parse-error (type => 'not closed',
4900                                      text => $self->{open_elements}->[-1]->[0]
4901                                          ->manakai_local_name,
4902                                      token => $token);
4903                    } else {
4904                      !!!cp ('t160');
4905                  }                  }
4906                                    
4907                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4908                                    
4909                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4910                                    
4911                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4912                                    
4913                  ## reprocess                  ## reprocess
4914                  redo B;                  !!!ack-later;
4915                    next B;
4916                } else {                } else {
4917                    !!!cp ('t161');
4918                  #                  #
4919                }                }
4920              } else {              } else {
4921                  !!!cp ('t162');
4922                #                #
4923              }              }
4924            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4925              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4926                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4927                  ## have an element in table scope                  ## have an element in table scope
4928                  my $i;                  my $i;
4929                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4930                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4931                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4932                        !!!cp ('t163');
4933                      $i = $_;                      $i = $_;
4934                      last INSCOPE;                      last INSCOPE;
4935                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4936                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4937                      last INSCOPE;                      last INSCOPE;
4938                    }                    }
4939                  } # INSCOPE                  } # INSCOPE
4940                    unless (defined $i) {                    unless (defined $i) {
4941                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4942                        !!!parse-error (type => 'unmatched end tag',
4943                                        text => $token->{tag_name},
4944                                        token => $token);
4945                      ## Ignore the token                      ## Ignore the token
4946                      !!!next-token;                      !!!next-token;
4947                      redo B;                      next B;
4948                    }                    }
4949                                    
4950                  ## generate implied end tags                  ## generate implied end tags
4951                  if ({                  while ($self->{open_elements}->[-1]->[1]
4952                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4953                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4954                       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;  
4955                  }                  }
4956                    
4957                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4958                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4959                      !!!cp ('t167');
4960                      !!!parse-error (type => 'not closed',
4961                                      text => $self->{open_elements}->[-1]->[0]
4962                                          ->manakai_local_name,
4963                                      token => $token);
4964                    } else {
4965                      !!!cp ('t168');
4966                  }                  }
4967                                    
4968                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4969                                    
4970                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4971                                    
4972                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
4973                                    
4974                  !!!next-token;                  !!!next-token;
4975                  redo B;                  next B;
4976                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4977                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4978                    !!!parse-error (type => 'unmatched end tag',
4979                                    text => $token->{tag_name}, token => $token);
4980                  ## Ignore the token                  ## Ignore the token
4981                  !!!next-token;                  !!!next-token;
4982                  redo B;                  next B;
4983                } else {                } else {
4984                    !!!cp ('t170');
4985                  #                  #
4986                }                }
4987              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4988                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4989                  ## have a table element in table scope                  ## have a table element in table scope
4990                  my $i;                  my $i;
4991                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4992                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4993                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4994                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4995                      last INSCOPE;                        !!!cp ('t171');
4996                    } elsif ({                        $i = $_;
4997                              table => 1, html => 1,                        last INSCOPE;
4998                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4999                      last INSCOPE;                        !!!cp ('t172');
5000                          last;
5001                        }
5002                    }                    }
5003    
5004                      !!!cp ('t173');
5005                      !!!parse-error (type => 'unmatched end tag',
5006                                      text => $token->{tag_name}, token => $token);
5007                      ## Ignore the token
5008                      !!!next-token;
5009                      next B;
5010                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5011                                    
5012                  ## generate implied end tags                  ## generate implied end tags
5013                  if ({                  while ($self->{open_elements}->[-1]->[1]
5014                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5015                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5016                       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;  
5017                  }                  }
5018                                    
5019                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5020                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5021                      !!!parse-error (type => 'not closed',
5022                                      text => $self->{open_elements}->[-1]->[0]
5023                                          ->manakai_local_name,
5024                                      token => $token);
5025                    } else {
5026                      !!!cp ('t176');
5027                  }                  }
5028                                    
5029                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5030                                    
5031                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5032                                    
5033                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5034                                    
5035                  !!!next-token;                  !!!next-token;
5036                  redo B;                  next B;
5037                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5038                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5039                    !!!parse-error (type => 'unmatched end tag',
5040                                    text => $token->{tag_name}, token => $token);
5041                  ## Ignore the token                  ## Ignore the token
5042                  !!!next-token;                  !!!next-token;
5043                  redo B;                  next B;
5044                } else {                } else {
5045                    !!!cp ('t178');
5046                  #                  #
5047                }                }
5048              } elsif ({              } elsif ({
5049                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
5050                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5051                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5052                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
5053                ## have an element in table scope                ## have an element in table scope
5054                my $i;                my $i;
5055                my $tn;                my $tn;
5056                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5057                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5058                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5059                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5060                    last INSCOPE;                      !!!cp ('t179');
5061                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5062                    $tn = $node->[1];  
5063                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5064                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5065                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5066                            table => 1, html => 1,                                line => $token->{line},
5067                           }->{$node->[1]}) {                                column => $token->{column}};
5068                    last INSCOPE;                      next B;
5069                      } elsif ($node->[1] & TABLE_CELL_EL) {
5070                        !!!cp ('t180');
5071                        $tn = $node->[0]->manakai_local_name;
5072                        ## NOTE: There is exactly one |td| or |th| element
5073                        ## in scope in the stack of open elements by definition.
5074                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5075                        ## ISSUE: Can this be reached?
5076                        !!!cp ('t181');
5077                        last;
5078                      }
5079                  }                  }
5080                } # INSCOPE  
5081                unless (defined $i) {                  !!!cp ('t182');
5082                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5083                        text => $token->{tag_name}, token => $token);
5084                  ## Ignore the token                  ## Ignore the token
5085                  !!!next-token;                  !!!next-token;
5086                  redo B;                  next B;
5087                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5088              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5089                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5090                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5091                                  token => $token);
5092    
5093                ## As if </caption>                ## As if </caption>
5094                ## have a table element in table scope                ## have a table element in table scope
5095                my $i;                my $i;
5096                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5097                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5098                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5099                      !!!cp ('t184');
5100                    $i = $_;                    $i = $_;
5101                    last INSCOPE;                    last INSCOPE;
5102                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5103                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5104                    last INSCOPE;                    last INSCOPE;
5105                  }                  }
5106                } # INSCOPE                } # INSCOPE
5107                unless (defined $i) {                unless (defined $i) {
5108                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5109                    !!!parse-error (type => 'unmatched end tag',
5110                                    text => 'caption', token => $token);
5111                  ## Ignore the token                  ## Ignore the token
5112                  !!!next-token;                  !!!next-token;
5113                  redo B;                  next B;
5114                }                }
5115                                
5116                ## generate implied end tags                ## generate implied end tags
5117                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5118                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5119                     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;  
5120                }                }
5121    
5122                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5123                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5124                    !!!parse-error (type => 'not closed',
5125                                    text => $self->{open_elements}->[-1]->[0]
5126                                        ->manakai_local_name,
5127                                    token => $token);
5128                  } else {
5129                    !!!cp ('t189');
5130                }                }
5131    
5132                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5133    
5134                $clear_up_to_marker->();                $clear_up_to_marker->();
5135    
5136                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5137    
5138                ## reprocess                ## reprocess
5139                redo B;                next B;
5140              } elsif ({              } elsif ({
5141                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5142                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5143                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5144                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5145                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5146                                    text => $token->{tag_name}, token => $token);
5147                  ## Ignore the token                  ## Ignore the token
5148                  !!!next-token;                  !!!next-token;
5149                  redo B;                  next B;
5150                } else {                } else {
5151                    !!!cp ('t191');
5152                  #                  #
5153                }                }
5154              } elsif ({              } elsif ({
5155                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5156                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5157                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5158                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5160                  !!!parse-error (type => 'unmatched end tag',
5161                                  text => $token->{tag_name}, token => $token);
5162                ## Ignore the token                ## Ignore the token
5163                !!!next-token;                !!!next-token;
5164                redo B;                next B;
5165              } else {              } else {
5166                  !!!cp ('t193');
5167                #                #
5168              }              }
5169            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5170              #          for my $entry (@{$self->{open_elements}}) {
5171              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5172                !!!cp ('t75');
5173                !!!parse-error (type => 'in body:#eof', token => $token);
5174                last;
5175            }            }
5176                      }
5177            $in_body->($insert_to_current);  
5178            redo B;          ## Stop parsing.
5179          } elsif ($self->{insertion_mode} eq 'in table') {          last B;
5180            if ($token->{type} eq 'character') {        } else {
5181              ## NOTE: There are "character in table" code clones.          die "$0: $token->{type}: Unknown token type";
5182              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        }
5183                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5184          $insert = $insert_to_current;
5185          #
5186        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5187          if ($token->{type} == CHARACTER_TOKEN) {
5188            if (not $open_tables->[-1]->[1] and # tainted
5189                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5190              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5191                                
5192                unless (length $token->{data}) {            unless (length $token->{data}) {
5193                  !!!next-token;              !!!cp ('t194');
5194                  redo B;              !!!next-token;
5195                }              next B;
5196              }            } else {
5197                !!!cp ('t195');
5198              }
5199            }
5200    
5201              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5202    
5203              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5204              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3990  sub _tree_construction_main ($) { Line 5206  sub _tree_construction_main ($) {
5206              ## result in a new Text node.              ## result in a new Text node.
5207              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5208                            
5209              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]}) {  
5210                # MUST                # MUST
5211                my $foster_parent_element;                my $foster_parent_element;
5212                my $next_sibling;                my $next_sibling;
5213                my $prev_sibling;                my $prev_sibling;
5214                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5215                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5216                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5217                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5218                        !!!cp ('t196');
5219                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5220                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5221                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5222                    } else {                    } else {
5223                        !!!cp ('t197');
5224                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5225                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5226                    }                    }
# Line 4017  sub _tree_construction_main ($) { Line 5232  sub _tree_construction_main ($) {
5232                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5233                if (defined $prev_sibling and                if (defined $prev_sibling and
5234                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5235                    !!!cp ('t198');
5236                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5237                } else {                } else {
5238                    !!!cp ('t199');
5239                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5240                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5241                     $next_sibling);                     $next_sibling);
5242                }                }
5243              } else {            $open_tables->[-1]->[1] = 1; # tainted
5244                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5245              !!!cp ('t200');
5246              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5247            }
5248                
5249            !!!next-token;
5250            next B;
5251          } elsif ($token->{type} == START_TAG_TOKEN) {
5252            if ({
5253                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5254                 th => 1, td => 1,
5255                }->{$token->{tag_name}}) {
5256              if ($self->{insertion_mode} == IN_TABLE_IM) {
5257                ## Clear back to table context
5258                while (not ($self->{open_elements}->[-1]->[1]
5259                                & TABLE_SCOPING_EL)) {
5260                  !!!cp ('t201');
5261                  pop @{$self->{open_elements}};
5262              }              }
5263                            
5264              !!!next-token;              !!!insert-element ('tbody',, $token);
5265              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5266            } elsif ($token->{type} eq 'start tag') {              ## reprocess in the "in table body" insertion mode...
5267              if ({            }
5268                   caption => 1,            
5269                   colgroup => 1,            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5270                   tbody => 1, tfoot => 1, thead => 1,              unless ($token->{tag_name} eq 'tr') {
5271                  }->{$token->{tag_name}}) {                !!!cp ('t202');
5272                ## Clear back to table context                !!!parse-error (type => 'missing start tag:tr', token => $token);
5273                while ($self->{open_elements}->[-1]->[1] ne 'table' and              }
5274                       $self->{open_elements}->[-1]->[1] ne 'html') {                  
5275                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## Clear back to table body context
5276                  pop @{$self->{open_elements}};              while (not ($self->{open_elements}->[-1]->[1]
5277                                & TABLE_ROWS_SCOPING_EL)) {
5278                  !!!cp ('t203');
5279                  ## ISSUE: Can this case be reached?
5280                  pop @{$self->{open_elements}};
5281                }
5282                    
5283                    $self->{insertion_mode} = IN_ROW_IM;
5284                    if ($token->{tag_name} eq 'tr') {
5285                      !!!cp ('t204');
5286                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5287                      !!!nack ('t204');
5288                      !!!next-token;
5289                      next B;
5290                    } else {
5291                      !!!cp ('t205');
5292                      !!!insert-element ('tr',, $token);
5293                      ## reprocess in the "in row" insertion mode
5294                    }
5295                  } else {
5296                    !!!cp ('t206');
5297                }                }
5298    
5299                push @$active_formatting_elements, ['#marker', '']                ## Clear back to table row context
5300                  if $token->{tag_name} eq 'caption';                while (not ($self->{open_elements}->[-1]->[1]
5301                                  & TABLE_ROW_SCOPING_EL)) {
5302                    !!!cp ('t207');
5303                    pop @{$self->{open_elements}};
5304                  }
5305                  
5306                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5307                  $self->{insertion_mode} = IN_CELL_IM;
5308    
5309                !!!insert-element ($token->{tag_name}, $token->{attributes});                push @$active_formatting_elements, ['#marker', ''];
5310                $self->{insertion_mode} = {                
5311                                   caption => 'in caption',                !!!nack ('t207.1');
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
5312                !!!next-token;                !!!next-token;
5313                redo B;                next B;
5314              } elsif ({              } elsif ({
5315                        col => 1,                        caption => 1, col => 1, colgroup => 1,
5316                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
5317                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5318                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5319                ## Clear back to table context                if ($self->{insertion_mode} == IN_ROW_IM) {
5320                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
5321                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
5322                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  my $i;
5323                  pop @{$self->{open_elements}};                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5324                      my $node = $self->{open_elements}->[$_];
5325                      if ($node->[1] & TABLE_ROW_EL) {
5326                        !!!cp ('t208');
5327                        $i = $_;
5328                        last INSCOPE;
5329                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5330                        !!!cp ('t209');
5331                        last INSCOPE;
5332                      }
5333                    } # INSCOPE
5334                    unless (defined $i) {
5335                      !!!cp ('t210');
5336    ## TODO: This type is wrong.
5337                      !!!parse-error (type => 'unmacthed end tag',
5338                                      text => $token->{tag_name}, token => $token);
5339                      ## Ignore the token
5340                      !!!nack ('t210.1');
5341                      !!!next-token;
5342                      next B;
5343                    }
5344                    
5345                    ## Clear back to table row context
5346                    while (not ($self->{open_elements}->[-1]->[1]
5347                                    & TABLE_ROW_SCOPING_EL)) {
5348                      !!!cp ('t211');
5349                      ## ISSUE: Can this case be reached?
5350                      pop @{$self->{open_elements}};
5351                    }
5352                    
5353                    pop @{$self->{open_elements}}; # tr
5354                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5355                    if ($token->{tag_name} eq 'tr') {
5356                      !!!cp ('t212');
5357                      ## reprocess
5358                      !!!ack-later;
5359                      next B;
5360                    } else {
5361                      !!!cp ('t213');
5362                      ## reprocess in the "in table body" insertion mode...
5363                    }
5364                }                }
5365    
5366                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5367                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## have an element in table scope
5368                  ? 'in column group' : 'in table body';                  my $i;
5369                ## reprocess                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5370                redo B;                    my $node = $self->{open_elements}->[$_];
5371              } elsif ($token->{tag_name} eq 'table') {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5372                ## NOTE: There are code clones for this "table in table"                      !!!cp ('t214');
5373                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      $i = $_;
5374                        last INSCOPE;
5375                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5376                        !!!cp ('t215');
5377                        last INSCOPE;
5378                      }
5379                    } # INSCOPE
5380                    unless (defined $i) {
5381                      !!!cp ('t216');
5382    ## TODO: This erorr type is wrong.
5383                      !!!parse-error (type => 'unmatched end tag',
5384                                      text => $token->{tag_name}, token => $token);
5385                      ## Ignore the token
5386                      !!!nack ('t216.1');
5387                      !!!next-token;
5388                      next B;
5389                    }
5390    
5391                ## As if </table>                  ## Clear back to table body context
5392                ## have a table element in table scope                  while (not ($self->{open_elements}->[-1]->[1]
5393                my $i;                                  & TABLE_ROWS_SCOPING_EL)) {
5394                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    !!!cp ('t217');
5395                  my $node = $self->{open_elements}->[$_];                    ## ISSUE: Can this state be reached?
5396                  if ($node->[1] eq 'table') {                    pop @{$self->{open_elements}};
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5397                  }                  }
5398                } # INSCOPE                  
5399                unless (defined $i) {                  ## As if <{current node}>
5400                  !!!parse-error (type => 'unmatched end tag:table');                  ## have an element in table scope
5401                  ## Ignore tokens </table><table>                  ## true by definition
5402                  !!!next-token;                  
5403                  redo B;                  ## Clear back to table body context
5404                }                  ## nop by definition
5405                                  
5406                ## generate implied end tags                  pop @{$self->{open_elements}};
5407                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5408                     dd => 1, dt => 1, li => 1, p => 1,                  ## reprocess in "in table" insertion mode...
5409                     td => 1, th => 1, tr => 1,                } else {
5410                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t218');
                   }->{$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;  
5411                }                }
5412    
5413                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($token->{tag_name} eq 'col') {
5414                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Clear back to table context
5415                    while (not ($self->{open_elements}->[-1]->[1]
5416                                    & TABLE_SCOPING_EL)) {
5417                      !!!cp ('t219');
5418                      ## ISSUE: Can this state be reached?
5419                      pop @{$self->{open_elements}};
5420                    }
5421                    
5422                    !!!insert-element ('colgroup',, $token);
5423                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5424                    ## reprocess
5425                    !!!ack-later;
5426                    next B;
5427                  } elsif ({
5428                            caption => 1,
5429                            colgroup => 1,
5430                            tbody => 1, tfoot => 1, thead => 1,
5431                           }->{$token->{tag_name}}) {
5432                    ## Clear back to table context
5433                    while (not ($self->{open_elements}->[-1]->[1]
5434                                    & TABLE_SCOPING_EL)) {
5435                      !!!cp ('t220');
5436                      ## ISSUE: Can this state be reached?
5437                      pop @{$self->{open_elements}};
5438                    }
5439                    
5440                    push @$active_formatting_elements, ['#marker', '']
5441                        if $token->{tag_name} eq 'caption';
5442                    
5443                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5444                    $self->{insertion_mode} = {
5445                                               caption => IN_CAPTION_IM,
5446                                               colgroup => IN_COLUMN_GROUP_IM,
5447                                               tbody => IN_TABLE_BODY_IM,
5448                                               tfoot => IN_TABLE_BODY_IM,
5449                                               thead => IN_TABLE_BODY_IM,
5450                                              }->{$token->{tag_name}};
5451                    !!!next-token;
5452                    !!!nack ('t220.1');
5453                    next B;
5454                  } else {
5455                    die "$0: in table: <>: $token->{tag_name}";
5456                }                }
5457                } elsif ($token->{tag_name} eq 'table') {
5458                  !!!parse-error (type => 'not closed',
5459                                  text => $self->{open_elements}->[-1]->[0]
5460                                      ->manakai_local_name,
5461                                  token => $token);
5462    
5463                splice @{$self->{open_elements}}, $i;                ## As if </table>
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
5464                ## have a table element in table scope                ## have a table element in table scope
5465                my $i;                my $i;
5466                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5467                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5468                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5469                      !!!cp ('t221');
5470                    $i = $_;                    $i = $_;
5471                    last INSCOPE;                    last INSCOPE;
5472                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5473                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5474                    last INSCOPE;                    last INSCOPE;
5475                  }                  }
5476                } # INSCOPE                } # INSCOPE
5477                unless (defined $i) {                unless (defined $i) {
5478                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t223');
5479                  ## Ignore the token  ## TODO: The following is wrong, maybe.
5480                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5481                                    token => $token);
5482                    ## Ignore tokens </table><table>
5483                    !!!nack ('t223.1');
5484                  !!!next-token;                  !!!next-token;
5485                  redo B;                  next B;
5486                }                }
5487                                
5488    ## TODO: Followings are removed from the latest spec.
5489                ## generate implied end tags                ## generate implied end tags
5490                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5491                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5492                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    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;  
5493                }                }
5494    
5495                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5496                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5497                    ## NOTE: |<table><tr><table>|
5498                    !!!parse-error (type => 'not closed',
5499                                    text => $self->{open_elements}->[-1]->[0]
5500                                        ->manakai_local_name,
5501                                    token => $token);
5502                  } else {
5503                    !!!cp ('t226');
5504                }                }
5505    
5506                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5507                  pop @{$open_tables};
5508    
5509                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5510    
5511                !!!next-token;            ## reprocess
5512                redo B;            !!!ack-later;
5513              } elsif ({            next B;
5514                        body => 1, caption => 1, col => 1, colgroup => 1,          } elsif ($token->{tag_name} eq 'style') {
5515                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,            if (not $open_tables->[-1]->[1]) { # tainted
5516                        thead => 1, tr => 1,              !!!cp ('t227.8');
5517                       }->{$token->{tag_name}}) {              ## NOTE: This is a "as if in head" code clone.
5518                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              $parse_rcdata->(CDATA_CONTENT_MODEL);
5519                ## Ignore the token              next B;
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
5520            } else {            } else {
5521                !!!cp ('t227.7');
5522              #              #
5523            }            }
5524            } elsif ($token->{tag_name} eq 'script') {
5525            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
5526            $in_body->($insert_to_foster);              !!!cp ('t227.6');
5527            redo B;              ## NOTE: This is a "as if in head" code clone.
5528          } elsif ($self->{insertion_mode} eq 'in column group') {              $script_start_tag->();
5529            if ($token->{type} eq 'character') {              next B;
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $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 {  
               #  
             }  
5530            } else {            } else {
5531                !!!cp ('t227.5');
5532              #              #
5533            }            }
5534            } elsif ($token->{tag_name} eq 'input') {
5535              if (not $open_tables->[-1]->[1]) { # tainted
5536                if ($token->{attributes}->{type}) { ## TODO: case
5537                  my $type = lc $token->{attributes}->{type}->{value};
5538                  if ($type eq 'hidden') {
5539                    !!!cp ('t227.3');
5540                    !!!parse-error (type => 'in table',
5541                                    text => $token->{tag_name}, token => $token);
5542    
5543            ## As if </colgroup>                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
           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;  
               }  
             }  
5544    
5545              !!!parse-error (type => 'in table:#character');                  ## TODO: form element pointer
5546    
5547              ## As if in body, but insert into foster parent element                  pop @{$self->{open_elements}};
             ## 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);  
5548    
5549              if ({                  !!!next-token;
5550                   table => 1, tbody => 1, tfoot => 1,                  !!!ack ('t227.2.1');
5551                   thead => 1, tr => 1,                  next B;
                 }->{$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});  
5552                } else {                } else {
5553                  $foster_parent_element->insert_before                  !!!cp ('t227.2');
5554                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
5555                }                }
5556              } else {              } else {
5557                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                !!!cp ('t227.1');
5558                  #
5559              }              }
5560                          } else {
5561              !!!next-token;              !!!cp ('t227.4');
5562              redo B;              #
5563            } elsif ($token->{type} eq 'start tag') {            }
5564              if ({          } else {
5565                   tr => 1,            !!!cp ('t227');
5566                   th => 1, td => 1,            #
5567                  }->{$token->{tag_name}}) {          }
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
5568    
5569                ## Clear back to table body context          !!!parse-error (type => 'in table', text => $token->{tag_name},
5570                while (not {                          token => $token);
5571                  tbody => 1, tfoot => 1, thead => 1, html => 1,  
5572                }->{$self->{open_elements}->[-1]->[1]}) {          $insert = $insert_to_foster;
5573                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          #
5574                  pop @{$self->{open_elements}};        } elsif ($token->{type} == END_TAG_TOKEN) {
5575                }              if ($token->{tag_name} eq 'tr' and
5576                                  $self->{insertion_mode} == IN_ROW_IM) {
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
5577                ## have an element in table scope                ## have an element in table scope
5578                my $i;                my $i;
5579                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5580                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5581                  if ({                  if ($node->[1] & TABLE_ROW_EL) {
5582                       tbody => 1, thead => 1, tfoot => 1,                    !!!cp ('t228');
                     }->{$node->[1]}) {  
5583                    $i = $_;                    $i = $_;
5584                    last INSCOPE;                    last INSCOPE;
5585                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5586                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5587                    last INSCOPE;                    last INSCOPE;
5588                  }                  }
5589                } # INSCOPE                } # INSCOPE
5590                unless (defined $i) {                unless (defined $i) {
5591                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5592                    !!!parse-error (type => 'unmatched end tag',
5593                                    text => $token->{tag_name}, token => $token);
5594                  ## Ignore the token                  ## Ignore the token
5595                    !!!nack ('t230.1');
5596                  !!!next-token;                  !!!next-token;
5597                  redo B;                  next B;
5598                  } else {
5599                    !!!cp ('t232');
5600                }                }
5601    
5602                ## Clear back to table body context                ## Clear back to table row context
5603                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5604                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5605                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5606                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5607                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5608                }                }
5609    
5610                ## As if <{current node}>                pop @{$self->{open_elements}}; # tr
5611                ## have an element in table scope                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5612                ## true by definition                !!!next-token;
5613                  !!!nack ('t231.1');
5614                ## Clear back to table body context                next B;
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
5615              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5616                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5617                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5618                    ## have an element in table scope
5619                ## As if </table>                  my $i;
5620                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5621                my $i;                    my $node = $self->{open_elements}->[$_];
5622                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5623                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5624                  if ($node->[1] eq 'table') {                      $i = $_;
5625                    $i = $_;                      last INSCOPE;
5626                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5627                  } elsif ({                      !!!cp ('t234');
5628                            table => 1, html => 1,                      last INSCOPE;
5629                           }->{$node->[1]}) {                    }
5630                    last INSCOPE;                  } # INSCOPE
5631                    unless (defined $i) {
5632                      !!!cp ('t235');
5633    ## TODO: The following is wrong.
5634                      !!!parse-error (type => 'unmatched end tag',
5635                                      text => $token->{type}, token => $token);
5636                      ## Ignore the token
5637                      !!!nack ('t236.1');
5638                      !!!next-token;
5639                      next B;
5640                  }                  }
5641                } # INSCOPE                  
5642                unless (defined $i) {                  ## Clear back to table row context
5643                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5644                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5645                  !!!next-token;                    !!!cp ('t236');
5646                  redo B;  ## ISSUE: Can this state be reached?
5647                }                    pop @{$self->{open_elements}};
5648                                  }
5649                ## generate implied end tags                  
5650                if ({                  pop @{$self->{open_elements}}; # tr
5651                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5652                     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;  
5653                }                }
5654    
5655                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5656                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## have an element in table scope
5657                    my $i;
5658                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5659                      my $node = $self->{open_elements}->[$_];
5660                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5661                        !!!cp ('t237');
5662                        $i = $_;
5663                        last INSCOPE;
5664                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5665                        !!!cp ('t238');
5666                        last INSCOPE;
5667                      }
5668                    } # INSCOPE
5669                    unless (defined $i) {
5670                      !!!cp ('t239');
5671                      !!!parse-error (type => 'unmatched end tag',
5672                                      text => $token->{tag_name}, token => $token);
5673                      ## Ignore the token
5674                      !!!nack ('t239.1');
5675                      !!!next-token;
5676                      next B;
5677                    }
5678                    
5679                    ## Clear back to table body context
5680                    while (not ($self->{open_elements}->[-1]->[1]
5681                                    & TABLE_ROWS_SCOPING_EL)) {
5682                      !!!cp ('t240');
5683                      pop @{$self->{open_elements}};
5684                    }
5685                    
5686                    ## As if <{current node}>
5687                    ## have an element in table scope
5688                    ## true by definition
5689                    
5690                    ## Clear back to table body context
5691                    ## nop by definition
5692                    
5693                    pop @{$self->{open_elements}};
5694                    $self->{insertion_mode} = IN_TABLE_IM;
5695                    ## reprocess in the "in table" insertion mode...
5696                }                }
5697    
5698                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
5699                  ## When you edit the code fragment below, please ensure that
5700                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
5701                  ## is synced with it.
5702    
5703                ## reprocess                ## have a table element in table scope
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
5704                my $i;                my $i;
5705                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5706                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5707                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5708                      !!!cp ('t241');
5709                    $i = $_;                    $i = $_;
5710                    last INSCOPE;                    last INSCOPE;
5711                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5712                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5713                    last INSCOPE;                    last INSCOPE;
5714                  }                  }
5715                } # INSCOPE                } # INSCOPE
5716                unless (defined $i) {                unless (defined $i) {
5717                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5718                    !!!parse-error (type => 'unmatched end tag',
5719                                    text => $token->{tag_name}, token => $token);
5720                  ## Ignore the token                  ## Ignore the token
5721                    !!!nack ('t243.1');
5722                  !!!next-token;                  !!!next-token;
5723                  redo B;                  next B;
5724                }                }
5725                    
5726                ## Clear back to table body context                splice @{$self->{open_elements}}, $i;
5727                while (not {                pop @{$open_tables};
5728                  tbody => 1, tfoot => 1, thead => 1, html => 1,                
5729                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5730                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5731                  pop @{$self->{open_elements}};                !!!next-token;
5732                  next B;
5733                } elsif ({
5734                          tbody => 1, tfoot => 1, thead => 1,
5735                         }->{$token->{tag_name}} and
5736                         $self->{insertion_mode} & ROW_IMS) {
5737                  if ($self->{insertion_mode} == IN_ROW_IM) {
5738                    ## have an element in table scope
5739                    my $i;
5740                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5741                      my $node = $self->{open_elements}->[$_];
5742                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5743                        !!!cp ('t247');
5744                        $i = $_;
5745                        last INSCOPE;
5746                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5747                        !!!cp ('t248');
5748                        last INSCOPE;
5749                      }
5750                    } # INSCOPE
5751                      unless (defined $i) {
5752                        !!!cp ('t249');
5753                        !!!parse-error (type => 'unmatched end tag',
5754                                        text => $token->{tag_name}, token => $token);
5755                        ## Ignore the token
5756                        !!!nack ('t249.1');
5757                        !!!next-token;
5758                        next B;
5759                      }
5760                    
5761                    ## As if </tr>
5762                    ## have an element in table scope
5763                    my $i;
5764                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5765                      my $node = $self->{open_elements}->[$_];
5766                      if ($node->[1] & TABLE_ROW_EL) {
5767                        !!!cp ('t250');
5768                        $i = $_;
5769                        last INSCOPE;
5770                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5771                        !!!cp ('t251');
5772                        last INSCOPE;
5773                      }
5774                    } # INSCOPE
5775                      unless (defined $i) {
5776                        !!!cp ('t252');
5777                        !!!parse-error (type => 'unmatched end tag',
5778                                        text => 'tr', token => $token);
5779                        ## Ignore the token
5780                        !!!nack ('t252.1');
5781                        !!!next-token;
5782                        next B;
5783                      }
5784                    
5785                    ## Clear back to table row context
5786                    while (not ($self->{open_elements}->[-1]->[1]
5787                                    & TABLE_ROW_SCOPING_EL)) {
5788                      !!!cp ('t253');
5789    ## ISSUE: Can this case be reached?
5790                      pop @{$self->{open_elements}};
5791                    }
5792                    
5793                    pop @{$self->{open_elements}}; # tr
5794                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5795                    ## reprocess in the "in table body" insertion mode...
5796                }                }
5797    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
5798                ## have an element in table scope                ## have an element in table scope
5799                my $i;                my $i;
5800                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5801                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5802                  if ({                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5803                       tbody => 1, thead => 1, tfoot => 1,                    !!!cp ('t254');
                     }->{$node->[1]}) {  
5804                    $i = $_;                    $i = $_;
5805                    last INSCOPE;                    last INSCOPE;
5806                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5807                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5808                    last INSCOPE;                    last INSCOPE;
5809                  }                  }
5810                } # INSCOPE                } # INSCOPE
5811                unless (defined $i) {                unless (defined $i) {
5812                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5813                    !!!parse-error (type => 'unmatched end tag',
5814                                    text => $token->{tag_name}, token => $token);
5815                  ## Ignore the token                  ## Ignore the token
5816                    !!!nack ('t256.1');
5817                  !!!next-token;                  !!!next-token;
5818                  redo B;                  next B;
5819                }                }
5820    
5821                ## Clear back to table body context                ## Clear back to table body context
5822                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5823                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5824                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5825                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5826                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5827                }                }
5828    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
5829                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5830                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5831                ## reprocess                !!!nack ('t257.1');
5832                redo B;                !!!next-token;
5833                  next B;
5834              } elsif ({              } elsif ({
5835                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5836                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1,
5837                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5838                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5839                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5840                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5841                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5842                !!!next-token;                            text => $token->{tag_name}, token => $token);
5843                redo B;            ## Ignore the token
5844              } else {            !!!nack ('t258.1');
5845                #             !!!next-token;
5846              }            next B;
5847            } else {          } else {
5848              #            !!!cp ('t259');
5849            }            !!!parse-error (type => 'in table:/',
5850                                        text => $token->{tag_name}, token => $token);
5851            ## As if in table  
5852            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
5853            $in_body->($insert_to_foster);            #
5854            redo B;          }
5855          } elsif ($self->{insertion_mode} eq 'in row') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5856            if ($token->{type} eq 'character') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5857              ## NOTE: This is a "character in table" code clone.                  @{$self->{open_elements}} == 1) { # redundant, maybe
5858              !!!parse-error (type => 'in body:#eof', token => $token);
5859              !!!cp ('t259.1');
5860              #
5861            } else {
5862              !!!cp ('t259.2');
5863              #
5864            }
5865    
5866            ## Stop parsing
5867            last B;
5868          } else {
5869            die "$0: $token->{type}: Unknown token type";
5870          }
5871        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5872              if ($token->{type} == CHARACTER_TOKEN) {
5873              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5874                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
5875                unless (length $token->{data}) {                unless (length $token->{data}) {
5876                    !!!cp ('t260');
5877                  !!!next-token;                  !!!next-token;
5878                  redo B;                  next B;
5879                }                }
5880              }              }
   
             !!!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);  
5881                            
5882              if ({              !!!cp ('t261');
5883                   table => 1, tbody => 1, tfoot => 1,              #
5884                   thead => 1, tr => 1,            } elsif ($token->{type} == START_TAG_TOKEN) {
5885                  }->{$self->{open_elements}->[-1]->[1]}) {              if ($token->{tag_name} eq 'col') {
5886                # MUST                !!!cp ('t262');
5887                my $foster_parent_element;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5888                my $next_sibling;                pop @{$self->{open_elements}};
5889                my $prev_sibling;                !!!ack ('t262.1');
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
5890                !!!next-token;                !!!next-token;
5891                redo B;                next B;
5892              } elsif ({              } else {
5893                        caption => 1, col => 1, colgroup => 1,                !!!cp ('t263');
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## 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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
5894                #                #
5895              }              }
5896            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5897              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
5898                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5899                my $i;                  !!!cp ('t264');
5900                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5901                  my $node = $self->{open_elements}->[$_];                                  text => 'colgroup', token => $token);
                 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;  
               }  
   
               ## 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}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## 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});  
5902                  ## Ignore the token                  ## Ignore the token
5903                  !!!next-token;                  !!!next-token;
5904                  redo B;                  next B;
5905                }                } else {
5906                    !!!cp ('t265');
5907                ## As if </tr>                  pop @{$self->{open_elements}}; # colgroup
5908                ## have an element in table scope                  $self->{insertion_mode} = IN_TABLE_IM;
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
5909                  !!!next-token;                  !!!next-token;
5910                  redo B;                  next B;            
5911                }                }
5912                } elsif ($token->{tag_name} eq 'col') {
5913                ## Clear back to table row context                !!!cp ('t266');
5914                while (not {                !!!parse-error (type => 'unmatched end tag',
5915                  tr => 1, html => 1,                                text => 'col', token => $token);
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## 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});  
5916                ## Ignore the token                ## Ignore the token
5917                !!!next-token;                !!!next-token;
5918                redo B;                next B;
5919              } else {              } else {
5920                #                !!!cp ('t267');
5921                  #
5922              }              }
5923            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5924              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5925            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5926              !!!cp ('t270.2');
5927              ## Stop parsing.
5928              last B;
5929            } else {
5930              ## NOTE: As if </colgroup>.
5931              !!!cp ('t270.1');
5932              pop @{$self->{open_elements}}; # colgroup
5933              $self->{insertion_mode} = IN_TABLE_IM;
5934              ## Reprocess.
5935              next B;
5936            }
5937          } else {
5938            die "$0: $token->{type}: Unknown token type";
5939          }
5940    
5941            ## As if in table            ## As if </colgroup>
5942            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5943            $in_body->($insert_to_foster);              !!!cp ('t269');
5944            redo B;  ## TODO: Wrong error type?
5945          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag',
5946            if ($token->{type} eq 'character') {                              text => 'colgroup', token => $token);
5947              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              ## Ignore the token
5948                !!!nack ('t269.1');
5949              !!!next-token;              !!!next-token;
5950              redo B;              next B;
5951            } elsif ($token->{type} eq 'start tag') {            } else {
5952              if ($token->{tag_name} eq 'option') {              !!!cp ('t270');
5953                if ($self->{open_elements}->[-1]->[1] eq 'option') {              pop @{$self->{open_elements}}; # colgroup
5954                  ## As if </option>              $self->{insertion_mode} = IN_TABLE_IM;
5955                  pop @{$self->{open_elements}};              !!!ack-later;
5956                }              ## reprocess
5957                next B;
5958              }
5959        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5960          if ($token->{type} == CHARACTER_TOKEN) {
5961            !!!cp ('t271');
5962            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5963            !!!next-token;
5964            next B;
5965          } elsif ($token->{type} == START_TAG_TOKEN) {
5966            if ($token->{tag_name} eq 'option') {
5967              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5968                !!!cp ('t272');
5969                ## As if </option>
5970                pop @{$self->{open_elements}};
5971              } else {
5972                !!!cp ('t273');
5973              }
5974    
5975                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5976                !!!next-token;            !!!nack ('t273.1');
5977                redo B;            !!!next-token;
5978              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5979                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5980                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5981                  pop @{$self->{open_elements}};              !!!cp ('t274');
5982                }              ## As if </option>
5983                pop @{$self->{open_elements}};
5984              } else {
5985                !!!cp ('t275');
5986              }
5987    
5988                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5989                  ## As if </optgroup>              !!!cp ('t276');
5990                  pop @{$self->{open_elements}};              ## As if </optgroup>
5991                }              pop @{$self->{open_elements}};
5992              } else {
5993                !!!cp ('t277');
5994              }
5995    
5996                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5997                !!!next-token;            !!!nack ('t277.1');
5998                redo B;            !!!next-token;
5999              } elsif ($token->{tag_name} eq 'select') {            next B;
6000                !!!parse-error (type => 'not closed:select');          } elsif ({
6001                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6002                ## have an element in table scope                   }->{$token->{tag_name}} or
6003                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6004                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6005                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6006                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6007                    $i = $_;                     tr => 1, td => 1, th => 1,
6008                    last INSCOPE;                    }->{$token->{tag_name}})) {
6009                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6010                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6011                           }->{$node->[1]}) {                            token => $token);
6012                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6013                  }            ## as if there were </select> (otherwise).
6014                } # INSCOPE            ## have an element in table scope
6015                unless (defined $i) {            my $i;
6016                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6017                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6018                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6019                  redo B;                !!!cp ('t278');
6020                }                $i = $_;
6021                  last INSCOPE;
6022                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6023                  !!!cp ('t279');
6024                  last INSCOPE;
6025                }
6026              } # INSCOPE
6027              unless (defined $i) {
6028                !!!cp ('t280');
6029                !!!parse-error (type => 'unmatched end tag',
6030                                text => 'select', token => $token);
6031                ## Ignore the token
6032                !!!nack ('t280.1');
6033                !!!next-token;
6034                next B;
6035              }
6036                                
6037                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6038              splice @{$self->{open_elements}}, $i;
6039    
6040                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6041    
6042                !!!next-token;            if ($token->{tag_name} eq 'select') {
6043                redo B;              !!!nack ('t281.2');
6044              } else {              !!!next-token;
6045                #              next B;
6046              } else {
6047                !!!cp ('t281.1');
6048                !!!ack-later;
6049                ## Reprocess the token.
6050                next B;
6051              }
6052            } else {
6053              !!!cp ('t282');
6054              !!!parse-error (type => 'in select',
6055                              text => $token->{tag_name}, token => $token);
6056              ## Ignore the token
6057              !!!nack ('t282.1');
6058              !!!next-token;
6059              next B;
6060            }
6061          } elsif ($token->{type} == END_TAG_TOKEN) {
6062            if ($token->{tag_name} eq 'optgroup') {
6063              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6064                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6065                !!!cp ('t283');
6066                ## As if </option>
6067                splice @{$self->{open_elements}}, -2;
6068              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6069                !!!cp ('t284');
6070                pop @{$self->{open_elements}};
6071              } else {
6072                !!!cp ('t285');
6073                !!!parse-error (type => 'unmatched end tag',
6074                                text => $token->{tag_name}, token => $token);
6075                ## Ignore the token
6076              }
6077              !!!nack ('t285.1');
6078              !!!next-token;
6079              next B;
6080            } elsif ($token->{tag_name} eq 'option') {
6081              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6082                !!!cp ('t286');
6083                pop @{$self->{open_elements}};
6084              } else {
6085                !!!cp ('t287');
6086                !!!parse-error (type => 'unmatched end tag',
6087                                text => $token->{tag_name}, token => $token);
6088                ## Ignore the token
6089              }
6090              !!!nack ('t287.1');
6091              !!!next-token;
6092              next B;
6093            } elsif ($token->{tag_name} eq 'select') {
6094              ## have an element in table scope
6095              my $i;
6096              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6097                my $node = $self->{open_elements}->[$_];
6098                if ($node->[1] & SELECT_EL) {
6099                  !!!cp ('t288');
6100                  $i = $_;
6101                  last INSCOPE;
6102                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6103                  !!!cp ('t289');
6104                  last INSCOPE;
6105              }              }
6106            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6107              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6108                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6109                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6110                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6111                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6112                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6113                  pop @{$self->{open_elements}};              !!!next-token;
6114                } else {              next B;
6115                  !!!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;  
               }  
6116                                
6117                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6118              splice @{$self->{open_elements}}, $i;
6119    
6120                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6121    
6122                !!!next-token;            !!!nack ('t291.1');
6123                redo B;            !!!next-token;
6124              } elsif ({            next B;
6125                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6126                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6127                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6128                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6129                                   }->{$token->{tag_name}}) {
6130                ## have an element in table scope  ## TODO: The following is wrong?
6131                my $i;            !!!parse-error (type => 'unmatched end tag',
6132                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                            text => $token->{tag_name}, token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
6133                                
6134                ## As if </select>            ## have an element in table scope
6135                ## have an element in table scope            my $i;
6136                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6137                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6138                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6139                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6140                    $i = $_;                $i = $_;
6141                    last INSCOPE;                last INSCOPE;
6142                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6143                            table => 1, html => 1,                !!!cp ('t293');
6144                           }->{$node->[1]}) {                last INSCOPE;
6145                    last INSCOPE;              }
6146                  }            } # INSCOPE
6147                } # INSCOPE            unless (defined $i) {
6148                unless (defined $i) {              !!!cp ('t294');
6149                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6150                  ## Ignore the </select> token              !!!nack ('t294.1');
6151                  !!!next-token; ## TODO: ok?              !!!next-token;
6152                  redo B;              next B;
6153                }            }
6154                                
6155                splice @{$self->{open_elements}}, $i;            ## As if </select>
6156              ## have an element in table scope
6157                $self->_reset_insertion_mode;            undef $i;
6158              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6159                ## reprocess              my $node = $self->{open_elements}->[$_];
6160                redo B;              if ($node->[1] & SELECT_EL) {
6161              } else {                !!!cp ('t295');
6162                #                $i = $_;
6163                  last INSCOPE;
6164                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6165    ## ISSUE: Can this state be reached?
6166                  !!!cp ('t296');
6167                  last INSCOPE;
6168              }              }
6169            } else {            } # INSCOPE
6170              #            unless (defined $i) {
6171                !!!cp ('t297');
6172    ## TODO: The following error type is correct?
6173                !!!parse-error (type => 'unmatched end tag',
6174                                text => 'select', token => $token);
6175                ## Ignore the </select> token
6176                !!!nack ('t297.1');
6177                !!!next-token; ## TODO: ok?
6178                next B;
6179            }            }
6180                  
6181              !!!cp ('t298');
6182              splice @{$self->{open_elements}}, $i;
6183    
6184              $self->_reset_insertion_mode;
6185    
6186            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6187              ## reprocess
6188              next B;
6189            } else {
6190              !!!cp ('t299');
6191              !!!parse-error (type => 'in select:/',
6192                              text => $token->{tag_name}, token => $token);
6193            ## Ignore the token            ## Ignore the token
6194              !!!nack ('t299.3');
6195            !!!next-token;            !!!next-token;
6196            redo B;            next B;
6197          } elsif ($self->{insertion_mode} eq 'after body') {          }
6198            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6199              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6200                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6201                ## As if in body            !!!cp ('t299.1');
6202                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6203            } else {
6204              !!!cp ('t299.2');
6205            }
6206    
6207            ## Stop parsing.
6208            last B;
6209          } else {
6210            die "$0: $token->{type}: Unknown token type";
6211          }
6212        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6213          if ($token->{type} == CHARACTER_TOKEN) {
6214            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6215              my $data = $1;
6216              ## As if in body
6217              $reconstruct_active_formatting_elements->($insert_to_current);
6218                                
6219                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6220              
6221              unless (length $token->{data}) {
6222                !!!cp ('t300');
6223                !!!next-token;
6224                next B;
6225              }
6226            }
6227            
6228            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6229              !!!cp ('t301');
6230              !!!parse-error (type => 'after html:#text', token => $token);
6231    
6232                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6233                  !!!next-token;          } else {
6234                  redo B;            !!!cp ('t302');
6235                }          }
6236              }          
6237                        ## "after body" insertion mode
6238              #          !!!parse-error (type => 'after body:#text', token => $token);
6239              !!!parse-error (type => 'after body:#character');  
6240            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6241              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6242              #          next B;
6243            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6244              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6246                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html',
6247                  ## Ignore the token                            text => $token->{tag_name}, token => $token);
6248                  !!!next-token;            
6249                  redo B;            ## Reprocess in the "after body" insertion mode.
6250                } else {          } else {
6251                  $previous_insertion_mode = $self->{insertion_mode};            !!!cp ('t304');
6252                  $self->{insertion_mode} = 'trailing end';          }
6253                  !!!next-token;  
6254                  redo B;          ## "after body" insertion mode
6255                }          !!!parse-error (type => 'after body',
6256              } else {                          text => $token->{tag_name}, token => $token);
6257                !!!parse-error (type => 'after body:/'.$token->{tag_name});  
6258              }          $self->{insertion_mode} = IN_BODY_IM;
6259            !!!ack-later;
6260            ## reprocess
6261            next B;
6262          } elsif ($token->{type} == END_TAG_TOKEN) {
6263            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6264              !!!cp ('t305');
6265              !!!parse-error (type => 'after html:/',
6266                              text => $token->{tag_name}, token => $token);
6267              
6268              $self->{insertion_mode} = AFTER_BODY_IM;
6269              ## Reprocess in the "after body" insertion mode.
6270            } else {
6271              !!!cp ('t306');
6272            }
6273    
6274            ## "after body" insertion mode
6275            if ($token->{tag_name} eq 'html') {
6276              if (defined $self->{inner_html_node}) {
6277                !!!cp ('t307');
6278                !!!parse-error (type => 'unmatched end tag',
6279                                text => 'html', token => $token);
6280                ## Ignore the token
6281                !!!next-token;
6282                next B;
6283            } else {            } else {
6284              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6285                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6286                !!!next-token;
6287                next B;
6288            }            }
6289            } else {
6290              !!!cp ('t309');
6291              !!!parse-error (type => 'after body:/',
6292                              text => $token->{tag_name}, token => $token);
6293    
6294            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6295            ## reprocess            ## reprocess
6296            redo B;            next B;
6297      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6298        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6299            !!!cp ('t309.2');
6300            ## Stop parsing
6301            last B;
6302          } else {
6303            die "$0: $token->{type}: Unknown token type";
6304          }
6305        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6306          if ($token->{type} == CHARACTER_TOKEN) {
6307          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6308            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6309              
6310            unless (length $token->{data}) {            unless (length $token->{data}) {
6311                !!!cp ('t310');
6312              !!!next-token;              !!!next-token;
6313              redo B;              next B;
6314            }            }
6315          }          }
6316            
6317          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6318          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6319          !!!next-token;              !!!cp ('t311');
6320          redo B;              !!!parse-error (type => 'in frameset:#text', token => $token);
6321        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6322          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6323            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#text', token => $token);
6324              } else { # "after after frameset"
6325                !!!cp ('t313');
6326                !!!parse-error (type => 'after html:#text', token => $token);
6327              }
6328              
6329              ## Ignore the token.
6330              if (length $token->{data}) {
6331                !!!cp ('t314');
6332                ## reprocess the rest of characters
6333              } else {
6334                !!!cp ('t315');
6335                !!!next-token;
6336              }
6337              next B;
6338            }
6339            
6340            die qq[$0: Character "$token->{data}"];
6341          } elsif ($token->{type} == START_TAG_TOKEN) {
6342            if ($token->{tag_name} eq 'frameset' and
6343                $self->{insertion_mode} == IN_FRAMESET_IM) {
6344              !!!cp ('t318');
6345              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6346              !!!nack ('t318.1');
6347            !!!next-token;            !!!next-token;
6348            redo B;            next B;
6349          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6350            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6351              !!!cp ('t319');
6352              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6353            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6354              !!!ack ('t319.1');
6355            !!!next-token;            !!!next-token;
6356            redo B;            next B;
6357          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6358            ## NOTE: As if in body.            !!!cp ('t320');
6359            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6360            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6361          } else {            next B;
6362            !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
6363              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6364              ## has no parse error.
6365            } else {
6366              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6367                !!!cp ('t321');
6368                !!!parse-error (type => 'in frameset',
6369                                text => $token->{tag_name}, token => $token);
6370              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6371                !!!cp ('t322');
6372                !!!parse-error (type => 'after frameset',
6373                                text => $token->{tag_name}, token => $token);
6374              } else { # "after after frameset"
6375                !!!cp ('t322.2');
6376                !!!parse-error (type => 'after after frameset',
6377                                text => $token->{tag_name}, token => $token);
6378              }
6379            ## Ignore the token            ## Ignore the token
6380              !!!nack ('t322.1');
6381            !!!next-token;            !!!next-token;
6382            redo B;            next B;
6383          }          }
6384        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6385          if ($token->{tag_name} eq 'frameset') {          if ($token->{tag_name} eq 'frameset' and
6386            if ($self->{open_elements}->[-1]->[1] eq 'html' and              $self->{insertion_mode} == IN_FRAMESET_IM) {
6387              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6388                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6389              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6390                !!!parse-error (type => 'unmatched end tag',
6391                                text => $token->{tag_name}, token => $token);
6392              ## Ignore the token              ## Ignore the token
6393              !!!next-token;              !!!next-token;
6394            } else {            } else {
6395                !!!cp ('t326');
6396              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6397              !!!next-token;              !!!next-token;
6398            }            }
6399    
6400            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6401                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6402              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6403                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6404              } else {
6405                !!!cp ('t328');
6406            }            }
6407            redo B;            next B;
6408            } elsif ($token->{tag_name} eq 'html' and
6409                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6410              !!!cp ('t329');
6411              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6412              !!!next-token;
6413              next B;
6414          } else {          } else {
6415            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6416                !!!cp ('t330');
6417                !!!parse-error (type => 'in frameset:/',
6418                                text => $token->{tag_name}, token => $token);
6419              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6420                !!!cp ('t330.1');
6421                !!!parse-error (type => 'after frameset:/',
6422                                text => $token->{tag_name}, token => $token);
6423              } else { # "after after html"
6424                !!!cp ('t331');
6425                !!!parse-error (type => 'after after frameset:/',
6426                                text => $token->{tag_name}, token => $token);
6427              }
6428            ## Ignore the token            ## Ignore the token
6429            !!!next-token;            !!!next-token;
6430            redo B;            next B;
6431            }
6432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6434                    @{$self->{open_elements}} == 1) { # redundant, maybe
6435              !!!cp ('t331.1');
6436              !!!parse-error (type => 'in body:#eof', token => $token);
6437            } else {
6438              !!!cp ('t331.2');
6439          }          }
6440            
6441            ## Stop parsing
6442            last B;
6443        } else {        } else {
6444          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6445        }        }
     } 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);  
6446    
6447                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6448                  !!!next-token;      } else {
6449                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6450                }      }
             }  
6451    
6452              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6453                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6454          if ($token->{tag_name} eq 'script') {
6455            !!!cp ('t332');
6456            ## NOTE: This is an "as if in head" code clone
6457            $script_start_tag->();
6458            next B;
6459          } elsif ($token->{tag_name} eq 'style') {
6460            !!!cp ('t333');
6461            ## NOTE: This is an "as if in head" code clone
6462            $parse_rcdata->(CDATA_CONTENT_MODEL);
6463            next B;
6464          } elsif ({
6465                    base => 1, link => 1,
6466                   }->{$token->{tag_name}}) {
6467            !!!cp ('t334');
6468            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6469            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6470            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6471            !!!ack ('t334.1');
6472            !!!next-token;
6473            next B;
6474          } elsif ($token->{tag_name} eq 'meta') {
6475            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6476            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6477            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6478    
6479                ## Ignore the token.          unless ($self->{confident}) {
6480                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6481                  ## reprocess the rest of characters              !!!cp ('t335');
6482                } else {              ## NOTE: Whether the encoding is supported or not is handled
6483                  !!!next-token;              ## in the {change_encoding} callback.
6484                }              $self->{change_encoding}
6485                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6486                
6487                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6488                    ->set_user_data (manakai_has_reference =>
6489                                         $token->{attributes}->{charset}
6490                                             ->{has_reference});
6491              } elsif ($token->{attributes}->{content}) {
6492                if ($token->{attributes}->{content}->{value}
6493                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6494                        [\x09-\x0D\x20]*=
6495                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6496                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6497                  !!!cp ('t336');
6498                  ## NOTE: Whether the encoding is supported or not is handled
6499                  ## in the {change_encoding} callback.
6500                  $self->{change_encoding}
6501                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6502                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6503                      ->set_user_data (manakai_has_reference =>
6504                                           $token->{attributes}->{content}
6505                                                 ->{has_reference});
6506              }              }
6507              }
6508            } else {
6509              if ($token->{attributes}->{charset}) {
6510                !!!cp ('t337');
6511                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6512                    ->set_user_data (manakai_has_reference =>
6513                                         $token->{attributes}->{charset}
6514                                             ->{has_reference});
6515              }
6516              if ($token->{attributes}->{content}) {
6517                !!!cp ('t338');
6518                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6519                    ->set_user_data (manakai_has_reference =>
6520                                         $token->{attributes}->{content}
6521                                             ->{has_reference});
6522              }
6523            }
6524    
6525          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6526        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6527          if ($token->{tag_name} eq 'noframes') {          next B;
6528            ## NOTE: As if in body.        } elsif ($token->{tag_name} eq 'title') {
6529            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          !!!cp ('t341');
6530            redo B;          ## NOTE: This is an "as if in head" code clone
6531            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6532            next B;
6533          } elsif ($token->{tag_name} eq 'body') {
6534            !!!parse-error (type => 'in body', text => 'body', token => $token);
6535                  
6536            if (@{$self->{open_elements}} == 1 or
6537                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6538              !!!cp ('t342');
6539              ## Ignore the token
6540          } else {          } else {
6541            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6542              for my $attr_name (keys %{$token->{attributes}}) {
6543                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6544                  !!!cp ('t343');
6545                  $body_el->set_attribute_ns
6546                    (undef, [undef, $attr_name],
6547                     $token->{attributes}->{$attr_name}->{value});
6548                }
6549              }
6550            }
6551            !!!nack ('t343.1');
6552            !!!next-token;
6553            next B;
6554          } elsif ({
6555                    address => 1, blockquote => 1, center => 1, dir => 1,
6556                    div => 1, dl => 1, fieldset => 1,
6557                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6558                    menu => 1, ol => 1, p => 1, ul => 1,
6559                    pre => 1, listing => 1,
6560                    form => 1,
6561                    table => 1,
6562                    hr => 1,
6563                   }->{$token->{tag_name}}) {
6564            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6565              !!!cp ('t350');
6566              !!!parse-error (type => 'in form:form', token => $token);
6567            ## Ignore the token            ## Ignore the token
6568              !!!nack ('t350.1');
6569            !!!next-token;            !!!next-token;
6570            redo B;            next B;
6571          }          }
6572        } elsif ($token->{type} eq 'end tag') {  
6573          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6574            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6575            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6576                !!!cp ('t344');
6577                !!!back-token; # <form>
6578                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6579                          line => $token->{line}, column => $token->{column}};
6580                next B;
6581              } elsif ($_->[1] & SCOPING_EL) {
6582                !!!cp ('t345');
6583                last INSCOPE;
6584              }
6585            } # INSCOPE
6586              
6587            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6588            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6589              !!!nack ('t346.1');
6590              !!!next-token;
6591              if ($token->{type} == CHARACTER_TOKEN) {
6592                $token->{data} =~ s/^\x0A//;
6593                unless (length $token->{data}) {
6594                  !!!cp ('t346');
6595                  !!!next-token;
6596                } else {
6597                  !!!cp ('t349');
6598                }
6599              } else {
6600                !!!cp ('t348');
6601              }
6602            } elsif ($token->{tag_name} eq 'form') {
6603              !!!cp ('t347.1');
6604              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6605    
6606              !!!nack ('t347.2');
6607              !!!next-token;
6608            } elsif ($token->{tag_name} eq 'table') {
6609              !!!cp ('t382');
6610              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6611              
6612              $self->{insertion_mode} = IN_TABLE_IM;
6613    
6614              !!!nack ('t382.1');
6615              !!!next-token;
6616            } elsif ($token->{tag_name} eq 'hr') {
6617              !!!cp ('t386');
6618              pop @{$self->{open_elements}};
6619            
6620              !!!nack ('t386.1');
6621            !!!next-token;            !!!next-token;
           redo B;  
6622          } else {          } else {
6623            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
           ## Ignore the token  
6624            !!!next-token;            !!!next-token;
           redo B;  
6625          }          }
6626        } else {          next B;
6627          die "$0: $token->{type}: Unknown token type";        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6628        }          ## has a p element in scope
6629            INSCOPE: for (reverse @{$self->{open_elements}}) {
6630              if ($_->[1] & P_EL) {
6631                !!!cp ('t353');
6632                !!!back-token; # <x>
6633                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6634                          line => $token->{line}, column => $token->{column}};
6635                next B;
6636              } elsif ($_->[1] & SCOPING_EL) {
6637                !!!cp ('t354');
6638                last INSCOPE;
6639              }
6640            } # INSCOPE
6641              
6642            ## Step 1
6643            my $i = -1;
6644            my $node = $self->{open_elements}->[$i];
6645            my $li_or_dtdd = {li => {li => 1},
6646                              dt => {dt => 1, dd => 1},
6647                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6648            LI: {
6649              ## Step 2
6650              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6651                if ($i != -1) {
6652                  !!!cp ('t355');
6653                  !!!parse-error (type => 'not closed',
6654                                  text => $self->{open_elements}->[-1]->[0]
6655                                      ->manakai_local_name,
6656                                  token => $token);
6657                } else {
6658                  !!!cp ('t356');
6659                }
6660                splice @{$self->{open_elements}}, $i;
6661                last LI;
6662              } else {
6663                !!!cp ('t357');
6664              }
6665              
6666              ## Step 3
6667              if (not ($node->[1] & FORMATTING_EL) and
6668                  #not $phrasing_category->{$node->[1]} and
6669                  ($node->[1] & SPECIAL_EL or
6670                   $node->[1] & SCOPING_EL) and
6671                  not ($node->[1] & ADDRESS_EL) and
6672                  not ($node->[1] & DIV_EL)) {
6673                !!!cp ('t358');
6674                last LI;
6675              }
6676              
6677              !!!cp ('t359');
6678              ## Step 4
6679              $i--;
6680              $node = $self->{open_elements}->[$i];
6681              redo LI;
6682            } # LI
6683              
6684            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6685            !!!nack ('t359.1');
6686            !!!next-token;
6687            next B;
6688          } elsif ($token->{tag_name} eq 'plaintext') {
6689            ## has a p element in scope
6690            INSCOPE: for (reverse @{$self->{open_elements}}) {
6691              if ($_->[1] & P_EL) {
6692                !!!cp ('t367');
6693                !!!back-token; # <plaintext>
6694                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6695                          line => $token->{line}, column => $token->{column}};
6696                next B;
6697              } elsif ($_->[1] & SCOPING_EL) {
6698                !!!cp ('t368');
6699                last INSCOPE;
6700              }
6701            } # INSCOPE
6702              
6703            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6704              
6705            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6706              
6707            !!!nack ('t368.1');
6708            !!!next-token;
6709            next B;
6710          } elsif ($token->{tag_name} eq 'a') {
6711            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6712              my $node = $active_formatting_elements->[$i];
6713              if ($node->[1] & A_EL) {
6714                !!!cp ('t371');
6715                !!!parse-error (type => 'in a:a', token => $token);
6716                
6717                !!!back-token; # <a>
6718                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6719                          line => $token->{line}, column => $token->{column}};
6720                $formatting_end_tag->($token);
6721                
6722                AFE2: for (reverse 0..$#$active_formatting_elements) {
6723                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6724                    !!!cp ('t372');
6725                    splice @$active_formatting_elements, $_, 1;
6726                    last AFE2;
6727                  }
6728                } # AFE2
6729                OE: for (reverse 0..$#{$self->{open_elements}}) {
6730                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6731                    !!!cp ('t373');
6732                    splice @{$self->{open_elements}}, $_, 1;
6733                    last OE;
6734                  }
6735                } # OE
6736                last AFE;
6737              } elsif ($node->[0] eq '#marker') {
6738                !!!cp ('t374');
6739                last AFE;
6740              }
6741            } # AFE
6742              
6743            $reconstruct_active_formatting_elements->($insert_to_current);
6744    
6745        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6746      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
6747        ## states in the main stage is preserved yet # MUST  
6748                  !!!nack ('t374.1');
6749        if ($token->{type} eq 'character') {          !!!next-token;
6750          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          next B;
6751            my $data = $1;        } elsif ($token->{tag_name} eq 'nobr') {
6752            ## As if in the main phase.          $reconstruct_active_formatting_elements->($insert_to_current);
6753            ## NOTE: The insertion mode in the main phase  
6754            ## just before the phase has been changed to the trailing          ## has a |nobr| element in scope
6755            ## end phase is either "after body" or "after frameset".          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6756            $reconstruct_active_formatting_elements->($insert_to_current);            my $node = $self->{open_elements}->[$_];
6757              if ($node->[1] & NOBR_EL) {
6758                !!!cp ('t376');
6759                !!!parse-error (type => 'in nobr:nobr', token => $token);
6760                !!!back-token; # <nobr>
6761                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6762                          line => $token->{line}, column => $token->{column}};
6763                next B;
6764              } elsif ($node->[1] & SCOPING_EL) {
6765                !!!cp ('t377');
6766                last INSCOPE;
6767              }
6768            } # INSCOPE
6769            
6770            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6771            push @$active_formatting_elements, $self->{open_elements}->[-1];
6772            
6773            !!!nack ('t377.1');
6774            !!!next-token;
6775            next B;
6776          } elsif ($token->{tag_name} eq 'button') {
6777            ## has a button element in scope
6778            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6779              my $node = $self->{open_elements}->[$_];
6780              if ($node->[1] & BUTTON_EL) {
6781                !!!cp ('t378');
6782                !!!parse-error (type => 'in button:button', token => $token);
6783                !!!back-token; # <button>
6784                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6785                          line => $token->{line}, column => $token->{column}};
6786                next B;
6787              } elsif ($node->[1] & SCOPING_EL) {
6788                !!!cp ('t379');
6789                last INSCOPE;
6790              }
6791            } # INSCOPE
6792                        
6793            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
6794                        
6795            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6796    
6797            ## TODO: associate with $self->{form_element} if defined
6798    
6799            push @$active_formatting_elements, ['#marker', ''];
6800    
6801            !!!nack ('t379.1');
6802            !!!next-token;
6803            next B;
6804          } elsif ({
6805                    xmp => 1,
6806                    iframe => 1,
6807                    noembed => 1,
6808                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6809                    noscript => 0, ## TODO: 1 if scripting is enabled
6810                   }->{$token->{tag_name}}) {
6811            if ($token->{tag_name} eq 'xmp') {
6812              !!!cp ('t381');
6813              $reconstruct_active_formatting_elements->($insert_to_current);
6814            } else {
6815              !!!cp ('t399');
6816            }
6817            ## NOTE: There is an "as if in body" code clone.
6818            $parse_rcdata->(CDATA_CONTENT_MODEL);
6819            next B;
6820          } elsif ($token->{tag_name} eq 'isindex') {
6821            !!!parse-error (type => 'isindex', token => $token);
6822            
6823            if (defined $self->{form_element}) {
6824              !!!cp ('t389');
6825              ## Ignore the token
6826              !!!nack ('t389'); ## NOTE: Not acknowledged.
6827              !!!next-token;
6828              next B;
6829            } else {
6830              !!!ack ('t391.1');
6831    
6832              my $at = $token->{attributes};
6833              my $form_attrs;
6834              $form_attrs->{action} = $at->{action} if $at->{action};
6835              my $prompt_attr = $at->{prompt};
6836              $at->{name} = {name => 'name', value => 'isindex'};
6837              delete $at->{action};
6838              delete $at->{prompt};
6839              my @tokens = (
6840                            {type => START_TAG_TOKEN, tag_name => 'form',
6841                             attributes => $form_attrs,
6842                             line => $token->{line}, column => $token->{column}},
6843                            {type => START_TAG_TOKEN, tag_name => 'hr',
6844                             line => $token->{line}, column => $token->{column}},
6845                            {type => START_TAG_TOKEN, tag_name => 'p',
6846                             line => $token->{line}, column => $token->{column}},
6847                            {type => START_TAG_TOKEN, tag_name => 'label',
6848                             line => $token->{line}, column => $token->{column}},
6849                           );
6850              if ($prompt_attr) {
6851                !!!cp ('t390');
6852                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6853                               #line => $token->{line}, column => $token->{column},
6854                              };
6855              } else {
6856                !!!cp ('t391');
6857                push @tokens, {type => CHARACTER_TOKEN,
6858                               data => 'This is a searchable index. Insert your search keywords here: ',
6859                               #line => $token->{line}, column => $token->{column},
6860                              }; # SHOULD
6861                ## TODO: make this configurable
6862              }
6863              push @tokens,
6864                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6865                             line => $token->{line}, column => $token->{column}},
6866                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6867                            {type => END_TAG_TOKEN, tag_name => 'label',
6868                             line => $token->{line}, column => $token->{column}},
6869                            {type => END_TAG_TOKEN, tag_name => 'p',
6870                             line => $token->{line}, column => $token->{column}},
6871                            {type => START_TAG_TOKEN, tag_name => 'hr',
6872                             line => $token->{line}, column => $token->{column}},
6873                            {type => END_TAG_TOKEN, tag_name => 'form',
6874                             line => $token->{line}, column => $token->{column}};
6875              !!!back-token (@tokens);
6876              !!!next-token;
6877              next B;
6878            }
6879          } elsif ($token->{tag_name} eq 'textarea') {
6880            my $tag_name = $token->{tag_name};
6881            my $el;
6882            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6883            
6884            ## TODO: $self->{form_element} if defined
6885            $self->{content_model} = RCDATA_CONTENT_MODEL;
6886            delete $self->{escape}; # MUST
6887            
6888            $insert->($el);
6889            
6890            my $text = '';
6891            !!!nack ('t392.1');
6892            !!!next-token;
6893            if ($token->{type} == CHARACTER_TOKEN) {
6894              $token->{data} =~ s/^\x0A//;
6895            unless (length $token->{data}) {            unless (length $token->{data}) {
6896                !!!cp ('t392');
6897              !!!next-token;              !!!next-token;
6898              redo B;            } else {
6899                !!!cp ('t393');
6900            }            }
6901            } else {
6902              !!!cp ('t394');
6903            }
6904            while ($token->{type} == CHARACTER_TOKEN) {
6905              !!!cp ('t395');
6906              $text .= $token->{data};
6907              !!!next-token;
6908            }
6909            if (length $text) {
6910              !!!cp ('t396');
6911              $el->manakai_append_text ($text);
6912            }
6913            
6914            $self->{content_model} = PCDATA_CONTENT_MODEL;
6915            
6916            if ($token->{type} == END_TAG_TOKEN and
6917                $token->{tag_name} eq $tag_name) {
6918              !!!cp ('t397');
6919              ## Ignore the token
6920            } else {
6921              !!!cp ('t398');
6922              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6923          }          }
6924            !!!next-token;
6925            next B;
6926          } elsif ($token->{tag_name} eq 'rt' or
6927                   $token->{tag_name} eq 'rp') {
6928            ## has a |ruby| element in scope
6929            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6930              my $node = $self->{open_elements}->[$_];
6931              if ($node->[1] & RUBY_EL) {
6932                !!!cp ('t398.1');
6933                ## generate implied end tags
6934                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6935                  !!!cp ('t398.2');
6936                  pop @{$self->{open_elements}};
6937                }
6938                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6939                  !!!cp ('t398.3');
6940                  !!!parse-error (type => 'not closed',
6941                                  text => $self->{open_elements}->[-1]->[0]
6942                                      ->manakai_local_name,
6943                                  token => $token);
6944                  pop @{$self->{open_elements}}
6945                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6946                }
6947                last INSCOPE;
6948              } elsif ($node->[1] & SCOPING_EL) {
6949                !!!cp ('t398.4');
6950                last INSCOPE;
6951              }
6952            } # INSCOPE
6953    
6954          !!!parse-error (type => 'after html:#character');          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6955          $self->{insertion_mode} = $previous_insertion_mode;  
6956          ## reprocess          !!!nack ('t398.5');
6957          redo B;          !!!next-token;
       } elsif ($token->{type} eq 'start tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:/'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
6958          redo B;          redo B;
6959          } elsif ($token->{tag_name} eq 'math' or
6960                   $token->{tag_name} eq 'svg') {
6961            $reconstruct_active_formatting_elements->($insert_to_current);
6962    
6963            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6964    
6965            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6966    
6967            ## "adjust foreign attributes" - done in insert-element-f
6968            
6969            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6970            
6971            if ($self->{self_closing}) {
6972              pop @{$self->{open_elements}};
6973              !!!ack ('t398.1');
6974            } else {
6975              !!!cp ('t398.2');
6976              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6977              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6978              ## mode, "in body" (not "in foreign content") secondary insertion
6979              ## mode, maybe.
6980            }
6981    
6982            !!!next-token;
6983            next B;
6984          } elsif ({
6985                    caption => 1, col => 1, colgroup => 1, frame => 1,
6986                    frameset => 1, head => 1, option => 1, optgroup => 1,
6987                    tbody => 1, td => 1, tfoot => 1, th => 1,
6988                    thead => 1, tr => 1,
6989                   }->{$token->{tag_name}}) {
6990            !!!cp ('t401');
6991            !!!parse-error (type => 'in body',
6992                            text => $token->{tag_name}, token => $token);
6993            ## Ignore the token
6994            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6995            !!!next-token;
6996            next B;
6997            
6998            ## ISSUE: An issue on HTML5 new elements in the spec.
6999        } else {        } else {
7000          die "$0: $token->{type}: Unknown token";          if ($token->{tag_name} eq 'image') {
7001              !!!cp ('t384');
7002              !!!parse-error (type => 'image', token => $token);
7003              $token->{tag_name} = 'img';
7004            } else {
7005              !!!cp ('t385');
7006            }
7007    
7008            ## NOTE: There is an "as if <br>" code clone.
7009            $reconstruct_active_formatting_elements->($insert_to_current);
7010            
7011            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7012    
7013            if ({
7014                 applet => 1, marquee => 1, object => 1,
7015                }->{$token->{tag_name}}) {
7016              !!!cp ('t380');
7017              push @$active_formatting_elements, ['#marker', ''];
7018              !!!nack ('t380.1');
7019            } elsif ({
7020                      b => 1, big => 1, em => 1, font => 1, i => 1,
7021                      s => 1, small => 1, strile => 1,
7022                      strong => 1, tt => 1, u => 1,
7023                     }->{$token->{tag_name}}) {
7024              !!!cp ('t375');
7025              push @$active_formatting_elements, $self->{open_elements}->[-1];
7026              !!!nack ('t375.1');
7027            } elsif ($token->{tag_name} eq 'input') {
7028              !!!cp ('t388');
7029              ## TODO: associate with $self->{form_element} if defined
7030              pop @{$self->{open_elements}};
7031              !!!ack ('t388.2');
7032            } elsif ({
7033                      area => 1, basefont => 1, bgsound => 1, br => 1,
7034                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7035                      #image => 1,
7036                     }->{$token->{tag_name}}) {
7037              !!!cp ('t388.1');
7038              pop @{$self->{open_elements}};
7039              !!!ack ('t388.3');
7040            } elsif ($token->{tag_name} eq 'select') {
7041              ## TODO: associate with $self->{form_element} if defined
7042            
7043              if ($self->{insertion_mode} & TABLE_IMS or
7044                  $self->{insertion_mode} & BODY_TABLE_IMS or
7045                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7046                !!!cp ('t400.1');
7047                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7048              } else {
7049                !!!cp ('t400.2');
7050                $self->{insertion_mode} = IN_SELECT_IM;
7051              }
7052              !!!nack ('t400.3');
7053            } else {
7054              !!!nack ('t402');
7055            }
7056            
7057            !!!next-token;
7058            next B;
7059          }
7060        } elsif ($token->{type} == END_TAG_TOKEN) {
7061          if ($token->{tag_name} eq 'body') {
7062            ## has a |body| element in scope
7063            my $i;
7064            INSCOPE: {
7065              for (reverse @{$self->{open_elements}}) {
7066                if ($_->[1] & BODY_EL) {
7067                  !!!cp ('t405');
7068                  $i = $_;
7069                  last INSCOPE;
7070                } elsif ($_->[1] & SCOPING_EL) {
7071                  !!!cp ('t405.1');
7072                  last;
7073                }
7074              }
7075    
7076              !!!parse-error (type => 'start tag not allowed',
7077                              text => $token->{tag_name}, token => $token);
7078              ## NOTE: Ignore the token.
7079              !!!next-token;
7080              next B;
7081            } # INSCOPE
7082    
7083            for (@{$self->{open_elements}}) {
7084              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7085                !!!cp ('t403');
7086                !!!parse-error (type => 'not closed',
7087                                text => $_->[0]->manakai_local_name,
7088                                token => $token);
7089                last;
7090              } else {
7091                !!!cp ('t404');
7092              }
7093            }
7094    
7095            $self->{insertion_mode} = AFTER_BODY_IM;
7096            !!!next-token;
7097            next B;
7098          } elsif ($token->{tag_name} eq 'html') {
7099            ## TODO: Update this code.  It seems that the code below is not
7100            ## up-to-date, though it has same effect as speced.
7101            if (@{$self->{open_elements}} > 1 and
7102                $self->{open_elements}->[1]->[1] & BODY_EL) {
7103              ## ISSUE: There is an issue in the spec.
7104              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7105                !!!cp ('t406');
7106                !!!parse-error (type => 'not closed',
7107                                text => $self->{open_elements}->[1]->[0]
7108                                    ->manakai_local_name,
7109                                token => $token);
7110              } else {
7111                !!!cp ('t407');
7112              }
7113              $self->{insertion_mode} = AFTER_BODY_IM;
7114              ## reprocess
7115              next B;
7116            } else {
7117              !!!cp ('t408');
7118              !!!parse-error (type => 'unmatched end tag',
7119                              text => $token->{tag_name}, token => $token);
7120              ## Ignore the token
7121              !!!next-token;
7122              next B;
7123            }
7124          } elsif ({
7125                    address => 1, blockquote => 1, center => 1, dir => 1,
7126                    div => 1, dl => 1, fieldset => 1, listing => 1,
7127                    menu => 1, ol => 1, pre => 1, ul => 1,
7128                    dd => 1, dt => 1, li => 1,
7129                    applet => 1, button => 1, marquee => 1, object => 1,
7130                   }->{$token->{tag_name}}) {
7131            ## has an element in scope
7132            my $i;
7133            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7134              my $node = $self->{open_elements}->[$_];
7135              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7136                !!!cp ('t410');
7137                $i = $_;
7138                last INSCOPE;
7139              } elsif ($node->[1] & SCOPING_EL) {
7140                !!!cp ('t411');
7141                last INSCOPE;
7142              }
7143            } # INSCOPE
7144    
7145            unless (defined $i) { # has an element in scope
7146              !!!cp ('t413');
7147              !!!parse-error (type => 'unmatched end tag',
7148                              text => $token->{tag_name}, token => $token);
7149              ## NOTE: Ignore the token.
7150            } else {
7151              ## Step 1. generate implied end tags
7152              while ({
7153                      ## END_TAG_OPTIONAL_EL
7154                      dd => ($token->{tag_name} ne 'dd'),
7155                      dt => ($token->{tag_name} ne 'dt'),
7156                      li => ($token->{tag_name} ne 'li'),
7157                      p => 1,
7158                      rt => 1,
7159                      rp => 1,
7160                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7161                !!!cp ('t409');
7162                pop @{$self->{open_elements}};
7163              }
7164    
7165              ## Step 2.
7166              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7167                      ne $token->{tag_name}) {
7168                !!!cp ('t412');
7169                !!!parse-error (type => 'not closed',
7170                                text => $self->{open_elements}->[-1]->[0]
7171                                    ->manakai_local_name,
7172                                token => $token);
7173              } else {
7174                !!!cp ('t414');
7175              }
7176    
7177              ## Step 3.
7178              splice @{$self->{open_elements}}, $i;
7179    
7180              ## Step 4.
7181              $clear_up_to_marker->()
7182                  if {
7183                    applet => 1, button => 1, marquee => 1, object => 1,
7184                  }->{$token->{tag_name}};
7185            }
7186            !!!next-token;
7187            next B;
7188          } elsif ($token->{tag_name} eq 'form') {
7189            undef $self->{form_element};
7190    
7191            ## has an element in scope
7192            my $i;
7193            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7194              my $node = $self->{open_elements}->[$_];
7195              if ($node->[1] & FORM_EL) {
7196                !!!cp ('t418');
7197                $i = $_;
7198                last INSCOPE;
7199              } elsif ($node->[1] & SCOPING_EL) {
7200                !!!cp ('t419');
7201                last INSCOPE;
7202              }
7203            } # INSCOPE
7204    
7205            unless (defined $i) { # has an element in scope
7206              !!!cp ('t421');
7207              !!!parse-error (type => 'unmatched end tag',
7208                              text => $token->{tag_name}, token => $token);
7209              ## NOTE: Ignore the token.
7210            } else {
7211              ## Step 1. generate implied end tags
7212              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7213                !!!cp ('t417');
7214                pop @{$self->{open_elements}};
7215              }
7216              
7217              ## Step 2.
7218              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7219                      ne $token->{tag_name}) {
7220                !!!cp ('t417.1');
7221                !!!parse-error (type => 'not closed',
7222                                text => $self->{open_elements}->[-1]->[0]
7223                                    ->manakai_local_name,
7224                                token => $token);
7225              } else {
7226                !!!cp ('t420');
7227              }  
7228              
7229              ## Step 3.
7230              splice @{$self->{open_elements}}, $i;
7231            }
7232    
7233            !!!next-token;
7234            next B;
7235          } elsif ({
7236                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7237                   }->{$token->{tag_name}}) {
7238            ## has an element in scope
7239            my $i;
7240            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7241              my $node = $self->{open_elements}->[$_];
7242              if ($node->[1] & HEADING_EL) {
7243                !!!cp ('t423');
7244                $i = $_;
7245                last INSCOPE;
7246              } elsif ($node->[1] & SCOPING_EL) {
7247                !!!cp ('t424');
7248                last INSCOPE;
7249              }
7250            } # INSCOPE
7251    
7252            unless (defined $i) { # has an element in scope
7253              !!!cp ('t425.1');
7254              !!!parse-error (type => 'unmatched end tag',
7255                              text => $token->{tag_name}, token => $token);
7256              ## NOTE: Ignore the token.
7257            } else {
7258              ## Step 1. generate implied end tags
7259              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7260                !!!cp ('t422');
7261                pop @{$self->{open_elements}};
7262              }
7263              
7264              ## Step 2.
7265              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7266                      ne $token->{tag_name}) {
7267                !!!cp ('t425');
7268                !!!parse-error (type => 'unmatched end tag',
7269                                text => $token->{tag_name}, token => $token);
7270              } else {
7271                !!!cp ('t426');
7272              }
7273    
7274              ## Step 3.
7275              splice @{$self->{open_elements}}, $i;
7276            }
7277            
7278            !!!next-token;
7279            next B;
7280          } elsif ($token->{tag_name} eq 'p') {
7281            ## has an element in scope
7282            my $i;
7283            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7284              my $node = $self->{open_elements}->[$_];
7285              if ($node->[1] & P_EL) {
7286                !!!cp ('t410.1');
7287                $i = $_;
7288                last INSCOPE;
7289              } elsif ($node->[1] & SCOPING_EL) {
7290                !!!cp ('t411.1');
7291                last INSCOPE;
7292              }
7293            } # INSCOPE
7294    
7295            if (defined $i) {
7296              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7297                      ne $token->{tag_name}) {
7298                !!!cp ('t412.1');
7299                !!!parse-error (type => 'not closed',
7300                                text => $self->{open_elements}->[-1]->[0]
7301                                    ->manakai_local_name,
7302                                token => $token);
7303              } else {
7304                !!!cp ('t414.1');
7305              }
7306    
7307              splice @{$self->{open_elements}}, $i;
7308            } else {
7309              !!!cp ('t413.1');
7310              !!!parse-error (type => 'unmatched end tag',
7311                              text => $token->{tag_name}, token => $token);
7312    
7313              !!!cp ('t415.1');
7314              ## As if <p>, then reprocess the current token
7315              my $el;
7316              !!!create-element ($el, $HTML_NS, 'p',, $token);
7317              $insert->($el);
7318              ## NOTE: Not inserted into |$self->{open_elements}|.
7319            }
7320    
7321            !!!next-token;
7322            next B;
7323          } elsif ({
7324                    a => 1,
7325                    b => 1, big => 1, em => 1, font => 1, i => 1,
7326                    nobr => 1, s => 1, small => 1, strile => 1,
7327                    strong => 1, tt => 1, u => 1,
7328                   }->{$token->{tag_name}}) {
7329            !!!cp ('t427');
7330            $formatting_end_tag->($token);
7331            next B;
7332          } elsif ($token->{tag_name} eq 'br') {
7333            !!!cp ('t428');
7334            !!!parse-error (type => 'unmatched end tag',
7335                            text => 'br', token => $token);
7336    
7337            ## As if <br>
7338            $reconstruct_active_formatting_elements->($insert_to_current);
7339            
7340            my $el;
7341            !!!create-element ($el, $HTML_NS, 'br',, $token);
7342            $insert->($el);
7343            
7344            ## Ignore the token.
7345            !!!next-token;
7346            next B;
7347          } elsif ({
7348                    caption => 1, col => 1, colgroup => 1, frame => 1,
7349                    frameset => 1, head => 1, option => 1, optgroup => 1,
7350                    tbody => 1, td => 1, tfoot => 1, th => 1,
7351                    thead => 1, tr => 1,
7352                    area => 1, basefont => 1, bgsound => 1,
7353                    embed => 1, hr => 1, iframe => 1, image => 1,
7354                    img => 1, input => 1, isindex => 1, noembed => 1,
7355                    noframes => 1, param => 1, select => 1, spacer => 1,
7356                    table => 1, textarea => 1, wbr => 1,
7357                    noscript => 0, ## TODO: if scripting is enabled
7358                   }->{$token->{tag_name}}) {
7359            !!!cp ('t429');
7360            !!!parse-error (type => 'unmatched end tag',
7361                            text => $token->{tag_name}, token => $token);
7362            ## Ignore the token
7363            !!!next-token;
7364            next B;
7365            
7366            ## ISSUE: Issue on HTML5 new elements in spec
7367            
7368          } else {
7369            ## Step 1
7370            my $node_i = -1;
7371            my $node = $self->{open_elements}->[$node_i];
7372    
7373            ## Step 2
7374            S2: {
7375              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7376                ## Step 1
7377                ## generate implied end tags
7378                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7379                  !!!cp ('t430');
7380                  ## NOTE: |<ruby><rt></ruby>|.
7381                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7382                  ## which seems wrong.
7383                  pop @{$self->{open_elements}};
7384                  $node_i++;
7385                }
7386            
7387                ## Step 2
7388                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7389                        ne $token->{tag_name}) {
7390                  !!!cp ('t431');
7391                  ## NOTE: <x><y></x>
7392                  !!!parse-error (type => 'not closed',
7393                                  text => $self->{open_elements}->[-1]->[0]
7394                                      ->manakai_local_name,
7395                                  token => $token);
7396                } else {
7397                  !!!cp ('t432');
7398                }
7399                
7400                ## Step 3
7401                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7402    
7403                !!!next-token;
7404                last S2;
7405              } else {
7406                ## Step 3
7407                if (not ($node->[1] & FORMATTING_EL) and
7408                    #not $phrasing_category->{$node->[1]} and
7409                    ($node->[1] & SPECIAL_EL or
7410                     $node->[1] & SCOPING_EL)) {
7411                  !!!cp ('t433');
7412                  !!!parse-error (type => 'unmatched end tag',
7413                                  text => $token->{tag_name}, token => $token);
7414                  ## Ignore the token
7415                  !!!next-token;
7416                  last S2;
7417                }
7418    
7419                !!!cp ('t434');
7420              }
7421              
7422              ## Step 4
7423              $node_i--;
7424              $node = $self->{open_elements}->[$node_i];
7425              
7426              ## Step 5;
7427              redo S2;
7428            } # S2
7429            next B;
7430        }        }
7431      } else {      }
7432        die "$0: $self->{insertion_mode}: Unknown insertion mode";      next B;
7433      } continue { # B
7434        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7435          ## NOTE: The code below is executed in cases where it does not have
7436          ## to be, but it it is harmless even in those cases.
7437          ## has an element in scope
7438          INSCOPE: {
7439            for (reverse 0..$#{$self->{open_elements}}) {
7440              my $node = $self->{open_elements}->[$_];
7441              if ($node->[1] & FOREIGN_EL) {
7442                last INSCOPE;
7443              } elsif ($node->[1] & SCOPING_EL) {
7444                last;
7445              }
7446            }
7447            
7448            ## NOTE: No foreign element in scope.
7449            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7450          } # INSCOPE
7451      }      }
7452    } # B    } # B
7453    
# Line 5214  sub set_inner_html ($$$) { Line 7462  sub set_inner_html ($$$) {
7462    my $s = \$_[0];    my $s = \$_[0];
7463    my $onerror = $_[1];    my $onerror = $_[1];
7464    
7465      ## ISSUE: Should {confident} be true?
7466    
7467    my $nt = $node->node_type;    my $nt = $node->node_type;
7468    if ($nt == 9) {    if ($nt == 9) {
7469      # MUST      # MUST
# Line 5242  sub set_inner_html ($$$) { Line 7492  sub set_inner_html ($$$) {
7492      my $p = $class->new;      my $p = $class->new;
7493      $p->{document} = $doc;      $p->{document} = $doc;
7494    
7495      ## Step 9 # MUST      ## Step 8 # MUST
7496      my $i = 0;      my $i = 0;
7497      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7498      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7499      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7500        my $self = shift;        my $self = shift;
7501    
7502        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7503        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7504    
7505        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7506        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7507        $column++;  
7508          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7509        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
7510          $line++;  
7511          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
7512        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
7513            $p->{column} = 0;
7514            !!!cp ('i1');
7515          } elsif ($self->{next_char} == 0x000D) { # CR
7516          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7517          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7518          $line++;          $p->{line}++;
7519          $column = 0;          $p->{column} = 0;
7520        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7521          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7522        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7523            !!!cp ('i3');
7524          } elsif ($self->{next_char} == 0x0000) { # NULL
7525            !!!cp ('i4');
7526          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7527          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7528          } elsif ($self->{next_char} <= 0x0008 or
7529                   (0x000E <= $self->{next_char} and
7530                    $self->{next_char} <= 0x001F) or
7531                   (0x007F <= $self->{next_char} and
7532                    $self->{next_char} <= 0x009F) or
7533                   (0xD800 <= $self->{next_char} and
7534                    $self->{next_char} <= 0xDFFF) or
7535                   (0xFDD0 <= $self->{next_char} and
7536                    $self->{next_char} <= 0xFDDF) or
7537                   {
7538                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7539                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7540                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7541                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7542                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7543                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7544                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7545                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7546                    0x10FFFE => 1, 0x10FFFF => 1,
7547                   }->{$self->{next_char}}) {
7548            !!!cp ('i4.1');
7549            if ($self->{next_char} < 0x10000) {
7550              !!!parse-error (type => 'control char',
7551                              text => (sprintf 'U+%04X', $self->{next_char}));
7552            } else {
7553              !!!parse-error (type => 'control char',
7554                              text => (sprintf 'U-%08X', $self->{next_char}));
7555            }
7556        }        }
7557      };      };
7558      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7559      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7560            
7561      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7562        my (%opt) = @_;        my (%opt) = @_;
7563        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7564          my $column = $opt{column};
7565          if (defined $opt{token} and defined $opt{token}->{line}) {
7566            $line = $opt{token}->{line};
7567            $column = $opt{token}->{column};
7568          }
7569          warn "Parse error ($opt{type}) at line $line column $column\n";
7570      };      };
7571      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7572        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7573      };      };
7574            
7575      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7576      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7577    
7578      ## Step 2      ## Step 2
7579      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7580      $p->{content_model} = {      $p->{content_model} = {
7581        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7582        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7593  sub set_inner_html ($$$) {
7593          unless defined $p->{content_model};          unless defined $p->{content_model};
7594          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7595    
7596      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7597          ## TODO: Foreign element OK?
7598    
7599      ## Step 4      ## Step 3
7600      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7601        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7602    
7603      ## Step 5 # MUST      ## Step 4 # MUST
7604      $doc->append_child ($root);      $doc->append_child ($root);
7605    
7606      ## Step 6 # MUST      ## Step 5 # MUST
7607      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7608    
7609      undef $p->{head_element};      undef $p->{head_element};
7610    
7611      ## Step 7 # MUST      ## Step 6 # MUST
7612      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7613    
7614      ## Step 8 # MUST      ## Step 7 # MUST
7615      my $anode = $node;      my $anode = $node;
7616      AN: while (defined $anode) {      AN: while (defined $anode) {
7617        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7618          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7619          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7620            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7621                !!!cp ('i5');
7622              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7623              last AN;              last AN;
7624            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7627  sub set_inner_html ($$$) {
7627        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7628      } # AN      } # AN
7629            
7630      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7631      {      {
7632        my $self = $p;        my $self = $p;
7633        !!!next-token;        !!!next-token;
7634      }      }
7635      $p->_tree_construction_main;      $p->_tree_construction_main;
7636    
7637      ## Step 11 # MUST      ## Step 10 # MUST
7638      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7639      for (@cn) {      for (@cn) {
7640        $node->remove_child ($_);        $node->remove_child ($_);
7641      }      }
7642      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7643    
7644      ## Step 12 # MUST      ## Step 11 # MUST
7645      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7646      for (@cn) {      for (@cn) {
7647        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 7650  sub set_inner_html ($$$) {
7650      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7651    
7652      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7653    
7654        delete $p->{parse_error}; # delete loop
7655    } else {    } else {
7656      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";
7657    }    }
# Line 5366  sub set_inner_html ($$$) { Line 7659  sub set_inner_html ($$$) {
7659    
7660  } # tree construction stage  } # tree construction stage
7661    
7662  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7663    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  
7664    
7665  1;  1;
7666  # $Date$  # $Date$

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.160

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24