/[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.162 by wakaba, Thu Sep 11 09:12:27 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, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
361  };    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  
362    
363  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
364    my $self = shift->new;      my (%opt) = @_;
365    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
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 =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
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        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
670        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
671        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
672        $line++;        my $next = $input->getc;
673        $column = 0;        if (defined $next and $next ne "\x0A") {
674      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
675        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
676      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
677          $self->{line}++;
678          $self->{column} = 0;
679        } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682        } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
713    $self->{next_input_character} = -1;    $self->{next_char} = -1;
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738    $self->{set_next_input_character} = sub {      level => {must => 'm',
739      $self->{next_input_character} = -1;                should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744      $self->{set_next_char} = sub {
745        $self->{next_char} = -1;
746    };    };
747    $self->{parse_error} = sub {    $self->{parse_error} = sub {
748      #      #
749    };    };
750      $self->{change_encoding} = sub {
751        # if ($_[0] is a supported encoding) {
752        #   run "change the encoding" algorithm;
753        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754        # }
755      };
756      $self->{application_cache_selection} = sub {
757        #
758      };
759    return $self;    return $self;
760  } # new  } # new
761    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 768  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
768  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
769  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
770    
771    sub DATA_STATE () { 0 }
772    sub ENTITY_DATA_STATE () { 1 }
773    sub TAG_OPEN_STATE () { 2 }
774    sub CLOSE_TAG_OPEN_STATE () { 3 }
775    sub TAG_NAME_STATE () { 4 }
776    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
777    sub ATTRIBUTE_NAME_STATE () { 6 }
778    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
779    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
780    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
781    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
782    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
783    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
784    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
785    sub COMMENT_START_STATE () { 14 }
786    sub COMMENT_START_DASH_STATE () { 15 }
787    sub COMMENT_STATE () { 16 }
788    sub COMMENT_END_STATE () { 17 }
789    sub COMMENT_END_DASH_STATE () { 18 }
790    sub BOGUS_COMMENT_STATE () { 19 }
791    sub DOCTYPE_STATE () { 20 }
792    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
793    sub DOCTYPE_NAME_STATE () { 22 }
794    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
795    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
796    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
797    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
798    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
799    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
800    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
801    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803    sub BOGUS_DOCTYPE_STATE () { 32 }
804    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808    sub DOCTYPE_TOKEN () { 1 }
809    sub COMMENT_TOKEN () { 2 }
810    sub START_TAG_TOKEN () { 3 }
811    sub END_TAG_TOKEN () { 4 }
812    sub END_OF_FILE_TOKEN () { 5 }
813    sub CHARACTER_TOKEN () { 6 }
814    
815    sub AFTER_HTML_IMS () { 0b100 }
816    sub HEAD_IMS ()       { 0b1000 }
817    sub BODY_IMS ()       { 0b10000 }
818    sub BODY_TABLE_IMS () { 0b100000 }
819    sub TABLE_IMS ()      { 0b1000000 }
820    sub ROW_IMS ()        { 0b10000000 }
821    sub BODY_AFTER_IMS () { 0b100000000 }
822    sub FRAME_IMS ()      { 0b1000000000 }
823    sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829    ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
831    ## NOTE: "after after body" insertion mode.
832    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
833    
834    ## NOTE: "after after frameset" insertion mode.
835    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
836    
837    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
838    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
839    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
840    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
841    sub IN_BODY_IM () { BODY_IMS }
842    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
843    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
844    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
845    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
846    sub IN_TABLE_IM () { TABLE_IMS }
847    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
848    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
849    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
850    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
851    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
852    sub IN_COLUMN_GROUP_IM () { 0b10 }
853    
854  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
855    
856  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
857    my $self = shift;    my $self = shift;
858    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
859    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
860    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_input_character}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
868    $self->{token} = [];    $self->{token} = [];
869    # $self->{escape}    # $self->{escape}
870  } # _initialize_tokenizer  } # _initialize_tokenizer
871    
872  ## A token has:  ## A token has:
873  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
874  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
875  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
876  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
877  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
878  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
879  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
880  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
881    ##        ->{name}
882    ##        ->{value}
883    ##        ->{has_reference} == 1 or 0
884    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 194  sub _initialize_tokenizer ($) { Line 894  sub _initialize_tokenizer ($) {
894  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
895  ## and removed from the list.  ## and removed from the list.
896    
897    ## NOTE: HTML5 "Writing HTML documents" section, applied to
898    ## documents and not to user agents and conformance checkers,
899    ## contains some requirements that are not detected by the
900    ## parsing algorithm:
901    ## - Some requirements on character encoding declarations. ## TODO
902    ## - "Elements MUST NOT contain content that their content model disallows."
903    ##   ... Some are parse error, some are not (will be reported by c.c.).
904    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
905    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
906    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
907    
908    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
909    ## be detected by the HTML5 parsing algorithm:
910    ## - Text,
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
928    A: {    A: {
929      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
930        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
931          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
932            $self->{state} = 'entity data';              not $self->{escape}) {
933              !!!cp (1);
934              $self->{state} = ENTITY_DATA_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (2);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
942          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
943            unless ($self->{escape}) {            unless ($self->{escape}) {
944              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
945                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
946                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
947                  !!!cp (3);
948                $self->{escape} = 1;                $self->{escape} = 1;
949                } else {
950                  !!!cp (4);
951              }              }
952              } else {
953                !!!cp (5);
954            }            }
955          }          }
956                    
957          #          #
958        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
959          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
960              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
961               not $self->{escape})) {               not $self->{escape})) {
962            $self->{state} = 'tag open';            !!!cp (6);
963              $self->{state} = TAG_OPEN_STATE;
964            !!!next-input-character;            !!!next-input-character;
965            redo A;            redo A;
966          } else {          } else {
967              !!!cp (7);
968            #            #
969          }          }
970        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
971          if ($self->{escape} and          if ($self->{escape} and
972              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
973            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
974                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
975                !!!cp (8);
976              delete $self->{escape};              delete $self->{escape};
977              } else {
978                !!!cp (9);
979            }            }
980            } else {
981              !!!cp (10);
982          }          }
983                    
984          #          #
985        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
986          !!!emit ({type => 'end-of-file'});          !!!cp (11);
987            !!!emit ({type => END_OF_FILE_TOKEN,
988                      line => $self->{line}, column => $self->{column}});
989          last A; ## TODO: ok?          last A; ## TODO: ok?
990          } else {
991            !!!cp (12);
992        }        }
993        # Anything else        # Anything else
994        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
996                       line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
1001        !!!emit ($token);        !!!emit ($token);
1002    
1003        redo A;        redo A;
1004      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1005        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1006    
1007          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1008                
1009        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1010    
1011        $self->{state} = 'data';        $self->{state} = DATA_STATE;
1012        # next-input-character is already done        # next-input-character is already done
1013    
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1016            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                      line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020            !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
1022        }        }
1023    
1024        redo A;        redo A;
1025      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1026        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1027          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1028              !!!cp (15);
1029            !!!next-input-character;            !!!next-input-character;
1030            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1031            redo A;            redo A;
1032          } else {          } else {
1033              !!!cp (16);
1034            ## reconsume            ## reconsume
1035            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1036    
1037            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                        line => $self->{line_prev},
1039                        column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
1044        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1045          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1046            $self->{state} = 'markup declaration open';            !!!cp (17);
1047              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1051            $self->{state} = 'close tag open';            !!!cp (18);
1052              $self->{state} = CLOSE_TAG_OPEN_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054            redo A;            redo A;
1055          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1056                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1057              !!!cp (19);
1058            $self->{current_token}            $self->{current_token}
1059              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1060                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1061            $self->{state} = 'tag name';                 line => $self->{line_prev},
1062                   column => $self->{column_prev}};
1063              $self->{state} = TAG_NAME_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
1066          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1067                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1068            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1069                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1070            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1071                                        line => $self->{line_prev},
1072                                        column => $self->{column_prev}};
1073              $self->{state} = TAG_NAME_STATE;
1074            !!!next-input-character;            !!!next-input-character;
1075            redo A;            redo A;
1076          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1077            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1078            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1079                              line => $self->{line_prev},
1080                              column => $self->{column_prev});
1081              $self->{state} = DATA_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083    
1084            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                        line => $self->{line_prev},
1086                        column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1091            !!!parse-error (type => 'pio');            !!!cp (22);
1092            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1093            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1094                              column => $self->{column_prev});
1095              $self->{state} = BOGUS_COMMENT_STATE;
1096              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                        line => $self->{line_prev},
1098                                        column => $self->{column_prev},
1099                                       };
1100              ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!parse-error (type => 'bare stago');            !!!cp (23);
1104            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107              $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                        line => $self->{line_prev},
1112                        column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
1117        } else {        } else {
1118          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1119        }        }
1120      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1121          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1122        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1123          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1124    
1125            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1126            my @next_char;            my @next_char;
1127            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++) {
1128              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1129              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1130              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1131              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1132                  !!!cp (24);
1133                !!!next-input-character;                !!!next-input-character;
1134                next TAGNAME;                next TAGNAME;
1135              } else {              } else {
1136                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1137                  $self->{next_char} = shift @next_char; # reconsume
1138                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1139                $self->{state} = 'data';                $self->{state} = DATA_STATE;
1140    
1141                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                            line => $l, column => $c,
1143                           });
1144        
1145                redo A;                redo A;
1146              }              }
1147            }            }
1148            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1149                
1150            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1151                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1152                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1153                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1154                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1155                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1156                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1157                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1158              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1159                $self->{next_char} = shift @next_char; # reconsume
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = 'data';              $self->{state} = DATA_STATE;
1162              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163                          line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166            } else {            } else {
1167              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1168                $self->{next_char} = shift @next_char;
1169              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1170              # and consume...              # and consume...
1171            }            }
1172          } else {          } else {
1173            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1174              !!!cp (28);
1175            # next-input-character is already done            # next-input-character is already done
1176            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1177            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                        line => $l, column => $c,
1179                       });
1180            redo A;            redo A;
1181          }          }
1182        }        }
1183                
1184        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1185            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1186          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1187                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1188          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1189          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1190          redo A;                 line => $l, column => $c};
1191        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1192                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1193          $self->{current_token} = {type => 'end tag',          redo A;
1194                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1195          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1196          !!!next-input-character;          !!!cp (30);
1197          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1198        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1199          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1200          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1201            !!!next-input-character;
1202            redo A;
1203          } elsif ($self->{next_char} == 0x003E) { # >
1204            !!!cp (31);
1205            !!!parse-error (type => 'empty end tag',
1206                            line => $self->{line_prev}, ## "<" in "</>"
1207                            column => $self->{column_prev} - 1);
1208            $self->{state} = DATA_STATE;
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
1211        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1212            !!!cp (32);
1213          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1214          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                      line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
1223            !!!cp (33);
1224          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1225          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1226          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1228        }                                    column => $self->{column_prev} - 1,
1229      } elsif ($self->{state} eq 'tag name') {                                   };
1230        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
1231            $self->{next_input_character} == 0x000A or # LF          redo A;
1232            $self->{next_input_character} == 0x000B or # VT        }
1233            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
1234            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
1235          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
1236          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
1237          redo A;            $self->{next_char} == 0x000C or # FF
1238        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
1239          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
1240            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1241                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
1242            redo A;
1243          } elsif ($self->{next_char} == 0x003E) { # >
1244            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245              !!!cp (35);
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1249            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1250              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1251            }            #  !!! cp (36);
1252              #  !!! parse-error (type => 'end tag attribute');
1253              #} else {
1254                !!!cp (37);
1255              #}
1256          } else {          } else {
1257            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1258          }          }
1259          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1260          !!!next-input-character;          !!!next-input-character;
1261    
1262          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1263    
1264          redo A;          redo A;
1265        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1266                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1267          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1268            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1269            # start tag or end tag            # start tag or end tag
1270          ## Stay in this state          ## Stay in this state
1271          !!!next-input-character;          !!!next-input-character;
1272          redo A;          redo A;
1273        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1280            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1281              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1282            }            #  !!! cp (40);
1283              #  !!! parse-error (type => 'end tag attribute');
1284              #} else {
1285                !!!cp (41);
1286              #}
1287          } else {          } else {
1288            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1289          }          }
1290          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1291          # reconsume          # reconsume
1292    
1293          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!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  
1300          redo A;          redo A;
1301        } else {        } else {
1302          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1303            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1304            # start tag or end tag            # start tag or end tag
1305          ## Stay in the state          ## Stay in the state
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1310        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1311            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1312            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1313            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1314            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1315            !!!cp (45);
1316          ## Stay in the state          ## Stay in the state
1317          !!!next-input-character;          !!!next-input-character;
1318          redo A;          redo A;
1319        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1325            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1326                !!!cp (47);
1327              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1328              } else {
1329                !!!cp (48);
1330            }            }
1331          } else {          } else {
1332            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1333          }          }
1334          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336    
1337          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1338    
1339          redo A;          redo A;
1340        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1343                                value => ''};          $self->{current_attribute}
1344          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347            $self->{state} = ATTRIBUTE_NAME_STATE;
1348            !!!next-input-character;
1349            redo A;
1350          } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354          redo A;          redo A;
1355        } 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) {  
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1362            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1363                !!!cp (53);
1364              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1365              } else {
1366                !!!cp (54);
1367            }            }
1368          } else {          } else {
1369            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1370          }          }
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          # reconsume          # reconsume
1373    
1374          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1375    
1376          redo A;          redo A;
1377        } else {        } else {
1378          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1379                                value => ''};               0x0022 => 1, # "
1380          $self->{state} = 'attribute name';               0x0027 => 1, # '
1381                 0x003D => 1, # =
1382                }->{$self->{next_char}}) {
1383              !!!cp (55);
1384              !!!parse-error (type => 'bad attribute name');
1385            } else {
1386              !!!cp (56);
1387            }
1388            $self->{current_attribute}
1389                = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392            $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
1395        }        }
1396      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1397        my $before_leave = sub {        my $before_leave = sub {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1401              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404              !!!cp (58);
1405            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1406              = $self->{current_attribute};              = $self->{current_attribute};
1407          }          }
1408        }; # $before_leave        }; # $before_leave
1409    
1410        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1411            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1412            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1413            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1414            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1415            !!!cp (59);
1416          $before_leave->();          $before_leave->();
1417          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1421            !!!cp (60);
1422          $before_leave->();          $before_leave->();
1423          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432              !!!cp (62);
1433            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1434            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1435              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1437  sub _get_next_token ($) {
1437          } else {          } else {
1438            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1439          }          }
1440          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1441          !!!next-input-character;          !!!next-input-character;
1442    
1443          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1444    
1445          redo A;          redo A;
1446        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1447                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1448          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1449            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!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  
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468                !!!cp (67);
1469              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1470              } else {
1471                ## NOTE: This state should never be reached.
1472                !!!cp (68);
1473            }            }
1474          } else {          } else {
1475            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1476          }          }
1477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1478          # reconsume          # reconsume
1479    
1480          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1481    
1482          redo A;          redo A;
1483        } else {        } else {
1484          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1485                $self->{next_char} == 0x0027) { # '
1486              !!!cp (69);
1487              !!!parse-error (type => 'bad attribute name');
1488            } else {
1489              !!!cp (70);
1490            }
1491            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1492          ## Stay in the state          ## Stay in the state
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        }        }
1496      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1497        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1498            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1499            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1500            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1501            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1502            !!!cp (71);
1503          ## Stay in the state          ## Stay in the state
1504          !!!next-input-character;          !!!next-input-character;
1505          redo A;          redo A;
1506        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1507          $self->{state} = 'before attribute value';          !!!cp (72);
1508            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1509          !!!next-input-character;          !!!next-input-character;
1510          redo A;          redo A;
1511        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1517            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1518                !!!cp (74);
1519              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1520              } else {
1521                ## NOTE: This state should never be reached.
1522                !!!cp (75);
1523            }            }
1524          } else {          } else {
1525            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1526          }          }
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          !!!next-input-character;          !!!next-input-character;
1529    
1530          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1531    
1532          redo A;          redo A;
1533        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1536                                value => ''};          $self->{current_attribute}
1537          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540            $self->{state} = ATTRIBUTE_NAME_STATE;
1541            !!!next-input-character;
1542            redo A;
1543          } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!next-input-character;          !!!next-input-character;
1547          redo A;          redo A;
1548        } 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) {  
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1556                !!!cp (80);
1557              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                ## NOTE: This state should never be reached.
1560                !!!cp (81);
1561            }            }
1562          } else {          } else {
1563            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1564          }          }
1565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1566          # reconsume          # reconsume
1567    
1568          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1573                                value => ''};              $self->{next_char} == 0x0027) { # '
1574          $self->{state} = 'attribute name';            !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583            $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
1586        }        }
1587      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1588        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1589            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1590            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1591            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1592            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1593            !!!cp (83);
1594          ## Stay in the state          ## Stay in the state
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1598          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1599            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1603          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1604            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1605          ## reconsume          ## reconsume
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1608          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1609            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1614            $self->{current_token}->{first_start_tag}          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1620                !!!cp (88);
1621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1622              } else {
1623                ## NOTE: This state should never be reached.
1624                !!!cp (89);
1625            }            }
1626          } else {          } else {
1627            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1628          }          }
1629          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1630          !!!next-input-character;          !!!next-input-character;
1631    
1632          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1633    
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1642            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1643                !!!cp (91);
1644              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1645              } else {
1646                ## NOTE: This state should never be reached.
1647                !!!cp (92);
1648            }            }
1649          } else {          } else {
1650            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1651          }          }
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1656    
1657          redo A;          redo A;
1658        } else {        } else {
1659          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1660          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1661              !!!parse-error (type => 'bad attribute value');
1662            } else {
1663              !!!cp (94);
1664            }
1665            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1666            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        }        }
1670      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1671        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1672          $self->{state} = 'before attribute name';          !!!cp (95);
1673            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1677          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1678          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1679            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1689            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1690                !!!cp (98);
1691              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1692              } else {
1693                ## NOTE: This state should never be reached.
1694                !!!cp (99);
1695            }            }
1696          } else {          } else {
1697            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1698          }          }
1699          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1700          ## reconsume          ## reconsume
1701    
1702          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1703    
1704          redo A;          redo A;
1705        } else {        } else {
1706          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1707            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1708          ## Stay in the state          ## Stay in the state
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1713        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1714          $self->{state} = 'before attribute name';          !!!cp (101);
1715            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1719          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1720          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1721            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1731            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1732                !!!cp (104);
1733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1734              } else {
1735                ## NOTE: This state should never be reached.
1736                !!!cp (105);
1737            }            }
1738          } else {          } else {
1739            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1740          }          }
1741          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1742          ## reconsume          ## reconsume
1743    
1744          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1745    
1746          redo A;          redo A;
1747        } else {        } else {
1748          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1755        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1756            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1757            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1758            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1759            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1760          $self->{state} = 'before attribute name';          !!!cp (107);
1761          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1762          redo A;          !!!next-input-character;
1763        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1764          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1765          $self->{state} = 'entity in attribute value';          !!!cp (108);
1766          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1767          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1768        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1769          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1770            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1771                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772              !!!cp (109);
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1776            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1777                !!!cp (110);
1778              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1779              } else {
1780                ## NOTE: This state should never be reached.
1781                !!!cp (111);
1782            }            }
1783          } else {          } else {
1784            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1785          }          }
1786          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1787          !!!next-input-character;          !!!next-input-character;
1788    
1789          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1790    
1791          redo A;          redo A;
1792        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1799            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1800                !!!cp (113);
1801              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1802              } else {
1803                ## NOTE: This state should never be reached.
1804                !!!cp (114);
1805            }            }
1806          } else {          } else {
1807            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1808          }          }
1809          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1810          ## reconsume          ## reconsume
1811    
1812          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1813    
1814          redo A;          redo A;
1815        } else {        } else {
1816          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1817                 0x0022 => 1, # "
1818                 0x0027 => 1, # '
1819                 0x003D => 1, # =
1820                }->{$self->{next_char}}) {
1821              !!!cp (115);
1822              !!!parse-error (type => 'bad attribute value');
1823            } else {
1824              !!!cp (116);
1825            }
1826            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1827          ## Stay in the state          ## Stay in the state
1828          !!!next-input-character;          !!!next-input-character;
1829          redo A;          redo A;
1830        }        }
1831      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1832        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1833              (1,
1834               $self->{last_attribute_value_state}
1835                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1836               $self->{last_attribute_value_state}
1837                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1838               -1);
1839    
1840        unless (defined $token) {        unless (defined $token) {
1841            !!!cp (117);
1842          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1843        } else {        } else {
1844            !!!cp (118);
1845          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1846            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1847          ## 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"
1848        }        }
1849    
1850        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1851        # next-input-character is already done        # next-input-character is already done
1852        redo A;        redo A;
1853      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1854          if ($self->{next_char} == 0x0009 or # HT
1855              $self->{next_char} == 0x000A or # LF
1856              $self->{next_char} == 0x000B or # VT
1857              $self->{next_char} == 0x000C or # FF
1858              $self->{next_char} == 0x0020) { # SP
1859            !!!cp (118);
1860            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1861            !!!next-input-character;
1862            redo A;
1863          } elsif ($self->{next_char} == 0x003E) { # >
1864            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865              !!!cp (119);
1866              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1869              if ($self->{current_token}->{attributes}) {
1870                !!!cp (120);
1871                !!!parse-error (type => 'end tag attribute');
1872              } else {
1873                ## NOTE: This state should never be reached.
1874                !!!cp (121);
1875              }
1876            } else {
1877              die "$0: $self->{current_token}->{type}: Unknown token type";
1878            }
1879            $self->{state} = DATA_STATE;
1880            !!!next-input-character;
1881    
1882            !!!emit ($self->{current_token}); # start tag or end tag
1883    
1884            redo A;
1885          } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888            !!!next-input-character;
1889            redo A;
1890          } elsif ($self->{next_char} == -1) {
1891            !!!parse-error (type => 'unclosed tag');
1892            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893              !!!cp (122.3);
1894              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903            } else {
1904              die "$0: $self->{current_token}->{type}: Unknown token type";
1905            }
1906            $self->{state} = DATA_STATE;
1907            ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909            redo A;
1910          } else {
1911            !!!cp ('124.1');
1912            !!!parse-error (type => 'no space between attributes');
1913            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914            ## reconsume
1915            redo A;
1916          }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
1973        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1974          #my $token = {type => COMMENT_TOKEN, data => ''};
1975    
1976        BC: {        BC: {
1977          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1978            $self->{state} = 'data';            !!!cp (124);
1979              $self->{state} = DATA_STATE;
1980            !!!next-input-character;            !!!next-input-character;
1981    
1982            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1983    
1984            redo A;            redo A;
1985          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1986            $self->{state} = 'data';            !!!cp (125);
1987              $self->{state} = DATA_STATE;
1988            ## reconsume            ## reconsume
1989    
1990            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;            redo A;
1993          } else {          } else {
1994            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1995              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1996            !!!next-input-character;            !!!next-input-character;
1997            redo BC;            redo BC;
1998          }          }
1999        } # BC        } # BC
2000      } elsif ($self->{state} eq 'markup declaration open') {  
2001          die "$0: _get_next_token: unexpected case [BC]";
2002        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2003        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2004    
2005          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
2006    
2007        my @next_char;        my @next_char;
2008        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
2009                
2010        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2011          !!!next-input-character;          !!!next-input-character;
2012          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2013          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
2015            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                        line => $l, column => $c,
2017                                       };
2018              $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
2021            } else {
2022              !!!cp (128);
2023            }
2024          } elsif ($self->{next_char} == 0x0044 or # D
2025                   $self->{next_char} == 0x0064) { # d
2026            !!!next-input-character;
2027            push @next_char, $self->{next_char};
2028            if ($self->{next_char} == 0x004F or # O
2029                $self->{next_char} == 0x006F) { # o
2030              !!!next-input-character;
2031              push @next_char, $self->{next_char};
2032              if ($self->{next_char} == 0x0043 or # C
2033                  $self->{next_char} == 0x0063) { # c
2034                !!!next-input-character;
2035                push @next_char, $self->{next_char};
2036                if ($self->{next_char} == 0x0054 or # T
2037                    $self->{next_char} == 0x0074) { # t
2038                  !!!next-input-character;
2039                  push @next_char, $self->{next_char};
2040                  if ($self->{next_char} == 0x0059 or # Y
2041                      $self->{next_char} == 0x0079) { # y
2042                    !!!next-input-character;
2043                    push @next_char, $self->{next_char};
2044                    if ($self->{next_char} == 0x0050 or # P
2045                        $self->{next_char} == 0x0070) { # p
2046                      !!!next-input-character;
2047                      push @next_char, $self->{next_char};
2048                      if ($self->{next_char} == 0x0045 or # E
2049                          $self->{next_char} == 0x0065) { # e
2050                        !!!cp (129);
2051                        ## TODO: What a stupid code this is!
2052                        $self->{state} = DOCTYPE_STATE;
2053                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                  quirks => 1,
2055                                                  line => $l, column => $c,
2056                                                 };
2057                        !!!next-input-character;
2058                        redo A;
2059                      } else {
2060                        !!!cp (130);
2061                      }
2062                    } else {
2063                      !!!cp (131);
2064                    }
2065                  } else {
2066                    !!!cp (132);
2067                  }
2068                } else {
2069                  !!!cp (133);
2070                }
2071              } else {
2072                !!!cp (134);
2073              }
2074            } else {
2075              !!!cp (135);
2076          }          }
2077        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                 $self->{next_input_character} == 0x0064) { # d                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080          !!!next-input-character;          !!!next-input-character;
2081          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2082          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x0043) { # C
             $self->{next_input_character} == 0x006F) { # o  
2083            !!!next-input-character;            !!!next-input-character;
2084            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2085            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0044) { # D
               $self->{next_input_character} == 0x0063) { # c  
2086              !!!next-input-character;              !!!next-input-character;
2087              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2088              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0041) { # A
                 $self->{next_input_character} == 0x0074) { # t  
2089                !!!next-input-character;                !!!next-input-character;
2090                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2091                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0054) { # T
                   $self->{next_input_character} == 0x0079) { # y  
2092                  !!!next-input-character;                  !!!next-input-character;
2093                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2094                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0041) { # A
                     $self->{next_input_character} == 0x0070) { # p  
2095                    !!!next-input-character;                    !!!next-input-character;
2096                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2097                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x005B) { # [
2098                        $self->{next_input_character} == 0x0065) { # e                      !!!cp (135.1);
2099                      ## ISSUE: What a stupid code this is!                      $self->{state} = CDATA_BLOCK_STATE;
                     $self->{state} = 'DOCTYPE';  
2100                      !!!next-input-character;                      !!!next-input-character;
2101                      redo A;                      redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                    }                    }
2105                    } else {
2106                      !!!cp (135.3);
2107                  }                  }
2108                  } else {
2109                    !!!cp (135.4);                
2110                }                }
2111                } else {
2112                  !!!cp (135.5);
2113              }              }
2114              } else {
2115                !!!cp (135.6);
2116            }            }
2117            } else {
2118              !!!cp (135.7);
2119          }          }
2120          } else {
2121            !!!cp (136);
2122        }        }
2123    
2124        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2125        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
2128          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                    line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2134        ## 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?
2135      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
2136        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2137          $self->{state} = 'comment start dash';          !!!cp (137);
2138            $self->{state} = COMMENT_START_DASH_STATE;
2139          !!!next-input-character;          !!!next-input-character;
2140          redo A;          redo A;
2141        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2142            !!!cp (138);
2143          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2144          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2145          !!!next-input-character;          !!!next-input-character;
2146    
2147          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2148    
2149          redo A;          redo A;
2150        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2151            !!!cp (139);
2152          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2153          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2154          ## reconsume          ## reconsume
2155    
2156          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2157    
2158          redo A;          redo A;
2159        } else {        } else {
2160            !!!cp (140);
2161          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2162              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2163          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2164          !!!next-input-character;          !!!next-input-character;
2165          redo A;          redo A;
2166        }        }
2167      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2168        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2169          $self->{state} = 'comment end';          !!!cp (141);
2170            $self->{state} = COMMENT_END_STATE;
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
2173        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2174            !!!cp (142);
2175          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2176          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2177          !!!next-input-character;          !!!next-input-character;
2178    
2179          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2180    
2181          redo A;          redo A;
2182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2183            !!!cp (143);
2184          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2186          ## reconsume          ## reconsume
2187    
2188          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2189    
2190          redo A;          redo A;
2191        } else {        } else {
2192            !!!cp (144);
2193          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2194              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2195          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        }        }
2199      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2200        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2201          $self->{state} = 'comment end dash';          !!!cp (145);
2202            $self->{state} = COMMENT_END_DASH_STATE;
2203          !!!next-input-character;          !!!next-input-character;
2204          redo A;          redo A;
2205        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2206            !!!cp (146);
2207          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2208          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2209          ## reconsume          ## reconsume
2210    
2211          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2212    
2213          redo A;          redo A;
2214        } else {        } else {
2215          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2216            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2217          ## Stay in the state          ## Stay in the state
2218          !!!next-input-character;          !!!next-input-character;
2219          redo A;          redo A;
2220        }        }
2221      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2222        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2223          $self->{state} = 'comment end';          !!!cp (148);
2224            $self->{state} = COMMENT_END_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2228            !!!cp (149);
2229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2231          ## reconsume          ## reconsume
2232    
2233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2234    
2235          redo A;          redo A;
2236        } else {        } else {
2237          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2238          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2239            $self->{state} = COMMENT_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        }        }
2243      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2244        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2245          $self->{state} = 'data';          !!!cp (151);
2246            $self->{state} = DATA_STATE;
2247          !!!next-input-character;          !!!next-input-character;
2248    
2249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2250    
2251          redo A;          redo A;
2252        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2253          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2254            !!!parse-error (type => 'dash in comment',
2255                            line => $self->{line_prev},
2256                            column => $self->{column_prev});
2257          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2258          ## Stay in the state          ## Stay in the state
2259          !!!next-input-character;          !!!next-input-character;
2260          redo A;          redo A;
2261        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2262            !!!cp (153);
2263          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2264          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2265          ## reconsume          ## reconsume
2266    
2267          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2268    
2269          redo A;          redo A;
2270        } else {        } else {
2271          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2272          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2273          $self->{state} = 'comment';                          line => $self->{line_prev},
2274                            column => $self->{column_prev});
2275            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2276            $self->{state} = COMMENT_STATE;
2277          !!!next-input-character;          !!!next-input-character;
2278          redo A;          redo A;
2279        }        }
2280      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2286          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2287            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } else {        } else {
2291            !!!cp (156);
2292          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2293          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2294          ## reconsume          ## reconsume
2295          redo A;          redo A;
2296        }        }
2297      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2298        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2299            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2300            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2301            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2302            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2303            !!!cp (157);
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2308            !!!cp (158);
2309          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2310          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2311          !!!next-input-character;          !!!next-input-character;
2312    
2313          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2314    
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2317            !!!cp (159);
2318          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token}          !!!cp (160);
2327              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2328                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2329  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2330          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2331          !!!next-input-character;          !!!next-input-character;
2332          redo A;          redo A;
2333        }        }
2334      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2335  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2336        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2337            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2338            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2339            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2340            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2341          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2342            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2346          $self->{state} = 'data';          !!!cp (162);
2347            $self->{state} = DATA_STATE;
2348          !!!next-input-character;          !!!next-input-character;
2349    
2350          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2351    
2352          redo A;          redo A;
2353        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2354            !!!cp (163);
2355          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2356          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2357          ## reconsume          ## reconsume
2358    
2359          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2360          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362          redo A;          redo A;
2363        } else {        } else {
2364            !!!cp (164);
2365          $self->{current_token}->{name}          $self->{current_token}->{name}
2366            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2367          ## Stay in the state          ## Stay in the state
2368          !!!next-input-character;          !!!next-input-character;
2369          redo A;          redo A;
2370        }        }
2371      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2372        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2373            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2374            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2375            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2376            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2377            !!!cp (165);
2378          ## Stay in the state          ## Stay in the state
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2382          $self->{state} = 'data';          !!!cp (166);
2383            $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2390            !!!cp (167);
2391          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2392          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2393          ## reconsume          ## reconsume
2394    
2395          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2396          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2400                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2401          !!!next-input-character;          !!!next-input-character;
2402          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2403              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2404            !!!next-input-character;            !!!next-input-character;
2405            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2406                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2407              !!!next-input-character;              !!!next-input-character;
2408              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2409                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2410                !!!next-input-character;                !!!next-input-character;
2411                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2412                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2413                  !!!next-input-character;                  !!!next-input-character;
2414                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2415                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2416                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
2417                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2418                    !!!next-input-character;                    !!!next-input-character;
2419                    redo A;                    redo A;
2420                    } else {
2421                      !!!cp (169);
2422                  }                  }
2423                  } else {
2424                    !!!cp (170);
2425                }                }
2426                } else {
2427                  !!!cp (171);
2428              }              }
2429              } else {
2430                !!!cp (172);
2431            }            }
2432            } else {
2433              !!!cp (173);
2434          }          }
2435    
2436          #          #
2437        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2438                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2439          !!!next-input-character;          !!!next-input-character;
2440          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2441              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2442            !!!next-input-character;            !!!next-input-character;
2443            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2444                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2445              !!!next-input-character;              !!!next-input-character;
2446              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2447                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2448                !!!next-input-character;                !!!next-input-character;
2449                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2450                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2451                  !!!next-input-character;                  !!!next-input-character;
2452                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2453                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2454                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2455                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2456                    !!!next-input-character;                    !!!next-input-character;
2457                    redo A;                    redo A;
2458                    } else {
2459                      !!!cp (175);
2460                  }                  }
2461                  } else {
2462                    !!!cp (176);
2463                }                }
2464                } else {
2465                  !!!cp (177);
2466              }              }
2467              } else {
2468                !!!cp (178);
2469            }            }
2470            } else {
2471              !!!cp (179);
2472          }          }
2473    
2474          #          #
2475        } else {        } else {
2476            !!!cp (180);
2477          !!!next-input-character;          !!!next-input-character;
2478          #          #
2479        }        }
2480    
2481        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2482        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2483    
2484          $self->{state} = BOGUS_DOCTYPE_STATE;
2485        # next-input-character is already done        # next-input-character is already done
2486        redo A;        redo A;
2487      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2488        if ({        if ({
2489              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2490              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2491            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2492            !!!cp (181);
2493          ## Stay in the state          ## Stay in the state
2494          !!!next-input-character;          !!!next-input-character;
2495          redo A;          redo A;
2496        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2497            !!!cp (182);
2498          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2499          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2503            !!!cp (183);
2504          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2505          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          redo A;          redo A;
2508        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2509            !!!cp (184);
2510          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2511    
2512          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2513          !!!next-input-character;          !!!next-input-character;
2514    
2515          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2516          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2517    
2518          redo A;          redo A;
2519        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2520            !!!cp (185);
2521          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2522    
2523          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2524          ## reconsume          ## reconsume
2525    
2526          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2527          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2528    
2529          redo A;          redo A;
2530        } else {        } else {
2531            !!!cp (186);
2532          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2533          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2534    
2535            $self->{state} = BOGUS_DOCTYPE_STATE;
2536          !!!next-input-character;          !!!next-input-character;
2537          redo A;          redo A;
2538        }        }
2539      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2541          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2542            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543          !!!next-input-character;          !!!next-input-character;
2544          redo A;          redo A;
2545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2546            !!!cp (188);
2547          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2550            !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553            !!!emit ($self->{current_token}); # DOCTYPE
2554    
2555            redo A;
2556          } elsif ($self->{next_char} == -1) {
2557            !!!cp (189);
2558            !!!parse-error (type => 'unclosed PUBLIC literal');
2559    
2560            $self->{state} = DATA_STATE;
2561          ## reconsume          ## reconsume
2562    
2563          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2564          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566          redo A;          redo A;
2567        } else {        } else {
2568            !!!cp (190);
2569          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2570              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
2574        }        }
2575      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2576        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2577          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2578            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2579            !!!next-input-character;
2580            redo A;
2581          } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (192);
2583            !!!parse-error (type => 'unclosed PUBLIC literal');
2584    
2585            $self->{state} = DATA_STATE;
2586          !!!next-input-character;          !!!next-input-character;
2587    
2588            $self->{current_token}->{quirks} = 1;
2589            !!!emit ($self->{current_token}); # DOCTYPE
2590    
2591          redo A;          redo A;
2592        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2593            !!!cp (193);
2594          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2597          ## reconsume          ## reconsume
2598    
2599          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2600          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602          redo A;          redo A;
2603        } else {        } else {
2604            !!!cp (194);
2605          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2606              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2607          ## Stay in the state          ## Stay in the state
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
2610        }        }
2611      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2612        if ({        if ({
2613              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2614              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2615            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2616            !!!cp (195);
2617          ## Stay in the state          ## Stay in the state
2618          !!!next-input-character;          !!!next-input-character;
2619          redo A;          redo A;
2620        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2621            !!!cp (196);
2622          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2623          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2624          !!!next-input-character;          !!!next-input-character;
2625          redo A;          redo A;
2626        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2627            !!!cp (197);
2628          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2629          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2630          !!!next-input-character;          !!!next-input-character;
2631          redo A;          redo A;
2632        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2633          $self->{state} = 'data';          !!!cp (198);
2634            $self->{state} = DATA_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636    
2637          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2638    
2639          redo A;          redo A;
2640        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2641            !!!cp (199);
2642          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2643    
2644          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2645          ## reconsume          ## reconsume
2646    
2647          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2649    
2650          redo A;          redo A;
2651        } else {        } else {
2652            !!!cp (200);
2653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2654          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2655    
2656            $self->{state} = BOGUS_DOCTYPE_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658          redo A;          redo A;
2659        }        }
2660      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2661        if ({        if ({
2662              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2663              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2664            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2665            !!!cp (201);
2666          ## Stay in the state          ## Stay in the state
2667          !!!next-input-character;          !!!next-input-character;
2668          redo A;          redo A;
2669        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2670            !!!cp (202);
2671          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2672          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2673          !!!next-input-character;          !!!next-input-character;
2674          redo A;          redo A;
2675        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2676            !!!cp (203);
2677          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2678          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2679          !!!next-input-character;          !!!next-input-character;
2680          redo A;          redo A;
2681        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2682            !!!cp (204);
2683          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2684          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2685          !!!next-input-character;          !!!next-input-character;
2686    
2687          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2688          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2689    
2690          redo A;          redo A;
2691        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2692            !!!cp (205);
2693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2694    
2695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2696          ## reconsume          ## reconsume
2697    
2698          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2699          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2700    
2701          redo A;          redo A;
2702        } else {        } else {
2703            !!!cp (206);
2704          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2705          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2706    
2707            $self->{state} = BOGUS_DOCTYPE_STATE;
2708          !!!next-input-character;          !!!next-input-character;
2709          redo A;          redo A;
2710        }        }
2711      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2712        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2713          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2714            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
2717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2718            !!!cp (208);
2719            !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721            $self->{state} = DATA_STATE;
2722            !!!next-input-character;
2723    
2724            $self->{current_token}->{quirks} = 1;
2725            !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727            redo A;
2728          } elsif ($self->{next_char} == -1) {
2729            !!!cp (209);
2730          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2731    
2732          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2733          ## reconsume          ## reconsume
2734    
2735          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2736          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2737    
2738          redo A;          redo A;
2739        } else {        } else {
2740            !!!cp (210);
2741          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2742              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2743          ## Stay in the state          ## Stay in the state
2744          !!!next-input-character;          !!!next-input-character;
2745          redo A;          redo A;
2746        }        }
2747      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2748        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2749          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2750            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2751            !!!next-input-character;
2752            redo A;
2753          } elsif ($self->{next_char} == 0x003E) { # >
2754            !!!cp (212);
2755            !!!parse-error (type => 'unclosed SYSTEM literal');
2756    
2757            $self->{state} = DATA_STATE;
2758          !!!next-input-character;          !!!next-input-character;
2759    
2760            $self->{current_token}->{quirks} = 1;
2761            !!!emit ($self->{current_token}); # DOCTYPE
2762    
2763          redo A;          redo A;
2764        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2765            !!!cp (213);
2766          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2769          ## reconsume          ## reconsume
2770    
2771          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2773    
2774          redo A;          redo A;
2775        } else {        } else {
2776            !!!cp (214);
2777          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2778              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2779          ## Stay in the state          ## Stay in the state
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        }        }
2783      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2784        if ({        if ({
2785              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2786              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2787            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2788            !!!cp (215);
2789          ## Stay in the state          ## Stay in the state
2790          !!!next-input-character;          !!!next-input-character;
2791          redo A;          redo A;
2792        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2793          $self->{state} = 'data';          !!!cp (216);
2794            $self->{state} = DATA_STATE;
2795          !!!next-input-character;          !!!next-input-character;
2796    
2797          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2798    
2799          redo A;          redo A;
2800        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2801            !!!cp (217);
2802          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2803            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2804          ## reconsume          ## reconsume
2805    
2806          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2807          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2808    
2809          redo A;          redo A;
2810        } else {        } else {
2811            !!!cp (218);
2812          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2813          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2814    
2815            $self->{state} = BOGUS_DOCTYPE_STATE;
2816          !!!next-input-character;          !!!next-input-character;
2817          redo A;          redo A;
2818        }        }
2819      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2820        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2821          $self->{state} = 'data';          !!!cp (219);
2822            $self->{state} = DATA_STATE;
2823          !!!next-input-character;          !!!next-input-character;
2824    
         delete $self->{current_token}->{correct};  
2825          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2826    
2827          redo A;          redo A;
2828        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2829            !!!cp (220);
2830          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2831          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2832          ## reconsume          ## reconsume
2833    
         delete $self->{current_token}->{correct};  
2834          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2835    
2836          redo A;          redo A;
2837        } else {        } else {
2838            !!!cp (221);
2839          ## Stay in the state          ## Stay in the state
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2844          my $s = '';
2845          
2846          my ($l, $c) = ($self->{line}, $self->{column});
2847    
2848          CS: while ($self->{next_char} != -1) {
2849            if ($self->{next_char} == 0x005D) { # ]
2850              !!!next-input-character;
2851              if ($self->{next_char} == 0x005D) { # ]
2852                !!!next-input-character;
2853                MDC: {
2854                  if ($self->{next_char} == 0x003E) { # >
2855                    !!!cp (221.1);
2856                    !!!next-input-character;
2857                    last CS;
2858                  } elsif ($self->{next_char} == 0x005D) { # ]
2859                    !!!cp (221.2);
2860                    $s .= ']';
2861                    !!!next-input-character;
2862                    redo MDC;
2863                  } else {
2864                    !!!cp (221.3);
2865                    $s .= ']]';
2866                    #
2867                  }
2868                } # MDC
2869              } else {
2870                !!!cp (221.4);
2871                $s .= ']';
2872                #
2873              }
2874            } else {
2875              !!!cp (221.5);
2876              #
2877            }
2878            $s .= chr $self->{next_char};
2879            !!!next-input-character;
2880          } # CS
2881    
2882          $self->{state} = DATA_STATE;
2883          ## next-input-character done or EOF, which is reconsumed.
2884    
2885          if (length $s) {
2886            !!!cp (221.6);
2887            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2888                      line => $l, column => $c});
2889          } else {
2890            !!!cp (221.7);
2891          }
2892    
2893          redo A;
2894    
2895          ## ISSUE: "text tokens" in spec.
2896          ## TODO: Streaming support
2897      } else {      } else {
2898        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2899      }      }
# Line 1609  sub _get_next_token ($) { Line 2902  sub _get_next_token ($) {
2902    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2903  } # _get_next_token  } # _get_next_token
2904    
2905  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2906    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2907    
2908      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2909    
2910    if ({    if ({
2911         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2912         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2913        }->{$self->{next_input_character}}) {         $additional => 1,
2914          }->{$self->{next_char}}) {
2915        !!!cp (1001);
2916      ## Don't consume      ## Don't consume
2917      ## No error      ## No error
2918      return undef;      return undef;
2919    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2920      !!!next-input-character;      !!!next-input-character;
2921      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2922          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2923        my $code;        my $code;
2924        X: {        X: {
2925          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2926          !!!next-input-character;          !!!next-input-character;
2927          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2928              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2929              !!!cp (1002);
2930            $code ||= 0;            $code ||= 0;
2931            $code *= 0x10;            $code *= 0x10;
2932            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2933            redo X;            redo X;
2934          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2935                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2936              !!!cp (1003);
2937            $code ||= 0;            $code ||= 0;
2938            $code *= 0x10;            $code *= 0x10;
2939            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2940            redo X;            redo X;
2941          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2942                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2943              !!!cp (1004);
2944            $code ||= 0;            $code ||= 0;
2945            $code *= 0x10;            $code *= 0x10;
2946            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2947            redo X;            redo X;
2948          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2949            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2950            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2951            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2952              $self->{next_char} = 0x0023; # #
2953            return undef;            return undef;
2954          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2955              !!!cp (1006);
2956            !!!next-input-character;            !!!next-input-character;
2957          } else {          } else {
2958            !!!parse-error (type => 'no refc');            !!!cp (1007);
2959              !!!parse-error (type => 'no refc', line => $l, column => $c);
2960          }          }
2961    
2962          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2963            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2964              !!!parse-error (type => 'invalid character reference',
2965                              text => (sprintf 'U+%04X', $code),
2966                              line => $l, column => $c);
2967            $code = 0xFFFD;            $code = 0xFFFD;
2968          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2969            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2970              !!!parse-error (type => 'invalid character reference',
2971                              text => (sprintf 'U-%08X', $code),
2972                              line => $l, column => $c);
2973            $code = 0xFFFD;            $code = 0xFFFD;
2974          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2975            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2976              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2977            $code = 0x000A;            $code = 0x000A;
2978          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2979            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2980              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2981            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2982          }          }
2983    
2984          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2985                    has_reference => 1,
2986                    line => $l, column => $c,
2987                   };
2988        } # X        } # X
2989      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2990               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2991        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2992        !!!next-input-character;        !!!next-input-character;
2993                
2994        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2995                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2996            !!!cp (1012);
2997          $code *= 10;          $code *= 10;
2998          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2999                    
3000          !!!next-input-character;          !!!next-input-character;
3001        }        }
3002    
3003        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
3004            !!!cp (1013);
3005          !!!next-input-character;          !!!next-input-character;
3006        } else {        } else {
3007          !!!parse-error (type => 'no refc');          !!!cp (1014);
3008            !!!parse-error (type => 'no refc', line => $l, column => $c);
3009        }        }
3010    
3011        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3012          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
3013            !!!parse-error (type => 'invalid character reference',
3014                            text => (sprintf 'U+%04X', $code),
3015                            line => $l, column => $c);
3016          $code = 0xFFFD;          $code = 0xFFFD;
3017        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3018          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
3019            !!!parse-error (type => 'invalid character reference',
3020                            text => (sprintf 'U-%08X', $code),
3021                            line => $l, column => $c);
3022          $code = 0xFFFD;          $code = 0xFFFD;
3023        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3024          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
3025            !!!parse-error (type => 'CR character reference',
3026                            line => $l, column => $c);
3027          $code = 0x000A;          $code = 0x000A;
3028        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3029          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
3030            !!!parse-error (type => 'C1 character reference',
3031                            text => (sprintf 'U+%04X', $code),
3032                            line => $l, column => $c);
3033          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3034        }        }
3035                
3036        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3037                  line => $l, column => $c,
3038                 };
3039      } else {      } else {
3040        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3041        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3042        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3043          $self->{next_char} = 0x0023; # #
3044        return undef;        return undef;
3045      }      }
3046    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3047              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3048             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3049              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3050      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3051      !!!next-input-character;      !!!next-input-character;
3052    
3053      my $value = $entity_name;      my $value = $entity_name;
# Line 1724  sub _tokenize_attempt_to_consume_an_enti Line 3055  sub _tokenize_attempt_to_consume_an_enti
3055      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3056      our $EntityChar;      our $EntityChar;
3057    
3058      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3059             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3060             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3061               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3062              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3063               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3064              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3065               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3066              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3067        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3068        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3069          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3070              !!!cp (1020);
3071            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3072            $match = 1;            $match = 1;
3073            !!!next-input-character;            !!!next-input-character;
3074            last;            last;
3075          } else {          } else {
3076              !!!cp (1021);
3077            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3078            $match = -1;            $match = -1;
3079            !!!next-input-character;            !!!next-input-character;
3080          }          }
3081        } else {        } else {
3082          $value .= chr $self->{next_input_character};          !!!cp (1022);
3083            $value .= chr $self->{next_char};
3084          $match *= 2;          $match *= 2;
3085          !!!next-input-character;          !!!next-input-character;
3086        }        }
3087      }      }
3088            
3089      if ($match > 0) {      if ($match > 0) {
3090        return {type => 'character', data => $value};        !!!cp (1023);
3091          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3092                  line => $l, column => $c,
3093                 };
3094      } elsif ($match < 0) {      } elsif ($match < 0) {
3095        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3096        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3097          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
3098        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3099          return {type => 'character', data => $value};                  line => $l, column => $c,
3100                   };
3101          } else {
3102            !!!cp (1025);
3103            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3104                    line => $l, column => $c,
3105                   };
3106        }        }
3107      } else {      } else {
3108        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3109        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3110        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3111          return {type => CHARACTER_TOKEN, data => '&'.$value,
3112                  line => $l, column => $c,
3113                 };
3114      }      }
3115    } else {    } else {
3116        !!!cp (1027);
3117      ## no characters are consumed      ## no characters are consumed
3118      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3119      return undef;      return undef;
3120    }    }
3121  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1780  sub _initialize_tree_constructor ($) { Line 3127  sub _initialize_tree_constructor ($) {
3127    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3128    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3129    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3130      $self->{document}->set_user_data (manakai_source_line => 1);
3131      $self->{document}->set_user_data (manakai_source_column => 1);
3132  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3133    
3134  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1806  sub _construct_tree ($) { Line 3155  sub _construct_tree ($) {
3155        
3156    !!!next-token;    !!!next-token;
3157    
   $self->{insertion_mode} = 'before head';  
3158    undef $self->{form_element};    undef $self->{form_element};
3159    undef $self->{head_element};    undef $self->{head_element};
3160    $self->{open_elements} = [];    $self->{open_elements} = [];
3161    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3162    
3163      ## NOTE: The "initial" insertion mode.
3164    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3165    
3166      ## NOTE: The "before html" insertion mode.
3167    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3168      $self->{insertion_mode} = BEFORE_HEAD_IM;
3169    
3170      ## NOTE: The "before head" insertion mode and so on.
3171    $self->_tree_construction_main;    $self->_tree_construction_main;
3172  } # _construct_tree  } # _construct_tree
3173    
3174  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3175    my $self = shift;    my $self = shift;
3176    
3177      ## NOTE: "initial" insertion mode
3178    
3179    INITIAL: {    INITIAL: {
3180      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3181        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3182        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3183        ## language.        ## language.
3184        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3185        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3186        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3187        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3188            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3189          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3190            !!!parse-error (type => 'not HTML5', token => $token);
3191        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3192          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3193          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3194          } elsif (defined $token->{public_identifier}) {
3195            if ($token->{public_identifier} eq 'XSLT-compat') {
3196              !!!cp ('t1.2');
3197              !!!parse-error (type => 'XSLT-compat', token => $token,
3198                              level => $self->{level}->{should});
3199            } else {
3200              !!!parse-error (type => 'not HTML5', token => $token);
3201            }
3202          } else {
3203            !!!cp ('t3');
3204            #
3205        }        }
3206                
3207        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3208          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3209          ## NOTE: Default value for both |public_id| and |system_id| attributes
3210          ## are empty strings, so that we don't set any value in missing cases.
3211        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3212            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3213        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 3216  sub _tree_construction_initial ($) {
3216        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3217        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3218                
3219        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3220            !!!cp ('t4');
3221          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3222        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3223          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3224          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3225          if ({          my $prefix = [
3226            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3227            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3228            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3229            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3230            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3231            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3232            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3233            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3234            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3235            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3236            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3237            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3238            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3239            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3240            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3241            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3242            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3243            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3244            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3245            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3246            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3247            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3248            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3249            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3250            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3251            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3252            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3253            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3254            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3255            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3256            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3257            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3258            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3259            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3260            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3261            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3262            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3263            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3264            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3265            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3266            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3267            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3268            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3269            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3270            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3271            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3272            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3273            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3274            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3275            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3276            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3277            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3278            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3279            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3280            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3281            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3282            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3283            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3284            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3285            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3286            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3287            "-//W3C//DTD W3 HTML//EN" => 1,            }
3288            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3289            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3290            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3291            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3292            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3293            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3294            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3295          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3296                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3297            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3298                !!!cp ('t6');
3299              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3300            } else {            } else {
3301                !!!cp ('t7');
3302              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3303            }            }
3304          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3305                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3306              !!!cp ('t8');
3307            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3308            } else {
3309              !!!cp ('t9');
3310          }          }
3311          } else {
3312            !!!cp ('t10');
3313        }        }
3314        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3315          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3316          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3317          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") {
3318              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3319              ## marked as quirks.
3320            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3321              !!!cp ('t11');
3322            } else {
3323              !!!cp ('t12');
3324          }          }
3325          } else {
3326            !!!cp ('t13');
3327        }        }
3328                
3329        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3330        !!!next-token;        !!!next-token;
3331        return;        return;
3332      } elsif ({      } elsif ({
3333                'start tag' => 1,                START_TAG_TOKEN, 1,
3334                'end tag' => 1,                END_TAG_TOKEN, 1,
3335                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3336               }->{$token->{type}}) {               }->{$token->{type}}) {
3337        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3338          !!!parse-error (type => 'no DOCTYPE', token => $token);
3339        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3340        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3341        ## reprocess        ## reprocess
3342          !!!ack-later;
3343        return;        return;
3344      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3345        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3346          ## Ignore the token          ## Ignore the token
3347    
3348          unless (length $token->{data}) {          unless (length $token->{data}) {
3349            ## Stay in the phase            !!!cp ('t15');
3350              ## Stay in the insertion mode.
3351            !!!next-token;            !!!next-token;
3352            redo INITIAL;            redo INITIAL;
3353            } else {
3354              !!!cp ('t16');
3355          }          }
3356          } else {
3357            !!!cp ('t17');
3358        }        }
3359    
3360        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3361        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3362        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3363        ## reprocess        ## reprocess
3364        return;        return;
3365      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3366          !!!cp ('t18');
3367        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3368        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3369                
3370        ## Stay in the phase.        ## Stay in the insertion mode.
3371        !!!next-token;        !!!next-token;
3372        redo INITIAL;        redo INITIAL;
3373      } else {      } else {
3374        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3375      }      }
3376    } # INITIAL    } # INITIAL
3377    
3378      die "$0: _tree_construction_initial: This should be never reached";
3379  } # _tree_construction_initial  } # _tree_construction_initial
3380    
3381  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3382    my $self = shift;    my $self = shift;
3383    
3384      ## NOTE: "before html" insertion mode.
3385        
3386    B: {    B: {
3387        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3388          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3389            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3390          ## Ignore the token          ## Ignore the token
3391          ## Stay in the phase          ## Stay in the insertion mode.
3392          !!!next-token;          !!!next-token;
3393          redo B;          redo B;
3394        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3395            !!!cp ('t20');
3396          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3397          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3398          ## Stay in the phase          ## Stay in the insertion mode.
3399          !!!next-token;          !!!next-token;
3400          redo B;          redo B;
3401        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3402          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3403            ## Ignore the token.            ## Ignore the token.
3404    
3405            unless (length $token->{data}) {            unless (length $token->{data}) {
3406              ## Stay in the phase              !!!cp ('t21');
3407                ## Stay in the insertion mode.
3408              !!!next-token;              !!!next-token;
3409              redo B;              redo B;
3410              } else {
3411                !!!cp ('t22');
3412            }            }
3413            } else {
3414              !!!cp ('t23');
3415          }          }
3416    
3417            $self->{application_cache_selection}->(undef);
3418    
3419          #          #
3420          } elsif ($token->{type} == START_TAG_TOKEN) {
3421            if ($token->{tag_name} eq 'html') {
3422              my $root_element;
3423              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3424              $self->{document}->append_child ($root_element);
3425              push @{$self->{open_elements}},
3426                  [$root_element, $el_category->{html}];
3427    
3428              if ($token->{attributes}->{manifest}) {
3429                !!!cp ('t24');
3430                $self->{application_cache_selection}
3431                    ->($token->{attributes}->{manifest}->{value});
3432                ## ISSUE: Spec is unclear on relative references.
3433                ## According to Hixie (#whatwg 2008-03-19), it should be
3434                ## resolved against the base URI of the document in HTML
3435                ## or xml:base of the element in XHTML.
3436              } else {
3437                !!!cp ('t25');
3438                $self->{application_cache_selection}->(undef);
3439              }
3440    
3441              !!!nack ('t25c');
3442    
3443              !!!next-token;
3444              return; ## Go to the "before head" insertion mode.
3445            } else {
3446              !!!cp ('t25.1');
3447              #
3448            }
3449        } elsif ({        } elsif ({
3450                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3451                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3452                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3453          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3454          #          #
3455        } else {        } else {
3456          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3457        }        }
3458        my $root_element; !!!create-element ($root_element, 'html');  
3459        $self->{document}->append_child ($root_element);      my $root_element;
3460        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3461        ## reprocess      $self->{document}->append_child ($root_element);
3462        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3463        return; ## Go to the main phase.  
3464        $self->{application_cache_selection}->(undef);
3465    
3466        ## NOTE: Reprocess the token.
3467        !!!ack-later;
3468        return; ## Go to the "before head" insertion mode.
3469    
3470        ## ISSUE: There is an issue in the spec
3471    } # B    } # B
3472    
3473      die "$0: _tree_construction_root_element: This should never be reached";
3474  } # _tree_construction_root_element  } # _tree_construction_root_element
3475    
3476  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3485  sub _reset_insertion_mode ($) {
3485            
3486      ## Step 3      ## Step 3
3487      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"!?  
3488        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3489          $last = 1;          $last = 1;
3490          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3491            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3492                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3493              #          } else {
3494            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3495          }          }
3496        }        }
3497              
3498        ## Step 4..13        ## Step 4..14
3499        my $new_mode = {        my $new_mode;
3500                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3501                        td => 'in cell',          !!!cp ('t28.1');
3502                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3503                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3504                        tbody => 'in table body',          ## SVG elements as foreigners.
3505                        thead => 'in table head',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3506                        tfoot => 'in table foot',        } elsif ($node->[1] & TABLE_CELL_EL) {
3507                        caption => 'in caption',          if ($last) {
3508                        colgroup => 'in column group',            !!!cp ('t28.2');
3509                        table => 'in table',            #
3510                        head => 'in body', # not in head!          } else {
3511                        body => 'in body',            !!!cp ('t28.3');
3512                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3513                       }->{$node->[1]};          }
3514          } else {
3515            !!!cp ('t28.4');
3516            $new_mode = {
3517                          select => IN_SELECT_IM,
3518                          ## NOTE: |option| and |optgroup| do not set
3519                          ## insertion mode to "in select" by themselves.
3520                          tr => IN_ROW_IM,
3521                          tbody => IN_TABLE_BODY_IM,
3522                          thead => IN_TABLE_BODY_IM,
3523                          tfoot => IN_TABLE_BODY_IM,
3524                          caption => IN_CAPTION_IM,
3525                          colgroup => IN_COLUMN_GROUP_IM,
3526                          table => IN_TABLE_IM,
3527                          head => IN_BODY_IM, # not in head!
3528                          body => IN_BODY_IM,
3529                          frameset => IN_FRAMESET_IM,
3530                         }->{$node->[0]->manakai_local_name};
3531          }
3532        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3533                
3534        ## Step 14        ## Step 15
3535        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3536          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3537            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3538              $self->{insertion_mode} = BEFORE_HEAD_IM;
3539          } else {          } else {
3540            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3541              !!!cp ('t30');
3542              $self->{insertion_mode} = AFTER_HEAD_IM;
3543          }          }
3544          return;          return;
3545          } else {
3546            !!!cp ('t31');
3547        }        }
3548                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3549        ## Step 16        ## Step 16
3550          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3551          
3552          ## Step 17
3553        $i--;        $i--;
3554        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3555                
3556        ## Step 17        ## Step 18
3557        redo S3;        redo S3;
3558      } # S3      } # S3
3559    
3560      die "$0: _reset_insertion_mode: This line should never be reached";
3561  } # _reset_insertion_mode  } # _reset_insertion_mode
3562    
3563  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3564    my $self = shift;    my $self = shift;
3565    
   my $previous_insertion_mode;  
   
3566    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3567    
3568    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3579  sub _tree_construction_main ($) {
3579      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3580      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3581        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3582            !!!cp ('t32');
3583          return;          return;
3584        }        }
3585      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595        ## Step 6        ## Step 6
3596        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3597            !!!cp ('t33_1');
3598          #          #
3599        } else {        } else {
3600          my $in_open_elements;          my $in_open_elements;
3601          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3602            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3603                !!!cp ('t33');
3604              $in_open_elements = 1;              $in_open_elements = 1;
3605              last OE;              last OE;
3606            }            }
3607          }          }
3608          if ($in_open_elements) {          if ($in_open_elements) {
3609              !!!cp ('t34');
3610            #            #
3611          } else {          } else {
3612              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3613              !!!cp ('t35');
3614            redo S4;            redo S4;
3615          }          }
3616        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3633  sub _tree_construction_main ($) {
3633    
3634        ## Step 11        ## Step 11
3635        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3636            !!!cp ('t36');
3637          ## Step 7'          ## Step 7'
3638          $i++;          $i++;
3639          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3640                    
3641          redo S7;          redo S7;
3642        }        }
3643    
3644          !!!cp ('t37');
3645      } # S7      } # S7
3646    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3647    
3648    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3649      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3650        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3651            !!!cp ('t38');
3652          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3653          return;          return;
3654        }        }
3655      }      }
3656    
3657        !!!cp ('t39');
3658    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3659    
3660    my $parse_rcdata = sub ($$) {    my $insert;
3661      my ($content_model_flag, $insert) = @_;  
3662      my $parse_rcdata = sub ($) {
3663        my ($content_model_flag) = @_;
3664    
3665      ## Step 1      ## Step 1
3666      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3667      my $el;      my $el;
3668      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3669    
3670      ## Step 2      ## Step 2
3671      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3672    
3673      ## Step 3      ## Step 3
3674      $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 3676  sub _tree_construction_main ($) {
3676    
3677      ## Step 4      ## Step 4
3678      my $text = '';      my $text = '';
3679        !!!nack ('t40.1');
3680      !!!next-token;      !!!next-token;
3681      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3682          !!!cp ('t40');
3683        $text .= $token->{data};        $text .= $token->{data};
3684        !!!next-token;        !!!next-token;
3685      }      }
3686    
3687      ## Step 5      ## Step 5
3688      if (length $text) {      if (length $text) {
3689          !!!cp ('t41');
3690        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3691        $el->append_child ($text);        $el->append_child ($text);
3692      }      }
# Line 2220  sub _tree_construction_main ($) { Line 3695  sub _tree_construction_main ($) {
3695      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3696    
3697      ## Step 7      ## Step 7
3698      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3699            $token->{tag_name} eq $start_tag_name) {
3700          !!!cp ('t42');
3701        ## 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});  
3702      } else {      } else {
3703        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3704          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3705            !!!cp ('t43');
3706            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3707          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3708            !!!cp ('t44');
3709            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3710          } else {
3711            die "$0: $content_model_flag in parse_rcdata";
3712          }
3713      }      }
3714      !!!next-token;      !!!next-token;
3715    }; # $parse_rcdata    }; # $parse_rcdata
3716    
3717    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3718      my $script_el;      my $script_el;
3719      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3720      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3721    
3722      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3723      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3724            
3725      my $text = '';      my $text = '';
3726        !!!nack ('t45.1');
3727      !!!next-token;      !!!next-token;
3728      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3729          !!!cp ('t45');
3730        $text .= $token->{data};        $text .= $token->{data};
3731        !!!next-token;        !!!next-token;
3732      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3733      if (length $text) {      if (length $text) {
3734          !!!cp ('t46');
3735        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3736      }      }
3737                                
3738      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3739    
3740      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3741          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3742          !!!cp ('t47');
3743        ## Ignore the token        ## Ignore the token
3744      } else {      } else {
3745        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3746          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3747        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3748        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3749      }      }
3750            
3751      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3752          !!!cp ('t49');
3753        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3754      } else {      } else {
3755          !!!cp ('t50');
3756        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3757        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3758    
# Line 2278  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766      !!!next-token;      !!!next-token;
3767    }; # $script_start_tag    }; # $script_start_tag
3768    
3769      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3770      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3771      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3772    
3773    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3774      my $tag_name = shift;      my $end_tag_token = shift;
3775        my $tag_name = $end_tag_token->{tag_name};
3776    
3777        ## NOTE: The adoption agency algorithm (AAA).
3778    
3779      FET: {      FET: {
3780        ## Step 1        ## Step 1
3781        my $formatting_element;        my $formatting_element;
3782        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3783        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3784          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3785              !!!cp ('t52');
3786              last AFE;
3787            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3788                         eq $tag_name) {
3789              !!!cp ('t51');
3790            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3791            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3792            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3793          }          }
3794        } # AFE        } # AFE
3795        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3796          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3797            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3798          ## Ignore the token          ## Ignore the token
3799          !!!next-token;          !!!next-token;
3800          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3807          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3808            if ($in_scope) {            if ($in_scope) {
3809                !!!cp ('t54');
3810              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3811              last INSCOPE;              last INSCOPE;
3812            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3813              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3814                !!!parse-error (type => 'unmatched end tag',
3815                                text => $token->{tag_name},
3816                                token => $end_tag_token);
3817              ## Ignore the token              ## Ignore the token
3818              !!!next-token;              !!!next-token;
3819              return;              return;
3820            }            }
3821          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3822                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3823            $in_scope = 0;            $in_scope = 0;
3824          }          }
3825        } # INSCOPE        } # INSCOPE
3826        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3827          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3828            !!!parse-error (type => 'unmatched end tag',
3829                            text => $token->{tag_name},
3830                            token => $end_tag_token);
3831          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3832          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3833          return;          return;
3834        }        }
3835        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3836          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3837            !!!parse-error (type => 'not closed',
3838                            text => $self->{open_elements}->[-1]->[0]
3839                                ->manakai_local_name,
3840                            token => $end_tag_token);
3841        }        }
3842                
3843        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3846        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3847          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3848          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3849              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3850              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3851               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3852              !!!cp ('t59');
3853            $furthest_block = $node;            $furthest_block = $node;
3854            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3855          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3856              !!!cp ('t60');
3857            last OE;            last OE;
3858          }          }
3859        } # OE        } # OE
3860                
3861        ## Step 3        ## Step 3
3862        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3863            !!!cp ('t61');
3864          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3865          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3866          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873        ## Step 5        ## Step 5
3874        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3875        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3876            !!!cp ('t62');
3877          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3878        }        }
3879                
# Line 2384  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896          S7S2: {          S7S2: {
3897            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3898              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3899                  !!!cp ('t63');
3900                $node_i_in_active = $_;                $node_i_in_active = $_;
3901                last S7S2;                last S7S2;
3902              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3910  sub _tree_construction_main ($) {
3910                    
3911          ## Step 4          ## Step 4
3912          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3913              !!!cp ('t64');
3914            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3915          }          }
3916                    
3917          ## Step 5          ## Step 5
3918          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3919              !!!cp ('t65');
3920            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3921            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3922            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934        } # S7          } # S7  
3935                
3936        ## Step 8        ## Step 8
3937        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3938            my $foster_parent_element;
3939            my $next_sibling;
3940            OE: for (reverse 0..$#{$self->{open_elements}}) {
3941              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3942                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3943                                 if (defined $parent and $parent->node_type == 1) {
3944                                   !!!cp ('t65.1');
3945                                   $foster_parent_element = $parent;
3946                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3947                                 } else {
3948                                   !!!cp ('t65.2');
3949                                   $foster_parent_element
3950                                     = $self->{open_elements}->[$_ - 1]->[0];
3951                                 }
3952                                 last OE;
3953                               }
3954                             } # OE
3955                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3956                               unless defined $foster_parent_element;
3957            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3958            $open_tables->[-1]->[1] = 1; # tainted
3959          } else {
3960            !!!cp ('t65.3');
3961            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3962          }
3963                
3964        ## Step 9        ## Step 9
3965        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3976  sub _tree_construction_main ($) {
3976        my $i;        my $i;
3977        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3978          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3979              !!!cp ('t66');
3980            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3981            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3982          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3983              !!!cp ('t67');
3984            $i = $_;            $i = $_;
3985          }          }
3986        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990        undef $i;        undef $i;
3991        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3992          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3993              !!!cp ('t68');
3994            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3995            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3996          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3997              !!!cp ('t69');
3998            $i = $_;            $i = $_;
3999          }          }
4000        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005      } # FET      } # FET
4006    }; # $formatting_end_tag    }; # $formatting_end_tag
4007    
4008    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4009      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4010    }; # $insert_to_current    }; # $insert_to_current
4011    
4012    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4013                         my $child = shift;      my $child = shift;
4014                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4015                              table => 1, tbody => 1, tfoot => 1,        # MUST
4016                              thead => 1, tr => 1,        my $foster_parent_element;
4017                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4018                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4019                           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') {  
4020                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4021                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4022                                   !!!cp ('t70');
4023                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4024                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4025                               } else {                               } else {
4026                                   !!!cp ('t71');
4027                                 $foster_parent_element                                 $foster_parent_element
4028                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4029                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 4034  sub _tree_construction_main ($) {
4034                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4035                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4036                             ($child, $next_sibling);                             ($child, $next_sibling);
4037                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4038                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4039                         }        !!!cp ('t72');
4040          $self->{open_elements}->[-1]->[0]->append_child ($child);
4041        }
4042    }; # $insert_to_foster    }; # $insert_to_foster
4043    
4044    my $in_body = sub {    B: while (1) {
4045      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4046      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4047        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4048          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
4049          $script_start_tag->($insert);        ## Stay in the phase
4050          return;        !!!next-token;
4051        } elsif ($token->{tag_name} eq 'style') {        next B;
4052          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
4053          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);               $token->{tag_name} eq 'html') {
4054          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4055        } elsif ({          !!!cp ('t79');
4056                  base => 1, link => 1,          !!!parse-error (type => 'after html', text => 'html', token => $token);
4057                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
4058          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4059          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4060          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html', text => 'html', token => $token);
4061          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4062          return;        } else {
4063        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
4064          ## 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  
         }  
4065    
4066          !!!next-token;        !!!cp ('t82');
4067          return;        !!!parse-error (type => 'not first start tag', token => $token);
4068        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4069          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4070          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4071          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {            !!!cp ('t84');
4072            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
4073              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
4074            } 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;  
4075          }          }
4076        } elsif ($token->{tag_name} eq 'li') {        }
4077          ## has a p element in scope        !!!nack ('t84.1');
4078          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
4079            if ($_->[1] eq 'p') {        next B;
4080              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
4081              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
4082              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4083            } elsif ({          !!!cp ('t85');
4084                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
4085                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4086                     }->{$_->[1]}) {          !!!cp ('t86');
4087              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
4088            }        } else {
4089          } # INSCOPE          !!!cp ('t87');
4090                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
4091          ## Step 1        }
4092          my $i = -1;        !!!next-token;
4093          my $node = $self->{open_elements}->[$i];        next B;
4094          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4095            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
4096            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
4097              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});  
             
4098          !!!next-token;          !!!next-token;
4099          return;          next B;
4100        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4101          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4102            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4103            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4104              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4105                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4106              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4107              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4108              $formatting_end_tag->($token->{tag_name});            #
4109                        } elsif ({
4110              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4111                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4112                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4113                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4114                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4115              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4116              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4117                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4118                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4119                  last OE;            !!!cp ('t87.2');
4120                }            !!!parse-error (type => 'not closed',
4121              } # OE                            text => $self->{open_elements}->[-1]->[0]
4122              last AFE;                                ->manakai_local_name,
4123            } elsif ($node->[0] eq '#marker') {                            token => $token);
4124              last AFE;  
4125              pop @{$self->{open_elements}}
4126                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4127    
4128              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4129              ## Reprocess.
4130              next B;
4131            } else {
4132              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4133              my $tag_name = $token->{tag_name};
4134              if ($nsuri eq $SVG_NS) {
4135                $tag_name = {
4136                   altglyph => 'altGlyph',
4137                   altglyphdef => 'altGlyphDef',
4138                   altglyphitem => 'altGlyphItem',
4139                   animatecolor => 'animateColor',
4140                   animatemotion => 'animateMotion',
4141                   animatetransform => 'animateTransform',
4142                   clippath => 'clipPath',
4143                   feblend => 'feBlend',
4144                   fecolormatrix => 'feColorMatrix',
4145                   fecomponenttransfer => 'feComponentTransfer',
4146                   fecomposite => 'feComposite',
4147                   feconvolvematrix => 'feConvolveMatrix',
4148                   fediffuselighting => 'feDiffuseLighting',
4149                   fedisplacementmap => 'feDisplacementMap',
4150                   fedistantlight => 'feDistantLight',
4151                   feflood => 'feFlood',
4152                   fefunca => 'feFuncA',
4153                   fefuncb => 'feFuncB',
4154                   fefuncg => 'feFuncG',
4155                   fefuncr => 'feFuncR',
4156                   fegaussianblur => 'feGaussianBlur',
4157                   feimage => 'feImage',
4158                   femerge => 'feMerge',
4159                   femergenode => 'feMergeNode',
4160                   femorphology => 'feMorphology',
4161                   feoffset => 'feOffset',
4162                   fepointlight => 'fePointLight',
4163                   fespecularlighting => 'feSpecularLighting',
4164                   fespotlight => 'feSpotLight',
4165                   fetile => 'feTile',
4166                   feturbulence => 'feTurbulence',
4167                   foreignobject => 'foreignObject',
4168                   glyphref => 'glyphRef',
4169                   lineargradient => 'linearGradient',
4170                   radialgradient => 'radialGradient',
4171                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4172                   textpath => 'textPath',  
4173                }->{$tag_name} || $tag_name;
4174            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4175    
4176          !!!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];  
4177    
4178          !!!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);  
4179    
4180          ## 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', ''];  
4181    
4182          !!!next-token;            if ($self->{self_closing}) {
4183          return;              pop @{$self->{open_elements}};
4184        } elsif ($token->{tag_name} eq 'marquee' or              !!!ack ('t87.3');
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4185            } else {            } else {
4186              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
4187            }            }
4188            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};  
4189            !!!next-token;            !!!next-token;
4190              next B;
4191          }          }
4192          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4193            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4194          }          !!!cp ('t87.5');
4195                    #
4196          $self->{content_model} = PCDATA_CONTENT_MODEL;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4197                    !!!cp ('t87.6');
4198          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
4199              $token->{tag_name} eq $tag_name) {                          text => $self->{open_elements}->[-1]->[0]
4200            ## Ignore the token                              ->manakai_local_name,
4201          } else {                          token => $token);
4202            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4203          }          pop @{$self->{open_elements}}
4204          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4205          return;  
4206        } elsif ({          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4207                  iframe => 1,          ## Reprocess.
4208                  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.  
4209        } else {        } else {
4210          $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;  
4211        }        }
4212      } 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]);  
             }  
           }  
4213    
4214            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4215            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4216            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4217          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4218            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4219            ## 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]);  
4220            } else {            } else {
4221              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4222            }              ## Ignore the token.
4223          }              !!!next-token;
4224                        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;  
4225            }            }
4226          } # INSCOPE            unless (length $token->{data}) {
4227                        !!!cp ('t88');
4228          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {              !!!next-token;
4229            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;  
4230            }            }
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4231          }          }
           
         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');  
4232    
4233          ## As if <br>          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4234          $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t89');
4235                      ## As if <head>
4236          my $el;            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4237          !!!create-element ($el, 'br');            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4238          $insert->($el);            push @{$self->{open_elements}},
4239                          [$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];  
4240    
4241          ## Step 2            ## Reprocess in the "in head" insertion mode...
4242          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;  
4243    
4244              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4245              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4246            } else {            !!!cp ('t90');
4247              ## Step 3            ## As if </noscript>
4248              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4249                  #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];  
4250                        
4251            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4252            redo S2;            ## As if </head>
4253          } # 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.  
       }  
4254    
4255        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4256        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4257      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4258               $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;  
       }  
4259    
4260  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4261  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4262        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});  
4263          }          }
4264        }  
4265        !!!next-token;          ## "after head" insertion mode
4266        redo B;          ## As if <body>
4267      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4268        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4269        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4270          $self->{document}->append_child ($comment);          next B;
4271        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4272          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4273        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4275        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4276        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4277        redo B;                  ($self->{head_element});
4278      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4279            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4280              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4281                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4282                unless (length $token->{data}) {              !!!next-token;
4283                  !!!next-token;              next B;
4284                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4285                }              !!!cp ('t93.2');
4286              }              !!!parse-error (type => 'after head', text => 'head',
4287              ## As if <head>                              token => $token);
4288              !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4289              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t93.3');
4290              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4291              $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;  
             }  
4292            } else {            } else {
4293              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4294                !!!parse-error (type => 'in head:head',
4295                                token => $token); # or in head noscript
4296                ## Ignore the token
4297                !!!nack ('t95.1');
4298                !!!next-token;
4299                next B;
4300            }            }
4301          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4302                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4303                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4304            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4305              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4306                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4307                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4308                  !!!next-token;  
4309                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4310              ## Reprocess in the "in head" insertion mode...
4311            } else {
4312              !!!cp ('t97');
4313            }
4314    
4315                if ($token->{tag_name} eq 'base') {
4316                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4317                    !!!cp ('t98');
4318                    ## As if </noscript>
4319                    pop @{$self->{open_elements}};
4320                    !!!parse-error (type => 'in noscript', text => 'base',
4321                                    token => $token);
4322                  
4323                    $self->{insertion_mode} = IN_HEAD_IM;
4324                    ## Reprocess in the "in head" insertion mode...
4325                  } else {
4326                    !!!cp ('t99');
4327                }                }
4328              }  
               
             #  
           } 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}}) {  
4329                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4330                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4332                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4333                                    text => $token->{tag_name}, token => $token);
4334                    push @{$self->{open_elements}},
4335                        [$self->{head_element}, $el_category->{head}];
4336                  } else {
4337                    !!!cp ('t101');
4338                }                }
4339                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4342                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4343                  !!!nack ('t101.1');
4344                !!!next-token;                !!!next-token;
4345                redo B;                next B;
4346              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4347                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4348                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4350                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4351                                    text => $token->{tag_name}, token => $token);
4352                    push @{$self->{open_elements}},
4353                        [$self->{head_element}, $el_category->{head}];
4354                  } else {
4355                    !!!cp ('t103');
4356                }                }
4357                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4358                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4359                  pop @{$self->{open_elements}} # <head>
4360                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                  !!!ack ('t103.1');
4362                  !!!next-token;
4363                  next B;
4364                } elsif ($token->{tag_name} eq 'meta') {
4365                  ## NOTE: There is a "as if in head" code clone.
4366                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                    !!!cp ('t104');
4368                    !!!parse-error (type => 'after head',
4369                                    text => $token->{tag_name}, token => $token);
4370                    push @{$self->{open_elements}},
4371                        [$self->{head_element}, $el_category->{head}];
4372                  } else {
4373                    !!!cp ('t105');
4374                  }
4375                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4376                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4377    
4378                unless ($self->{confident}) {                unless ($self->{confident}) {
4379                  my $charset;                  if ($token->{attributes}->{charset}) {
4380                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4381                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4382                  }                    ## in the {change_encoding} callback.
4383                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4384                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4385                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4386                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4387                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4388                          ->set_user_data (manakai_has_reference =>
4389                                               $token->{attributes}->{charset}
4390                                                   ->{has_reference});
4391                    } elsif ($token->{attributes}->{content}) {
4392                      if ($token->{attributes}->{content}->{value}
4393                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4394                              [\x09-\x0D\x20]*=
4395                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4396                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4397                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4398                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4399                        ## in the {change_encoding} callback.
4400                        $self->{change_encoding}
4401                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4402                               $token);
4403                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4404                            ->set_user_data (manakai_has_reference =>
4405                                                 $token->{attributes}->{content}
4406                                                       ->{has_reference});
4407                      } else {
4408                        !!!cp ('t108');
4409                      }
4410                    }
4411                  } else {
4412                    if ($token->{attributes}->{charset}) {
4413                      !!!cp ('t109');
4414                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4415                          ->set_user_data (manakai_has_reference =>
4416                                               $token->{attributes}->{charset}
4417                                                   ->{has_reference});
4418                    }
4419                    if ($token->{attributes}->{content}) {
4420                      !!!cp ('t110');
4421                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4422                          ->set_user_data (manakai_has_reference =>
4423                                               $token->{attributes}->{content}
4424                                                   ->{has_reference});
4425                  }                  }
                 ## TODO: Change the encoding  
4426                }                }
4427    
4428                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4429                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4434                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4436                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4437                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4438                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript', text => 'title',
4439                                    token => $token);
4440                  
4441                    $self->{insertion_mode} = IN_HEAD_IM;
4442                    ## Reprocess in the "in head" insertion mode...
4443                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4444                    !!!cp ('t112');
4445                    !!!parse-error (type => 'after head',
4446                                    text => $token->{tag_name}, token => $token);
4447                    push @{$self->{open_elements}},
4448                        [$self->{head_element}, $el_category->{head}];
4449                  } else {
4450                    !!!cp ('t113');
4451                }                }
4452    
4453                  ## NOTE: There is a "as if in head" code clone.
4454                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4455                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4456                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4457                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4458                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4459                    if $self->{insertion_mode} eq 'after head';                next B;
4460                redo B;              } elsif ($token->{tag_name} eq 'style' or
4461              } elsif ($token->{tag_name} eq 'style') {                       $token->{tag_name} eq 'noframes') {
4462                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4463                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4464                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4465                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4466                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4467                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4468                                    text => $token->{tag_name}, token => $token);
4469                    push @{$self->{open_elements}},
4470                        [$self->{head_element}, $el_category->{head}];
4471                  } else {
4472                    !!!cp ('t115');
4473                }                }
4474                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4475                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4476                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4477                redo B;                next B;
4478              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4479                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4480                    !!!cp ('t116');
4481                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4482                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4483                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4484                    !!!nack ('t116.1');
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4488                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4489                    !!!parse-error (type => 'in noscript', text => 'noscript',
4490                                    token => $token);
4491                  ## Ignore the token                  ## Ignore the token
4492                    !!!nack ('t117.1');
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                } else {                } else {
4496                    !!!cp ('t118');
4497                  #                  #
4498                }                }
4499              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4500                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4501                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4502                ## Ignore the token                  ## As if </noscript>
4503                !!!next-token;                  pop @{$self->{open_elements}};
4504                redo B;                  !!!parse-error (type => 'in noscript', text => 'script',
4505              } elsif ($self->{insertion_mode} ne 'in head noscript' and                                  token => $token);
4506                       $token->{tag_name} eq 'script') {                
4507                if ($self->{insertion_mode} eq 'after head') {                  $self->{insertion_mode} = IN_HEAD_IM;
4508                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  ## Reprocess in the "in head" insertion mode...
4509                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4510                    !!!cp ('t120');
4511                    !!!parse-error (type => 'after head',
4512                                    text => $token->{tag_name}, token => $token);
4513                    push @{$self->{open_elements}},
4514                        [$self->{head_element}, $el_category->{head}];
4515                  } else {
4516                    !!!cp ('t121');
4517                }                }
4518    
4519                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4520                $script_start_tag->($insert_to_current);                $script_start_tag->();
4521                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4522                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4523                redo B;                next B;
4524              } 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  
4525                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4526                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4527                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4528                    ## As if </noscript>
4529                    pop @{$self->{open_elements}};
4530                    !!!parse-error (type => 'in noscript',
4531                                    text => $token->{tag_name}, token => $token);
4532                    
4533                    ## Reprocess in the "in head" insertion mode...
4534                    ## As if </head>
4535                    pop @{$self->{open_elements}};
4536                    
4537                    ## Reprocess in the "after head" insertion mode...
4538                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4539                    !!!cp ('t124');
4540                    pop @{$self->{open_elements}};
4541                    
4542                    ## Reprocess in the "after head" insertion mode...
4543                  } else {
4544                    !!!cp ('t125');
4545                  }
4546    
4547                  ## "after head" insertion mode
4548                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4549                  if ($token->{tag_name} eq 'body') {
4550                    !!!cp ('t126');
4551                    $self->{insertion_mode} = IN_BODY_IM;
4552                  } elsif ($token->{tag_name} eq 'frameset') {
4553                    !!!cp ('t127');
4554                    $self->{insertion_mode} = IN_FRAMESET_IM;
4555                  } else {
4556                    die "$0: tag name: $self->{tag_name}";
4557                  }
4558                  !!!nack ('t127.1');
4559                !!!next-token;                !!!next-token;
4560                redo B;                next B;
4561              } else {              } else {
4562                  !!!cp ('t128');
4563                #                #
4564              }              }
4565            } elsif ($token->{type} eq 'end tag') {  
4566              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4567                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4568                  ## As if </noscript>
4569                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4570                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/',
4571                !!!next-token;                                text => $token->{tag_name}, token => $token);
4572                redo B;                
4573              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## Reprocess in the "in head" insertion mode...
4574                  $token->{tag_name} eq 'noscript') {                ## As if </head>
4575                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4576                $self->{insertion_mode} = 'in head';  
4577                !!!next-token;                ## Reprocess in the "after head" insertion mode...
4578                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4579              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
4580                       {                ## As if </head>
4581                  pop @{$self->{open_elements}};
4582    
4583                  ## Reprocess in the "after head" insertion mode...
4584                } else {
4585                  !!!cp ('t131');
4586                }
4587    
4588                ## "after head" insertion mode
4589                ## As if <body>
4590                !!!insert-element ('body',, $token);
4591                $self->{insertion_mode} = IN_BODY_IM;
4592                ## reprocess
4593                !!!ack-later;
4594                next B;
4595              } elsif ($token->{type} == END_TAG_TOKEN) {
4596                if ($token->{tag_name} eq 'head') {
4597                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4598                    !!!cp ('t132');
4599                    ## As if <head>
4600                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4601                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4602                    push @{$self->{open_elements}},
4603                        [$self->{head_element}, $el_category->{head}];
4604    
4605                    ## Reprocess in the "in head" insertion mode...
4606                    pop @{$self->{open_elements}};
4607                    $self->{insertion_mode} = AFTER_HEAD_IM;
4608                    !!!next-token;
4609                    next B;
4610                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                    !!!cp ('t133');
4612                    ## As if </noscript>
4613                    pop @{$self->{open_elements}};
4614                    !!!parse-error (type => 'in noscript:/',
4615                                    text => 'head', token => $token);
4616                    
4617                    ## Reprocess in the "in head" insertion mode...
4618                    pop @{$self->{open_elements}};
4619                    $self->{insertion_mode} = AFTER_HEAD_IM;
4620                    !!!next-token;
4621                    next B;
4622                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4623                    !!!cp ('t134');
4624                    pop @{$self->{open_elements}};
4625                    $self->{insertion_mode} = AFTER_HEAD_IM;
4626                    !!!next-token;
4627                    next B;
4628                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                    !!!cp ('t134.1');
4630                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4631                                    token => $token);
4632                    ## Ignore the token
4633                    !!!next-token;
4634                    next B;
4635                  } else {
4636                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4637                  }
4638                } elsif ($token->{tag_name} eq 'noscript') {
4639                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4640                    !!!cp ('t136');
4641                    pop @{$self->{open_elements}};
4642                    $self->{insertion_mode} = IN_HEAD_IM;
4643                    !!!next-token;
4644                    next B;
4645                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4646                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4647                    !!!cp ('t137');
4648                    !!!parse-error (type => 'unmatched end tag',
4649                                    text => 'noscript', token => $token);
4650                    ## Ignore the token ## ISSUE: An issue in the spec.
4651                    !!!next-token;
4652                    next B;
4653                  } else {
4654                    !!!cp ('t138');
4655                    #
4656                  }
4657                } elsif ({
4658                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
4659                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4660                #                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661              } elsif ($self->{insertion_mode} eq 'in head noscript' and                    $self->{insertion_mode} == IN_HEAD_IM or
4662                       {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4663                        p => 1, br => 1,                  !!!cp ('t140');
4664                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'unmatched end tag',
4665                #                                  text => $token->{tag_name}, token => $token);
4666              } elsif ($self->{insertion_mode} ne 'after head') {                  ## Ignore the token
4667                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!next-token;
4668                    next B;
4669                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                    !!!cp ('t140.1');
4671                    !!!parse-error (type => 'unmatched end tag',
4672                                    text => $token->{tag_name}, token => $token);
4673                    ## Ignore the token
4674                    !!!next-token;
4675                    next B;
4676                  } else {
4677                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4678                  }
4679                } elsif ($token->{tag_name} eq 'p') {
4680                  !!!cp ('t142');
4681                  !!!parse-error (type => 'unmatched end tag',
4682                                  text => $token->{tag_name}, token => $token);
4683                  ## Ignore the token
4684                  !!!next-token;
4685                  next B;
4686                } elsif ($token->{tag_name} eq 'br') {
4687                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4688                    !!!cp ('t142.2');
4689                    ## (before head) as if <head>, (in head) as if </head>
4690                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4691                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4692                    $self->{insertion_mode} = AFTER_HEAD_IM;
4693      
4694                    ## Reprocess in the "after head" insertion mode...
4695                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4696                    !!!cp ('t143.2');
4697                    ## As if </head>
4698                    pop @{$self->{open_elements}};
4699                    $self->{insertion_mode} = AFTER_HEAD_IM;
4700      
4701                    ## Reprocess in the "after head" insertion mode...
4702                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4703                    !!!cp ('t143.3');
4704                    ## ISSUE: Two parse errors for <head><noscript></br>
4705                    !!!parse-error (type => 'unmatched end tag',
4706                                    text => 'br', token => $token);
4707                    ## As if </noscript>
4708                    pop @{$self->{open_elements}};
4709                    $self->{insertion_mode} = IN_HEAD_IM;
4710    
4711                    ## Reprocess in the "in head" insertion mode...
4712                    ## As if </head>
4713                    pop @{$self->{open_elements}};
4714                    $self->{insertion_mode} = AFTER_HEAD_IM;
4715    
4716                    ## Reprocess in the "after head" insertion mode...
4717                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4718                    !!!cp ('t143.4');
4719                    #
4720                  } else {
4721                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4722                  }
4723    
4724                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4725                  !!!parse-error (type => 'unmatched end tag',
4726                                  text => 'br', token => $token);
4727                ## Ignore the token                ## Ignore the token
4728                !!!next-token;                !!!next-token;
4729                redo B;                next B;
4730              } else {              } else {
4731                #                !!!cp ('t145');
4732                  !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                  ## Ignore the token
4735                  !!!next-token;
4736                  next B;
4737              }              }
           } else {  
             #  
           }  
4738    
4739            ## As if </head> or </noscript> or <body>              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4740            if ($self->{insertion_mode} eq 'in head') {                !!!cp ('t146');
4741              pop @{$self->{open_elements}};                ## As if </noscript>
4742              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
4743            } elsif ($self->{insertion_mode} eq 'in head noscript') {                !!!parse-error (type => 'in noscript:/',
4744              pop @{$self->{open_elements}};                                text => $token->{tag_name}, token => $token);
4745              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));                
4746              $self->{insertion_mode} = 'in head';                ## Reprocess in the "in head" insertion mode...
4747            } else { # 'after head'                ## As if </head>
4748              !!!insert-element ('body');                pop @{$self->{open_elements}};
4749              $self->{insertion_mode} = 'in body';  
4750            }                ## Reprocess in the "after head" insertion mode...
4751            ## reprocess              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4752            redo B;                !!!cp ('t147');
4753                  ## As if </head>
4754                  pop @{$self->{open_elements}};
4755    
4756                  ## Reprocess in the "after head" insertion mode...
4757                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4758    ## ISSUE: This case cannot be reached?
4759                  !!!cp ('t148');
4760                  !!!parse-error (type => 'unmatched end tag',
4761                                  text => $token->{tag_name}, token => $token);
4762                  ## Ignore the token ## ISSUE: An issue in the spec.
4763                  !!!next-token;
4764                  next B;
4765                } else {
4766                  !!!cp ('t149');
4767                }
4768    
4769                ## "after head" insertion mode
4770                ## As if <body>
4771                !!!insert-element ('body',, $token);
4772                $self->{insertion_mode} = IN_BODY_IM;
4773                ## reprocess
4774                next B;
4775          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4776            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4777              !!!cp ('t149.1');
4778    
4779              ## NOTE: As if <head>
4780              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4781              $self->{open_elements}->[-1]->[0]->append_child
4782                  ($self->{head_element});
4783              #push @{$self->{open_elements}},
4784              #    [$self->{head_element}, $el_category->{head}];
4785              #$self->{insertion_mode} = IN_HEAD_IM;
4786              ## NOTE: Reprocess.
4787    
4788              ## NOTE: As if </head>
4789              #pop @{$self->{open_elements}};
4790              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4791              ## NOTE: Reprocess.
4792              
4793              #
4794            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4795              !!!cp ('t149.2');
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            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804              !!!cp ('t149.3');
4805    
4806              !!!parse-error (type => 'in noscript:#eof', token => $token);
4807    
4808              ## As if </noscript>
4809              pop @{$self->{open_elements}};
4810              #$self->{insertion_mode} = IN_HEAD_IM;
4811              ## NOTE: Reprocess.
4812    
4813              ## NOTE: As if </head>
4814              pop @{$self->{open_elements}};
4815              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4816              ## NOTE: Reprocess.
4817    
4818              #
4819            } else {
4820              !!!cp ('t149.4');
4821              #
4822            }
4823    
4824            ## NOTE: As if <body>
4825            !!!insert-element ('body',, $token);
4826            $self->{insertion_mode} = IN_BODY_IM;
4827            ## NOTE: Reprocess.
4828            next B;
4829          } else {
4830            die "$0: $token->{type}: Unknown token type";
4831          }
4832    
4833            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4834          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4835                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
4836                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4837              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4838              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4839                            
4840              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4841    
4842              !!!next-token;              !!!next-token;
4843              redo B;              next B;
4844            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4845              if ({              if ({
4846                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4847                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4848                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4849                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4850                  ## have an element in table scope                  ## have an element in table scope
4851                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4852                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4853                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4854                      $tn = $node->[1];                      !!!cp ('t151');
4855                      last INSCOPE;  
4856                    } elsif ({                      ## Close the cell
4857                              table => 1, html => 1,                      !!!back-token; # <x>
4858                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4859                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4860                    }                                line => $token->{line},
4861                  } # INSCOPE                                column => $token->{column}};
4862                    unless (defined $tn) {                      next B;
4863                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4864                      ## Ignore the token                      !!!cp ('t152');
4865                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4866                      redo B;                      last;
4867                    }                    }
4868                    }
4869    
4870                    !!!cp ('t153');
4871                    !!!parse-error (type => 'start tag not allowed',
4872                        text => $token->{tag_name}, token => $token);
4873                    ## Ignore the token
4874                    !!!nack ('t153.1');
4875                    !!!next-token;
4876                    next B;
4877                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                    !!!parse-error (type => 'not closed', text => 'caption',
4879                                    token => $token);
4880                                    
4881                  ## 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>  
4882                  ## have a table element in table scope                  ## have a table element in table scope
4883                  my $i;                  my $i;
4884                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4885                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4886                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4887                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4888                      last INSCOPE;                        !!!cp ('t155');
4889                    } elsif ({                        $i = $_;
4890                              table => 1, html => 1,                        last INSCOPE;
4891                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4892                      last INSCOPE;                        !!!cp ('t156');
4893                          last;
4894                        }
4895                    }                    }
4896    
4897                      !!!cp ('t157');
4898                      !!!parse-error (type => 'start tag not allowed',
4899                                      text => $token->{tag_name}, token => $token);
4900                      ## Ignore the token
4901                      !!!nack ('t157.1');
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4909                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4910                       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;  
4911                  }                  }
4912    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4915                      !!!parse-error (type => 'not closed',
4916                                      text => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                    } else {
4920                      !!!cp ('t160');
4921                  }                  }
4922                                    
4923                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4924                                    
4925                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4926                                    
4927                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  ## reprocess                  ## reprocess
4930                  redo B;                  !!!ack-later;
4931                    next B;
4932                } else {                } else {
4933                    !!!cp ('t161');
4934                  #                  #
4935                }                }
4936              } else {              } else {
4937                  !!!cp ('t162');
4938                #                #
4939              }              }
4940            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4941              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4942                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4943                  ## have an element in table scope                  ## have an element in table scope
4944                  my $i;                  my $i;
4945                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4946                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4947                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4948                        !!!cp ('t163');
4949                      $i = $_;                      $i = $_;
4950                      last INSCOPE;                      last INSCOPE;
4951                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4952                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4953                      last INSCOPE;                      last INSCOPE;
4954                    }                    }
4955                  } # INSCOPE                  } # INSCOPE
4956                    unless (defined $i) {                    unless (defined $i) {
4957                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4958                        !!!parse-error (type => 'unmatched end tag',
4959                                        text => $token->{tag_name},
4960                                        token => $token);
4961                      ## Ignore the token                      ## Ignore the token
4962                      !!!next-token;                      !!!next-token;
4963                      redo B;                      next B;
4964                    }                    }
4965                                    
4966                  ## generate implied end tags                  ## generate implied end tags
4967                  if ({                  while ($self->{open_elements}->[-1]->[1]
4968                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4969                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4970                       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;  
4971                  }                  }
4972                    
4973                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4974                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4975                      !!!cp ('t167');
4976                      !!!parse-error (type => 'not closed',
4977                                      text => $self->{open_elements}->[-1]->[0]
4978                                          ->manakai_local_name,
4979                                      token => $token);
4980                    } else {
4981                      !!!cp ('t168');
4982                  }                  }
4983                                    
4984                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4985                                    
4986                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4987                                    
4988                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
4989                                    
4990                  !!!next-token;                  !!!next-token;
4991                  redo B;                  next B;
4992                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4993                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => $token->{tag_name}, token => $token);
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  next B;
4999                } else {                } else {
5000                    !!!cp ('t170');
5001                  #                  #
5002                }                }
5003              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5004                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5005                  ## have a table element in table scope                  ## have a table element in table scope
5006                  my $i;                  my $i;
5007                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5008                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5009                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5010                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5011                      last INSCOPE;                        !!!cp ('t171');
5012                    } elsif ({                        $i = $_;
5013                              table => 1, html => 1,                        last INSCOPE;
5014                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5015                      last INSCOPE;                        !!!cp ('t172');
5016                          last;
5017                        }
5018                    }                    }
5019    
5020                      !!!cp ('t173');
5021                      !!!parse-error (type => 'unmatched end tag',
5022                                      text => $token->{tag_name}, token => $token);
5023                      ## Ignore the token
5024                      !!!next-token;
5025                      next B;
5026                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5027                                    
5028                  ## generate implied end tags                  ## generate implied end tags
5029                  if ({                  while ($self->{open_elements}->[-1]->[1]
5030                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5031                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5032                       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;  
5033                  }                  }
5034                                    
5035                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5037                      !!!parse-error (type => 'not closed',
5038                                      text => $self->{open_elements}->[-1]->[0]
5039                                          ->manakai_local_name,
5040                                      token => $token);
5041                    } else {
5042                      !!!cp ('t176');
5043                  }                  }
5044                                    
5045                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5046                                    
5047                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5048                                    
5049                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5050                                    
5051                  !!!next-token;                  !!!next-token;
5052                  redo B;                  next B;
5053                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5054                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5055                    !!!parse-error (type => 'unmatched end tag',
5056                                    text => $token->{tag_name}, token => $token);
5057                  ## Ignore the token                  ## Ignore the token
5058                  !!!next-token;                  !!!next-token;
5059                  redo B;                  next B;
5060                } else {                } else {
5061                    !!!cp ('t178');
5062                  #                  #
5063                }                }
5064              } elsif ({              } elsif ({
5065                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
5066                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5067                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5068                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
5069                ## have an element in table scope                ## have an element in table scope
5070                my $i;                my $i;
5071                my $tn;                my $tn;
5072                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5073                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5074                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5075                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5076                    last INSCOPE;                      !!!cp ('t179');
5077                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5078                    $tn = $node->[1];  
5079                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5080                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5081                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5082                            table => 1, html => 1,                                line => $token->{line},
5083                           }->{$node->[1]}) {                                column => $token->{column}};
5084                    last INSCOPE;                      next B;
5085                      } elsif ($node->[1] & TABLE_CELL_EL) {
5086                        !!!cp ('t180');
5087                        $tn = $node->[0]->manakai_local_name;
5088                        ## NOTE: There is exactly one |td| or |th| element
5089                        ## in scope in the stack of open elements by definition.
5090                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5091                        ## ISSUE: Can this be reached?
5092                        !!!cp ('t181');
5093                        last;
5094                      }
5095                  }                  }
5096                } # INSCOPE  
5097                unless (defined $i) {                  !!!cp ('t182');
5098                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5099                        text => $token->{tag_name}, token => $token);
5100                  ## Ignore the token                  ## Ignore the token
5101                  !!!next-token;                  !!!next-token;
5102                  redo B;                  next B;
5103                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5104              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5105                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5106                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5107                                  token => $token);
5108    
5109                ## As if </caption>                ## As if </caption>
5110                ## have a table element in table scope                ## have a table element in table scope
5111                my $i;                my $i;
5112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5113                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5114                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5115                      !!!cp ('t184');
5116                    $i = $_;                    $i = $_;
5117                    last INSCOPE;                    last INSCOPE;
5118                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5119                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5120                    last INSCOPE;                    last INSCOPE;
5121                  }                  }
5122                } # INSCOPE                } # INSCOPE
5123                unless (defined $i) {                unless (defined $i) {
5124                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5125                    !!!parse-error (type => 'unmatched end tag',
5126                                    text => 'caption', token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                }                }
5131                                
5132                ## generate implied end tags                ## generate implied end tags
5133                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5134                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5135                     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;  
5136                }                }
5137    
5138                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5139                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5140                    !!!parse-error (type => 'not closed',
5141                                    text => $self->{open_elements}->[-1]->[0]
5142                                        ->manakai_local_name,
5143                                    token => $token);
5144                  } else {
5145                    !!!cp ('t189');
5146                }                }
5147    
5148                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5149    
5150                $clear_up_to_marker->();                $clear_up_to_marker->();
5151    
5152                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5153    
5154                ## reprocess                ## reprocess
5155                redo B;                next B;
5156              } elsif ({              } elsif ({
5157                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5159                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5160                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5161                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5162                                    text => $token->{tag_name}, token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                } else {                } else {
5167                    !!!cp ('t191');
5168                  #                  #
5169                }                }
5170              } elsif ({              } elsif ({
5171                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5172                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5173                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5174                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5176                  !!!parse-error (type => 'unmatched end tag',
5177                                  text => $token->{tag_name}, token => $token);
5178                ## Ignore the token                ## Ignore the token
5179                !!!next-token;                !!!next-token;
5180                redo B;                next B;
5181              } else {              } else {
5182                  !!!cp ('t193');
5183                #                #
5184              }              }
5185            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5186              #          for my $entry (@{$self->{open_elements}}) {
5187              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5188                !!!cp ('t75');
5189                !!!parse-error (type => 'in body:#eof', token => $token);
5190                last;
5191            }            }
5192                      }
5193            $in_body->($insert_to_current);  
5194            redo B;          ## Stop parsing.
5195          } elsif ($self->{insertion_mode} eq 'in table') {          last B;
5196            if ($token->{type} eq 'character') {        } else {
5197              ## NOTE: There are "character in table" code clones.          die "$0: $token->{type}: Unknown token type";
5198              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        }
5199                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5200          $insert = $insert_to_current;
5201          #
5202        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5203          if ($token->{type} == CHARACTER_TOKEN) {
5204            if (not $open_tables->[-1]->[1] and # tainted
5205                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5207                                
5208                unless (length $token->{data}) {            unless (length $token->{data}) {
5209                  !!!next-token;              !!!cp ('t194');
5210                  redo B;              !!!next-token;
5211                }              next B;
5212              }            } else {
5213                !!!cp ('t195');
5214              }
5215            }
5216    
5217              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5218    
5219              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5220              ## 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 5222  sub _tree_construction_main ($) {
5222              ## result in a new Text node.              ## result in a new Text node.
5223              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5224                            
5225              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]}) {  
5226                # MUST                # MUST
5227                my $foster_parent_element;                my $foster_parent_element;
5228                my $next_sibling;                my $next_sibling;
5229                my $prev_sibling;                my $prev_sibling;
5230                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5231                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5232                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5233                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5234                        !!!cp ('t196');
5235                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5236                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5237                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5238                    } else {                    } else {
5239                        !!!cp ('t197');
5240                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5241                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5242                    }                    }
# Line 4017  sub _tree_construction_main ($) { Line 5248  sub _tree_construction_main ($) {
5248                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5249                if (defined $prev_sibling and                if (defined $prev_sibling and
5250                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5251                    !!!cp ('t198');
5252                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5253                } else {                } else {
5254                    !!!cp ('t199');
5255                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5256                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5257                     $next_sibling);                     $next_sibling);
5258                }                }
5259              } else {            $open_tables->[-1]->[1] = 1; # tainted
5260                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5261              !!!cp ('t200');
5262              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5263            }
5264                
5265            !!!next-token;
5266            next B;
5267          } elsif ($token->{type} == START_TAG_TOKEN) {
5268            if ({
5269                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5270                 th => 1, td => 1,
5271                }->{$token->{tag_name}}) {
5272              if ($self->{insertion_mode} == IN_TABLE_IM) {
5273                ## Clear back to table context
5274                while (not ($self->{open_elements}->[-1]->[1]
5275                                & TABLE_SCOPING_EL)) {
5276                  !!!cp ('t201');
5277                  pop @{$self->{open_elements}};
5278              }              }
5279                            
5280              !!!next-token;              !!!insert-element ('tbody',, $token);
5281              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5282            } elsif ($token->{type} eq 'start tag') {              ## reprocess in the "in table body" insertion mode...
5283              if ({            }
5284                   caption => 1,            
5285                   colgroup => 1,            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5286                   tbody => 1, tfoot => 1, thead => 1,              unless ($token->{tag_name} eq 'tr') {
5287                  }->{$token->{tag_name}}) {                !!!cp ('t202');
5288                ## Clear back to table context                !!!parse-error (type => 'missing start tag:tr', token => $token);
5289                while ($self->{open_elements}->[-1]->[1] ne 'table' and              }
5290                       $self->{open_elements}->[-1]->[1] ne 'html') {                  
5291                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## Clear back to table body context
5292                  pop @{$self->{open_elements}};              while (not ($self->{open_elements}->[-1]->[1]
5293                                & TABLE_ROWS_SCOPING_EL)) {
5294                  !!!cp ('t203');
5295                  ## ISSUE: Can this case be reached?
5296                  pop @{$self->{open_elements}};
5297                }
5298                    
5299                    $self->{insertion_mode} = IN_ROW_IM;
5300                    if ($token->{tag_name} eq 'tr') {
5301                      !!!cp ('t204');
5302                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5303                      !!!nack ('t204');
5304                      !!!next-token;
5305                      next B;
5306                    } else {
5307                      !!!cp ('t205');
5308                      !!!insert-element ('tr',, $token);
5309                      ## reprocess in the "in row" insertion mode
5310                    }
5311                  } else {
5312                    !!!cp ('t206');
5313                }                }
5314    
5315                push @$active_formatting_elements, ['#marker', '']                ## Clear back to table row context
5316                  if $token->{tag_name} eq 'caption';                while (not ($self->{open_elements}->[-1]->[1]
5317                                  & TABLE_ROW_SCOPING_EL)) {
5318                    !!!cp ('t207');
5319                    pop @{$self->{open_elements}};
5320                  }
5321                  
5322                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5323                  $self->{insertion_mode} = IN_CELL_IM;
5324    
5325                !!!insert-element ($token->{tag_name}, $token->{attributes});                push @$active_formatting_elements, ['#marker', ''];
5326                $self->{insertion_mode} = {                
5327                                   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}};  
5328                !!!next-token;                !!!next-token;
5329                redo B;                next B;
5330              } elsif ({              } elsif ({
5331                        col => 1,                        caption => 1, col => 1, colgroup => 1,
5332                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
5333                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5334                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5335                ## Clear back to table context                if ($self->{insertion_mode} == IN_ROW_IM) {
5336                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
5337                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
5338                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  my $i;
5339                  pop @{$self->{open_elements}};                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                      my $node = $self->{open_elements}->[$_];
5341                      if ($node->[1] & TABLE_ROW_EL) {
5342                        !!!cp ('t208');
5343                        $i = $_;
5344                        last INSCOPE;
5345                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5346                        !!!cp ('t209');
5347                        last INSCOPE;
5348                      }
5349                    } # INSCOPE
5350                    unless (defined $i) {
5351                      !!!cp ('t210');
5352    ## TODO: This type is wrong.
5353                      !!!parse-error (type => 'unmacthed end tag',
5354                                      text => $token->{tag_name}, token => $token);
5355                      ## Ignore the token
5356                      !!!nack ('t210.1');
5357                      !!!next-token;
5358                      next B;
5359                    }
5360                    
5361                    ## Clear back to table row context
5362                    while (not ($self->{open_elements}->[-1]->[1]
5363                                    & TABLE_ROW_SCOPING_EL)) {
5364                      !!!cp ('t211');
5365                      ## ISSUE: Can this case be reached?
5366                      pop @{$self->{open_elements}};
5367                    }
5368                    
5369                    pop @{$self->{open_elements}}; # tr
5370                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                    if ($token->{tag_name} eq 'tr') {
5372                      !!!cp ('t212');
5373                      ## reprocess
5374                      !!!ack-later;
5375                      next B;
5376                    } else {
5377                      !!!cp ('t213');
5378                      ## reprocess in the "in table body" insertion mode...
5379                    }
5380                }                }
5381    
5382                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5383                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## have an element in table scope
5384                  ? 'in column group' : 'in table body';                  my $i;
5385                ## reprocess                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5386                redo B;                    my $node = $self->{open_elements}->[$_];
5387              } elsif ($token->{tag_name} eq 'table') {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5388                ## NOTE: There are code clones for this "table in table"                      !!!cp ('t214');
5389                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      $i = $_;
5390                        last INSCOPE;
5391                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5392                        !!!cp ('t215');
5393                        last INSCOPE;
5394                      }
5395                    } # INSCOPE
5396                    unless (defined $i) {
5397                      !!!cp ('t216');
5398    ## TODO: This erorr type is wrong.
5399                      !!!parse-error (type => 'unmatched end tag',
5400                                      text => $token->{tag_name}, token => $token);
5401                      ## Ignore the token
5402                      !!!nack ('t216.1');
5403                      !!!next-token;
5404                      next B;
5405                    }
5406    
5407                ## As if </table>                  ## Clear back to table body context
5408                ## have a table element in table scope                  while (not ($self->{open_elements}->[-1]->[1]
5409                my $i;                                  & TABLE_ROWS_SCOPING_EL)) {
5410                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    !!!cp ('t217');
5411                  my $node = $self->{open_elements}->[$_];                    ## ISSUE: Can this state be reached?
5412                  if ($node->[1] eq 'table') {                    pop @{$self->{open_elements}};
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5413                  }                  }
5414                } # INSCOPE                  
5415                unless (defined $i) {                  ## As if <{current node}>
5416                  !!!parse-error (type => 'unmatched end tag:table');                  ## have an element in table scope
5417                  ## Ignore tokens </table><table>                  ## true by definition
5418                  !!!next-token;                  
5419                  redo B;                  ## Clear back to table body context
5420                }                  ## nop by definition
5421                                  
5422                ## generate implied end tags                  pop @{$self->{open_elements}};
5423                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
5424                     dd => 1, dt => 1, li => 1, p => 1,                  ## reprocess in "in table" insertion mode...
5425                     td => 1, th => 1, tr => 1,                } else {
5426                     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;  
5427                }                }
5428    
5429                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($token->{tag_name} eq 'col') {
5430                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Clear back to table context
5431                    while (not ($self->{open_elements}->[-1]->[1]
5432                                    & TABLE_SCOPING_EL)) {
5433                      !!!cp ('t219');
5434                      ## ISSUE: Can this state be reached?
5435                      pop @{$self->{open_elements}};
5436                    }
5437                    
5438                    !!!insert-element ('colgroup',, $token);
5439                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5440                    ## reprocess
5441                    !!!ack-later;
5442                    next B;
5443                  } elsif ({
5444                            caption => 1,
5445                            colgroup => 1,
5446                            tbody => 1, tfoot => 1, thead => 1,
5447                           }->{$token->{tag_name}}) {
5448                    ## Clear back to table context
5449                    while (not ($self->{open_elements}->[-1]->[1]
5450                                    & TABLE_SCOPING_EL)) {
5451                      !!!cp ('t220');
5452                      ## ISSUE: Can this state be reached?
5453                      pop @{$self->{open_elements}};
5454                    }
5455                    
5456                    push @$active_formatting_elements, ['#marker', '']
5457                        if $token->{tag_name} eq 'caption';
5458                    
5459                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5460                    $self->{insertion_mode} = {
5461                                               caption => IN_CAPTION_IM,
5462                                               colgroup => IN_COLUMN_GROUP_IM,
5463                                               tbody => IN_TABLE_BODY_IM,
5464                                               tfoot => IN_TABLE_BODY_IM,
5465                                               thead => IN_TABLE_BODY_IM,
5466                                              }->{$token->{tag_name}};
5467                    !!!next-token;
5468                    !!!nack ('t220.1');
5469                    next B;
5470                  } else {
5471                    die "$0: in table: <>: $token->{tag_name}";
5472                }                }
5473                } elsif ($token->{tag_name} eq 'table') {
5474                  !!!parse-error (type => 'not closed',
5475                                  text => $self->{open_elements}->[-1]->[0]
5476                                      ->manakai_local_name,
5477                                  token => $token);
5478    
5479                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') {  
5480                ## have a table element in table scope                ## have a table element in table scope
5481                my $i;                my $i;
5482                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5483                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5484                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5485                      !!!cp ('t221');
5486                    $i = $_;                    $i = $_;
5487                    last INSCOPE;                    last INSCOPE;
5488                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5489                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5490                    last INSCOPE;                    last INSCOPE;
5491                  }                  }
5492                } # INSCOPE                } # INSCOPE
5493                unless (defined $i) {                unless (defined $i) {
5494                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t223');
5495                  ## Ignore the token  ## TODO: The following is wrong, maybe.
5496                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5497                                    token => $token);
5498                    ## Ignore tokens </table><table>
5499                    !!!nack ('t223.1');
5500                  !!!next-token;                  !!!next-token;
5501                  redo B;                  next B;
5502                }                }
5503                                
5504    ## TODO: Followings are removed from the latest spec.
5505                ## generate implied end tags                ## generate implied end tags
5506                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5507                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5508                     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;  
5509                }                }
5510    
5511                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5512                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5513                    ## NOTE: |<table><tr><table>|
5514                    !!!parse-error (type => 'not closed',
5515                                    text => $self->{open_elements}->[-1]->[0]
5516                                        ->manakai_local_name,
5517                                    token => $token);
5518                  } else {
5519                    !!!cp ('t226');
5520                }                }
5521    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524    
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526    
5527                !!!next-token;            ## reprocess
5528                redo B;            !!!ack-later;
5529              } elsif ({            next B;
5530                        body => 1, caption => 1, col => 1, colgroup => 1,          } elsif ($token->{tag_name} eq 'style') {
5531                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,            if (not $open_tables->[-1]->[1]) { # tainted
5532                        thead => 1, tr => 1,              !!!cp ('t227.8');
5533                       }->{$token->{tag_name}}) {              ## NOTE: This is a "as if in head" code clone.
5534                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              $parse_rcdata->(CDATA_CONTENT_MODEL);
5535                ## Ignore the token              next B;
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
5536            } else {            } else {
5537                !!!cp ('t227.7');
5538              #              #
5539            }            }
5540            } elsif ($token->{tag_name} eq 'script') {
5541            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
5542            $in_body->($insert_to_foster);              !!!cp ('t227.6');
5543            redo B;              ## NOTE: This is a "as if in head" code clone.
5544          } elsif ($self->{insertion_mode} eq 'in column group') {              $script_start_tag->();
5545            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 {  
               #  
             }  
5546            } else {            } else {
5547                !!!cp ('t227.5');
5548              #              #
5549            }            }
5550            } elsif ($token->{tag_name} eq 'input') {
5551              if (not $open_tables->[-1]->[1]) { # tainted
5552                if ($token->{attributes}->{type}) { ## TODO: case
5553                  my $type = lc $token->{attributes}->{type}->{value};
5554                  if ($type eq 'hidden') {
5555                    !!!cp ('t227.3');
5556                    !!!parse-error (type => 'in table',
5557                                    text => $token->{tag_name}, token => $token);
5558    
5559            ## 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;  
               }  
             }  
5560    
5561              !!!parse-error (type => 'in table:#character');                  ## TODO: form element pointer
5562    
5563              ## 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);  
5564    
5565              if ({                  !!!next-token;
5566                   table => 1, tbody => 1, tfoot => 1,                  !!!ack ('t227.2.1');
5567                   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});  
5568                } else {                } else {
5569                  $foster_parent_element->insert_before                  !!!cp ('t227.2');
5570                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
5571                }                }
5572              } else {              } else {
5573                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                !!!cp ('t227.1');
5574                  #
5575              }              }
5576                          } else {
5577              !!!next-token;              !!!cp ('t227.4');
5578              redo B;              #
5579            } elsif ($token->{type} eq 'start tag') {            }
5580              if ({          } else {
5581                   tr => 1,            !!!cp ('t227');
5582                   th => 1, td => 1,            #
5583                  }->{$token->{tag_name}}) {          }
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
5584    
5585                ## Clear back to table body context          !!!parse-error (type => 'in table', text => $token->{tag_name},
5586                while (not {                          token => $token);
5587                  tbody => 1, tfoot => 1, thead => 1, html => 1,  
5588                }->{$self->{open_elements}->[-1]->[1]}) {          $insert = $insert_to_foster;
5589                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          #
5590                  pop @{$self->{open_elements}};        } elsif ($token->{type} == END_TAG_TOKEN) {
5591                }              if ($token->{tag_name} eq 'tr' and
5592                                  $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}}) {  
5593                ## have an element in table scope                ## have an element in table scope
5594                my $i;                my $i;
5595                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5596                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5597                  if ({                  if ($node->[1] & TABLE_ROW_EL) {
5598                       tbody => 1, thead => 1, tfoot => 1,                    !!!cp ('t228');
                     }->{$node->[1]}) {  
5599                    $i = $_;                    $i = $_;
5600                    last INSCOPE;                    last INSCOPE;
5601                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5602                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5603                    last INSCOPE;                    last INSCOPE;
5604                  }                  }
5605                } # INSCOPE                } # INSCOPE
5606                unless (defined $i) {                unless (defined $i) {
5607                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5608                    !!!parse-error (type => 'unmatched end tag',
5609                                    text => $token->{tag_name}, token => $token);
5610                  ## Ignore the token                  ## Ignore the token
5611                    !!!nack ('t230.1');
5612                  !!!next-token;                  !!!next-token;
5613                  redo B;                  next B;
5614                  } else {
5615                    !!!cp ('t232');
5616                }                }
5617    
5618                ## Clear back to table body context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5621                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5622                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5624                }                }
5625    
5626                ## As if <{current node}>                pop @{$self->{open_elements}}; # tr
5627                ## have an element in table scope                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5628                ## true by definition                !!!next-token;
5629                  !!!nack ('t231.1');
5630                ## Clear back to table body context                next B;
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
5631              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5632                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5633                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5634                    ## have an element in table scope
5635                ## As if </table>                  my $i;
5636                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5637                my $i;                    my $node = $self->{open_elements}->[$_];
5638                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5639                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5640                  if ($node->[1] eq 'table') {                      $i = $_;
5641                    $i = $_;                      last INSCOPE;
5642                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5643                  } elsif ({                      !!!cp ('t234');
5644                            table => 1, html => 1,                      last INSCOPE;
5645                           }->{$node->[1]}) {                    }
5646                    last INSCOPE;                  } # INSCOPE
5647                    unless (defined $i) {
5648                      !!!cp ('t235');
5649    ## TODO: The following is wrong.
5650                      !!!parse-error (type => 'unmatched end tag',
5651                                      text => $token->{type}, token => $token);
5652                      ## Ignore the token
5653                      !!!nack ('t236.1');
5654                      !!!next-token;
5655                      next B;
5656                  }                  }
5657                } # INSCOPE                  
5658                unless (defined $i) {                  ## Clear back to table row context
5659                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5660                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5661                  !!!next-token;                    !!!cp ('t236');
5662                  redo B;  ## ISSUE: Can this state be reached?
5663                }                    pop @{$self->{open_elements}};
5664                                  }
5665                ## generate implied end tags                  
5666                if ({                  pop @{$self->{open_elements}}; # tr
5667                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5668                     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;  
5669                }                }
5670    
5671                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5672                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## have an element in table scope
5673                    my $i;
5674                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                      my $node = $self->{open_elements}->[$_];
5676                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5677                        !!!cp ('t237');
5678                        $i = $_;
5679                        last INSCOPE;
5680                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5681                        !!!cp ('t238');
5682                        last INSCOPE;
5683                      }
5684                    } # INSCOPE
5685                    unless (defined $i) {
5686                      !!!cp ('t239');
5687                      !!!parse-error (type => 'unmatched end tag',
5688                                      text => $token->{tag_name}, token => $token);
5689                      ## Ignore the token
5690                      !!!nack ('t239.1');
5691                      !!!next-token;
5692                      next B;
5693                    }
5694                    
5695                    ## Clear back to table body context
5696                    while (not ($self->{open_elements}->[-1]->[1]
5697                                    & TABLE_ROWS_SCOPING_EL)) {
5698                      !!!cp ('t240');
5699                      pop @{$self->{open_elements}};
5700                    }
5701                    
5702                    ## As if <{current node}>
5703                    ## have an element in table scope
5704                    ## true by definition
5705                    
5706                    ## Clear back to table body context
5707                    ## nop by definition
5708                    
5709                    pop @{$self->{open_elements}};
5710                    $self->{insertion_mode} = IN_TABLE_IM;
5711                    ## reprocess in the "in table" insertion mode...
5712                }                }
5713    
5714                splice @{$self->{open_elements}}, $i;                ## NOTE: </table> in the "in table" insertion mode.
5715                  ## When you edit the code fragment below, please ensure that
5716                $self->_reset_insertion_mode;                ## the code for <table> in the "in table" insertion mode
5717                  ## is synced with it.
5718    
5719                ## 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  
5720                my $i;                my $i;
5721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5723                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5724                      !!!cp ('t241');
5725                    $i = $_;                    $i = $_;
5726                    last INSCOPE;                    last INSCOPE;
5727                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5728                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5729                    last INSCOPE;                    last INSCOPE;
5730                  }                  }
5731                } # INSCOPE                } # INSCOPE
5732                unless (defined $i) {                unless (defined $i) {
5733                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5734                    !!!parse-error (type => 'unmatched end tag',
5735                                    text => $token->{tag_name}, token => $token);
5736                  ## Ignore the token                  ## Ignore the token
5737                    !!!nack ('t243.1');
5738                  !!!next-token;                  !!!next-token;
5739                  redo B;                  next B;
5740                }                }
5741                    
5742                ## Clear back to table body context                splice @{$self->{open_elements}}, $i;
5743                while (not {                pop @{$open_tables};
5744                  tbody => 1, tfoot => 1, thead => 1, html => 1,                
5745                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5746                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5747                  pop @{$self->{open_elements}};                !!!next-token;
5748                  next B;
5749                } elsif ({
5750                          tbody => 1, tfoot => 1, thead => 1,
5751                         }->{$token->{tag_name}} and
5752                         $self->{insertion_mode} & ROW_IMS) {
5753                  if ($self->{insertion_mode} == IN_ROW_IM) {
5754                    ## have an element in table scope
5755                    my $i;
5756                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5757                      my $node = $self->{open_elements}->[$_];
5758                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5759                        !!!cp ('t247');
5760                        $i = $_;
5761                        last INSCOPE;
5762                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5763                        !!!cp ('t248');
5764                        last INSCOPE;
5765                      }
5766                    } # INSCOPE
5767                      unless (defined $i) {
5768                        !!!cp ('t249');
5769                        !!!parse-error (type => 'unmatched end tag',
5770                                        text => $token->{tag_name}, token => $token);
5771                        ## Ignore the token
5772                        !!!nack ('t249.1');
5773                        !!!next-token;
5774                        next B;
5775                      }
5776                    
5777                    ## As if </tr>
5778                    ## have an element in table scope
5779                    my $i;
5780                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                      my $node = $self->{open_elements}->[$_];
5782                      if ($node->[1] & TABLE_ROW_EL) {
5783                        !!!cp ('t250');
5784                        $i = $_;
5785                        last INSCOPE;
5786                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5787                        !!!cp ('t251');
5788                        last INSCOPE;
5789                      }
5790                    } # INSCOPE
5791                      unless (defined $i) {
5792                        !!!cp ('t252');
5793                        !!!parse-error (type => 'unmatched end tag',
5794                                        text => 'tr', token => $token);
5795                        ## Ignore the token
5796                        !!!nack ('t252.1');
5797                        !!!next-token;
5798                        next B;
5799                      }
5800                    
5801                    ## Clear back to table row context
5802                    while (not ($self->{open_elements}->[-1]->[1]
5803                                    & TABLE_ROW_SCOPING_EL)) {
5804                      !!!cp ('t253');
5805    ## ISSUE: Can this case be reached?
5806                      pop @{$self->{open_elements}};
5807                    }
5808                    
5809                    pop @{$self->{open_elements}}; # tr
5810                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5811                    ## reprocess in the "in table body" insertion mode...
5812                }                }
5813    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
5814                ## have an element in table scope                ## have an element in table scope
5815                my $i;                my $i;
5816                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5817                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5818                  if ({                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5819                       tbody => 1, thead => 1, tfoot => 1,                    !!!cp ('t254');
                     }->{$node->[1]}) {  
5820                    $i = $_;                    $i = $_;
5821                    last INSCOPE;                    last INSCOPE;
5822                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5823                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5824                    last INSCOPE;                    last INSCOPE;
5825                  }                  }
5826                } # INSCOPE                } # INSCOPE
5827                unless (defined $i) {                unless (defined $i) {
5828                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5829                    !!!parse-error (type => 'unmatched end tag',
5830                                    text => $token->{tag_name}, token => $token);
5831                  ## Ignore the token                  ## Ignore the token
5832                    !!!nack ('t256.1');
5833                  !!!next-token;                  !!!next-token;
5834                  redo B;                  next B;
5835                }                }
5836    
5837                ## Clear back to table body context                ## Clear back to table body context
5838                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5839                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5840                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5841                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5843                }                }
5844    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
5845                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5846                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5847                ## reprocess                !!!nack ('t257.1');
5848                redo B;                !!!next-token;
5849                  next B;
5850              } elsif ({              } elsif ({
5851                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5852                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1,
5853                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5854                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5857                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5858                !!!next-token;                            text => $token->{tag_name}, token => $token);
5859                redo B;            ## Ignore the token
5860              } else {            !!!nack ('t258.1');
5861                #             !!!next-token;
5862              }            next B;
5863            } else {          } else {
5864              #            !!!cp ('t259');
5865            }            !!!parse-error (type => 'in table:/',
5866                                        text => $token->{tag_name}, token => $token);
5867            ## As if in table  
5868            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
5869            $in_body->($insert_to_foster);            #
5870            redo B;          }
5871          } elsif ($self->{insertion_mode} eq 'in row') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5872            if ($token->{type} eq 'character') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5873              ## NOTE: This is a "character in table" code clone.                  @{$self->{open_elements}} == 1) { # redundant, maybe
5874              !!!parse-error (type => 'in body:#eof', token => $token);
5875              !!!cp ('t259.1');
5876              #
5877            } else {
5878              !!!cp ('t259.2');
5879              #
5880            }
5881    
5882            ## Stop parsing
5883            last B;
5884          } else {
5885            die "$0: $token->{type}: Unknown token type";
5886          }
5887        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5888              if ($token->{type} == CHARACTER_TOKEN) {
5889              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5890                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
5891                unless (length $token->{data}) {                unless (length $token->{data}) {
5892                    !!!cp ('t260');
5893                  !!!next-token;                  !!!next-token;
5894                  redo B;                  next B;
5895                }                }
5896              }              }
   
             !!!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);  
5897                            
5898              if ({              !!!cp ('t261');
5899                   table => 1, tbody => 1, tfoot => 1,              #
5900                   thead => 1, tr => 1,            } elsif ($token->{type} == START_TAG_TOKEN) {
5901                  }->{$self->{open_elements}->[-1]->[1]}) {              if ($token->{tag_name} eq 'col') {
5902                # MUST                !!!cp ('t262');
5903                my $foster_parent_element;                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5904                my $next_sibling;                pop @{$self->{open_elements}};
5905                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', ''];  
                 
5906                !!!next-token;                !!!next-token;
5907                redo B;                next B;
5908              } elsif ({              } else {
5909                        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 {  
5910                #                #
5911              }              }
5912            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5913              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
5914                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5915                my $i;                  !!!cp ('t264');
5916                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
5917                  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});  
5918                  ## Ignore the token                  ## Ignore the token
5919                  !!!next-token;                  !!!next-token;
5920                  redo B;                  next B;
5921                }                } else {
5922                    !!!cp ('t265');
5923                ## As if </tr>                  pop @{$self->{open_elements}}; # colgroup
5924                ## 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  
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                  next B;            
5927                }                }
5928                } elsif ($token->{tag_name} eq 'col') {
5929                ## Clear back to table row context                !!!cp ('t266');
5930                while (not {                !!!parse-error (type => 'unmatched end tag',
5931                  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});  
5932                ## Ignore the token                ## Ignore the token
5933                !!!next-token;                !!!next-token;
5934                redo B;                next B;
5935              } else {              } else {
5936                #                !!!cp ('t267');
5937                  #
5938              }              }
5939            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5940              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5941            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5942              !!!cp ('t270.2');
5943              ## Stop parsing.
5944              last B;
5945            } else {
5946              ## NOTE: As if </colgroup>.
5947              !!!cp ('t270.1');
5948              pop @{$self->{open_elements}}; # colgroup
5949              $self->{insertion_mode} = IN_TABLE_IM;
5950              ## Reprocess.
5951              next B;
5952            }
5953          } else {
5954            die "$0: $token->{type}: Unknown token type";
5955          }
5956    
5957            ## As if in table            ## As if </colgroup>
5958            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5959            $in_body->($insert_to_foster);              !!!cp ('t269');
5960            redo B;  ## TODO: Wrong error type?
5961          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag',
5962            if ($token->{type} eq 'character') {                              text => 'colgroup', token => $token);
5963              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              ## Ignore the token
5964                !!!nack ('t269.1');
5965              !!!next-token;              !!!next-token;
5966              redo B;              next B;
5967            } elsif ($token->{type} eq 'start tag') {            } else {
5968              if ($token->{tag_name} eq 'option') {              !!!cp ('t270');
5969                if ($self->{open_elements}->[-1]->[1] eq 'option') {              pop @{$self->{open_elements}}; # colgroup
5970                  ## As if </option>              $self->{insertion_mode} = IN_TABLE_IM;
5971                  pop @{$self->{open_elements}};              !!!ack-later;
5972                }              ## reprocess
5973                next B;
5974              }
5975        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5976          if ($token->{type} == CHARACTER_TOKEN) {
5977            !!!cp ('t271');
5978            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5979            !!!next-token;
5980            next B;
5981          } elsif ($token->{type} == START_TAG_TOKEN) {
5982            if ($token->{tag_name} eq 'option') {
5983              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5984                !!!cp ('t272');
5985                ## As if </option>
5986                pop @{$self->{open_elements}};
5987              } else {
5988                !!!cp ('t273');
5989              }
5990    
5991                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5992                !!!next-token;            !!!nack ('t273.1');
5993                redo B;            !!!next-token;
5994              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5995                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5996                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5997                  pop @{$self->{open_elements}};              !!!cp ('t274');
5998                }              ## As if </option>
5999                pop @{$self->{open_elements}};
6000              } else {
6001                !!!cp ('t275');
6002              }
6003    
6004                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6005                  ## As if </optgroup>              !!!cp ('t276');
6006                  pop @{$self->{open_elements}};              ## As if </optgroup>
6007                }              pop @{$self->{open_elements}};
6008              } else {
6009                !!!cp ('t277');
6010              }
6011    
6012                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6013                !!!next-token;            !!!nack ('t277.1');
6014                redo B;            !!!next-token;
6015              } elsif ($token->{tag_name} eq 'select') {            next B;
6016                !!!parse-error (type => 'not closed:select');          } elsif ({
6017                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6018                ## have an element in table scope                   }->{$token->{tag_name}} or
6019                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6020                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6021                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6022                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6023                    $i = $_;                     tr => 1, td => 1, th => 1,
6024                    last INSCOPE;                    }->{$token->{tag_name}})) {
6025                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6026                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6027                           }->{$node->[1]}) {                            token => $token);
6028                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6029                  }            ## as if there were </select> (otherwise).
6030                } # INSCOPE            ## have an element in table scope
6031                unless (defined $i) {            my $i;
6032                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6034                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6035                  redo B;                !!!cp ('t278');
6036                }                $i = $_;
6037                  last INSCOPE;
6038                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6039                  !!!cp ('t279');
6040                  last INSCOPE;
6041                }
6042              } # INSCOPE
6043              unless (defined $i) {
6044                !!!cp ('t280');
6045                !!!parse-error (type => 'unmatched end tag',
6046                                text => 'select', token => $token);
6047                ## Ignore the token
6048                !!!nack ('t280.1');
6049                !!!next-token;
6050                next B;
6051              }
6052                                
6053                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6054              splice @{$self->{open_elements}}, $i;
6055    
6056                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6057    
6058                !!!next-token;            if ($token->{tag_name} eq 'select') {
6059                redo B;              !!!nack ('t281.2');
6060              } else {              !!!next-token;
6061                #              next B;
6062              } else {
6063                !!!cp ('t281.1');
6064                !!!ack-later;
6065                ## Reprocess the token.
6066                next B;
6067              }
6068            } else {
6069              !!!cp ('t282');
6070              !!!parse-error (type => 'in select',
6071                              text => $token->{tag_name}, token => $token);
6072              ## Ignore the token
6073              !!!nack ('t282.1');
6074              !!!next-token;
6075              next B;
6076            }
6077          } elsif ($token->{type} == END_TAG_TOKEN) {
6078            if ($token->{tag_name} eq 'optgroup') {
6079              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6080                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6081                !!!cp ('t283');
6082                ## As if </option>
6083                splice @{$self->{open_elements}}, -2;
6084              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6085                !!!cp ('t284');
6086                pop @{$self->{open_elements}};
6087              } else {
6088                !!!cp ('t285');
6089                !!!parse-error (type => 'unmatched end tag',
6090                                text => $token->{tag_name}, token => $token);
6091                ## Ignore the token
6092              }
6093              !!!nack ('t285.1');
6094              !!!next-token;
6095              next B;
6096            } elsif ($token->{tag_name} eq 'option') {
6097              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6098                !!!cp ('t286');
6099                pop @{$self->{open_elements}};
6100              } else {
6101                !!!cp ('t287');
6102                !!!parse-error (type => 'unmatched end tag',
6103                                text => $token->{tag_name}, token => $token);
6104                ## Ignore the token
6105              }
6106              !!!nack ('t287.1');
6107              !!!next-token;
6108              next B;
6109            } elsif ($token->{tag_name} eq 'select') {
6110              ## have an element in table scope
6111              my $i;
6112              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6113                my $node = $self->{open_elements}->[$_];
6114                if ($node->[1] & SELECT_EL) {
6115                  !!!cp ('t288');
6116                  $i = $_;
6117                  last INSCOPE;
6118                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6119                  !!!cp ('t289');
6120                  last INSCOPE;
6121              }              }
6122            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6123              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6124                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6125                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6126                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6127                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6128                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6129                  pop @{$self->{open_elements}};              !!!next-token;
6130                } else {              next B;
6131                  !!!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;  
               }  
6132                                
6133                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6134              splice @{$self->{open_elements}}, $i;
6135    
6136                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6137    
6138                !!!next-token;            !!!nack ('t291.1');
6139                redo B;            !!!next-token;
6140              } elsif ({            next B;
6141                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6142                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6143                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6144                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6145                                   }->{$token->{tag_name}}) {
6146                ## have an element in table scope  ## TODO: The following is wrong?
6147                my $i;            !!!parse-error (type => 'unmatched end tag',
6148                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;  
               }  
6149                                
6150                ## As if </select>            ## have an element in table scope
6151                ## have an element in table scope            my $i;
6152                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6153                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6154                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6155                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6156                    $i = $_;                $i = $_;
6157                    last INSCOPE;                last INSCOPE;
6158                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6159                            table => 1, html => 1,                !!!cp ('t293');
6160                           }->{$node->[1]}) {                last INSCOPE;
6161                    last INSCOPE;              }
6162                  }            } # INSCOPE
6163                } # INSCOPE            unless (defined $i) {
6164                unless (defined $i) {              !!!cp ('t294');
6165                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6166                  ## Ignore the </select> token              !!!nack ('t294.1');
6167                  !!!next-token; ## TODO: ok?              !!!next-token;
6168                  redo B;              next B;
6169                }            }
6170                                
6171                splice @{$self->{open_elements}}, $i;            ## As if </select>
6172              ## have an element in table scope
6173                $self->_reset_insertion_mode;            undef $i;
6174              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175                ## reprocess              my $node = $self->{open_elements}->[$_];
6176                redo B;              if ($node->[1] & SELECT_EL) {
6177              } else {                !!!cp ('t295');
6178                #                $i = $_;
6179                  last INSCOPE;
6180                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6181    ## ISSUE: Can this state be reached?
6182                  !!!cp ('t296');
6183                  last INSCOPE;
6184              }              }
6185            } else {            } # INSCOPE
6186              #            unless (defined $i) {
6187                !!!cp ('t297');
6188    ## TODO: The following error type is correct?
6189                !!!parse-error (type => 'unmatched end tag',
6190                                text => 'select', token => $token);
6191                ## Ignore the </select> token
6192                !!!nack ('t297.1');
6193                !!!next-token; ## TODO: ok?
6194                next B;
6195            }            }
6196                  
6197              !!!cp ('t298');
6198              splice @{$self->{open_elements}}, $i;
6199    
6200              $self->_reset_insertion_mode;
6201    
6202            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6203              ## reprocess
6204              next B;
6205            } else {
6206              !!!cp ('t299');
6207              !!!parse-error (type => 'in select:/',
6208                              text => $token->{tag_name}, token => $token);
6209            ## Ignore the token            ## Ignore the token
6210              !!!nack ('t299.3');
6211            !!!next-token;            !!!next-token;
6212            redo B;            next B;
6213          } elsif ($self->{insertion_mode} eq 'after body') {          }
6214            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6215              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6216                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6217                ## As if in body            !!!cp ('t299.1');
6218                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6219            } else {
6220              !!!cp ('t299.2');
6221            }
6222    
6223            ## Stop parsing.
6224            last B;
6225          } else {
6226            die "$0: $token->{type}: Unknown token type";
6227          }
6228        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6229          if ($token->{type} == CHARACTER_TOKEN) {
6230            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6231              my $data = $1;
6232              ## As if in body
6233              $reconstruct_active_formatting_elements->($insert_to_current);
6234                                
6235                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6236              
6237              unless (length $token->{data}) {
6238                !!!cp ('t300');
6239                !!!next-token;
6240                next B;
6241              }
6242            }
6243            
6244            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245              !!!cp ('t301');
6246              !!!parse-error (type => 'after html:#text', token => $token);
6247    
6248                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6249                  !!!next-token;          } else {
6250                  redo B;            !!!cp ('t302');
6251                }          }
6252              }          
6253                        ## "after body" insertion mode
6254              #          !!!parse-error (type => 'after body:#text', token => $token);
6255              !!!parse-error (type => 'after body:#character');  
6256            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6257              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6258              #          next B;
6259            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6260              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6261                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6262                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html',
6263                  ## Ignore the token                            text => $token->{tag_name}, token => $token);
6264                  !!!next-token;            
6265                  redo B;            ## Reprocess in the "after body" insertion mode.
6266                } else {          } else {
6267                  $previous_insertion_mode = $self->{insertion_mode};            !!!cp ('t304');
6268                  $self->{insertion_mode} = 'trailing end';          }
6269                  !!!next-token;  
6270                  redo B;          ## "after body" insertion mode
6271                }          !!!parse-error (type => 'after body',
6272              } else {                          text => $token->{tag_name}, token => $token);
6273                !!!parse-error (type => 'after body:/'.$token->{tag_name});  
6274              }          $self->{insertion_mode} = IN_BODY_IM;
6275            !!!ack-later;
6276            ## reprocess
6277            next B;
6278          } elsif ($token->{type} == END_TAG_TOKEN) {
6279            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6280              !!!cp ('t305');
6281              !!!parse-error (type => 'after html:/',
6282                              text => $token->{tag_name}, token => $token);
6283              
6284              $self->{insertion_mode} = AFTER_BODY_IM;
6285              ## Reprocess in the "after body" insertion mode.
6286            } else {
6287              !!!cp ('t306');
6288            }
6289    
6290            ## "after body" insertion mode
6291            if ($token->{tag_name} eq 'html') {
6292              if (defined $self->{inner_html_node}) {
6293                !!!cp ('t307');
6294                !!!parse-error (type => 'unmatched end tag',
6295                                text => 'html', token => $token);
6296                ## Ignore the token
6297                !!!next-token;
6298                next B;
6299            } else {            } else {
6300              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6301                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6302                !!!next-token;
6303                next B;
6304            }            }
6305            } else {
6306              !!!cp ('t309');
6307              !!!parse-error (type => 'after body:/',
6308                              text => $token->{tag_name}, token => $token);
6309    
6310            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6311            ## reprocess            ## reprocess
6312            redo B;            next B;
6313      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6314        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6315            !!!cp ('t309.2');
6316            ## Stop parsing
6317            last B;
6318          } else {
6319            die "$0: $token->{type}: Unknown token type";
6320          }
6321        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6322          if ($token->{type} == CHARACTER_TOKEN) {
6323          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6324            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6325              
6326            unless (length $token->{data}) {            unless (length $token->{data}) {
6327                !!!cp ('t310');
6328              !!!next-token;              !!!next-token;
6329              redo B;              next B;
6330            }            }
6331          }          }
6332            
6333          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6334          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6335          !!!next-token;              !!!cp ('t311');
6336          redo B;              !!!parse-error (type => 'in frameset:#text', token => $token);
6337        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6338          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6339            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#text', token => $token);
6340              } else { # "after after frameset"
6341                !!!cp ('t313');
6342                !!!parse-error (type => 'after html:#text', token => $token);
6343              }
6344              
6345              ## Ignore the token.
6346              if (length $token->{data}) {
6347                !!!cp ('t314');
6348                ## reprocess the rest of characters
6349              } else {
6350                !!!cp ('t315');
6351                !!!next-token;
6352              }
6353              next B;
6354            }
6355            
6356            die qq[$0: Character "$token->{data}"];
6357          } elsif ($token->{type} == START_TAG_TOKEN) {
6358            if ($token->{tag_name} eq 'frameset' and
6359                $self->{insertion_mode} == IN_FRAMESET_IM) {
6360              !!!cp ('t318');
6361              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6362              !!!nack ('t318.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6366            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6367              !!!cp ('t319');
6368              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6369            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6370              !!!ack ('t319.1');
6371            !!!next-token;            !!!next-token;
6372            redo B;            next B;
6373          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6374            ## NOTE: As if in body.            !!!cp ('t320');
6375            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6376            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6377          } else {            next B;
6378            !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
6379              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6380              ## has no parse error.
6381            } else {
6382              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6383                !!!cp ('t321');
6384                !!!parse-error (type => 'in frameset',
6385                                text => $token->{tag_name}, token => $token);
6386              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6387                !!!cp ('t322');
6388                !!!parse-error (type => 'after frameset',
6389                                text => $token->{tag_name}, token => $token);
6390              } else { # "after after frameset"
6391                !!!cp ('t322.2');
6392                !!!parse-error (type => 'after after frameset',
6393                                text => $token->{tag_name}, token => $token);
6394              }
6395            ## Ignore the token            ## Ignore the token
6396              !!!nack ('t322.1');
6397            !!!next-token;            !!!next-token;
6398            redo B;            next B;
6399          }          }
6400        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6401          if ($token->{tag_name} eq 'frameset') {          if ($token->{tag_name} eq 'frameset' and
6402            if ($self->{open_elements}->[-1]->[1] eq 'html' and              $self->{insertion_mode} == IN_FRAMESET_IM) {
6403              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6404                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6405              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6406                !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408              ## Ignore the token              ## Ignore the token
6409              !!!next-token;              !!!next-token;
6410            } else {            } else {
6411                !!!cp ('t326');
6412              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6413              !!!next-token;              !!!next-token;
6414            }            }
6415    
6416            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6417                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6418              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6419                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6420              } else {
6421                !!!cp ('t328');
6422            }            }
6423            redo B;            next B;
6424            } elsif ($token->{tag_name} eq 'html' and
6425                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6426              !!!cp ('t329');
6427              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6428              !!!next-token;
6429              next B;
6430          } else {          } else {
6431            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6432                !!!cp ('t330');
6433                !!!parse-error (type => 'in frameset:/',
6434                                text => $token->{tag_name}, token => $token);
6435              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6436                !!!cp ('t330.1');
6437                !!!parse-error (type => 'after frameset:/',
6438                                text => $token->{tag_name}, token => $token);
6439              } else { # "after after html"
6440                !!!cp ('t331');
6441                !!!parse-error (type => 'after after frameset:/',
6442                                text => $token->{tag_name}, token => $token);
6443              }
6444            ## Ignore the token            ## Ignore the token
6445            !!!next-token;            !!!next-token;
6446            redo B;            next B;
6447            }
6448          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6449            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6450                    @{$self->{open_elements}} == 1) { # redundant, maybe
6451              !!!cp ('t331.1');
6452              !!!parse-error (type => 'in body:#eof', token => $token);
6453            } else {
6454              !!!cp ('t331.2');
6455          }          }
6456            
6457            ## Stop parsing
6458            last B;
6459        } else {        } else {
6460          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6461        }        }
     } 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);  
6462    
6463                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6464                  !!!next-token;      } else {
6465                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6466                }      }
             }  
6467    
6468              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6469                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6470          if ($token->{tag_name} eq 'script') {
6471            !!!cp ('t332');
6472            ## NOTE: This is an "as if in head" code clone
6473            $script_start_tag->();
6474            next B;
6475          } elsif ($token->{tag_name} eq 'style') {
6476            !!!cp ('t333');
6477            ## NOTE: This is an "as if in head" code clone
6478            $parse_rcdata->(CDATA_CONTENT_MODEL);
6479            next B;
6480          } elsif ({
6481                    base => 1, link => 1,
6482                   }->{$token->{tag_name}}) {
6483            !!!cp ('t334');
6484            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6485            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6486            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6487            !!!ack ('t334.1');
6488            !!!next-token;
6489            next B;
6490          } elsif ($token->{tag_name} eq 'meta') {
6491            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6492            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6493            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6494    
6495                ## Ignore the token.          unless ($self->{confident}) {
6496                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6497                  ## reprocess the rest of characters              !!!cp ('t335');
6498                } else {              ## NOTE: Whether the encoding is supported or not is handled
6499                  !!!next-token;              ## in the {change_encoding} callback.
6500                }              $self->{change_encoding}
6501                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6502                
6503                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6504                    ->set_user_data (manakai_has_reference =>
6505                                         $token->{attributes}->{charset}
6506                                             ->{has_reference});
6507              } elsif ($token->{attributes}->{content}) {
6508                if ($token->{attributes}->{content}->{value}
6509                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6510                        [\x09-\x0D\x20]*=
6511                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6512                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6513                  !!!cp ('t336');
6514                  ## NOTE: Whether the encoding is supported or not is handled
6515                  ## in the {change_encoding} callback.
6516                  $self->{change_encoding}
6517                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
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            } else {
6525              if ($token->{attributes}->{charset}) {
6526                !!!cp ('t337');
6527                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6528                    ->set_user_data (manakai_has_reference =>
6529                                         $token->{attributes}->{charset}
6530                                             ->{has_reference});
6531              }
6532              if ($token->{attributes}->{content}) {
6533                !!!cp ('t338');
6534                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6535                    ->set_user_data (manakai_has_reference =>
6536                                         $token->{attributes}->{content}
6537                                             ->{has_reference});
6538              }
6539            }
6540    
6541          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6542        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6543          if ($token->{tag_name} eq 'noframes') {          next B;
6544            ## NOTE: As if in body.        } elsif ($token->{tag_name} eq 'title') {
6545            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          !!!cp ('t341');
6546            redo B;          ## NOTE: This is an "as if in head" code clone
6547            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6548            next B;
6549          } elsif ($token->{tag_name} eq 'body') {
6550            !!!parse-error (type => 'in body', text => 'body', token => $token);
6551                  
6552            if (@{$self->{open_elements}} == 1 or
6553                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6554              !!!cp ('t342');
6555              ## Ignore the token
6556          } else {          } else {
6557            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6558              for my $attr_name (keys %{$token->{attributes}}) {
6559                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6560                  !!!cp ('t343');
6561                  $body_el->set_attribute_ns
6562                    (undef, [undef, $attr_name],
6563                     $token->{attributes}->{$attr_name}->{value});
6564                }
6565              }
6566            }
6567            !!!nack ('t343.1');
6568            !!!next-token;
6569            next B;
6570          } elsif ({
6571                    address => 1, blockquote => 1, center => 1, dir => 1,
6572                    div => 1, dl => 1, fieldset => 1,
6573                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6574                    menu => 1, ol => 1, p => 1, ul => 1,
6575                    pre => 1, listing => 1,
6576                    form => 1,
6577                    table => 1,
6578                    hr => 1,
6579                   }->{$token->{tag_name}}) {
6580            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6581              !!!cp ('t350');
6582              !!!parse-error (type => 'in form:form', token => $token);
6583            ## Ignore the token            ## Ignore the token
6584              !!!nack ('t350.1');
6585            !!!next-token;            !!!next-token;
6586            redo B;            next B;
6587          }          }
6588        } elsif ($token->{type} eq 'end tag') {  
6589          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6590            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6591            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6592                !!!cp ('t344');
6593                !!!back-token; # <form>
6594                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6595                          line => $token->{line}, column => $token->{column}};
6596                next B;
6597              } elsif ($_->[1] & SCOPING_EL) {
6598                !!!cp ('t345');
6599                last INSCOPE;
6600              }
6601            } # INSCOPE
6602              
6603            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6604            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6605              !!!nack ('t346.1');
6606              !!!next-token;
6607              if ($token->{type} == CHARACTER_TOKEN) {
6608                $token->{data} =~ s/^\x0A//;
6609                unless (length $token->{data}) {
6610                  !!!cp ('t346');
6611                  !!!next-token;
6612                } else {
6613                  !!!cp ('t349');
6614                }
6615              } else {
6616                !!!cp ('t348');
6617              }
6618            } elsif ($token->{tag_name} eq 'form') {
6619              !!!cp ('t347.1');
6620              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6621    
6622              !!!nack ('t347.2');
6623              !!!next-token;
6624            } elsif ($token->{tag_name} eq 'table') {
6625              !!!cp ('t382');
6626              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6627              
6628              $self->{insertion_mode} = IN_TABLE_IM;
6629    
6630              !!!nack ('t382.1');
6631              !!!next-token;
6632            } elsif ($token->{tag_name} eq 'hr') {
6633              !!!cp ('t386');
6634              pop @{$self->{open_elements}};
6635            
6636              !!!nack ('t386.1');
6637            !!!next-token;            !!!next-token;
           redo B;  
6638          } else {          } else {
6639            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
           ## Ignore the token  
6640            !!!next-token;            !!!next-token;
           redo B;  
6641          }          }
6642        } else {          next B;
6643          die "$0: $token->{type}: Unknown token type";        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6644        }          ## has a p element in scope
6645            INSCOPE: for (reverse @{$self->{open_elements}}) {
6646              if ($_->[1] & P_EL) {
6647                !!!cp ('t353');
6648                !!!back-token; # <x>
6649                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6650                          line => $token->{line}, column => $token->{column}};
6651                next B;
6652              } elsif ($_->[1] & SCOPING_EL) {
6653                !!!cp ('t354');
6654                last INSCOPE;
6655              }
6656            } # INSCOPE
6657              
6658            ## Step 1
6659            my $i = -1;
6660            my $node = $self->{open_elements}->[$i];
6661            my $li_or_dtdd = {li => {li => 1},
6662                              dt => {dt => 1, dd => 1},
6663                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6664            LI: {
6665              ## Step 2
6666              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6667                if ($i != -1) {
6668                  !!!cp ('t355');
6669                  !!!parse-error (type => 'not closed',
6670                                  text => $self->{open_elements}->[-1]->[0]
6671                                      ->manakai_local_name,
6672                                  token => $token);
6673                } else {
6674                  !!!cp ('t356');
6675                }
6676                splice @{$self->{open_elements}}, $i;
6677                last LI;
6678              } else {
6679                !!!cp ('t357');
6680              }
6681              
6682              ## Step 3
6683              if (not ($node->[1] & FORMATTING_EL) and
6684                  #not $phrasing_category->{$node->[1]} and
6685                  ($node->[1] & SPECIAL_EL or
6686                   $node->[1] & SCOPING_EL) and
6687                  not ($node->[1] & ADDRESS_EL) and
6688                  not ($node->[1] & DIV_EL)) {
6689                !!!cp ('t358');
6690                last LI;
6691              }
6692              
6693              !!!cp ('t359');
6694              ## Step 4
6695              $i--;
6696              $node = $self->{open_elements}->[$i];
6697              redo LI;
6698            } # LI
6699              
6700            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6701            !!!nack ('t359.1');
6702            !!!next-token;
6703            next B;
6704          } elsif ($token->{tag_name} eq 'plaintext') {
6705            ## has a p element in scope
6706            INSCOPE: for (reverse @{$self->{open_elements}}) {
6707              if ($_->[1] & P_EL) {
6708                !!!cp ('t367');
6709                !!!back-token; # <plaintext>
6710                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6711                          line => $token->{line}, column => $token->{column}};
6712                next B;
6713              } elsif ($_->[1] & SCOPING_EL) {
6714                !!!cp ('t368');
6715                last INSCOPE;
6716              }
6717            } # INSCOPE
6718              
6719            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6720              
6721            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6722              
6723            !!!nack ('t368.1');
6724            !!!next-token;
6725            next B;
6726          } elsif ($token->{tag_name} eq 'a') {
6727            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6728              my $node = $active_formatting_elements->[$i];
6729              if ($node->[1] & A_EL) {
6730                !!!cp ('t371');
6731                !!!parse-error (type => 'in a:a', token => $token);
6732                
6733                !!!back-token; # <a>
6734                $token = {type => END_TAG_TOKEN, tag_name => 'a',
6735                          line => $token->{line}, column => $token->{column}};
6736                $formatting_end_tag->($token);
6737                
6738                AFE2: for (reverse 0..$#$active_formatting_elements) {
6739                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6740                    !!!cp ('t372');
6741                    splice @$active_formatting_elements, $_, 1;
6742                    last AFE2;
6743                  }
6744                } # AFE2
6745                OE: for (reverse 0..$#{$self->{open_elements}}) {
6746                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6747                    !!!cp ('t373');
6748                    splice @{$self->{open_elements}}, $_, 1;
6749                    last OE;
6750                  }
6751                } # OE
6752                last AFE;
6753              } elsif ($node->[0] eq '#marker') {
6754                !!!cp ('t374');
6755                last AFE;
6756              }
6757            } # AFE
6758              
6759            $reconstruct_active_formatting_elements->($insert_to_current);
6760    
6761        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6762      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
6763        ## states in the main stage is preserved yet # MUST  
6764                  !!!nack ('t374.1');
6765        if ($token->{type} eq 'character') {          !!!next-token;
6766          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          next B;
6767            my $data = $1;        } elsif ($token->{tag_name} eq 'nobr') {
6768            ## As if in the main phase.          $reconstruct_active_formatting_elements->($insert_to_current);
6769            ## NOTE: The insertion mode in the main phase  
6770            ## just before the phase has been changed to the trailing          ## has a |nobr| element in scope
6771            ## end phase is either "after body" or "after frameset".          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6772            $reconstruct_active_formatting_elements->($insert_to_current);            my $node = $self->{open_elements}->[$_];
6773              if ($node->[1] & NOBR_EL) {
6774                !!!cp ('t376');
6775                !!!parse-error (type => 'in nobr:nobr', token => $token);
6776                !!!back-token; # <nobr>
6777                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6778                          line => $token->{line}, column => $token->{column}};
6779                next B;
6780              } elsif ($node->[1] & SCOPING_EL) {
6781                !!!cp ('t377');
6782                last INSCOPE;
6783              }
6784            } # INSCOPE
6785            
6786            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787            push @$active_formatting_elements, $self->{open_elements}->[-1];
6788            
6789            !!!nack ('t377.1');
6790            !!!next-token;
6791            next B;
6792          } elsif ($token->{tag_name} eq 'button') {
6793            ## has a button element in scope
6794            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6795              my $node = $self->{open_elements}->[$_];
6796              if ($node->[1] & BUTTON_EL) {
6797                !!!cp ('t378');
6798                !!!parse-error (type => 'in button:button', token => $token);
6799                !!!back-token; # <button>
6800                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6801                          line => $token->{line}, column => $token->{column}};
6802                next B;
6803              } elsif ($node->[1] & SCOPING_EL) {
6804                !!!cp ('t379');
6805                last INSCOPE;
6806              }
6807            } # INSCOPE
6808                        
6809            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
6810                        
6811            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6812    
6813            ## TODO: associate with $self->{form_element} if defined
6814    
6815            push @$active_formatting_elements, ['#marker', ''];
6816    
6817            !!!nack ('t379.1');
6818            !!!next-token;
6819            next B;
6820          } elsif ({
6821                    xmp => 1,
6822                    iframe => 1,
6823                    noembed => 1,
6824                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6825                    noscript => 0, ## TODO: 1 if scripting is enabled
6826                   }->{$token->{tag_name}}) {
6827            if ($token->{tag_name} eq 'xmp') {
6828              !!!cp ('t381');
6829              $reconstruct_active_formatting_elements->($insert_to_current);
6830            } else {
6831              !!!cp ('t399');
6832            }
6833            ## NOTE: There is an "as if in body" code clone.
6834            $parse_rcdata->(CDATA_CONTENT_MODEL);
6835            next B;
6836          } elsif ($token->{tag_name} eq 'isindex') {
6837            !!!parse-error (type => 'isindex', token => $token);
6838            
6839            if (defined $self->{form_element}) {
6840              !!!cp ('t389');
6841              ## Ignore the token
6842              !!!nack ('t389'); ## NOTE: Not acknowledged.
6843              !!!next-token;
6844              next B;
6845            } else {
6846              !!!ack ('t391.1');
6847    
6848              my $at = $token->{attributes};
6849              my $form_attrs;
6850              $form_attrs->{action} = $at->{action} if $at->{action};
6851              my $prompt_attr = $at->{prompt};
6852              $at->{name} = {name => 'name', value => 'isindex'};
6853              delete $at->{action};
6854              delete $at->{prompt};
6855              my @tokens = (
6856                            {type => START_TAG_TOKEN, tag_name => 'form',
6857                             attributes => $form_attrs,
6858                             line => $token->{line}, column => $token->{column}},
6859                            {type => START_TAG_TOKEN, tag_name => 'hr',
6860                             line => $token->{line}, column => $token->{column}},
6861                            {type => START_TAG_TOKEN, tag_name => 'p',
6862                             line => $token->{line}, column => $token->{column}},
6863                            {type => START_TAG_TOKEN, tag_name => 'label',
6864                             line => $token->{line}, column => $token->{column}},
6865                           );
6866              if ($prompt_attr) {
6867                !!!cp ('t390');
6868                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6869                               #line => $token->{line}, column => $token->{column},
6870                              };
6871              } else {
6872                !!!cp ('t391');
6873                push @tokens, {type => CHARACTER_TOKEN,
6874                               data => 'This is a searchable index. Insert your search keywords here: ',
6875                               #line => $token->{line}, column => $token->{column},
6876                              }; # SHOULD
6877                ## TODO: make this configurable
6878              }
6879              push @tokens,
6880                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6881                             line => $token->{line}, column => $token->{column}},
6882                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6883                            {type => END_TAG_TOKEN, tag_name => 'label',
6884                             line => $token->{line}, column => $token->{column}},
6885                            {type => END_TAG_TOKEN, tag_name => 'p',
6886                             line => $token->{line}, column => $token->{column}},
6887                            {type => START_TAG_TOKEN, tag_name => 'hr',
6888                             line => $token->{line}, column => $token->{column}},
6889                            {type => END_TAG_TOKEN, tag_name => 'form',
6890                             line => $token->{line}, column => $token->{column}};
6891              !!!back-token (@tokens);
6892              !!!next-token;
6893              next B;
6894            }
6895          } elsif ($token->{tag_name} eq 'textarea') {
6896            my $tag_name = $token->{tag_name};
6897            my $el;
6898            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6899            
6900            ## TODO: $self->{form_element} if defined
6901            $self->{content_model} = RCDATA_CONTENT_MODEL;
6902            delete $self->{escape}; # MUST
6903            
6904            $insert->($el);
6905            
6906            my $text = '';
6907            !!!nack ('t392.1');
6908            !!!next-token;
6909            if ($token->{type} == CHARACTER_TOKEN) {
6910              $token->{data} =~ s/^\x0A//;
6911            unless (length $token->{data}) {            unless (length $token->{data}) {
6912                !!!cp ('t392');
6913              !!!next-token;              !!!next-token;
6914              redo B;            } else {
6915                !!!cp ('t393');
6916            }            }
6917            } else {
6918              !!!cp ('t394');
6919            }
6920            while ($token->{type} == CHARACTER_TOKEN) {
6921              !!!cp ('t395');
6922              $text .= $token->{data};
6923              !!!next-token;
6924            }
6925            if (length $text) {
6926              !!!cp ('t396');
6927              $el->manakai_append_text ($text);
6928            }
6929            
6930            $self->{content_model} = PCDATA_CONTENT_MODEL;
6931            
6932            if ($token->{type} == END_TAG_TOKEN and
6933                $token->{tag_name} eq $tag_name) {
6934              !!!cp ('t397');
6935              ## Ignore the token
6936            } else {
6937              !!!cp ('t398');
6938              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6939          }          }
6940            !!!next-token;
6941            next B;
6942          } elsif ($token->{tag_name} eq 'rt' or
6943                   $token->{tag_name} eq 'rp') {
6944            ## has a |ruby| element in scope
6945            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946              my $node = $self->{open_elements}->[$_];
6947              if ($node->[1] & RUBY_EL) {
6948                !!!cp ('t398.1');
6949                ## generate implied end tags
6950                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6951                  !!!cp ('t398.2');
6952                  pop @{$self->{open_elements}};
6953                }
6954                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6955                  !!!cp ('t398.3');
6956                  !!!parse-error (type => 'not closed',
6957                                  text => $self->{open_elements}->[-1]->[0]
6958                                      ->manakai_local_name,
6959                                  token => $token);
6960                  pop @{$self->{open_elements}}
6961                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6962                }
6963                last INSCOPE;
6964              } elsif ($node->[1] & SCOPING_EL) {
6965                !!!cp ('t398.4');
6966                last INSCOPE;
6967              }
6968            } # INSCOPE
6969    
6970          !!!parse-error (type => 'after html:#character');          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6971          $self->{insertion_mode} = $previous_insertion_mode;  
6972          ## reprocess          !!!nack ('t398.5');
6973          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  
6974          redo B;          redo B;
6975          } elsif ($token->{tag_name} eq 'math' or
6976                   $token->{tag_name} eq 'svg') {
6977            $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6980    
6981            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6982    
6983            ## "adjust foreign attributes" - done in insert-element-f
6984            
6985            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6986            
6987            if ($self->{self_closing}) {
6988              pop @{$self->{open_elements}};
6989              !!!ack ('t398.1');
6990            } else {
6991              !!!cp ('t398.2');
6992              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6993              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6994              ## mode, "in body" (not "in foreign content") secondary insertion
6995              ## mode, maybe.
6996            }
6997    
6998            !!!next-token;
6999            next B;
7000          } elsif ({
7001                    caption => 1, col => 1, colgroup => 1, frame => 1,
7002                    frameset => 1, head => 1, option => 1, optgroup => 1,
7003                    tbody => 1, td => 1, tfoot => 1, th => 1,
7004                    thead => 1, tr => 1,
7005                   }->{$token->{tag_name}}) {
7006            !!!cp ('t401');
7007            !!!parse-error (type => 'in body',
7008                            text => $token->{tag_name}, token => $token);
7009            ## Ignore the token
7010            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7011            !!!next-token;
7012            next B;
7013            
7014            ## ISSUE: An issue on HTML5 new elements in the spec.
7015        } else {        } else {
7016          die "$0: $token->{type}: Unknown token";          if ($token->{tag_name} eq 'image') {
7017              !!!cp ('t384');
7018              !!!parse-error (type => 'image', token => $token);
7019              $token->{tag_name} = 'img';
7020            } else {
7021              !!!cp ('t385');
7022            }
7023    
7024            ## NOTE: There is an "as if <br>" code clone.
7025            $reconstruct_active_formatting_elements->($insert_to_current);
7026            
7027            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7028    
7029            if ({
7030                 applet => 1, marquee => 1, object => 1,
7031                }->{$token->{tag_name}}) {
7032              !!!cp ('t380');
7033              push @$active_formatting_elements, ['#marker', ''];
7034              !!!nack ('t380.1');
7035            } elsif ({
7036                      b => 1, big => 1, em => 1, font => 1, i => 1,
7037                      s => 1, small => 1, strile => 1,
7038                      strong => 1, tt => 1, u => 1,
7039                     }->{$token->{tag_name}}) {
7040              !!!cp ('t375');
7041              push @$active_formatting_elements, $self->{open_elements}->[-1];
7042              !!!nack ('t375.1');
7043            } elsif ($token->{tag_name} eq 'input') {
7044              !!!cp ('t388');
7045              ## TODO: associate with $self->{form_element} if defined
7046              pop @{$self->{open_elements}};
7047              !!!ack ('t388.2');
7048            } elsif ({
7049                      area => 1, basefont => 1, bgsound => 1, br => 1,
7050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7051                      #image => 1,
7052                     }->{$token->{tag_name}}) {
7053              !!!cp ('t388.1');
7054              pop @{$self->{open_elements}};
7055              !!!ack ('t388.3');
7056            } elsif ($token->{tag_name} eq 'select') {
7057              ## TODO: associate with $self->{form_element} if defined
7058            
7059              if ($self->{insertion_mode} & TABLE_IMS or
7060                  $self->{insertion_mode} & BODY_TABLE_IMS or
7061                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7062                !!!cp ('t400.1');
7063                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7064              } else {
7065                !!!cp ('t400.2');
7066                $self->{insertion_mode} = IN_SELECT_IM;
7067              }
7068              !!!nack ('t400.3');
7069            } else {
7070              !!!nack ('t402');
7071            }
7072            
7073            !!!next-token;
7074            next B;
7075          }
7076        } elsif ($token->{type} == END_TAG_TOKEN) {
7077          if ($token->{tag_name} eq 'body') {
7078            ## has a |body| element in scope
7079            my $i;
7080            INSCOPE: {
7081              for (reverse @{$self->{open_elements}}) {
7082                if ($_->[1] & BODY_EL) {
7083                  !!!cp ('t405');
7084                  $i = $_;
7085                  last INSCOPE;
7086                } elsif ($_->[1] & SCOPING_EL) {
7087                  !!!cp ('t405.1');
7088                  last;
7089                }
7090              }
7091    
7092              !!!parse-error (type => 'start tag not allowed',
7093                              text => $token->{tag_name}, token => $token);
7094              ## NOTE: Ignore the token.
7095              !!!next-token;
7096              next B;
7097            } # INSCOPE
7098    
7099            for (@{$self->{open_elements}}) {
7100              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7101                !!!cp ('t403');
7102                !!!parse-error (type => 'not closed',
7103                                text => $_->[0]->manakai_local_name,
7104                                token => $token);
7105                last;
7106              } else {
7107                !!!cp ('t404');
7108              }
7109            }
7110    
7111            $self->{insertion_mode} = AFTER_BODY_IM;
7112            !!!next-token;
7113            next B;
7114          } elsif ($token->{tag_name} eq 'html') {
7115            ## TODO: Update this code.  It seems that the code below is not
7116            ## up-to-date, though it has same effect as speced.
7117            if (@{$self->{open_elements}} > 1 and
7118                $self->{open_elements}->[1]->[1] & BODY_EL) {
7119              ## ISSUE: There is an issue in the spec.
7120              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7121                !!!cp ('t406');
7122                !!!parse-error (type => 'not closed',
7123                                text => $self->{open_elements}->[1]->[0]
7124                                    ->manakai_local_name,
7125                                token => $token);
7126              } else {
7127                !!!cp ('t407');
7128              }
7129              $self->{insertion_mode} = AFTER_BODY_IM;
7130              ## reprocess
7131              next B;
7132            } else {
7133              !!!cp ('t408');
7134              !!!parse-error (type => 'unmatched end tag',
7135                              text => $token->{tag_name}, token => $token);
7136              ## Ignore the token
7137              !!!next-token;
7138              next B;
7139            }
7140          } elsif ({
7141                    address => 1, blockquote => 1, center => 1, dir => 1,
7142                    div => 1, dl => 1, fieldset => 1, listing => 1,
7143                    menu => 1, ol => 1, pre => 1, ul => 1,
7144                    dd => 1, dt => 1, li => 1,
7145                    applet => 1, button => 1, marquee => 1, object => 1,
7146                   }->{$token->{tag_name}}) {
7147            ## has an element in scope
7148            my $i;
7149            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7150              my $node = $self->{open_elements}->[$_];
7151              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7152                !!!cp ('t410');
7153                $i = $_;
7154                last INSCOPE;
7155              } elsif ($node->[1] & SCOPING_EL) {
7156                !!!cp ('t411');
7157                last INSCOPE;
7158              }
7159            } # INSCOPE
7160    
7161            unless (defined $i) { # has an element in scope
7162              !!!cp ('t413');
7163              !!!parse-error (type => 'unmatched end tag',
7164                              text => $token->{tag_name}, token => $token);
7165              ## NOTE: Ignore the token.
7166            } else {
7167              ## Step 1. generate implied end tags
7168              while ({
7169                      ## END_TAG_OPTIONAL_EL
7170                      dd => ($token->{tag_name} ne 'dd'),
7171                      dt => ($token->{tag_name} ne 'dt'),
7172                      li => ($token->{tag_name} ne 'li'),
7173                      p => 1,
7174                      rt => 1,
7175                      rp => 1,
7176                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7177                !!!cp ('t409');
7178                pop @{$self->{open_elements}};
7179              }
7180    
7181              ## Step 2.
7182              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7183                      ne $token->{tag_name}) {
7184                !!!cp ('t412');
7185                !!!parse-error (type => 'not closed',
7186                                text => $self->{open_elements}->[-1]->[0]
7187                                    ->manakai_local_name,
7188                                token => $token);
7189              } else {
7190                !!!cp ('t414');
7191              }
7192    
7193              ## Step 3.
7194              splice @{$self->{open_elements}}, $i;
7195    
7196              ## Step 4.
7197              $clear_up_to_marker->()
7198                  if {
7199                    applet => 1, button => 1, marquee => 1, object => 1,
7200                  }->{$token->{tag_name}};
7201            }
7202            !!!next-token;
7203            next B;
7204          } elsif ($token->{tag_name} eq 'form') {
7205            undef $self->{form_element};
7206    
7207            ## has an element in scope
7208            my $i;
7209            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7210              my $node = $self->{open_elements}->[$_];
7211              if ($node->[1] & FORM_EL) {
7212                !!!cp ('t418');
7213                $i = $_;
7214                last INSCOPE;
7215              } elsif ($node->[1] & SCOPING_EL) {
7216                !!!cp ('t419');
7217                last INSCOPE;
7218              }
7219            } # INSCOPE
7220    
7221            unless (defined $i) { # has an element in scope
7222              !!!cp ('t421');
7223              !!!parse-error (type => 'unmatched end tag',
7224                              text => $token->{tag_name}, token => $token);
7225              ## NOTE: Ignore the token.
7226            } else {
7227              ## Step 1. generate implied end tags
7228              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7229                !!!cp ('t417');
7230                pop @{$self->{open_elements}};
7231              }
7232              
7233              ## Step 2.
7234              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7235                      ne $token->{tag_name}) {
7236                !!!cp ('t417.1');
7237                !!!parse-error (type => 'not closed',
7238                                text => $self->{open_elements}->[-1]->[0]
7239                                    ->manakai_local_name,
7240                                token => $token);
7241              } else {
7242                !!!cp ('t420');
7243              }  
7244              
7245              ## Step 3.
7246              splice @{$self->{open_elements}}, $i;
7247            }
7248    
7249            !!!next-token;
7250            next B;
7251          } elsif ({
7252                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7253                   }->{$token->{tag_name}}) {
7254            ## has an element in scope
7255            my $i;
7256            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7257              my $node = $self->{open_elements}->[$_];
7258              if ($node->[1] & HEADING_EL) {
7259                !!!cp ('t423');
7260                $i = $_;
7261                last INSCOPE;
7262              } elsif ($node->[1] & SCOPING_EL) {
7263                !!!cp ('t424');
7264                last INSCOPE;
7265              }
7266            } # INSCOPE
7267    
7268            unless (defined $i) { # has an element in scope
7269              !!!cp ('t425.1');
7270              !!!parse-error (type => 'unmatched end tag',
7271                              text => $token->{tag_name}, token => $token);
7272              ## NOTE: Ignore the token.
7273            } else {
7274              ## Step 1. generate implied end tags
7275              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7276                !!!cp ('t422');
7277                pop @{$self->{open_elements}};
7278              }
7279              
7280              ## Step 2.
7281              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7282                      ne $token->{tag_name}) {
7283                !!!cp ('t425');
7284                !!!parse-error (type => 'unmatched end tag',
7285                                text => $token->{tag_name}, token => $token);
7286              } else {
7287                !!!cp ('t426');
7288              }
7289    
7290              ## Step 3.
7291              splice @{$self->{open_elements}}, $i;
7292            }
7293            
7294            !!!next-token;
7295            next B;
7296          } elsif ($token->{tag_name} eq 'p') {
7297            ## has an element in scope
7298            my $i;
7299            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7300              my $node = $self->{open_elements}->[$_];
7301              if ($node->[1] & P_EL) {
7302                !!!cp ('t410.1');
7303                $i = $_;
7304                last INSCOPE;
7305              } elsif ($node->[1] & SCOPING_EL) {
7306                !!!cp ('t411.1');
7307                last INSCOPE;
7308              }
7309            } # INSCOPE
7310    
7311            if (defined $i) {
7312              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7313                      ne $token->{tag_name}) {
7314                !!!cp ('t412.1');
7315                !!!parse-error (type => 'not closed',
7316                                text => $self->{open_elements}->[-1]->[0]
7317                                    ->manakai_local_name,
7318                                token => $token);
7319              } else {
7320                !!!cp ('t414.1');
7321              }
7322    
7323              splice @{$self->{open_elements}}, $i;
7324            } else {
7325              !!!cp ('t413.1');
7326              !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328    
7329              !!!cp ('t415.1');
7330              ## As if <p>, then reprocess the current token
7331              my $el;
7332              !!!create-element ($el, $HTML_NS, 'p',, $token);
7333              $insert->($el);
7334              ## NOTE: Not inserted into |$self->{open_elements}|.
7335            }
7336    
7337            !!!next-token;
7338            next B;
7339          } elsif ({
7340                    a => 1,
7341                    b => 1, big => 1, em => 1, font => 1, i => 1,
7342                    nobr => 1, s => 1, small => 1, strile => 1,
7343                    strong => 1, tt => 1, u => 1,
7344                   }->{$token->{tag_name}}) {
7345            !!!cp ('t427');
7346            $formatting_end_tag->($token);
7347            next B;
7348          } elsif ($token->{tag_name} eq 'br') {
7349            !!!cp ('t428');
7350            !!!parse-error (type => 'unmatched end tag',
7351                            text => 'br', token => $token);
7352    
7353            ## As if <br>
7354            $reconstruct_active_formatting_elements->($insert_to_current);
7355            
7356            my $el;
7357            !!!create-element ($el, $HTML_NS, 'br',, $token);
7358            $insert->($el);
7359            
7360            ## Ignore the token.
7361            !!!next-token;
7362            next B;
7363          } elsif ({
7364                    caption => 1, col => 1, colgroup => 1, frame => 1,
7365                    frameset => 1, head => 1, option => 1, optgroup => 1,
7366                    tbody => 1, td => 1, tfoot => 1, th => 1,
7367                    thead => 1, tr => 1,
7368                    area => 1, basefont => 1, bgsound => 1,
7369                    embed => 1, hr => 1, iframe => 1, image => 1,
7370                    img => 1, input => 1, isindex => 1, noembed => 1,
7371                    noframes => 1, param => 1, select => 1, spacer => 1,
7372                    table => 1, textarea => 1, wbr => 1,
7373                    noscript => 0, ## TODO: if scripting is enabled
7374                   }->{$token->{tag_name}}) {
7375            !!!cp ('t429');
7376            !!!parse-error (type => 'unmatched end tag',
7377                            text => $token->{tag_name}, token => $token);
7378            ## Ignore the token
7379            !!!next-token;
7380            next B;
7381            
7382            ## ISSUE: Issue on HTML5 new elements in spec
7383            
7384          } else {
7385            ## Step 1
7386            my $node_i = -1;
7387            my $node = $self->{open_elements}->[$node_i];
7388    
7389            ## Step 2
7390            S2: {
7391              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7392                ## Step 1
7393                ## generate implied end tags
7394                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7395                  !!!cp ('t430');
7396                  ## NOTE: |<ruby><rt></ruby>|.
7397                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7398                  ## which seems wrong.
7399                  pop @{$self->{open_elements}};
7400                  $node_i++;
7401                }
7402            
7403                ## Step 2
7404                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7405                        ne $token->{tag_name}) {
7406                  !!!cp ('t431');
7407                  ## NOTE: <x><y></x>
7408                  !!!parse-error (type => 'not closed',
7409                                  text => $self->{open_elements}->[-1]->[0]
7410                                      ->manakai_local_name,
7411                                  token => $token);
7412                } else {
7413                  !!!cp ('t432');
7414                }
7415                
7416                ## Step 3
7417                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7418    
7419                !!!next-token;
7420                last S2;
7421              } else {
7422                ## Step 3
7423                if (not ($node->[1] & FORMATTING_EL) and
7424                    #not $phrasing_category->{$node->[1]} and
7425                    ($node->[1] & SPECIAL_EL or
7426                     $node->[1] & SCOPING_EL)) {
7427                  !!!cp ('t433');
7428                  !!!parse-error (type => 'unmatched end tag',
7429                                  text => $token->{tag_name}, token => $token);
7430                  ## Ignore the token
7431                  !!!next-token;
7432                  last S2;
7433                }
7434    
7435                !!!cp ('t434');
7436              }
7437              
7438              ## Step 4
7439              $node_i--;
7440              $node = $self->{open_elements}->[$node_i];
7441              
7442              ## Step 5;
7443              redo S2;
7444            } # S2
7445            next B;
7446        }        }
7447      } else {      }
7448        die "$0: $self->{insertion_mode}: Unknown insertion mode";      next B;
7449      } continue { # B
7450        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7451          ## NOTE: The code below is executed in cases where it does not have
7452          ## to be, but it it is harmless even in those cases.
7453          ## has an element in scope
7454          INSCOPE: {
7455            for (reverse 0..$#{$self->{open_elements}}) {
7456              my $node = $self->{open_elements}->[$_];
7457              if ($node->[1] & FOREIGN_EL) {
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                last;
7461              }
7462            }
7463            
7464            ## NOTE: No foreign element in scope.
7465            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7466          } # INSCOPE
7467      }      }
7468    } # B    } # B
7469    
# Line 5208  sub _tree_construction_main ($) { Line 7472  sub _tree_construction_main ($) {
7472    ## TODO: script stuffs    ## TODO: script stuffs
7473  } # _tree_construct_main  } # _tree_construct_main
7474    
7475  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7476    my $class = shift;    my $class = shift;
7477    my $node = shift;    my $node = shift;
7478    my $s = \$_[0];    my $s = \$_[0];
7479    my $onerror = $_[1];    my $onerror = $_[1];
7480      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7481    
7482      ## ISSUE: Should {confident} be true?
7483    
7484    my $nt = $node->node_type;    my $nt = $node->node_type;
7485    if ($nt == 9) {    if ($nt == 9) {
# Line 5229  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496      }      }
7497    
7498      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7499      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7500    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7501      ## TODO: If non-html element      ## TODO: If non-html element
7502    
7503      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7504    
7505    ## TODO: Support for $get_wrapper
7506    
7507      ## Step 1 # MUST      ## Step 1 # MUST
7508      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7509      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5242  sub set_inner_html ($$$) { Line 7511  sub set_inner_html ($$$) {
7511      my $p = $class->new;      my $p = $class->new;
7512      $p->{document} = $doc;      $p->{document} = $doc;
7513    
7514      ## Step 9 # MUST      ## Step 8 # MUST
7515      my $i = 0;      my $i = 0;
7516      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7517      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7518      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7519        my $self = shift;        my $self = shift;
7520    
7521        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7522        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7523    
7524        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7525        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7526        $column++;  
7527          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7528        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
7529          $line++;  
7530          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
7531        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
7532            $p->{column} = 0;
7533            !!!cp ('i1');
7534          } elsif ($self->{next_char} == 0x000D) { # CR
7535          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7536          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7537          $line++;          $p->{line}++;
7538          $column = 0;          $p->{column} = 0;
7539        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7540          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7541        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7542            !!!cp ('i3');
7543          } elsif ($self->{next_char} == 0x0000) { # NULL
7544            !!!cp ('i4');
7545          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7546          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7547          } elsif ($self->{next_char} <= 0x0008 or
7548                   (0x000E <= $self->{next_char} and
7549                    $self->{next_char} <= 0x001F) or
7550                   (0x007F <= $self->{next_char} and
7551                    $self->{next_char} <= 0x009F) or
7552                   (0xD800 <= $self->{next_char} and
7553                    $self->{next_char} <= 0xDFFF) or
7554                   (0xFDD0 <= $self->{next_char} and
7555                    $self->{next_char} <= 0xFDDF) or
7556                   {
7557                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7558                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7559                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7560                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7561                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7562                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7563                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7564                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7565                    0x10FFFE => 1, 0x10FFFF => 1,
7566                   }->{$self->{next_char}}) {
7567            !!!cp ('i4.1');
7568            if ($self->{next_char} < 0x10000) {
7569              !!!parse-error (type => 'control char',
7570                              text => (sprintf 'U+%04X', $self->{next_char}));
7571            } else {
7572              !!!parse-error (type => 'control char',
7573                              text => (sprintf 'U-%08X', $self->{next_char}));
7574            }
7575        }        }
7576      };      };
7577      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7578      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7579            
7580      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7581        my (%opt) = @_;        my (%opt) = @_;
7582        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7583          my $column = $opt{column};
7584          if (defined $opt{token} and defined $opt{token}->{line}) {
7585            $line = $opt{token}->{line};
7586            $column = $opt{token}->{column};
7587          }
7588          warn "Parse error ($opt{type}) at line $line column $column\n";
7589      };      };
7590      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7591        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7592      };      };
7593            
7594      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7595      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7596    
7597      ## Step 2      ## Step 2
7598      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7599      $p->{content_model} = {      $p->{content_model} = {
7600        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7601        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7612  sub set_inner_html ($$$) {
7612          unless defined $p->{content_model};          unless defined $p->{content_model};
7613          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7614    
7615      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7616          ## TODO: Foreign element OK?
7617    
7618      ## Step 4      ## Step 3
7619      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7620        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7621    
7622      ## Step 5 # MUST      ## Step 4 # MUST
7623      $doc->append_child ($root);      $doc->append_child ($root);
7624    
7625      ## Step 6 # MUST      ## Step 5 # MUST
7626      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7627    
7628      undef $p->{head_element};      undef $p->{head_element};
7629    
7630      ## Step 7 # MUST      ## Step 6 # MUST
7631      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7632    
7633      ## Step 8 # MUST      ## Step 7 # MUST
7634      my $anode = $node;      my $anode = $node;
7635      AN: while (defined $anode) {      AN: while (defined $anode) {
7636        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7637          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7638          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7639            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7640                !!!cp ('i5');
7641              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7642              last AN;              last AN;
7643            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7646  sub set_inner_html ($$$) {
7646        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7647      } # AN      } # AN
7648            
7649      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7650      {      {
7651        my $self = $p;        my $self = $p;
7652        !!!next-token;        !!!next-token;
7653      }      }
7654      $p->_tree_construction_main;      $p->_tree_construction_main;
7655    
7656      ## Step 11 # MUST      ## Step 10 # MUST
7657      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7658      for (@cn) {      for (@cn) {
7659        $node->remove_child ($_);        $node->remove_child ($_);
7660      }      }
7661      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7662    
7663      ## Step 12 # MUST      ## Step 11 # MUST
7664      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7665      for (@cn) {      for (@cn) {
7666        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 7669  sub set_inner_html ($$$) {
7669      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7670    
7671      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7672    
7673        delete $p->{parse_error}; # delete loop
7674    } else {    } else {
7675      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";
7676    }    }
# Line 5366  sub set_inner_html ($$$) { Line 7678  sub set_inner_html ($$$) {
7678    
7679  } # tree construction stage  } # tree construction stage
7680    
7681  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7682    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  
7683    
7684  1;  1;
7685  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24