/[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.20 by wakaba, Sat Jun 23 14:25:05 2007 UTC revision 1.181 by wakaba, Mon Sep 15 02:54:12 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  my $permitted_slash_tag_name = {  require IO::Handle;
12    base => 1,  
13    link => 1,  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    meta => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15    hr => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    br => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    img=> 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    embed => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    param => 1,  
20    area => 1,  sub A_EL () { 0b1 }
21    col => 1,  sub ADDRESS_EL () { 0b10 }
22    input => 1,  sub BODY_EL () { 0b100 }
23    sub BUTTON_EL () { 0b1000 }
24    sub CAPTION_EL () { 0b10000 }
25    sub DD_EL () { 0b100000 }
26    sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    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 56  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];
362  my $scoping_category = {  
363    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
364    table => 1, td => 1, th => 1,      my (%opt) = @_;
365  };      warn "Parse error ($opt{type})\n";
366  my $formatting_category = {    };
367    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
369  };    my $get_wrapper = $_[3] || sub ($) {
370  # $phrasing_category: all other elements      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  sub parse_string ($$$;$) {    $self->{change_encoding} = sub {
511    my $self = shift->new;      my $self = shift;
512    my $s = \$_[0];      $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                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
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      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($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    my $disallowed_control_chars =
632    {
633     0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
634     0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
635     0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
636     0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
637     0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
638     0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
639     0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
640     0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
641     0x10FFFE => 1, 0x10FFFF => 1,
642    };
643    $disallowed_control_chars->{$_} = 1
644        for 0x0001 .. 0x0008, 0x000E .. 0x001F, 0x007F .. 0x009F,
645            0xD800 .. 0xDFFF, 0xFDD0 .. 0xFDDF;
646    ## ISSUE: U+FDE0-U+FDEF are not excluded
647    
648    sub parse_char_stream ($$$;$) {
649      my $self = ref $_[0] ? shift : shift->new;
650      my $input = $_[0];
651    $self->{document} = $_[1];    $self->{document} = $_[1];
652      @{$self->{document}->child_nodes} = ();
653    
654    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
655    
656      $self->{confident} = 1 unless exists $self->{confident};
657      $self->{document}->input_encoding ($self->{input_encoding})
658          if defined $self->{input_encoding};
659    ## TODO: |{input_encoding}| is needless?
660    
661    my $i = 0;    my $i = 0;
662    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
663    my $column = 0;    $self->{column_prev} = -1;
664    $self->{set_next_input_character} = sub {    $self->{column} = 0;
665      $self->{set_next_char} = sub {
666      my $self = shift;      my $self = shift;
667    
668      pop @{$self->{prev_input_character}};      my $char = '';
669      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_next_char}) {
670          $char = $self->{next_next_char};
671          delete $self->{next_next_char};
672          $self->{next_char} = ord $char;
673        } else {
674          $self->{char_buffer} = '';
675          $self->{char_buffer_pos} = 0;
676    
677      $self->{next_input_character} = -1 and return if $i >= length $$s;        my $count = $input->manakai_read_until
678      $self->{next_input_character} = ord substr $$s, $i++, 1;           ($self->{char_buffer},
679      $column++;            qr/(?![\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
680              $self->{char_buffer_pos});
681          if ($count) {
682            $self->{line_prev} = $self->{line};
683            $self->{column_prev} = $self->{column};
684            $self->{column}++;
685            $self->{next_char}
686                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
687            return;
688          }
689    
690          if ($input->read ($char, 1)) {
691            $self->{next_char} = ord $char;
692          } else {
693            $self->{next_char} = -1;
694            return;
695          }
696        }
697    
698        ($self->{line_prev}, $self->{column_prev})
699            = ($self->{line}, $self->{column});
700        $self->{column}++;
701            
702      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
703        $line++;        !!!cp ('j1');
704        $column = 0;        $self->{line}++;
705      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
706        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
707        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
708        $line++;  ## TODO: support for abort/streaming
709        $column = 0;        my $next = '';
710      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
711        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_next_char} = $next;
712      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
713          $self->{next_char} = 0x000A; # LF # MUST
714          $self->{line}++;
715          $self->{column} = 0;
716        } elsif ($self->{next_char} > 0x10FFFF) {
717          !!!cp ('j3');
718          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
719        } elsif ($self->{next_char} == 0x0000) { # NULL
720          !!!cp ('j4');
721        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
722        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
723        } elsif ($disallowed_control_chars->{$self->{next_char}}) {
724          !!!cp ('j5');
725          if ($self->{next_char} < 0x10000) {
726            !!!parse-error (type => 'control char',
727                            text => (sprintf 'U+%04X', $self->{next_char}));
728          } else {
729            !!!parse-error (type => 'control char',
730                            text => (sprintf 'U-%08X', $self->{next_char}));
731          }
732      }      }
733    };    };
734    $self->{prev_input_character} = [-1, -1, -1];  
735    $self->{next_input_character} = -1;    $self->{read_until} = sub {
736        #my ($scalar, $specials_range, $offset) = @_;
737        return 0 if defined $self->{next_next_char};
738    
739        my $pattern = qr/(?![$_[1]\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/;
740        my $offset = $_[2] || 0;
741    
742        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
743          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
744          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
745            substr ($_[0], $offset)
746                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
747            my $count = $+[0] - $-[0];
748            if ($count) {
749              $self->{column} += $count;
750              $self->{char_buffer_pos} += $count;
751              $self->{line_prev} = $self->{line};
752              $self->{column_prev} = $self->{column} - 1;
753              $self->{prev_char} = [-1, -1, -1];
754              $self->{next_char} = -1;
755            }
756            return $count;
757          } else {
758            return 0;
759          }
760        } else {
761          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
762          if ($count) {
763            $self->{column} += $count;
764            $self->{line_prev} = $self->{line};
765            $self->{column_prev} = $self->{column} - 1;
766            $self->{prev_char} = [-1, -1, -1];
767            $self->{next_char} = -1;
768          }
769          return $count;
770        }
771      }; # $self->{read_until}
772    
773    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
774      my (%opt) = @_;      my (%opt) = @_;
775      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
776        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
777        warn "Parse error ($opt{type}) at line $line column $column\n";
778    };    };
779    $self->{parse_error} = sub {    $self->{parse_error} = sub {
780      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
781    };    };
782    
783    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 129  sub parse_string ($$$;$) { Line 785  sub parse_string ($$$;$) {
785    $self->_construct_tree;    $self->_construct_tree;
786    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
787    
788      delete $self->{parse_error}; # remove loop
789    
790    return $self->{document};    return $self->{document};
791  } # parse_string  } # parse_char_stream
792    
793  sub new ($) {  sub new ($) {
794    my $class = shift;    my $class = shift;
795    my $self = bless {}, $class;    my $self = bless {
796    $self->{set_next_input_character} = sub {      level => {must => 'm',
797      $self->{next_input_character} = -1;                should => 's',
798                  warn => 'w',
799                  info => 'i',
800                  uncertain => 'u'},
801      }, $class;
802      $self->{set_next_char} = sub {
803        $self->{next_char} = -1;
804    };    };
805    $self->{parse_error} = sub {    $self->{parse_error} = sub {
806      #      #
807    };    };
808      $self->{change_encoding} = sub {
809        # if ($_[0] is a supported encoding) {
810        #   run "change the encoding" algorithm;
811        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
812        # }
813      };
814      $self->{application_cache_selection} = sub {
815        #
816      };
817    return $self;    return $self;
818  } # new  } # new
819    
820    sub CM_ENTITY () { 0b001 } # & markup in data
821    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
822    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
823    
824    sub PLAINTEXT_CONTENT_MODEL () { 0 }
825    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
826    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
827    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
828    
829    sub DATA_STATE () { 0 }
830    #sub ENTITY_DATA_STATE () { 1 }
831    sub TAG_OPEN_STATE () { 2 }
832    sub CLOSE_TAG_OPEN_STATE () { 3 }
833    sub TAG_NAME_STATE () { 4 }
834    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
835    sub ATTRIBUTE_NAME_STATE () { 6 }
836    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
837    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
838    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
839    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
840    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
841    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
842    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
843    sub COMMENT_START_STATE () { 14 }
844    sub COMMENT_START_DASH_STATE () { 15 }
845    sub COMMENT_STATE () { 16 }
846    sub COMMENT_END_STATE () { 17 }
847    sub COMMENT_END_DASH_STATE () { 18 }
848    sub BOGUS_COMMENT_STATE () { 19 }
849    sub DOCTYPE_STATE () { 20 }
850    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
851    sub DOCTYPE_NAME_STATE () { 22 }
852    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
853    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
854    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
855    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
856    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
857    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
858    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
859    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
860    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
861    sub BOGUS_DOCTYPE_STATE () { 32 }
862    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
863    sub SELF_CLOSING_START_TAG_STATE () { 34 }
864    sub CDATA_SECTION_STATE () { 35 }
865    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
866    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
867    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
868    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
869    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
870    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
871    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
872    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
873    ## NOTE: "Entity data state", "entity in attribute value state", and
874    ## "consume a character reference" algorithm are jointly implemented
875    ## using the following six states:
876    sub ENTITY_STATE () { 44 }
877    sub ENTITY_HASH_STATE () { 45 }
878    sub NCR_NUM_STATE () { 46 }
879    sub HEXREF_X_STATE () { 47 }
880    sub HEXREF_HEX_STATE () { 48 }
881    sub ENTITY_NAME_STATE () { 49 }
882    
883    sub DOCTYPE_TOKEN () { 1 }
884    sub COMMENT_TOKEN () { 2 }
885    sub START_TAG_TOKEN () { 3 }
886    sub END_TAG_TOKEN () { 4 }
887    sub END_OF_FILE_TOKEN () { 5 }
888    sub CHARACTER_TOKEN () { 6 }
889    
890    sub AFTER_HTML_IMS () { 0b100 }
891    sub HEAD_IMS ()       { 0b1000 }
892    sub BODY_IMS ()       { 0b10000 }
893    sub BODY_TABLE_IMS () { 0b100000 }
894    sub TABLE_IMS ()      { 0b1000000 }
895    sub ROW_IMS ()        { 0b10000000 }
896    sub BODY_AFTER_IMS () { 0b100000000 }
897    sub FRAME_IMS ()      { 0b1000000000 }
898    sub SELECT_IMS ()     { 0b10000000000 }
899    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
900        ## NOTE: "in foreign content" insertion mode is special; it is combined
901        ## with the secondary insertion mode.  In this parser, they are stored
902        ## together in the bit-or'ed form.
903    
904    ## NOTE: "initial" and "before html" insertion modes have no constants.
905    
906    ## NOTE: "after after body" insertion mode.
907    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
908    
909    ## NOTE: "after after frameset" insertion mode.
910    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
911    
912    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
913    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
914    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
915    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
916    sub IN_BODY_IM () { BODY_IMS }
917    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
918    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
919    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
920    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
921    sub IN_TABLE_IM () { TABLE_IMS }
922    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
923    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
924    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
925    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
926    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
927    sub IN_COLUMN_GROUP_IM () { 0b10 }
928    
929  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
930    
931  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
932    my $self = shift;    my $self = shift;
933    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
934    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{state_keyword}; # initialized when used
935    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
936      #$self->{entity__match}; # initialized when used
937      $self->{content_model} = PCDATA_CONTENT_MODEL; # be
938      undef $self->{current_token};
939    undef $self->{current_attribute};    undef $self->{current_attribute};
940    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
941    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
942    $self->{char} = [];    delete $self->{self_closing};
943    # $self->{next_input_character}    $self->{char_buffer} = '';
944      $self->{char_buffer_pos} = 0;
945      $self->{prev_char} = [-1, -1, -1];
946      $self->{next_char} = -1;
947    !!!next-input-character;    !!!next-input-character;
948    $self->{token} = [];    $self->{token} = [];
949    # $self->{escape}    # $self->{escape}
950  } # _initialize_tokenizer  } # _initialize_tokenizer
951    
952  ## A token has:  ## A token has:
953  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
954  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
955  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
956  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
957  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
958  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
959  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
960  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
961    ##        ->{name}
962    ##        ->{value}
963    ##        ->{has_reference} == 1 or 0
964    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
965    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
966    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
967    ##     while the token is pushed back to the stack.
968    
969  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
970    
# Line 179  sub _initialize_tokenizer ($) { Line 974  sub _initialize_tokenizer ($) {
974  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
975  ## and removed from the list.  ## and removed from the list.
976    
977    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
978    ## (This requirement was dropped from HTML5 spec, unfortunately.)
979    
980  sub _get_next_token ($) {  sub _get_next_token ($) {
981    my $self = shift;    my $self = shift;
982    
983      if ($self->{self_closing}) {
984        !!!parse-error (type => 'nestc', token => $self->{current_token});
985        ## NOTE: The |self_closing| flag is only set by start tag token.
986        ## In addition, when a start tag token is emitted, it is always set to
987        ## |current_token|.
988        delete $self->{self_closing};
989      }
990    
991    if (@{$self->{token}}) {    if (@{$self->{token}}) {
992        $self->{self_closing} = $self->{token}->[0]->{self_closing};
993      return shift @{$self->{token}};      return shift @{$self->{token}};
994    }    }
995    
996    A: {    A: {
997      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
998        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
999          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1000              $self->{content_model_flag} eq 'RCDATA') {              not $self->{escape}) {
1001            $self->{state} = 'entity data';            !!!cp (1);
1002              ## NOTE: In the spec, the tokenizer is switched to the
1003              ## "entity data state".  In this implementation, the tokenizer
1004              ## is switched to the |ENTITY_STATE|, which is an implementation
1005              ## of the "consume a character reference" algorithm.
1006              $self->{entity_additional} = -1;
1007              $self->{prev_state} = DATA_STATE;
1008              $self->{state} = ENTITY_STATE;
1009            !!!next-input-character;            !!!next-input-character;
1010            redo A;            redo A;
1011          } else {          } else {
1012              !!!cp (2);
1013            #            #
1014          }          }
1015        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1016          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
1017            unless ($self->{escape}) {            unless ($self->{escape}) {
1018              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
1019                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
1020                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
1021                  !!!cp (3);
1022                $self->{escape} = 1;                $self->{escape} = 1;
1023                } else {
1024                  !!!cp (4);
1025              }              }
1026              } else {
1027                !!!cp (5);
1028            }            }
1029          }          }
1030                    
1031          #          #
1032        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
1033          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1034              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
1035               not $self->{escape})) {               not $self->{escape})) {
1036            $self->{state} = 'tag open';            !!!cp (6);
1037              $self->{state} = TAG_OPEN_STATE;
1038            !!!next-input-character;            !!!next-input-character;
1039            redo A;            redo A;
1040          } else {          } else {
1041              !!!cp (7);
1042            #            #
1043          }          }
1044        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1045          if ($self->{escape} and          if ($self->{escape} and
1046              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1047               $self->{content_model_flag} eq 'CDATA')) {            if ($self->{prev_char}->[0] == 0x002D and # -
1048            if ($self->{prev_input_character}->[0] == 0x002D and # -                $self->{prev_char}->[1] == 0x002D) { # -
1049                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
1050              delete $self->{escape};              delete $self->{escape};
1051              } else {
1052                !!!cp (9);
1053            }            }
1054            } else {
1055              !!!cp (10);
1056          }          }
1057                    
1058          #          #
1059        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1060          !!!emit ({type => 'end-of-file'});          !!!cp (11);
1061            !!!emit ({type => END_OF_FILE_TOKEN,
1062                      line => $self->{line}, column => $self->{column}});
1063          last A; ## TODO: ok?          last A; ## TODO: ok?
1064          } else {
1065            !!!cp (12);
1066        }        }
1067        # Anything else        # Anything else
1068        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1069                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1070                       line => $self->{line}, column => $self->{column},
1071                      };
1072          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1073    
1074        ## Stay in the data state        ## Stay in the data state
1075        !!!next-input-character;        !!!next-input-character;
1076    
1077        !!!emit ($token);        !!!emit ($token);
1078    
1079        redo A;        redo A;
1080      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1081        ## (cannot happen in CDATA state)        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1082                  if ($self->{next_char} == 0x002F) { # /
1083        my $token = $self->_tokenize_attempt_to_consume_an_entity;            !!!cp (15);
   
       $self->{state} = 'data';  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => 'character', data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
     } elsif ($self->{state} eq 'tag open') {  
       if ($self->{content_model_flag} eq 'RCDATA' or  
           $self->{content_model_flag} eq 'CDATA') {  
         if ($self->{next_input_character} == 0x002F) { # /  
1084            !!!next-input-character;            !!!next-input-character;
1085            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1086            redo A;            redo A;
1087          } else {          } else {
1088              !!!cp (16);
1089            ## reconsume            ## reconsume
1090            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1091    
1092            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1093                        line => $self->{line_prev},
1094                        column => $self->{column_prev},
1095                       });
1096    
1097            redo A;            redo A;
1098          }          }
1099        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1100          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1101            $self->{state} = 'markup declaration open';            !!!cp (17);
1102              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1103            !!!next-input-character;            !!!next-input-character;
1104            redo A;            redo A;
1105          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1106            $self->{state} = 'close tag open';            !!!cp (18);
1107              $self->{state} = CLOSE_TAG_OPEN_STATE;
1108            !!!next-input-character;            !!!next-input-character;
1109            redo A;            redo A;
1110          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1111                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1112              !!!cp (19);
1113            $self->{current_token}            $self->{current_token}
1114              = {type => 'start tag',              = {type => START_TAG_TOKEN,
1115                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1116            $self->{state} = 'tag name';                 line => $self->{line_prev},
1117                   column => $self->{column_prev}};
1118              $self->{state} = TAG_NAME_STATE;
1119            !!!next-input-character;            !!!next-input-character;
1120            redo A;            redo A;
1121          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1122                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1123            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1124                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
1125            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
1126                                        line => $self->{line_prev},
1127                                        column => $self->{column_prev}};
1128              $self->{state} = TAG_NAME_STATE;
1129            !!!next-input-character;            !!!next-input-character;
1130            redo A;            redo A;
1131          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1132            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1133            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1134                              line => $self->{line_prev},
1135                              column => $self->{column_prev});
1136              $self->{state} = DATA_STATE;
1137            !!!next-input-character;            !!!next-input-character;
1138    
1139            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1140                        line => $self->{line_prev},
1141                        column => $self->{column_prev},
1142                       });
1143    
1144            redo A;            redo A;
1145          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1146            !!!parse-error (type => 'pio');            !!!cp (22);
1147            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1148            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1149                              column => $self->{column_prev});
1150              $self->{state} = BOGUS_COMMENT_STATE;
1151              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1152                                        line => $self->{line_prev},
1153                                        column => $self->{column_prev},
1154                                       };
1155              ## $self->{next_char} is intentionally left as is
1156            redo A;            redo A;
1157          } else {          } else {
1158            !!!parse-error (type => 'bare stago');            !!!cp (23);
1159            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1160                              line => $self->{line_prev},
1161                              column => $self->{column_prev});
1162              $self->{state} = DATA_STATE;
1163            ## reconsume            ## reconsume
1164    
1165            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1166                        line => $self->{line_prev},
1167                        column => $self->{column_prev},
1168                       });
1169    
1170            redo A;            redo A;
1171          }          }
1172        } else {        } else {
1173          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1174        }        }
1175      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1176        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1177            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1178          my @next_char;  
1179          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1180            push @next_char, $self->{next_input_character};        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1181            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);          if (defined $self->{last_emitted_start_tag_name}) {
1182            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1183            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            $self->{state_keyword} = '';
1184              !!!next-input-character;            ## Reconsume.
             next TAGNAME;  
           } else {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = 'data';  
   
             !!!emit ({type => 'character', data => '</'});  
   
             redo A;  
           }  
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == -1) {  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
1185            redo A;            redo A;
1186          } else {          } else {
1187            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
1188            !!!back-next-input-character (@next_char);            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1189            # and consume...            !!!cp (28);
1190              $self->{state} = DATA_STATE;
1191              ## Reconsume.
1192              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1193                        line => $l, column => $c,
1194                       });
1195              redo A;
1196          }          }
1197        }        }
1198          
1199        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1200            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1201          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1202                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1203          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1204          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
1205          redo A;                 line => $l, column => $c};
1206        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1207                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1208          $self->{current_token} = {type => 'end tag',          redo A;
1209                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
1210          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
1211          !!!next-input-character;          !!!cp (30);
1212          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
1213        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
1214          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1215          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1216            !!!next-input-character;
1217            redo A;
1218          } elsif ($self->{next_char} == 0x003E) { # >
1219            !!!cp (31);
1220            !!!parse-error (type => 'empty end tag',
1221                            line => $self->{line_prev}, ## "<" in "</>"
1222                            column => $self->{column_prev} - 1);
1223            $self->{state} = DATA_STATE;
1224          !!!next-input-character;          !!!next-input-character;
1225          redo A;          redo A;
1226        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1227            !!!cp (32);
1228          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1229          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1230          # reconsume          # reconsume
1231    
1232          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1233                      line => $l, column => $c,
1234                     });
1235    
1236          redo A;          redo A;
1237        } else {        } else {
1238            !!!cp (33);
1239          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1240          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1241          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1242          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1243                                      column => $self->{column_prev} - 1,
1244                                     };
1245            ## NOTE: $self->{next_char} is intentionally left as is.
1246            ## Although the "anything else" case of the spec not explicitly
1247            ## states that the next input character is to be reconsumed,
1248            ## it will be included to the |data| of the comment token
1249            ## generated from the bogus end tag, as defined in the
1250            ## "bogus comment state" entry.
1251            redo A;
1252          }
1253        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1254          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1255          if (length $ch) {
1256            my $CH = $ch;
1257            $ch =~ tr/a-z/A-Z/;
1258            my $nch = chr $self->{next_char};
1259            if ($nch eq $ch or $nch eq $CH) {
1260              !!!cp (24);
1261              ## Stay in the state.
1262              $self->{state_keyword} .= $nch;
1263              !!!next-input-character;
1264              redo A;
1265            } else {
1266              !!!cp (25);
1267              $self->{state} = DATA_STATE;
1268              ## Reconsume.
1269              !!!emit ({type => CHARACTER_TOKEN,
1270                        data => '</' . $self->{state_keyword},
1271                        line => $self->{line_prev},
1272                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1273                       });
1274              redo A;
1275            }
1276          } else { # after "<{tag-name}"
1277            unless ({
1278                     0x0009 => 1, # HT
1279                     0x000A => 1, # LF
1280                     0x000B => 1, # VT
1281                     0x000C => 1, # FF
1282                     0x0020 => 1, # SP
1283                     0x003E => 1, # >
1284                     0x002F => 1, # /
1285                     -1 => 1, # EOF
1286                    }->{$self->{next_char}}) {
1287              !!!cp (26);
1288              ## Reconsume.
1289              $self->{state} = DATA_STATE;
1290              !!!emit ({type => CHARACTER_TOKEN,
1291                        data => '</' . $self->{state_keyword},
1292                        line => $self->{line_prev},
1293                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1294                       });
1295              redo A;
1296            } else {
1297              !!!cp (27);
1298              $self->{current_token}
1299                  = {type => END_TAG_TOKEN,
1300                     tag_name => $self->{last_emitted_start_tag_name},
1301                     line => $self->{line_prev},
1302                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1303              $self->{state} = TAG_NAME_STATE;
1304              ## Reconsume.
1305              redo A;
1306            }
1307        }        }
1308      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1309        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1310            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1311            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1312            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1313            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1314          $self->{state} = 'before attribute name';          !!!cp (34);
1315            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1316          !!!next-input-character;          !!!next-input-character;
1317          redo A;          redo A;
1318        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1319          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1320              !!!cp (35);
1321            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1322          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1323            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1324            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1325              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1326            }            #  !!! cp (36);
1327              #  !!! parse-error (type => 'end tag attribute');
1328              #} else {
1329                !!!cp (37);
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
         undef $self->{current_token};  
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_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1343            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1344            # start tag or end tag            # start tag or end tag
1345          ## Stay in this state          ## Stay in this state
1346          !!!next-input-character;          !!!next-input-character;
1347          redo A;          redo A;
1348        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1349          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1350          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1351              !!!cp (39);
1352            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1353          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1354            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1355            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1356              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1357            }            #  !!! cp (40);
1358              #  !!! parse-error (type => 'end tag attribute');
1359              #} else {
1360                !!!cp (41);
1361              #}
1362          } else {          } else {
1363            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1364          }          }
1365          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1366          # reconsume          # reconsume
1367    
1368          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1369    
1370          redo A;          redo A;
1371        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1372            !!!cp (42);
1373            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1374          !!!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  
1375          redo A;          redo A;
1376        } else {        } else {
1377          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1378            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1379            # start tag or end tag            # start tag or end tag
1380          ## Stay in the state          ## Stay in the state
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        }        }
1384      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1385        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1386            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1387            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1388            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1389            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1390            !!!cp (45);
1391          ## Stay in the state          ## Stay in the state
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1395          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1396              !!!cp (46);
1397            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1398          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1399            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1400            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1401                !!!cp (47);
1402              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1403              } else {
1404                !!!cp (48);
1405            }            }
1406          } else {          } else {
1407            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1408          }          }
1409          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1410          !!!next-input-character;          !!!next-input-character;
1411    
1412          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1413    
1414          redo A;          redo A;
1415        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1416                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1417          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1418                                value => ''};          $self->{current_attribute}
1419          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1420                   value => '',
1421                   line => $self->{line}, column => $self->{column}};
1422            $self->{state} = ATTRIBUTE_NAME_STATE;
1423            !!!next-input-character;
1424            redo A;
1425          } elsif ($self->{next_char} == 0x002F) { # /
1426            !!!cp (50);
1427            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } 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) {  
1431          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1432          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1433              !!!cp (52);
1434            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1435          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1436            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1437            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1438                !!!cp (53);
1439              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1440              } else {
1441                !!!cp (54);
1442            }            }
1443          } else {          } else {
1444            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1445          }          }
1446          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1447          # reconsume          # reconsume
1448    
1449          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1450    
1451          redo A;          redo A;
1452        } else {        } else {
1453          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1454                                value => ''};               0x0022 => 1, # "
1455          $self->{state} = 'attribute name';               0x0027 => 1, # '
1456                 0x003D => 1, # =
1457                }->{$self->{next_char}}) {
1458              !!!cp (55);
1459              !!!parse-error (type => 'bad attribute name');
1460            } else {
1461              !!!cp (56);
1462            }
1463            $self->{current_attribute}
1464                = {name => chr ($self->{next_char}),
1465                   value => '',
1466                   line => $self->{line}, column => $self->{column}};
1467            $self->{state} = ATTRIBUTE_NAME_STATE;
1468          !!!next-input-character;          !!!next-input-character;
1469          redo A;          redo A;
1470        }        }
1471      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1472        my $before_leave = sub {        my $before_leave = sub {
1473          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1474              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1475            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1476              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1477            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1478          } else {          } else {
1479              !!!cp (58);
1480            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1481              = $self->{current_attribute};              = $self->{current_attribute};
1482          }          }
1483        }; # $before_leave        }; # $before_leave
1484    
1485        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1486            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1487            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1488            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1489            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1490            !!!cp (59);
1491          $before_leave->();          $before_leave->();
1492          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1496            !!!cp (60);
1497          $before_leave->();          $before_leave->();
1498          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1499          !!!next-input-character;          !!!next-input-character;
1500          redo A;          redo A;
1501        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1502          $before_leave->();          $before_leave->();
1503          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1504              !!!cp (61);
1505            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1506          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1507            $self->{content_model_flag} = 'PCDATA'; # MUST            !!!cp (62);
1508              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1509            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1510              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1511            }            }
1512          } else {          } else {
1513            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1514          }          }
1515          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517    
1518          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1519    
1520          redo A;          redo A;
1521        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1522                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1523          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1524            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1525          ## Stay in the state          ## Stay in the state
1526          !!!next-input-character;          !!!next-input-character;
1527          redo A;          redo A;
1528        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1529            !!!cp (64);
1530          $before_leave->();          $before_leave->();
1531            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1532          !!!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  
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1535          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1536          $before_leave->();          $before_leave->();
1537          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1538              !!!cp (66);
1539            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1540          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1541            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1542            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1543                !!!cp (67);
1544              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1545              } else {
1546                ## NOTE: This state should never be reached.
1547                !!!cp (68);
1548            }            }
1549          } else {          } else {
1550            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1551          }          }
1552          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1553          # reconsume          # reconsume
1554    
1555          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1556    
1557          redo A;          redo A;
1558        } else {        } else {
1559          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1560                $self->{next_char} == 0x0027) { # '
1561              !!!cp (69);
1562              !!!parse-error (type => 'bad attribute name');
1563            } else {
1564              !!!cp (70);
1565            }
1566            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1567          ## Stay in the state          ## Stay in the state
1568          !!!next-input-character;          !!!next-input-character;
1569          redo A;          redo A;
1570        }        }
1571      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1572        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1573            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1574            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1575            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1576            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1577            !!!cp (71);
1578          ## Stay in the state          ## Stay in the state
1579          !!!next-input-character;          !!!next-input-character;
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1582          $self->{state} = 'before attribute value';          !!!cp (72);
1583            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1587          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1588              !!!cp (73);
1589            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1590          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1591            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1592            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1593                !!!cp (74);
1594              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1595              } else {
1596                ## NOTE: This state should never be reached.
1597                !!!cp (75);
1598            }            }
1599          } else {          } else {
1600            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1601          }          }
1602          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1603          !!!next-input-character;          !!!next-input-character;
1604    
1605          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1606    
1607          redo A;          redo A;
1608        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1609                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1610          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1611                                value => ''};          $self->{current_attribute}
1612          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1613                   value => '',
1614                   line => $self->{line}, column => $self->{column}};
1615            $self->{state} = ATTRIBUTE_NAME_STATE;
1616            !!!next-input-character;
1617            redo A;
1618          } elsif ($self->{next_char} == 0x002F) { # /
1619            !!!cp (77);
1620            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1621          !!!next-input-character;          !!!next-input-character;
1622          redo A;          redo A;
1623        } 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');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1624          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1625          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1626              !!!cp (79);
1627            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1628          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1629            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1630            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1631                !!!cp (80);
1632              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1633              } else {
1634                ## NOTE: This state should never be reached.
1635                !!!cp (81);
1636            }            }
1637          } else {          } else {
1638            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1639          }          }
1640          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1641          # reconsume          # reconsume
1642    
1643          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1644    
1645          redo A;          redo A;
1646        } else {        } else {
1647          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1648                                value => ''};              $self->{next_char} == 0x0027) { # '
1649          $self->{state} = 'attribute name';            !!!cp (78);
1650              !!!parse-error (type => 'bad attribute name');
1651            } else {
1652              !!!cp (82);
1653            }
1654            $self->{current_attribute}
1655                = {name => chr ($self->{next_char}),
1656                   value => '',
1657                   line => $self->{line}, column => $self->{column}};
1658            $self->{state} = ATTRIBUTE_NAME_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660          redo A;                  redo A;        
1661        }        }
1662      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1663        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1664            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1665            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1666            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1667            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1668            !!!cp (83);
1669          ## Stay in the state          ## Stay in the state
1670          !!!next-input-character;          !!!next-input-character;
1671          redo A;          redo A;
1672        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1673          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1674            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1675          !!!next-input-character;          !!!next-input-character;
1676          redo A;          redo A;
1677        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1678          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1679            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1680          ## reconsume          ## reconsume
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1683          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1684            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1685          !!!next-input-character;          !!!next-input-character;
1686          redo A;          redo A;
1687        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1688          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1689            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1690              !!!cp (87);
1691            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1692          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1693            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1694            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1695                !!!cp (88);
1696              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1697              } else {
1698                ## NOTE: This state should never be reached.
1699                !!!cp (89);
1700            }            }
1701          } else {          } else {
1702            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1703          }          }
1704          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1705          !!!next-input-character;          !!!next-input-character;
1706    
1707          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1708    
1709          redo A;          redo A;
1710        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1711          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1712          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1713              !!!cp (90);
1714            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1715          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1716            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1717            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1718                !!!cp (91);
1719              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1720              } else {
1721                ## NOTE: This state should never be reached.
1722                !!!cp (92);
1723            }            }
1724          } else {          } else {
1725            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1726          }          }
1727          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1728          ## reconsume          ## reconsume
1729    
1730          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1731    
1732          redo A;          redo A;
1733        } else {        } else {
1734          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1735          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1736              !!!parse-error (type => 'bad attribute value');
1737            } else {
1738              !!!cp (94);
1739            }
1740            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1741            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1742          !!!next-input-character;          !!!next-input-character;
1743          redo A;          redo A;
1744        }        }
1745      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1746        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1747          $self->{state} = 'before attribute name';          !!!cp (95);
1748            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750          redo A;          redo A;
1751        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1752          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1753          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1754            ## "entity in attribute value state".  In this implementation, the
1755            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1756            ## implementation of the "consume a character reference" algorithm.
1757            $self->{prev_state} = $self->{state};
1758            $self->{entity_additional} = 0x0022; # "
1759            $self->{state} = ENTITY_STATE;
1760          !!!next-input-character;          !!!next-input-character;
1761          redo A;          redo A;
1762        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1763          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1764          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1765              !!!cp (97);
1766            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1767          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1768            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1769            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1770                !!!cp (98);
1771              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1772              } else {
1773                ## NOTE: This state should never be reached.
1774                !!!cp (99);
1775            }            }
1776          } else {          } else {
1777            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1778          }          }
1779          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1780          ## reconsume          ## reconsume
1781    
1782          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1783    
1784          redo A;          redo A;
1785        } else {        } else {
1786          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1787            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1788            $self->{read_until}->($self->{current_attribute}->{value},
1789                                  q["&],
1790                                  length $self->{current_attribute}->{value});
1791    
1792          ## Stay in the state          ## Stay in the state
1793          !!!next-input-character;          !!!next-input-character;
1794          redo A;          redo A;
1795        }        }
1796      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1797        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1798          $self->{state} = 'before attribute name';          !!!cp (101);
1799          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1800          redo A;          !!!next-input-character;
1801        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1802          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1803          $self->{state} = 'entity in attribute value';          !!!cp (102);
1804            ## NOTE: In the spec, the tokenizer is switched to the
1805            ## "entity in attribute value state".  In this implementation, the
1806            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1807            ## implementation of the "consume a character reference" algorithm.
1808            $self->{entity_additional} = 0x0027; # '
1809            $self->{prev_state} = $self->{state};
1810            $self->{state} = ENTITY_STATE;
1811          !!!next-input-character;          !!!next-input-character;
1812          redo A;          redo A;
1813        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1814          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1815          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1816              !!!cp (103);
1817            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1818          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1819            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1820            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1821                !!!cp (104);
1822              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1823              } else {
1824                ## NOTE: This state should never be reached.
1825                !!!cp (105);
1826            }            }
1827          } else {          } else {
1828            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1829          }          }
1830          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1831          ## reconsume          ## reconsume
1832    
1833          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1834    
1835          redo A;          redo A;
1836        } else {        } else {
1837          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1838            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1839            $self->{read_until}->($self->{current_attribute}->{value},
1840                                  q['&],
1841                                  length $self->{current_attribute}->{value});
1842    
1843          ## Stay in the state          ## Stay in the state
1844          !!!next-input-character;          !!!next-input-character;
1845          redo A;          redo A;
1846        }        }
1847      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1848        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1849            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1850            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1851            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1852            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1853          $self->{state} = 'before attribute name';          !!!cp (107);
1854          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1855          redo A;          !!!next-input-character;
1856        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1857          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1858          $self->{state} = 'entity in attribute value';          !!!cp (108);
1859          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1860          redo A;          ## "entity in attribute value state".  In this implementation, the
1861        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1862          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1863            $self->{entity_additional} = -1;
1864            $self->{prev_state} = $self->{state};
1865            $self->{state} = ENTITY_STATE;
1866            !!!next-input-character;
1867            redo A;
1868          } elsif ($self->{next_char} == 0x003E) { # >
1869            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1870              !!!cp (109);
1871            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1872          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1873            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1874            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1875                !!!cp (110);
1876              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1877              } else {
1878                ## NOTE: This state should never be reached.
1879                !!!cp (111);
1880            }            }
1881          } else {          } else {
1882            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1883          }          }
1884          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1885          !!!next-input-character;          !!!next-input-character;
1886    
1887          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1888    
1889          redo A;          redo A;
1890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893              !!!cp (112);
1894            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1897            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1898                !!!cp (113);
1899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1900              } else {
1901                ## NOTE: This state should never be reached.
1902                !!!cp (114);
1903            }            }
1904          } else {          } else {
1905            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1906          }          }
1907          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1908          ## reconsume          ## reconsume
1909    
1910          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1911    
1912          redo A;          redo A;
1913        } else {        } else {
1914          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1915                 0x0022 => 1, # "
1916                 0x0027 => 1, # '
1917                 0x003D => 1, # =
1918                }->{$self->{next_char}}) {
1919              !!!cp (115);
1920              !!!parse-error (type => 'bad attribute value');
1921            } else {
1922              !!!cp (116);
1923            }
1924            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1925            $self->{read_until}->($self->{current_attribute}->{value},
1926                                  q["'=& >],
1927                                  length $self->{current_attribute}->{value});
1928    
1929          ## Stay in the state          ## Stay in the state
1930          !!!next-input-character;          !!!next-input-character;
1931          redo A;          redo A;
1932        }        }
1933      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1934        my $token = $self->_tokenize_attempt_to_consume_an_entity;        if ($self->{next_char} == 0x0009 or # HT
1935              $self->{next_char} == 0x000A or # LF
1936              $self->{next_char} == 0x000B or # VT
1937              $self->{next_char} == 0x000C or # FF
1938              $self->{next_char} == 0x0020) { # SP
1939            !!!cp (118);
1940            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1941            !!!next-input-character;
1942            redo A;
1943          } elsif ($self->{next_char} == 0x003E) { # >
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (119);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1949              if ($self->{current_token}->{attributes}) {
1950                !!!cp (120);
1951                !!!parse-error (type => 'end tag attribute');
1952              } else {
1953                ## NOTE: This state should never be reached.
1954                !!!cp (121);
1955              }
1956            } else {
1957              die "$0: $self->{current_token}->{type}: Unknown token type";
1958            }
1959            $self->{state} = DATA_STATE;
1960            !!!next-input-character;
1961    
1962        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1963          $self->{current_attribute}->{value} .= '&';  
1964            redo A;
1965          } elsif ($self->{next_char} == 0x002F) { # /
1966            !!!cp (122);
1967            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1968            !!!next-input-character;
1969            redo A;
1970          } elsif ($self->{next_char} == -1) {
1971            !!!parse-error (type => 'unclosed tag');
1972            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1973              !!!cp (122.3);
1974              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1975            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1976              if ($self->{current_token}->{attributes}) {
1977                !!!cp (122.1);
1978                !!!parse-error (type => 'end tag attribute');
1979              } else {
1980                ## NOTE: This state should never be reached.
1981                !!!cp (122.2);
1982              }
1983            } else {
1984              die "$0: $self->{current_token}->{type}: Unknown token type";
1985            }
1986            $self->{state} = DATA_STATE;
1987            ## Reconsume.
1988            !!!emit ($self->{current_token}); # start tag or end tag
1989            redo A;
1990        } else {        } else {
1991          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1992          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1993            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1994            ## reconsume
1995            redo A;
1996        }        }
1997        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1998          if ($self->{next_char} == 0x003E) { # >
1999            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
2000              !!!cp ('124.2');
2001              !!!parse-error (type => 'nestc', token => $self->{current_token});
2002              ## TODO: Different type than slash in start tag
2003              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2004              if ($self->{current_token}->{attributes}) {
2005                !!!cp ('124.4');
2006                !!!parse-error (type => 'end tag attribute');
2007              } else {
2008                !!!cp ('124.5');
2009              }
2010              ## TODO: Test |<title></title/>|
2011            } else {
2012              !!!cp ('124.3');
2013              $self->{self_closing} = 1;
2014            }
2015    
2016        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2017        # next-input-character is already done          !!!next-input-character;
       redo A;  
     } elsif ($self->{state} eq 'bogus comment') {  
       ## (only happen if PCDATA state)  
         
       my $token = {type => 'comment', data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = 'data';  
           !!!next-input-character;  
   
           !!!emit ($token);  
   
           redo A;  
         } elsif ($self->{next_input_character} == -1) {  
           $self->{state} = 'data';  
           ## reconsume  
2018    
2019            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
2020    
2021            redo A;          redo A;
2022          } elsif ($self->{next_char} == -1) {
2023            !!!parse-error (type => 'unclosed tag');
2024            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2025              !!!cp (124.7);
2026              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2027            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2028              if ($self->{current_token}->{attributes}) {
2029                !!!cp (124.5);
2030                !!!parse-error (type => 'end tag attribute');
2031              } else {
2032                ## NOTE: This state should never be reached.
2033                !!!cp (124.6);
2034              }
2035          } else {          } else {
2036            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2037          }          }
2038        } # BC          $self->{state} = DATA_STATE;
2039      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2040            !!!emit ($self->{current_token}); # start tag or end tag
2041            redo A;
2042          } else {
2043            !!!cp ('124.4');
2044            !!!parse-error (type => 'nestc');
2045            ## TODO: This error type is wrong.
2046            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2047            ## Reconsume.
2048            redo A;
2049          }
2050        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2051        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2052    
2053        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2054        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2055                
2056        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2057            !!!cp (124);
2058            $self->{state} = DATA_STATE;
2059          !!!next-input-character;          !!!next-input-character;
2060          push @next_char, $self->{next_input_character};  
2061          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2062            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2063            $self->{state} = 'comment';        } elsif ($self->{next_char} == -1) {
2064            !!!next-input-character;          !!!cp (125);
2065            redo A;          $self->{state} = DATA_STATE;
2066          }          ## reconsume
2067        } elsif ($self->{next_input_character} == 0x0044 or # D  
2068                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2069            redo A;
2070          } else {
2071            !!!cp (126);
2072            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2073            $self->{read_until}->($self->{current_token}->{data},
2074                                  q[>],
2075                                  length $self->{current_token}->{data});
2076    
2077            ## Stay in the state.
2078          !!!next-input-character;          !!!next-input-character;
2079          push @next_char, $self->{next_input_character};          redo A;
2080          if ($self->{next_input_character} == 0x004F or # O        }
2081              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2082            !!!next-input-character;        ## (only happen if PCDATA state)
2083            push @next_char, $self->{next_input_character};        
2084            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2085                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2086              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2087              push @next_char, $self->{next_input_character};          !!!next-input-character;
2088              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2089                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2090                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2091                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2092                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2093                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2094                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2095                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2096                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2097                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2098                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2099                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2100                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2101                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2102                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2103                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2104                      !!!next-input-character;          redo A;
2105                      redo A;        } else {
2106                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2107        }        }
2108    
2109        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment',
2110        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2111        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2112        $self->{state} = 'bogus comment';        ## Reconsume.
2113          $self->{state} = BOGUS_COMMENT_STATE;
2114          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2115                                    line => $self->{line_prev},
2116                                    column => $self->{column_prev} - 1,
2117                                   };
2118        redo A;        redo A;
2119              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2120        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2121        ## 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?          !!!cp (127);
2122      } elsif ($self->{state} eq 'comment') {          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2123        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2124          $self->{state} = 'comment dash';                                    column => $self->{column_prev} - 2,
2125                                     };
2126            $self->{state} = COMMENT_START_STATE;
2127            !!!next-input-character;
2128            redo A;
2129          } else {
2130            !!!cp (128);
2131            !!!parse-error (type => 'bogus comment',
2132                            line => $self->{line_prev},
2133                            column => $self->{column_prev} - 2);
2134            $self->{state} = BOGUS_COMMENT_STATE;
2135            ## Reconsume.
2136            $self->{current_token} = {type => COMMENT_TOKEN,
2137                                      data => '-',
2138                                      line => $self->{line_prev},
2139                                      column => $self->{column_prev} - 2,
2140                                     };
2141            redo A;
2142          }
2143        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2144          ## ASCII case-insensitive.
2145          if ($self->{next_char} == [
2146                undef,
2147                0x004F, # O
2148                0x0043, # C
2149                0x0054, # T
2150                0x0059, # Y
2151                0x0050, # P
2152              ]->[length $self->{state_keyword}] or
2153              $self->{next_char} == [
2154                undef,
2155                0x006F, # o
2156                0x0063, # c
2157                0x0074, # t
2158                0x0079, # y
2159                0x0070, # p
2160              ]->[length $self->{state_keyword}]) {
2161            !!!cp (131);
2162            ## Stay in the state.
2163            $self->{state_keyword} .= chr $self->{next_char};
2164            !!!next-input-character;
2165            redo A;
2166          } elsif ((length $self->{state_keyword}) == 6 and
2167                   ($self->{next_char} == 0x0045 or # E
2168                    $self->{next_char} == 0x0065)) { # e
2169            !!!cp (129);
2170            $self->{state} = DOCTYPE_STATE;
2171            $self->{current_token} = {type => DOCTYPE_TOKEN,
2172                                      quirks => 1,
2173                                      line => $self->{line_prev},
2174                                      column => $self->{column_prev} - 7,
2175                                     };
2176            !!!next-input-character;
2177            redo A;
2178          } else {
2179            !!!cp (132);        
2180            !!!parse-error (type => 'bogus comment',
2181                            line => $self->{line_prev},
2182                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2183            $self->{state} = BOGUS_COMMENT_STATE;
2184            ## Reconsume.
2185            $self->{current_token} = {type => COMMENT_TOKEN,
2186                                      data => $self->{state_keyword},
2187                                      line => $self->{line_prev},
2188                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2189                                     };
2190            redo A;
2191          }
2192        } elsif ($self->{state} == MD_CDATA_STATE) {
2193          if ($self->{next_char} == {
2194                '[' => 0x0043, # C
2195                '[C' => 0x0044, # D
2196                '[CD' => 0x0041, # A
2197                '[CDA' => 0x0054, # T
2198                '[CDAT' => 0x0041, # A
2199              }->{$self->{state_keyword}}) {
2200            !!!cp (135.1);
2201            ## Stay in the state.
2202            $self->{state_keyword} .= chr $self->{next_char};
2203            !!!next-input-character;
2204            redo A;
2205          } elsif ($self->{state_keyword} eq '[CDATA' and
2206                   $self->{next_char} == 0x005B) { # [
2207            !!!cp (135.2);
2208            $self->{current_token} = {type => CHARACTER_TOKEN,
2209                                      data => '',
2210                                      line => $self->{line_prev},
2211                                      column => $self->{column_prev} - 7};
2212            $self->{state} = CDATA_SECTION_STATE;
2213            !!!next-input-character;
2214            redo A;
2215          } else {
2216            !!!cp (135.3);
2217            !!!parse-error (type => 'bogus comment',
2218                            line => $self->{line_prev},
2219                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2220            $self->{state} = BOGUS_COMMENT_STATE;
2221            ## Reconsume.
2222            $self->{current_token} = {type => COMMENT_TOKEN,
2223                                      data => $self->{state_keyword},
2224                                      line => $self->{line_prev},
2225                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2226                                     };
2227            redo A;
2228          }
2229        } elsif ($self->{state} == COMMENT_START_STATE) {
2230          if ($self->{next_char} == 0x002D) { # -
2231            !!!cp (137);
2232            $self->{state} = COMMENT_START_DASH_STATE;
2233            !!!next-input-character;
2234            redo A;
2235          } elsif ($self->{next_char} == 0x003E) { # >
2236            !!!cp (138);
2237            !!!parse-error (type => 'bogus comment');
2238            $self->{state} = DATA_STATE;
2239            !!!next-input-character;
2240    
2241            !!!emit ($self->{current_token}); # comment
2242    
2243            redo A;
2244          } elsif ($self->{next_char} == -1) {
2245            !!!cp (139);
2246            !!!parse-error (type => 'unclosed comment');
2247            $self->{state} = DATA_STATE;
2248            ## reconsume
2249    
2250            !!!emit ($self->{current_token}); # comment
2251    
2252            redo A;
2253          } else {
2254            !!!cp (140);
2255            $self->{current_token}->{data} # comment
2256                .= chr ($self->{next_char});
2257            $self->{state} = COMMENT_STATE;
2258            !!!next-input-character;
2259            redo A;
2260          }
2261        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2262          if ($self->{next_char} == 0x002D) { # -
2263            !!!cp (141);
2264            $self->{state} = COMMENT_END_STATE;
2265          !!!next-input-character;          !!!next-input-character;
2266          redo A;          redo A;
2267        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2268            !!!cp (142);
2269            !!!parse-error (type => 'bogus comment');
2270            $self->{state} = DATA_STATE;
2271            !!!next-input-character;
2272    
2273            !!!emit ($self->{current_token}); # comment
2274    
2275            redo A;
2276          } elsif ($self->{next_char} == -1) {
2277            !!!cp (143);
2278          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2279          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2280          ## reconsume          ## reconsume
2281    
2282          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2283    
2284          redo A;          redo A;
2285        } else {        } else {
2286          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (144);
2287            $self->{current_token}->{data} # comment
2288                .= '-' . chr ($self->{next_char});
2289            $self->{state} = COMMENT_STATE;
2290            !!!next-input-character;
2291            redo A;
2292          }
2293        } elsif ($self->{state} == COMMENT_STATE) {
2294          if ($self->{next_char} == 0x002D) { # -
2295            !!!cp (145);
2296            $self->{state} = COMMENT_END_DASH_STATE;
2297            !!!next-input-character;
2298            redo A;
2299          } elsif ($self->{next_char} == -1) {
2300            !!!cp (146);
2301            !!!parse-error (type => 'unclosed comment');
2302            $self->{state} = DATA_STATE;
2303            ## reconsume
2304    
2305            !!!emit ($self->{current_token}); # comment
2306    
2307            redo A;
2308          } else {
2309            !!!cp (147);
2310            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2311            $self->{read_until}->($self->{current_token}->{data},
2312                                  q[-],
2313                                  length $self->{current_token}->{data});
2314    
2315          ## Stay in the state          ## Stay in the state
2316          !!!next-input-character;          !!!next-input-character;
2317          redo A;          redo A;
2318        }        }
2319      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2320        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2321          $self->{state} = 'comment end';          !!!cp (148);
2322            $self->{state} = COMMENT_END_STATE;
2323          !!!next-input-character;          !!!next-input-character;
2324          redo A;          redo A;
2325        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2326            !!!cp (149);
2327          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2328          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2329          ## reconsume          ## reconsume
2330    
2331          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2332    
2333          redo A;          redo A;
2334        } else {        } else {
2335          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2336          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2337            $self->{state} = COMMENT_STATE;
2338          !!!next-input-character;          !!!next-input-character;
2339          redo A;          redo A;
2340        }        }
2341      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2342        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2343          $self->{state} = 'data';          !!!cp (151);
2344            $self->{state} = DATA_STATE;
2345          !!!next-input-character;          !!!next-input-character;
2346    
2347          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2348    
2349          redo A;          redo A;
2350        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2351          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2352            !!!parse-error (type => 'dash in comment',
2353                            line => $self->{line_prev},
2354                            column => $self->{column_prev});
2355          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2356          ## Stay in the state          ## Stay in the state
2357          !!!next-input-character;          !!!next-input-character;
2358          redo A;          redo A;
2359        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2360            !!!cp (153);
2361          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2362          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2363          ## reconsume          ## reconsume
2364    
2365          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
2366    
2367          redo A;          redo A;
2368        } else {        } else {
2369          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2370          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2371          $self->{state} = 'comment';                          line => $self->{line_prev},
2372                            column => $self->{column_prev});
2373            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2374            $self->{state} = COMMENT_STATE;
2375          !!!next-input-character;          !!!next-input-character;
2376          redo A;          redo A;
2377        }        }
2378      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2379        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2380            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2381            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2382            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2383            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2384          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2385            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2386          !!!next-input-character;          !!!next-input-character;
2387          redo A;          redo A;
2388        } else {        } else {
2389            !!!cp (156);
2390          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2391          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2392          ## reconsume          ## reconsume
2393          redo A;          redo A;
2394        }        }
2395      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2396        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2397            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2398            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2399            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2400            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2401            !!!cp (157);
2402          ## Stay in the state          ## Stay in the state
2403          !!!next-input-character;          !!!next-input-character;
2404          redo A;          redo A;
2405        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2406            !!!cp (158);
2407          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2408          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2409          !!!next-input-character;          !!!next-input-character;
2410    
2411          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2412    
2413          redo A;          redo A;
2414        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2415            !!!cp (159);
2416          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2417          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2418          ## reconsume          ## reconsume
2419    
2420          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2421    
2422          redo A;          redo A;
2423        } else {        } else {
2424          $self->{current_token}          !!!cp (160);
2425              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
2426                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2427  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2428          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2429          !!!next-input-character;          !!!next-input-character;
2430          redo A;          redo A;
2431        }        }
2432      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2433  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2434        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2435            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2436            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2437            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2438            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2439          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2440            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2441          !!!next-input-character;          !!!next-input-character;
2442          redo A;          redo A;
2443        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2444          $self->{state} = 'data';          !!!cp (162);
2445            $self->{state} = DATA_STATE;
2446          !!!next-input-character;          !!!next-input-character;
2447    
2448          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2449    
2450          redo A;          redo A;
2451        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2452            !!!cp (163);
2453          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2454          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2455          ## reconsume          ## reconsume
2456    
2457          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2458          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2459    
2460          redo A;          redo A;
2461        } else {        } else {
2462            !!!cp (164);
2463          $self->{current_token}->{name}          $self->{current_token}->{name}
2464            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2465          ## Stay in the state          ## Stay in the state
2466          !!!next-input-character;          !!!next-input-character;
2467          redo A;          redo A;
2468        }        }
2469      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2470        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2471            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2472            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2473            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2474            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2475            !!!cp (165);
2476          ## Stay in the state          ## Stay in the state
2477          !!!next-input-character;          !!!next-input-character;
2478          redo A;          redo A;
2479        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2480          $self->{state} = 'data';          !!!cp (166);
2481            $self->{state} = DATA_STATE;
2482          !!!next-input-character;          !!!next-input-character;
2483    
2484          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2485    
2486          redo A;          redo A;
2487        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2488            !!!cp (167);
2489          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2490          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2491          ## reconsume          ## reconsume
2492    
2493          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2494          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2495    
2496          redo A;          redo A;
2497        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2498                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2499            $self->{state} = PUBLIC_STATE;
2500            $self->{state_keyword} = chr $self->{next_char};
2501          !!!next-input-character;          !!!next-input-character;
2502          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2503              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{next_char} == 0x0053 or # S
2504            !!!next-input-character;                 $self->{next_char} == 0x0073) { # s
2505            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2506                $self->{next_input_character} == 0x0062) { # b          $self->{state_keyword} = chr $self->{next_char};
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = 'before DOCTYPE public identifier';  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
2507          !!!next-input-character;          !!!next-input-character;
2508          if ($self->{next_input_character} == 0x0059 or # Y          redo A;
             $self->{next_input_character} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_input_character} == 0x0053 or # S  
               $self->{next_input_character} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0045 or # E  
                   $self->{next_input_character} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x004D or # M  
                     $self->{next_input_character} == 0x006D) { # m  
                   $self->{state} = 'before DOCTYPE system identifier';  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2509        } else {        } else {
2510            !!!cp (180);
2511            !!!parse-error (type => 'string after DOCTYPE name');
2512            $self->{current_token}->{quirks} = 1;
2513    
2514            $self->{state} = BOGUS_DOCTYPE_STATE;
2515          !!!next-input-character;          !!!next-input-character;
2516          #          redo A;
2517        }        }
2518        } elsif ($self->{state} == PUBLIC_STATE) {
2519          ## ASCII case-insensitive
2520          if ($self->{next_char} == [
2521                undef,
2522                0x0055, # U
2523                0x0042, # B
2524                0x004C, # L
2525                0x0049, # I
2526              ]->[length $self->{state_keyword}] or
2527              $self->{next_char} == [
2528                undef,
2529                0x0075, # u
2530                0x0062, # b
2531                0x006C, # l
2532                0x0069, # i
2533              ]->[length $self->{state_keyword}]) {
2534            !!!cp (175);
2535            ## Stay in the state.
2536            $self->{state_keyword} .= chr $self->{next_char};
2537            !!!next-input-character;
2538            redo A;
2539          } elsif ((length $self->{state_keyword}) == 5 and
2540                   ($self->{next_char} == 0x0043 or # C
2541                    $self->{next_char} == 0x0063)) { # c
2542            !!!cp (168);
2543            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2544            !!!next-input-character;
2545            redo A;
2546          } else {
2547            !!!cp (169);
2548            !!!parse-error (type => 'string after DOCTYPE name',
2549                            line => $self->{line_prev},
2550                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2551            $self->{current_token}->{quirks} = 1;
2552    
2553            $self->{state} = BOGUS_DOCTYPE_STATE;
2554            ## Reconsume.
2555            redo A;
2556          }
2557        } elsif ($self->{state} == SYSTEM_STATE) {
2558          ## ASCII case-insensitive
2559          if ($self->{next_char} == [
2560                undef,
2561                0x0059, # Y
2562                0x0053, # S
2563                0x0054, # T
2564                0x0045, # E
2565              ]->[length $self->{state_keyword}] or
2566              $self->{next_char} == [
2567                undef,
2568                0x0079, # y
2569                0x0073, # s
2570                0x0074, # t
2571                0x0065, # e
2572              ]->[length $self->{state_keyword}]) {
2573            !!!cp (170);
2574            ## Stay in the state.
2575            $self->{state_keyword} .= chr $self->{next_char};
2576            !!!next-input-character;
2577            redo A;
2578          } elsif ((length $self->{state_keyword}) == 5 and
2579                   ($self->{next_char} == 0x004D or # M
2580                    $self->{next_char} == 0x006D)) { # m
2581            !!!cp (171);
2582            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2583            !!!next-input-character;
2584            redo A;
2585          } else {
2586            !!!cp (172);
2587            !!!parse-error (type => 'string after DOCTYPE name',
2588                            line => $self->{line_prev},
2589                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2590            $self->{current_token}->{quirks} = 1;
2591    
2592        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2593        $self->{state} = 'bogus DOCTYPE';          ## Reconsume.
2594        # next-input-character is already done          redo A;
2595        redo A;        }
2596      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2597        if ({        if ({
2598              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2599              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2600            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2601            !!!cp (181);
2602          ## Stay in the state          ## Stay in the state
2603          !!!next-input-character;          !!!next-input-character;
2604          redo A;          redo A;
2605        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2606            !!!cp (182);
2607          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2608          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2609          !!!next-input-character;          !!!next-input-character;
2610          redo A;          redo A;
2611        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2612            !!!cp (183);
2613          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2614          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2615          !!!next-input-character;          !!!next-input-character;
2616          redo A;          redo A;
2617        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2618            !!!cp (184);
2619          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2620    
2621          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2622          !!!next-input-character;          !!!next-input-character;
2623    
2624          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2625          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2626    
2627          redo A;          redo A;
2628        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2629            !!!cp (185);
2630          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2631    
2632          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2633          ## reconsume          ## reconsume
2634    
2635          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2636          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2637    
2638          redo A;          redo A;
2639        } else {        } else {
2640            !!!cp (186);
2641          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2642          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2643    
2644            $self->{state} = BOGUS_DOCTYPE_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646          redo A;          redo A;
2647        }        }
2648      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2649        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2650          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2651            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2652          !!!next-input-character;          !!!next-input-character;
2653          redo A;          redo A;
2654        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2655            !!!cp (188);
2656          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2657    
2658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2659            !!!next-input-character;
2660    
2661            $self->{current_token}->{quirks} = 1;
2662            !!!emit ($self->{current_token}); # DOCTYPE
2663    
2664            redo A;
2665          } elsif ($self->{next_char} == -1) {
2666            !!!cp (189);
2667            !!!parse-error (type => 'unclosed PUBLIC literal');
2668    
2669            $self->{state} = DATA_STATE;
2670          ## reconsume          ## reconsume
2671    
2672          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2673          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2674    
2675          redo A;          redo A;
2676        } else {        } else {
2677            !!!cp (190);
2678          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2679              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2680            $self->{read_until}->($self->{current_token}->{public_identifier},
2681                                  q[">],
2682                                  length $self->{current_token}->{public_identifier});
2683    
2684          ## Stay in the state          ## Stay in the state
2685          !!!next-input-character;          !!!next-input-character;
2686          redo A;          redo A;
2687        }        }
2688      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2689        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2690          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2691            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2692            !!!next-input-character;
2693            redo A;
2694          } elsif ($self->{next_char} == 0x003E) { # >
2695            !!!cp (192);
2696            !!!parse-error (type => 'unclosed PUBLIC literal');
2697    
2698            $self->{state} = DATA_STATE;
2699          !!!next-input-character;          !!!next-input-character;
2700    
2701            $self->{current_token}->{quirks} = 1;
2702            !!!emit ($self->{current_token}); # DOCTYPE
2703    
2704          redo A;          redo A;
2705        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2706            !!!cp (193);
2707          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2708    
2709          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2710          ## reconsume          ## reconsume
2711    
2712          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2713          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2714    
2715          redo A;          redo A;
2716        } else {        } else {
2717            !!!cp (194);
2718          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2719              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2720            $self->{read_until}->($self->{current_token}->{public_identifier},
2721                                  q['>],
2722                                  length $self->{current_token}->{public_identifier});
2723    
2724          ## Stay in the state          ## Stay in the state
2725          !!!next-input-character;          !!!next-input-character;
2726          redo A;          redo A;
2727        }        }
2728      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2729        if ({        if ({
2730              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2731              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2732            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2733            !!!cp (195);
2734          ## Stay in the state          ## Stay in the state
2735          !!!next-input-character;          !!!next-input-character;
2736          redo A;          redo A;
2737        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2738            !!!cp (196);
2739          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2740          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2744            !!!cp (197);
2745          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2746          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2747          !!!next-input-character;          !!!next-input-character;
2748          redo A;          redo A;
2749        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2750          $self->{state} = 'data';          !!!cp (198);
2751            $self->{state} = DATA_STATE;
2752          !!!next-input-character;          !!!next-input-character;
2753    
2754          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2755    
2756          redo A;          redo A;
2757        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2758            !!!cp (199);
2759          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2760    
2761          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2762          ## recomsume          ## reconsume
2763    
2764          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2765          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2766    
2767          redo A;          redo A;
2768        } else {        } else {
2769            !!!cp (200);
2770          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2771          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2772    
2773            $self->{state} = BOGUS_DOCTYPE_STATE;
2774          !!!next-input-character;          !!!next-input-character;
2775          redo A;          redo A;
2776        }        }
2777      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2778        if ({        if ({
2779              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2780              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2781            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2782            !!!cp (201);
2783          ## Stay in the state          ## Stay in the state
2784          !!!next-input-character;          !!!next-input-character;
2785          redo A;          redo A;
2786        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2787            !!!cp (202);
2788          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2789          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2790          !!!next-input-character;          !!!next-input-character;
2791          redo A;          redo A;
2792        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2793            !!!cp (203);
2794          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2795          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2796          !!!next-input-character;          !!!next-input-character;
2797          redo A;          redo A;
2798        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2799            !!!cp (204);
2800          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2801          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2802          !!!next-input-character;          !!!next-input-character;
2803    
2804          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2805          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2806    
2807          redo A;          redo A;
2808        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2809            !!!cp (205);
2810          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2811    
2812          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2813          ## recomsume          ## reconsume
2814    
2815          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2816          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2817    
2818          redo A;          redo A;
2819        } else {        } else {
2820          !!!parse-error (type => 'string after PUBLIC literal');          !!!cp (206);
2821          $self->{state} = 'bogus DOCTYPE';          !!!parse-error (type => 'string after SYSTEM');
2822            $self->{current_token}->{quirks} = 1;
2823    
2824            $self->{state} = BOGUS_DOCTYPE_STATE;
2825          !!!next-input-character;          !!!next-input-character;
2826          redo A;          redo A;
2827        }        }
2828      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2829        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2830          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2831            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2832          !!!next-input-character;          !!!next-input-character;
2833          redo A;          redo A;
2834        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2835            !!!cp (208);
2836          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2837    
2838          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2839            !!!next-input-character;
2840    
2841            $self->{current_token}->{quirks} = 1;
2842            !!!emit ($self->{current_token}); # DOCTYPE
2843    
2844            redo A;
2845          } elsif ($self->{next_char} == -1) {
2846            !!!cp (209);
2847            !!!parse-error (type => 'unclosed SYSTEM literal');
2848    
2849            $self->{state} = DATA_STATE;
2850          ## reconsume          ## reconsume
2851    
2852          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2853          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2854    
2855          redo A;          redo A;
2856        } else {        } else {
2857            !!!cp (210);
2858          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2859              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2860            $self->{read_until}->($self->{current_token}->{system_identifier},
2861                                  q[">],
2862                                  length $self->{current_token}->{system_identifier});
2863    
2864          ## Stay in the state          ## Stay in the state
2865          !!!next-input-character;          !!!next-input-character;
2866          redo A;          redo A;
2867        }        }
2868      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2869        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2870          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2871            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2872            !!!next-input-character;
2873            redo A;
2874          } elsif ($self->{next_char} == 0x003E) { # >
2875            !!!cp (212);
2876            !!!parse-error (type => 'unclosed SYSTEM literal');
2877    
2878            $self->{state} = DATA_STATE;
2879          !!!next-input-character;          !!!next-input-character;
2880    
2881            $self->{current_token}->{quirks} = 1;
2882            !!!emit ($self->{current_token}); # DOCTYPE
2883    
2884          redo A;          redo A;
2885        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2886            !!!cp (213);
2887          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2888    
2889          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2890          ## reconsume          ## reconsume
2891    
2892          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2893          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2894    
2895          redo A;          redo A;
2896        } else {        } else {
2897            !!!cp (214);
2898          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2899              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2900            $self->{read_until}->($self->{current_token}->{system_identifier},
2901                                  q['>],
2902                                  length $self->{current_token}->{system_identifier});
2903    
2904          ## Stay in the state          ## Stay in the state
2905          !!!next-input-character;          !!!next-input-character;
2906          redo A;          redo A;
2907        }        }
2908      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2909        if ({        if ({
2910              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2911              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2912            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2913            !!!cp (215);
2914          ## Stay in the state          ## Stay in the state
2915          !!!next-input-character;          !!!next-input-character;
2916          redo A;          redo A;
2917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2918          $self->{state} = 'data';          !!!cp (216);
2919            $self->{state} = DATA_STATE;
2920          !!!next-input-character;          !!!next-input-character;
2921    
2922          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2923    
2924          redo A;          redo A;
2925        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2926            !!!cp (217);
2927          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2928            $self->{state} = DATA_STATE;
2929            ## reconsume
2930    
2931          $self->{state} = 'data';          $self->{current_token}->{quirks} = 1;
         ## recomsume  
   
         delete $self->{current_token}->{correct};  
2932          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2933    
2934          redo A;          redo A;
2935        } else {        } else {
2936            !!!cp (218);
2937          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2938          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2939    
2940            $self->{state} = BOGUS_DOCTYPE_STATE;
2941          !!!next-input-character;          !!!next-input-character;
2942          redo A;          redo A;
2943        }        }
2944      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2945        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2946          $self->{state} = 'data';          !!!cp (219);
2947            $self->{state} = DATA_STATE;
2948          !!!next-input-character;          !!!next-input-character;
2949    
         delete $self->{current_token}->{correct};  
2950          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2951    
2952          redo A;          redo A;
2953        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2954            !!!cp (220);
2955          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2956          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2957          ## reconsume          ## reconsume
2958    
         delete $self->{current_token}->{correct};  
2959          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
2960    
2961          redo A;          redo A;
2962        } else {        } else {
2963            !!!cp (221);
2964            my $s = '';
2965            $self->{read_until}->($s, q[>], 0);
2966    
2967          ## Stay in the state          ## Stay in the state
2968          !!!next-input-character;          !!!next-input-character;
2969          redo A;          redo A;
2970        }        }
2971      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2972        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2973      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2974    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2975          
2976    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2977  } # _get_next_token          !!!cp (221.1);
2978            $self->{state} = CDATA_SECTION_MSE1_STATE;
2979            !!!next-input-character;
2980            redo A;
2981          } elsif ($self->{next_char} == -1) {
2982            $self->{state} = DATA_STATE;
2983            !!!next-input-character;
2984            if (length $self->{current_token}->{data}) { # character
2985              !!!cp (221.2);
2986              !!!emit ($self->{current_token}); # character
2987            } else {
2988              !!!cp (221.3);
2989              ## No token to emit. $self->{current_token} is discarded.
2990            }        
2991            redo A;
2992          } else {
2993            !!!cp (221.4);
2994            $self->{current_token}->{data} .= chr $self->{next_char};
2995            $self->{read_until}->($self->{current_token}->{data},
2996                                  q<]>,
2997                                  length $self->{current_token}->{data});
2998    
2999  sub _tokenize_attempt_to_consume_an_entity ($) {          ## Stay in the state.
3000    my $self = shift;          !!!next-input-character;
3001            redo A;
3002          }
3003    
3004    if ({        ## ISSUE: "text tokens" in spec.
3005         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3006         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
3007        }->{$self->{next_input_character}}) {          !!!cp (221.5);
3008      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
3009      ## No error          !!!next-input-character;
3010      return undef;          redo A;
3011    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3012      !!!next-input-character;          !!!cp (221.6);
3013      if ($self->{next_input_character} == 0x0078 or # x          $self->{current_token}->{data} .= ']';
3014          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3015        my $num;          ## Reconsume.
3016        X: {          redo A;
3017          my $x_char = $self->{next_input_character};        }
3018          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3019          if (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x003E) { # >
3020              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3021            $num ||= 0;          !!!next-input-character;
3022            $num *= 0x10;          if (length $self->{current_token}->{data}) { # character
3023            $num += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3024            redo X;            !!!emit ($self->{current_token}); # character
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           ## ISSUE: the spec says U+0078, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           ## ISSUE: the spec says U+0058, which is apparently incorrect  
           $num ||= 0;  
           $num *= 0x10;  
           $num += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $num) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           $self->{next_input_character} = 0x0023; # #  
           !!!back-next-input-character ($x_char);  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3025          } else {          } else {
3026            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3027              ## No token to emit. $self->{current_token} is discarded.
3028          }          }
3029            redo A;
3030          } elsif ($self->{next_char} == 0x005D) { # ]
3031            !!!cp (221.9); # character
3032            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3033            ## Stay in the state.
3034            !!!next-input-character;
3035            redo A;
3036          } else {
3037            !!!cp (221.11);
3038            $self->{current_token}->{data} .= ']]'; # character
3039            $self->{state} = CDATA_SECTION_STATE;
3040            ## Reconsume.
3041            redo A;
3042          }
3043        } elsif ($self->{state} == ENTITY_STATE) {
3044          if ({
3045            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3046            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3047            $self->{entity_additional} => 1,
3048          }->{$self->{next_char}}) {
3049            !!!cp (1001);
3050            ## Don't consume
3051            ## No error
3052            ## Return nothing.
3053            #
3054          } elsif ($self->{next_char} == 0x0023) { # #
3055            !!!cp (999);
3056            $self->{state} = ENTITY_HASH_STATE;
3057            $self->{state_keyword} = '#';
3058            !!!next-input-character;
3059            redo A;
3060          } elsif ((0x0041 <= $self->{next_char} and
3061                    $self->{next_char} <= 0x005A) or # A..Z
3062                   (0x0061 <= $self->{next_char} and
3063                    $self->{next_char} <= 0x007A)) { # a..z
3064            !!!cp (998);
3065            require Whatpm::_NamedEntityList;
3066            $self->{state} = ENTITY_NAME_STATE;
3067            $self->{state_keyword} = chr $self->{next_char};
3068            $self->{entity__value} = $self->{state_keyword};
3069            $self->{entity__match} = 0;
3070            !!!next-input-character;
3071            redo A;
3072          } else {
3073            !!!cp (1027);
3074            !!!parse-error (type => 'bare ero');
3075            ## Return nothing.
3076            #
3077          }
3078    
3079          ## TODO: check the definition for |a valid Unicode character|.        ## NOTE: No character is consumed by the "consume a character
3080          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>        ## reference" algorithm.  In other word, there is an "&" character
3081          if ($num > 1114111 or $num == 0) {        ## that does not introduce a character reference, which would be
3082            $num = 0xFFFD; # REPLACEMENT CHARACTER        ## appended to the parent element or the attribute value in later
3083            ## ISSUE: Why this is not an error?        ## process of the tokenizer.
3084          } elsif (0x80 <= $num and $num <= 0x9F) {  
3085            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);        if ($self->{prev_state} == DATA_STATE) {
3086            $num = $c1_entity_char->{$num};          !!!cp (997);
3087          }          $self->{state} = $self->{prev_state};
3088            ## Reconsume.
3089          return {type => 'character', data => chr $num};          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3090        } # X                    line => $self->{line_prev},
3091      } elsif (0x0030 <= $self->{next_input_character} and                    column => $self->{column_prev},
3092               $self->{next_input_character} <= 0x0039) { # 0..9                   });
3093        my $code = $self->{next_input_character} - 0x0030;          redo A;
3094        !!!next-input-character;        } else {
3095                  !!!cp (996);
3096        while (0x0030 <= $self->{next_input_character} and          $self->{current_attribute}->{value} .= '&';
3097                  $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = $self->{prev_state};
3098          $code *= 10;          ## Reconsume.
3099          $code += $self->{next_input_character} - 0x0030;          redo A;
3100          }
3101        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3102          if ($self->{next_char} == 0x0078 or # x
3103              $self->{next_char} == 0x0058) { # X
3104            !!!cp (995);
3105            $self->{state} = HEXREF_X_STATE;
3106            $self->{state_keyword} .= chr $self->{next_char};
3107            !!!next-input-character;
3108            redo A;
3109          } elsif (0x0030 <= $self->{next_char} and
3110                   $self->{next_char} <= 0x0039) { # 0..9
3111            !!!cp (994);
3112            $self->{state} = NCR_NUM_STATE;
3113            $self->{state_keyword} = $self->{next_char} - 0x0030;
3114            !!!next-input-character;
3115            redo A;
3116          } else {
3117            !!!parse-error (type => 'bare nero',
3118                            line => $self->{line_prev},
3119                            column => $self->{column_prev} - 1);
3120    
3121            ## NOTE: According to the spec algorithm, nothing is returned,
3122            ## and then "&#" is appended to the parent element or the attribute
3123            ## value in the later processing.
3124    
3125            if ($self->{prev_state} == DATA_STATE) {
3126              !!!cp (1019);
3127              $self->{state} = $self->{prev_state};
3128              ## Reconsume.
3129              !!!emit ({type => CHARACTER_TOKEN,
3130                        data => '&#',
3131                        line => $self->{line_prev},
3132                        column => $self->{column_prev} - 1,
3133                       });
3134              redo A;
3135            } else {
3136              !!!cp (993);
3137              $self->{current_attribute}->{value} .= '&#';
3138              $self->{state} = $self->{prev_state};
3139              ## Reconsume.
3140              redo A;
3141            }
3142          }
3143        } elsif ($self->{state} == NCR_NUM_STATE) {
3144          if (0x0030 <= $self->{next_char} and
3145              $self->{next_char} <= 0x0039) { # 0..9
3146            !!!cp (1012);
3147            $self->{state_keyword} *= 10;
3148            $self->{state_keyword} += $self->{next_char} - 0x0030;
3149                    
3150            ## Stay in the state.
3151            !!!next-input-character;
3152            redo A;
3153          } elsif ($self->{next_char} == 0x003B) { # ;
3154            !!!cp (1013);
3155          !!!next-input-character;          !!!next-input-character;
3156            #
3157          } else {
3158            !!!cp (1014);
3159            !!!parse-error (type => 'no refc');
3160            ## Reconsume.
3161            #
3162        }        }
3163    
3164        if ($self->{next_input_character} == 0x003B) { # ;        my $code = $self->{state_keyword};
3165          my $l = $self->{line_prev};
3166          my $c = $self->{column_prev};
3167          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3168            !!!cp (1015);
3169            !!!parse-error (type => 'invalid character reference',
3170                            text => (sprintf 'U+%04X', $code),
3171                            line => $l, column => $c);
3172            $code = 0xFFFD;
3173          } elsif ($code > 0x10FFFF) {
3174            !!!cp (1016);
3175            !!!parse-error (type => 'invalid character reference',
3176                            text => (sprintf 'U-%08X', $code),
3177                            line => $l, column => $c);
3178            $code = 0xFFFD;
3179          } elsif ($code == 0x000D) {
3180            !!!cp (1017);
3181            !!!parse-error (type => 'CR character reference',
3182                            line => $l, column => $c);
3183            $code = 0x000A;
3184          } elsif (0x80 <= $code and $code <= 0x9F) {
3185            !!!cp (1018);
3186            !!!parse-error (type => 'C1 character reference',
3187                            text => (sprintf 'U+%04X', $code),
3188                            line => $l, column => $c);
3189            $code = $c1_entity_char->{$code};
3190          }
3191    
3192          if ($self->{prev_state} == DATA_STATE) {
3193            !!!cp (992);
3194            $self->{state} = $self->{prev_state};
3195            ## Reconsume.
3196            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3197                      line => $l, column => $c,
3198                     });
3199            redo A;
3200          } else {
3201            !!!cp (991);
3202            $self->{current_attribute}->{value} .= chr $code;
3203            $self->{current_attribute}->{has_reference} = 1;
3204            $self->{state} = $self->{prev_state};
3205            ## Reconsume.
3206            redo A;
3207          }
3208        } elsif ($self->{state} == HEXREF_X_STATE) {
3209          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3210              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3211              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3212            # 0..9, A..F, a..f
3213            !!!cp (990);
3214            $self->{state} = HEXREF_HEX_STATE;
3215            $self->{state_keyword} = 0;
3216            ## Reconsume.
3217            redo A;
3218          } else {
3219            !!!parse-error (type => 'bare hcro',
3220                            line => $self->{line_prev},
3221                            column => $self->{column_prev} - 2);
3222    
3223            ## NOTE: According to the spec algorithm, nothing is returned,
3224            ## and then "&#" followed by "X" or "x" is appended to the parent
3225            ## element or the attribute value in the later processing.
3226    
3227            if ($self->{prev_state} == DATA_STATE) {
3228              !!!cp (1005);
3229              $self->{state} = $self->{prev_state};
3230              ## Reconsume.
3231              !!!emit ({type => CHARACTER_TOKEN,
3232                        data => '&' . $self->{state_keyword},
3233                        line => $self->{line_prev},
3234                        column => $self->{column_prev} - length $self->{state_keyword},
3235                       });
3236              redo A;
3237            } else {
3238              !!!cp (989);
3239              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3240              $self->{state} = $self->{prev_state};
3241              ## Reconsume.
3242              redo A;
3243            }
3244          }
3245        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3246          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3247            # 0..9
3248            !!!cp (1002);
3249            $self->{state_keyword} *= 0x10;
3250            $self->{state_keyword} += $self->{next_char} - 0x0030;
3251            ## Stay in the state.
3252            !!!next-input-character;
3253            redo A;
3254          } elsif (0x0061 <= $self->{next_char} and
3255                   $self->{next_char} <= 0x0066) { # a..f
3256            !!!cp (1003);
3257            $self->{state_keyword} *= 0x10;
3258            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3259            ## Stay in the state.
3260            !!!next-input-character;
3261            redo A;
3262          } elsif (0x0041 <= $self->{next_char} and
3263                   $self->{next_char} <= 0x0046) { # A..F
3264            !!!cp (1004);
3265            $self->{state_keyword} *= 0x10;
3266            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3267            ## Stay in the state.
3268          !!!next-input-character;          !!!next-input-character;
3269            redo A;
3270          } elsif ($self->{next_char} == 0x003B) { # ;
3271            !!!cp (1006);
3272            !!!next-input-character;
3273            #
3274        } else {        } else {
3275          !!!parse-error (type => 'no refc');          !!!cp (1007);
3276            !!!parse-error (type => 'no refc',
3277                            line => $self->{line},
3278                            column => $self->{column});
3279            ## Reconsume.
3280            #
3281        }        }
3282    
3283        ## TODO: check the definition for |a valid Unicode character|.        my $code = $self->{state_keyword};
3284        if ($code > 1114111 or $code == 0) {        my $l = $self->{line_prev};
3285          $code = 0xFFFD; # REPLACEMENT CHARACTER        my $c = $self->{column_prev};
3286          ## ISSUE: Why this is not an error?        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3287            !!!cp (1008);
3288            !!!parse-error (type => 'invalid character reference',
3289                            text => (sprintf 'U+%04X', $code),
3290                            line => $l, column => $c);
3291            $code = 0xFFFD;
3292          } elsif ($code > 0x10FFFF) {
3293            !!!cp (1009);
3294            !!!parse-error (type => 'invalid character reference',
3295                            text => (sprintf 'U-%08X', $code),
3296                            line => $l, column => $c);
3297            $code = 0xFFFD;
3298          } elsif ($code == 0x000D) {
3299            !!!cp (1010);
3300            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3301            $code = 0x000A;
3302        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3303          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!cp (1011);
3304            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3305          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3306        }        }
3307          
3308        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3309      } else {          !!!cp (988);
3310        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3311        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3312        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3313        return undef;                    line => $l, column => $c,
3314      }                   });
3315    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3316              $self->{next_input_character} <= 0x005A) or        } else {
3317             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3318              $self->{next_input_character} <= 0x007A)) {          $self->{current_attribute}->{value} .= chr $code;
3319      my $entity_name = chr $self->{next_input_character};          $self->{current_attribute}->{has_reference} = 1;
3320      !!!next-input-character;          $self->{state} = $self->{prev_state};
3321            ## Reconsume.
3322      my $value = $entity_name;          redo A;
3323      my $match;        }
3324      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3325      our $EntityChar;        if (length $self->{state_keyword} < 30 and
3326              ## NOTE: Some number greater than the maximum length of entity name
3327      while (length $entity_name < 10 and            ((0x0041 <= $self->{next_char} and # a
3328             ## NOTE: Some number greater than the maximum length of entity name              $self->{next_char} <= 0x005A) or # x
3329             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{next_char} and # a
3330               $self->{next_input_character} <= 0x005A) or # x              $self->{next_char} <= 0x007A) or # z
3331              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{next_char} and # 0
3332               $self->{next_input_character} <= 0x007A) or # z              $self->{next_char} <= 0x0039) or # 9
3333              (0x0030 <= $self->{next_input_character} and # 0             $self->{next_char} == 0x003B)) { # ;
3334               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3335              $self->{next_input_character} == 0x003B)) { # ;          $self->{state_keyword} .= chr $self->{next_char};
3336        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{state_keyword}}) {
3337        if (defined $EntityChar->{$entity_name}) {            if ($self->{next_char} == 0x003B) { # ;
3338          $value = $EntityChar->{$entity_name};              !!!cp (1020);
3339          if ($self->{next_input_character} == 0x003B) { # ;              $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3340            $match = 1;              $self->{entity__match} = 1;
3341                !!!next-input-character;
3342                #
3343              } else {
3344                !!!cp (1021);
3345                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3346                $self->{entity__match} = -1;
3347                ## Stay in the state.
3348                !!!next-input-character;
3349                redo A;
3350              }
3351            } else {
3352              !!!cp (1022);
3353              $self->{entity__value} .= chr $self->{next_char};
3354              $self->{entity__match} *= 2;
3355              ## Stay in the state.
3356            !!!next-input-character;            !!!next-input-character;
3357            last;            redo A;
3358            }
3359          }
3360    
3361          my $data;
3362          my $has_ref;
3363          if ($self->{entity__match} > 0) {
3364            !!!cp (1023);
3365            $data = $self->{entity__value};
3366            $has_ref = 1;
3367            #
3368          } elsif ($self->{entity__match} < 0) {
3369            !!!parse-error (type => 'no refc');
3370            if ($self->{prev_state} != DATA_STATE and # in attribute
3371                $self->{entity__match} < -1) {
3372              !!!cp (1024);
3373              $data = '&' . $self->{state_keyword};
3374              #
3375          } else {          } else {
3376            $match = -1;            !!!cp (1025);
3377              $data = $self->{entity__value};
3378              $has_ref = 1;
3379              #
3380          }          }
3381        } else {        } else {
3382          $value .= chr $self->{next_input_character};          !!!cp (1026);
3383            !!!parse-error (type => 'bare ero',
3384                            line => $self->{line_prev},
3385                            column => $self->{column_prev} - length $self->{state_keyword});
3386            $data = '&' . $self->{state_keyword};
3387            #
3388          }
3389      
3390          ## NOTE: In these cases, when a character reference is found,
3391          ## it is consumed and a character token is returned, or, otherwise,
3392          ## nothing is consumed and returned, according to the spec algorithm.
3393          ## In this implementation, anything that has been examined by the
3394          ## tokenizer is appended to the parent element or the attribute value
3395          ## as string, either literal string when no character reference or
3396          ## entity-replaced string otherwise, in this stage, since any characters
3397          ## that would not be consumed are appended in the data state or in an
3398          ## appropriate attribute value state anyway.
3399    
3400          if ($self->{prev_state} == DATA_STATE) {
3401            !!!cp (986);
3402            $self->{state} = $self->{prev_state};
3403            ## Reconsume.
3404            !!!emit ({type => CHARACTER_TOKEN,
3405                      data => $data,
3406                      line => $self->{line_prev},
3407                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3408                     });
3409            redo A;
3410          } else {
3411            !!!cp (985);
3412            $self->{current_attribute}->{value} .= $data;
3413            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3414            $self->{state} = $self->{prev_state};
3415            ## Reconsume.
3416            redo A;
3417        }        }
       !!!next-input-character;  
     }  
       
     if ($match > 0) {  
       return {type => 'character', data => $value};  
     } elsif ($match < 0) {  
       !!!parse-error (type => 'refc');  
       return {type => 'character', data => $value};  
3418      } else {      } else {
3419        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       !!!back-token ({type => 'character', data => $value});  
       return undef;  
3420      }      }
3421    } else {    } # A  
3422      ## no characters are consumed  
3423      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3424      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3425    
3426  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3427    my $self = shift;    my $self = shift;
# Line 1702  sub _initialize_tree_constructor ($) { Line 3430  sub _initialize_tree_constructor ($) {
3430    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3431    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3432    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3433      $self->{document}->set_user_data (manakai_source_line => 1);
3434      $self->{document}->set_user_data (manakai_source_column => 1);
3435  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3436    
3437  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1728  sub _construct_tree ($) { Line 3458  sub _construct_tree ($) {
3458        
3459    !!!next-token;    !!!next-token;
3460    
   $self->{insertion_mode} = 'before head';  
3461    undef $self->{form_element};    undef $self->{form_element};
3462    undef $self->{head_element};    undef $self->{head_element};
3463    $self->{open_elements} = [];    $self->{open_elements} = [];
3464    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3465    
3466      ## NOTE: The "initial" insertion mode.
3467    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3468    
3469      ## NOTE: The "before html" insertion mode.
3470    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3471      $self->{insertion_mode} = BEFORE_HEAD_IM;
3472    
3473      ## NOTE: The "before head" insertion mode and so on.
3474    $self->_tree_construction_main;    $self->_tree_construction_main;
3475  } # _construct_tree  } # _construct_tree
3476    
3477  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3478    my $self = shift;    my $self = shift;
3479    
3480      ## NOTE: "initial" insertion mode
3481    
3482    INITIAL: {    INITIAL: {
3483      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3484        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3485        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3486        ## language.        ## language.
3487        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3488        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3489        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3490        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3491            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3492          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3493            !!!parse-error (type => 'not HTML5', token => $token);
3494        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3495          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3496          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3497          } elsif (defined $token->{public_identifier}) {
3498            if ($token->{public_identifier} eq 'XSLT-compat') {
3499              !!!cp ('t1.2');
3500              !!!parse-error (type => 'XSLT-compat', token => $token,
3501                              level => $self->{level}->{should});
3502            } else {
3503              !!!parse-error (type => 'not HTML5', token => $token);
3504            }
3505          } else {
3506            !!!cp ('t3');
3507            #
3508        }        }
3509                
3510        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3511          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3512          ## NOTE: Default value for both |public_id| and |system_id| attributes
3513          ## are empty strings, so that we don't set any value in missing cases.
3514        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3515            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3516        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1768  sub _tree_construction_initial ($) { Line 3519  sub _tree_construction_initial ($) {
3519        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3520        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3521                
3522        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3523            !!!cp ('t4');
3524          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3525        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3526          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3527          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3528          if ({          my $prefix = [
3529            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3530            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3531            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3532            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3533            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3534            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3535            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3536            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3537            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3538            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3539            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3540            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3541            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3542            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3543            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3544            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3545            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3546            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3547            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3548            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3549            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3550            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3551            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3552            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3553            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3554            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3555            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3556            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3557            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3558            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3559            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3560            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3561            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3562            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3563            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3564            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3565            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3566            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3567            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3568            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3569            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3570            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3571            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3572            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3573            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3574            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3575            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3576            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3577            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3578            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3579            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3580            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3581            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3582            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3583            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3584            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3585            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3586            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3587            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3588            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3589            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3590            "-//W3C//DTD W3 HTML//EN" => 1,            }
3591            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3592            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3593            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3594            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3595            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3596            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3597            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3598          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3599                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3600            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3601                !!!cp ('t6');
3602              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3603            } else {            } else {
3604                !!!cp ('t7');
3605              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3606            }            }
3607          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3608                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3609              !!!cp ('t8');
3610            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3611            } else {
3612              !!!cp ('t9');
3613          }          }
3614          } else {
3615            !!!cp ('t10');
3616        }        }
3617        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3618          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3619          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3620          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") {
3621              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3622              ## marked as quirks.
3623            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3624              !!!cp ('t11');
3625            } else {
3626              !!!cp ('t12');
3627          }          }
3628          } else {
3629            !!!cp ('t13');
3630        }        }
3631                
3632        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3633        !!!next-token;        !!!next-token;
3634        return;        return;
3635      } elsif ({      } elsif ({
3636                'start tag' => 1,                START_TAG_TOKEN, 1,
3637                'end tag' => 1,                END_TAG_TOKEN, 1,
3638                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3639               }->{$token->{type}}) {               }->{$token->{type}}) {
3640        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3641          !!!parse-error (type => 'no DOCTYPE', token => $token);
3642        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3643        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3644        ## reprocess        ## reprocess
3645          !!!ack-later;
3646        return;        return;
3647      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3648        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3649          ## Ignore the token          ## Ignore the token
3650    
3651          unless (length $token->{data}) {          unless (length $token->{data}) {
3652            ## Stay in the phase            !!!cp ('t15');
3653              ## Stay in the insertion mode.
3654            !!!next-token;            !!!next-token;
3655            redo INITIAL;            redo INITIAL;
3656            } else {
3657              !!!cp ('t16');
3658          }          }
3659          } else {
3660            !!!cp ('t17');
3661        }        }
3662    
3663        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3664        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3665        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3666        ## reprocess        ## reprocess
3667        return;        return;
3668      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3669          !!!cp ('t18');
3670        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3671        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3672                
3673        ## Stay in the phase.        ## Stay in the insertion mode.
3674        !!!next-token;        !!!next-token;
3675        redo INITIAL;        redo INITIAL;
3676      } else {      } else {
3677        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3678      }      }
3679    } # INITIAL    } # INITIAL
3680    
3681      die "$0: _tree_construction_initial: This should be never reached";
3682  } # _tree_construction_initial  } # _tree_construction_initial
3683    
3684  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3685    my $self = shift;    my $self = shift;
3686    
3687      ## NOTE: "before html" insertion mode.
3688        
3689    B: {    B: {
3690        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3691          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3692            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3693          ## Ignore the token          ## Ignore the token
3694          ## Stay in the phase          ## Stay in the insertion mode.
3695          !!!next-token;          !!!next-token;
3696          redo B;          redo B;
3697        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3698            !!!cp ('t20');
3699          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3700          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3701          ## Stay in the phase          ## Stay in the insertion mode.
3702          !!!next-token;          !!!next-token;
3703          redo B;          redo B;
3704        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3705          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3706            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
3707            ## ISSUE: DOM3 Core does not allow Document > Text  
3708            unless (length $token->{data}) {            unless (length $token->{data}) {
3709              ## Stay in the phase              !!!cp ('t21');
3710                ## Stay in the insertion mode.
3711              !!!next-token;              !!!next-token;
3712              redo B;              redo B;
3713              } else {
3714                !!!cp ('t22');
3715            }            }
3716            } else {
3717              !!!cp ('t23');
3718          }          }
3719    
3720            $self->{application_cache_selection}->(undef);
3721    
3722          #          #
3723          } elsif ($token->{type} == START_TAG_TOKEN) {
3724            if ($token->{tag_name} eq 'html') {
3725              my $root_element;
3726              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3727              $self->{document}->append_child ($root_element);
3728              push @{$self->{open_elements}},
3729                  [$root_element, $el_category->{html}];
3730    
3731              if ($token->{attributes}->{manifest}) {
3732                !!!cp ('t24');
3733                $self->{application_cache_selection}
3734                    ->($token->{attributes}->{manifest}->{value});
3735                ## ISSUE: Spec is unclear on relative references.
3736                ## According to Hixie (#whatwg 2008-03-19), it should be
3737                ## resolved against the base URI of the document in HTML
3738                ## or xml:base of the element in XHTML.
3739              } else {
3740                !!!cp ('t25');
3741                $self->{application_cache_selection}->(undef);
3742              }
3743    
3744              !!!nack ('t25c');
3745    
3746              !!!next-token;
3747              return; ## Go to the "before head" insertion mode.
3748            } else {
3749              !!!cp ('t25.1');
3750              #
3751            }
3752        } elsif ({        } elsif ({
3753                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3754                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3755                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3756          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3757          #          #
3758        } else {        } else {
3759          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3760        }        }
3761        my $root_element; !!!create-element ($root_element, 'html');  
3762        $self->{document}->append_child ($root_element);      my $root_element;
3763        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3764        #$phase = 'main';      $self->{document}->append_child ($root_element);
3765        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3766        #redo B;  
3767        return;      $self->{application_cache_selection}->(undef);
3768    
3769        ## NOTE: Reprocess the token.
3770        !!!ack-later;
3771        return; ## Go to the "before head" insertion mode.
3772    
3773        ## ISSUE: There is an issue in the spec
3774    } # B    } # B
3775    
3776      die "$0: _tree_construction_root_element: This should never be reached";
3777  } # _tree_construction_root_element  } # _tree_construction_root_element
3778    
3779  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 1965  sub _reset_insertion_mode ($) { Line 3788  sub _reset_insertion_mode ($) {
3788            
3789      ## Step 3      ## Step 3
3790      S3: {      S3: {
3791        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3792        if (defined $self->{inner_html_node}) {          $last = 1;
3793          if ($self->{inner_html_node}->[1] eq 'td' or          if (defined $self->{inner_html_node}) {
3794              $self->{inner_html_node}->[1] eq 'th') {            !!!cp ('t28');
3795              $node = $self->{inner_html_node};
3796            } else {
3797              die "_reset_insertion_mode: t27";
3798            }
3799          }
3800          
3801          ## Step 4..14
3802          my $new_mode;
3803          if ($node->[1] & FOREIGN_EL) {
3804            !!!cp ('t28.1');
3805            ## NOTE: Strictly spaking, the line below only applies to MathML and
3806            ## SVG elements.  Currently the HTML syntax supports only MathML and
3807            ## SVG elements as foreigners.
3808            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3809          } elsif ($node->[1] & TABLE_CELL_EL) {
3810            if ($last) {
3811              !!!cp ('t28.2');
3812            #            #
3813          } else {          } else {
3814            $node = $self->{inner_html_node};            !!!cp ('t28.3');
3815              $new_mode = IN_CELL_IM;
3816          }          }
3817          } else {
3818            !!!cp ('t28.4');
3819            $new_mode = {
3820                          select => IN_SELECT_IM,
3821                          ## NOTE: |option| and |optgroup| do not set
3822                          ## insertion mode to "in select" by themselves.
3823                          tr => IN_ROW_IM,
3824                          tbody => IN_TABLE_BODY_IM,
3825                          thead => IN_TABLE_BODY_IM,
3826                          tfoot => IN_TABLE_BODY_IM,
3827                          caption => IN_CAPTION_IM,
3828                          colgroup => IN_COLUMN_GROUP_IM,
3829                          table => IN_TABLE_IM,
3830                          head => IN_BODY_IM, # not in head!
3831                          body => IN_BODY_IM,
3832                          frameset => IN_FRAMESET_IM,
3833                         }->{$node->[0]->manakai_local_name};
3834        }        }
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
3835        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3836                
3837        ## Step 14        ## Step 15
3838        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3839          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3840            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3841              $self->{insertion_mode} = BEFORE_HEAD_IM;
3842          } else {          } else {
3843            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3844              !!!cp ('t30');
3845              $self->{insertion_mode} = AFTER_HEAD_IM;
3846          }          }
3847          return;          return;
3848          } else {
3849            !!!cp ('t31');
3850        }        }
3851                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3852        ## Step 16        ## Step 16
3853          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3854          
3855          ## Step 17
3856        $i--;        $i--;
3857        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3858                
3859        ## Step 17        ## Step 18
3860        redo S3;        redo S3;
3861      } # S3      } # S3
3862    
3863      die "$0: _reset_insertion_mode: This line should never be reached";
3864  } # _reset_insertion_mode  } # _reset_insertion_mode
3865    
3866  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3867    my $self = shift;    my $self = shift;
3868    
   my $phase = 'main';  
   
3869    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3870    
3871    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2036  sub _tree_construction_main ($) { Line 3882  sub _tree_construction_main ($) {
3882      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3883      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3884        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3885            !!!cp ('t32');
3886          return;          return;
3887        }        }
3888      }      }
# Line 2050  sub _tree_construction_main ($) { Line 3897  sub _tree_construction_main ($) {
3897    
3898        ## Step 6        ## Step 6
3899        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3900            !!!cp ('t33_1');
3901          #          #
3902        } else {        } else {
3903          my $in_open_elements;          my $in_open_elements;
3904          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3905            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3906                !!!cp ('t33');
3907              $in_open_elements = 1;              $in_open_elements = 1;
3908              last OE;              last OE;
3909            }            }
3910          }          }
3911          if ($in_open_elements) {          if ($in_open_elements) {
3912              !!!cp ('t34');
3913            #            #
3914          } else {          } else {
3915              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3916              !!!cp ('t35');
3917            redo S4;            redo S4;
3918          }          }
3919        }        }
# Line 2084  sub _tree_construction_main ($) { Line 3936  sub _tree_construction_main ($) {
3936    
3937        ## Step 11        ## Step 11
3938        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3939            !!!cp ('t36');
3940          ## Step 7'          ## Step 7'
3941          $i++;          $i++;
3942          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3943                    
3944          redo S7;          redo S7;
3945        }        }
3946    
3947          !!!cp ('t37');
3948      } # S7      } # S7
3949    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3950    
3951    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3952      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3953        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3954            !!!cp ('t38');
3955          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3956          return;          return;
3957        }        }
3958      }      }
3959    
3960        !!!cp ('t39');
3961    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3962    
3963    my $style_start_tag = sub {    my $insert;
3964      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});  
3965      ## $self->{insertion_mode} eq 'in head' and ... (always true)    my $parse_rcdata = sub ($) {
3966      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      my ($content_model_flag) = @_;
3967       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
3968        ->append_child ($style_el);      ## Step 1
3969      $self->{content_model_flag} = 'CDATA';      my $start_tag_name = $token->{tag_name};
3970        my $el;
3971        !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3972    
3973        ## Step 2
3974        $insert->($el);
3975    
3976        ## Step 3
3977        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3978      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3979                  
3980        ## Step 4
3981      my $text = '';      my $text = '';
3982        !!!nack ('t40.1');
3983      !!!next-token;      !!!next-token;
3984      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3985          !!!cp ('t40');
3986        $text .= $token->{data};        $text .= $token->{data};
3987        !!!next-token;        !!!next-token;
3988      } # stop if non-character token or tokenizer stops tokenising      }
3989    
3990        ## Step 5
3991      if (length $text) {      if (length $text) {
3992        $style_el->manakai_append_text ($text);        !!!cp ('t41');
3993          my $text = $self->{document}->create_text_node ($text);
3994          $el->append_child ($text);
3995      }      }
3996        
3997      $self->{content_model_flag} = 'PCDATA';      ## Step 6
3998                      $self->{content_model} = PCDATA_CONTENT_MODEL;
3999      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
4000        ## Step 7
4001        if ($token->{type} == END_TAG_TOKEN and
4002            $token->{tag_name} eq $start_tag_name) {
4003          !!!cp ('t42');
4004        ## Ignore the token        ## Ignore the token
4005      } else {      } else {
4006        !!!parse-error (type => 'in CDATA:#'.$token->{type});        ## NOTE: An end-of-file token.
4007        ## ISSUE: And ignore?        if ($content_model_flag == CDATA_CONTENT_MODEL) {
4008            !!!cp ('t43');
4009            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4010          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4011            !!!cp ('t44');
4012            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4013          } else {
4014            die "$0: $content_model_flag in parse_rcdata";
4015          }
4016      }      }
4017      !!!next-token;      !!!next-token;
4018    }; # $style_start_tag    }; # $parse_rcdata
4019    
4020    my $script_start_tag = sub {    my $script_start_tag = sub () {
4021      my $script_el;      my $script_el;
4022      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4023      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4024    
4025      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
4026      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4027            
4028      my $text = '';      my $text = '';
4029        !!!nack ('t45.1');
4030      !!!next-token;      !!!next-token;
4031      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4032          !!!cp ('t45');
4033        $text .= $token->{data};        $text .= $token->{data};
4034        !!!next-token;        !!!next-token;
4035      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4036      if (length $text) {      if (length $text) {
4037          !!!cp ('t46');
4038        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4039      }      }
4040                                
4041      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4042    
4043      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4044          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4045          !!!cp ('t47');
4046        ## Ignore the token        ## Ignore the token
4047      } else {      } else {
4048        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4049          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4050        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4051        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4052      }      }
4053            
4054      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4055          !!!cp ('t49');
4056        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4057      } else {      } else {
4058          !!!cp ('t50');
4059        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4060        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4061          
4062        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
4063                
4064        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
4065                
# Line 2178  sub _tree_construction_main ($) { Line 4069  sub _tree_construction_main ($) {
4069      !!!next-token;      !!!next-token;
4070    }; # $script_start_tag    }; # $script_start_tag
4071    
4072      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4073      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4074      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4075    
4076    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4077      my $tag_name = shift;      my $end_tag_token = shift;
4078        my $tag_name = $end_tag_token->{tag_name};
4079    
4080        ## NOTE: The adoption agency algorithm (AAA).
4081    
4082      FET: {      FET: {
4083        ## Step 1        ## Step 1
4084        my $formatting_element;        my $formatting_element;
4085        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4086        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4087          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4088              !!!cp ('t52');
4089              last AFE;
4090            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4091                         eq $tag_name) {
4092              !!!cp ('t51');
4093            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4094            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4095            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4096          }          }
4097        } # AFE        } # AFE
4098        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4099          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4100            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4101          ## Ignore the token          ## Ignore the token
4102          !!!next-token;          !!!next-token;
4103          return;          return;
# Line 2207  sub _tree_construction_main ($) { Line 4109  sub _tree_construction_main ($) {
4109          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4110          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4111            if ($in_scope) {            if ($in_scope) {
4112                !!!cp ('t54');
4113              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4114              last INSCOPE;              last INSCOPE;
4115            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4116              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4117                !!!parse-error (type => 'unmatched end tag',
4118                                text => $token->{tag_name},
4119                                token => $end_tag_token);
4120              ## Ignore the token              ## Ignore the token
4121              !!!next-token;              !!!next-token;
4122              return;              return;
4123            }            }
4124          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4125                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4126            $in_scope = 0;            $in_scope = 0;
4127          }          }
4128        } # INSCOPE        } # INSCOPE
4129        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4130          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4131            !!!parse-error (type => 'unmatched end tag',
4132                            text => $token->{tag_name},
4133                            token => $end_tag_token);
4134          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4135          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4136          return;          return;
4137        }        }
4138        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4139          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4140            !!!parse-error (type => 'not closed',
4141                            text => $self->{open_elements}->[-1]->[0]
4142                                ->manakai_local_name,
4143                            token => $end_tag_token);
4144        }        }
4145                
4146        ## Step 2        ## Step 2
# Line 2237  sub _tree_construction_main ($) { Line 4148  sub _tree_construction_main ($) {
4148        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4149        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4150          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4151          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4152              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4153              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4154               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4155              !!!cp ('t59');
4156            $furthest_block = $node;            $furthest_block = $node;
4157            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4158          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4159              !!!cp ('t60');
4160            last OE;            last OE;
4161          }          }
4162        } # OE        } # OE
4163                
4164        ## Step 3        ## Step 3
4165        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4166            !!!cp ('t61');
4167          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4168          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4169          !!!next-token;          !!!next-token;
# Line 2262  sub _tree_construction_main ($) { Line 4176  sub _tree_construction_main ($) {
4176        ## Step 5        ## Step 5
4177        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4178        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4179            !!!cp ('t62');
4180          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4181        }        }
4182                
# Line 2284  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199          S7S2: {          S7S2: {
4200            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4201              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4202                  !!!cp ('t63');
4203                $node_i_in_active = $_;                $node_i_in_active = $_;
4204                last S7S2;                last S7S2;
4205              }              }
# Line 2297  sub _tree_construction_main ($) { Line 4213  sub _tree_construction_main ($) {
4213                    
4214          ## Step 4          ## Step 4
4215          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4216              !!!cp ('t64');
4217            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4218          }          }
4219                    
4220          ## Step 5          ## Step 5
4221          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4222              !!!cp ('t65');
4223            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4224            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4225            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2319  sub _tree_construction_main ($) { Line 4237  sub _tree_construction_main ($) {
4237        } # S7          } # S7  
4238                
4239        ## Step 8        ## Step 8
4240        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4241            my $foster_parent_element;
4242            my $next_sibling;
4243            OE: for (reverse 0..$#{$self->{open_elements}}) {
4244              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4245                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4246                                 if (defined $parent and $parent->node_type == 1) {
4247                                   !!!cp ('t65.1');
4248                                   $foster_parent_element = $parent;
4249                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4250                                 } else {
4251                                   !!!cp ('t65.2');
4252                                   $foster_parent_element
4253                                     = $self->{open_elements}->[$_ - 1]->[0];
4254                                 }
4255                                 last OE;
4256                               }
4257                             } # OE
4258                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4259                               unless defined $foster_parent_element;
4260            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4261            $open_tables->[-1]->[1] = 1; # tainted
4262          } else {
4263            !!!cp ('t65.3');
4264            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4265          }
4266                
4267        ## Step 9        ## Step 9
4268        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2336  sub _tree_construction_main ($) { Line 4279  sub _tree_construction_main ($) {
4279        my $i;        my $i;
4280        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4281          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4282              !!!cp ('t66');
4283            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4284            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4285          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4286              !!!cp ('t67');
4287            $i = $_;            $i = $_;
4288          }          }
4289        } # AFE        } # AFE
# Line 2348  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293        undef $i;        undef $i;
4294        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4295          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4296              !!!cp ('t68');
4297            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4298            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4299          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4300              !!!cp ('t69');
4301            $i = $_;            $i = $_;
4302          }          }
4303        } # OE        } # OE
# Line 2361  sub _tree_construction_main ($) { Line 4308  sub _tree_construction_main ($) {
4308      } # FET      } # FET
4309    }; # $formatting_end_tag    }; # $formatting_end_tag
4310    
4311    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4312      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4313    }; # $insert_to_current    }; # $insert_to_current
4314    
4315    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4316                         my $child = shift;      my $child = shift;
4317                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4318                              table => 1, tbody => 1, tfoot => 1,        # MUST
4319                              thead => 1, tr => 1,        my $foster_parent_element;
4320                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4321                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4322                           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') {  
4323                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4324                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4325                                   !!!cp ('t70');
4326                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4327                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4328                               } else {                               } else {
4329                                   !!!cp ('t71');
4330                                 $foster_parent_element                                 $foster_parent_element
4331                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4332                               }                               }
# Line 2391  sub _tree_construction_main ($) { Line 4337  sub _tree_construction_main ($) {
4337                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4338                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4339                             ($child, $next_sibling);                             ($child, $next_sibling);
4340                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4341                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4342                         }        !!!cp ('t72');
4343          $self->{open_elements}->[-1]->[0]->append_child ($child);
4344        }
4345    }; # $insert_to_foster    }; # $insert_to_foster
4346    
4347    my $in_body = sub {    B: while (1) {
4348      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4349      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4350        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4351          $script_start_tag->();        ## Ignore the token
4352          return;        ## Stay in the phase
4353        } elsif ($token->{tag_name} eq 'style') {        !!!next-token;
4354          $style_start_tag->();        next B;
4355          return;      } elsif ($token->{type} == START_TAG_TOKEN and
4356        } elsif ({               $token->{tag_name} eq 'html') {
4357                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4358                 }->{$token->{tag_name}}) {          !!!cp ('t79');
4359          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'after html', text => 'html', token => $token);
4360          ## NOTE: This is an "as if in head" code clone          $self->{insertion_mode} = AFTER_BODY_IM;
4361          my $el;        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4362          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4363          if (defined $self->{head_element}) {          !!!parse-error (type => 'after html', text => 'html', token => $token);
4364            $self->{head_element}->append_child ($el);          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4365          } else {        } else {
4366            $insert->($el);          !!!cp ('t81');
4367          }        }
4368            
4369          !!!next-token;        !!!cp ('t82');
4370          return;        !!!parse-error (type => 'not first start tag', token => $token);
4371        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4372          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4373          ## NOTE: There is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4374          my $title_el;            !!!cp ('t84');
4375          !!!create-element ($title_el, 'title', $token->{attributes});            $top_el->set_attribute_ns
4376          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              (undef, [undef, $attr_name],
4377            ->append_child ($title_el);               $token->{attributes}->{$attr_name}->{value});
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         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;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## 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 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             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]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## 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;  
4378          }          }
4379                    }
4380          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        !!!nack ('t84.1');
4381                    !!!next-token;
4382          next B;
4383        } elsif ($token->{type} == COMMENT_TOKEN) {
4384          my $comment = $self->{document}->create_comment ($token->{data});
4385          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4386            !!!cp ('t85');
4387            $self->{document}->append_child ($comment);
4388          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4389            !!!cp ('t86');
4390            $self->{open_elements}->[0]->[0]->append_child ($comment);
4391          } else {
4392            !!!cp ('t87');
4393            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4394          }
4395          !!!next-token;
4396          next B;
4397        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4398          if ($token->{type} == CHARACTER_TOKEN) {
4399            !!!cp ('t87.1');
4400            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4401          !!!next-token;          !!!next-token;
4402          return;          next B;
4403        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4404          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4405            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4406            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4407              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4408                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4409              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4410              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4411              $formatting_end_tag->($token->{tag_name});            #
4412                        } elsif ({
4413              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4414                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4415                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4416                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4417                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4418              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4419              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4420                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4421                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4422                  last OE;            !!!cp ('t87.2');
4423                }            !!!parse-error (type => 'not closed',
4424              } # OE                            text => $self->{open_elements}->[-1]->[0]
4425              last AFE;                                ->manakai_local_name,
4426            } elsif ($node->[0] eq '#marker') {                            token => $token);
4427              last AFE;  
4428              pop @{$self->{open_elements}}
4429                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4430    
4431              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4432              ## Reprocess.
4433              next B;
4434            } else {
4435              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4436              my $tag_name = $token->{tag_name};
4437              if ($nsuri eq $SVG_NS) {
4438                $tag_name = {
4439                   altglyph => 'altGlyph',
4440                   altglyphdef => 'altGlyphDef',
4441                   altglyphitem => 'altGlyphItem',
4442                   animatecolor => 'animateColor',
4443                   animatemotion => 'animateMotion',
4444                   animatetransform => 'animateTransform',
4445                   clippath => 'clipPath',
4446                   feblend => 'feBlend',
4447                   fecolormatrix => 'feColorMatrix',
4448                   fecomponenttransfer => 'feComponentTransfer',
4449                   fecomposite => 'feComposite',
4450                   feconvolvematrix => 'feConvolveMatrix',
4451                   fediffuselighting => 'feDiffuseLighting',
4452                   fedisplacementmap => 'feDisplacementMap',
4453                   fedistantlight => 'feDistantLight',
4454                   feflood => 'feFlood',
4455                   fefunca => 'feFuncA',
4456                   fefuncb => 'feFuncB',
4457                   fefuncg => 'feFuncG',
4458                   fefuncr => 'feFuncR',
4459                   fegaussianblur => 'feGaussianBlur',
4460                   feimage => 'feImage',
4461                   femerge => 'feMerge',
4462                   femergenode => 'feMergeNode',
4463                   femorphology => 'feMorphology',
4464                   feoffset => 'feOffset',
4465                   fepointlight => 'fePointLight',
4466                   fespecularlighting => 'feSpecularLighting',
4467                   fespotlight => 'feSpotLight',
4468                   fetile => 'feTile',
4469                   feturbulence => 'feTurbulence',
4470                   foreignobject => 'foreignObject',
4471                   glyphref => 'glyphRef',
4472                   lineargradient => 'linearGradient',
4473                   radialgradient => 'radialGradient',
4474                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4475                   textpath => 'textPath',  
4476                }->{$tag_name} || $tag_name;
4477            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4478    
4479          !!!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];  
4480    
4481          !!!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);  
4482    
4483          ## 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') {  
             !!!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', ''];  
4484    
4485          !!!next-token;            if ($self->{self_closing}) {
4486          return;              pop @{$self->{open_elements}};
4487        } 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);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         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';  
         }  
           
         $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};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {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 ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4488            } else {            } else {
4489              !!!parse-error (type => 'in CDATA:#'.$token->{type});              !!!cp ('t87.4');
4490            }            }
4491            ## ISSUE: And ignore?  
4492              !!!next-token;
4493              next B;
4494          }          }
4495          !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4496          return;          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4497        } elsif ($token->{tag_name} eq 'select') {          !!!cp ('t87.5');
4498          $reconstruct_active_formatting_elements->($insert_to_current);          #
4499                  } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4500          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t87.6');
4501                    !!!parse-error (type => 'not closed',
4502          $self->{insertion_mode} = 'in select';                          text => $self->{open_elements}->[-1]->[0]
4503          !!!next-token;                              ->manakai_local_name,
4504          return;                          token => $token);
4505        } elsif ({  
4506                  caption => 1, col => 1, colgroup => 1, frame => 1,          pop @{$self->{open_elements}}
4507                  frameset => 1, head => 1, option => 1, optgroup => 1,              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4508                  tbody => 1, td => 1, tfoot => 1, th => 1,  
4509                  thead => 1, tr => 1,          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4510                 }->{$token->{tag_name}}) {          ## Reprocess.
4511          !!!parse-error (type => 'in body:'.$token->{tag_name});          next B;
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4512        } else {        } else {
4513          $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;  
4514        }        }
4515      } 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,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
4516    
4517            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4518            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4519            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4520          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4521            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4522            ## Ignore the token              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4523            !!!next-token;              #
4524            return;            } else {
4525          }              !!!cp ('t88.1');
4526        } elsif ($token->{tag_name} eq 'html') {              ## Ignore the token.
4527          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,  
                 }->{$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;  
4528            }            }
4529          } # INSCOPE            unless (length $token->{data}) {
4530                        !!!cp ('t88');
4531          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {              !!!next-token;
4532            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              next B;
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $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,  
                 }->{$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;  
4533            }            }
4534          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4535          }          }
4536    
4537          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4538          !!!next-token;            !!!cp ('t89');
4539          return;            ## As if <head>
4540        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4541                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4542                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4543          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         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, br => 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];  
4544    
4545          ## Step 2            ## Reprocess in the "in head" insertion mode...
4546          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,  
                 }->{$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;  
4547    
4548              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4549              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4550            } else {            !!!cp ('t90');
4551              ## Step 3            ## As if </noscript>
4552              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4553                  #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 => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4554                        
4555            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4556            redo S2;            ## As if </head>
4557          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
4558    
4559    B: {            ## Reprocess in the "after head" insertion mode...
4560      if ($phase eq 'main') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4561        if ($token->{type} eq 'DOCTYPE') {            !!!cp ('t91');
4562          !!!parse-error (type => 'in html:#DOCTYPE');            pop @{$self->{open_elements}};
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$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]);  
         }  
4563    
4564          ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4565          last B;          } else {
4566              !!!cp ('t92');
4567            }
4568    
4569          ## ISSUE: There is an issue in the spec.          ## "after head" insertion mode
4570        } else {          ## As if <body>
4571          if ($self->{insertion_mode} eq 'before head') {          !!!insert-element ('body',, $token);
4572            if ($token->{type} eq 'character') {          $self->{insertion_mode} = IN_BODY_IM;
4573              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## reprocess
4574                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          next B;
4575                unless (length $token->{data}) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4576                  !!!next-token;          if ($token->{tag_name} eq 'head') {
4577                  redo B;            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4578                }              !!!cp ('t93');
4579              }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4580              ## As if <head>              $self->{open_elements}->[-1]->[0]->append_child
4581              !!!create-element ($self->{head_element}, 'head');                  ($self->{head_element});
4582              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              push @{$self->{open_elements}},
4583              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  [$self->{head_element}, $el_category->{head}];
4584              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = IN_HEAD_IM;
4585              ## reprocess              !!!nack ('t93.1');
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4586              !!!next-token;              !!!next-token;
4587              redo B;              next B;
4588            } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4589              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              !!!cp ('t93.2');
4590              !!!create-element ($self->{head_element}, 'head', $attr);              !!!parse-error (type => 'after head', text => 'head',
4591              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token);
4592              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4593              $self->{insertion_mode} = 'in head';              !!!nack ('t93.3');
4594              if ($token->{tag_name} eq 'head') {              !!!next-token;
4595                !!!next-token;              next B;
             #} 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 ($token->{tag_name} eq 'html') {  
               ## 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  
               !!!next-token;  
               redo B;  
             }  
4596            } else {            } else {
4597              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4598            }              !!!parse-error (type => 'in head:head',
4599          } elsif ($self->{insertion_mode} eq 'in head') {                              token => $token); # or in head noscript
4600            if ($token->{type} eq 'character') {              ## Ignore the token
4601              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              !!!nack ('t95.1');
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4602              !!!next-token;              !!!next-token;
4603              redo B;              next B;
4604            } elsif ($token->{type} eq 'start tag') {            }
4605              if ($token->{tag_name} eq 'title') {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4606                ## NOTE: There is an "as if in head" code clone            !!!cp ('t96');
4607                my $title_el;            ## As if <head>
4608                !!!create-element ($title_el, 'title', $token->{attributes});            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4609                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4610                  ->append_child ($title_el);            push @{$self->{open_elements}},
4611                $self->{content_model_flag} = 'RCDATA';                [$self->{head_element}, $el_category->{head}];
4612                delete $self->{escape}; # MUST  
4613              $self->{insertion_mode} = IN_HEAD_IM;
4614                my $text = '';            ## Reprocess in the "in head" insertion mode...
4615                !!!next-token;          } else {
4616                while ($token->{type} eq 'character') {            !!!cp ('t97');
4617                  $text .= $token->{data};          }
4618                  !!!next-token;  
4619                }              if ($token->{tag_name} eq 'base') {
4620                if (length $text) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4621                  $title_el->manakai_append_text ($text);                  !!!cp ('t98');
4622                }                  ## As if </noscript>
4623                                  pop @{$self->{open_elements}};
4624                $self->{content_model_flag} = 'PCDATA';                  !!!parse-error (type => 'in noscript', text => 'base',
4625                                    token => $token);
4626                                
4627                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
4628                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
4629                } else {                } else {
4630                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  !!!cp ('t99');
                 ## ISSUE: And ignore?  
4631                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
4632    
4633                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
4634                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4635              } elsif ($token->{tag_name} eq 'head') {                  !!!cp ('t100');
4636                !!!parse-error (type => 'in head:head');                  !!!parse-error (type => 'after head',
4637                ## Ignore the token                                  text => $token->{tag_name}, token => $token);
4638                !!!next-token;                  push @{$self->{open_elements}},
4639                redo B;                      [$self->{head_element}, $el_category->{head}];
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
4640                } else {                } else {
4641                  !!!parse-error (type => 'unmatched end tag:head');                  !!!cp ('t101');
4642                }                }
4643                $self->{insertion_mode} = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4644                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4645                  pop @{$self->{open_elements}} # <head>
4646                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4647                  !!!nack ('t101.1');
4648                !!!next-token;                !!!next-token;
4649                redo B;                next B;
4650              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'link') {
4651                #                ## NOTE: There is a "as if in head" code clone.
4652              } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4653                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t102');
4654                ## Ignore the token                  !!!parse-error (type => 'after head',
4655                !!!next-token;                                  text => $token->{tag_name}, token => $token);
4656                redo B;                  push @{$self->{open_elements}},
4657              }                      [$self->{head_element}, $el_category->{head}];
4658            } else {                } else {
4659              #                  !!!cp ('t103');
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4660                }                }
4661              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4662                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4663              #                pop @{$self->{open_elements}} # <head>
4664            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4665              my $comment = $self->{document}->create_comment ($token->{data});                !!!ack ('t103.1');
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
4666                !!!next-token;                !!!next-token;
4667                redo B;                next B;
4668              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
4669                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
4670                        script => 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4671                       }->{$token->{tag_name}}) {                  !!!cp ('t104');
4672                !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4673                $self->{insertion_mode} = 'in head';                                  text => $token->{tag_name}, token => $token);
4674                ## reprocess                  push @{$self->{open_elements}},
4675                redo B;                      [$self->{head_element}, $el_category->{head}];
4676              } else {                } else {
4677                #                  !!!cp ('t105');
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
4678                }                }
4679              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4680                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
             !!!parse-error (type => 'in table:#character');  
4681    
4682              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
4683              ## ISSUE: Spec says that "whenever a node would be inserted                  if ($token->{attributes}->{charset}) {
4684              ## into the current node" while characters might not be                    !!!cp ('t106');
4685              ## result in a new Text node.                    ## NOTE: Whether the encoding is supported or not is handled
4686              $reconstruct_active_formatting_elements->($insert_to_foster);                    ## in the {change_encoding} callback.
4687                                  $self->{change_encoding}
4688              if ({                        ->($self, $token->{attributes}->{charset}->{value},
4689                   table => 1, tbody => 1, tfoot => 1,                           $token);
4690                   thead => 1, tr => 1,                    
4691                  }->{$self->{open_elements}->[-1]->[1]}) {                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4692                # MUST                        ->set_user_data (manakai_has_reference =>
4693                my $foster_parent_element;                                             $token->{attributes}->{charset}
4694                my $next_sibling;                                                 ->{has_reference});
4695                my $prev_sibling;                  } elsif ($token->{attributes}->{content}) {
4696                OE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($token->{attributes}->{content}->{value}
4697                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4698                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                            [\x09-\x0D\x20]*=
4699                    if (defined $parent and $parent->node_type == 1) {                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4700                      $foster_parent_element = $parent;                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4701                      $next_sibling = $self->{open_elements}->[$_]->[0];                      !!!cp ('t107');
4702                      $prev_sibling = $next_sibling->previous_sibling;                      ## NOTE: Whether the encoding is supported or not is handled
4703                        ## in the {change_encoding} callback.
4704                        $self->{change_encoding}
4705                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4706                               $token);
4707                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4708                            ->set_user_data (manakai_has_reference =>
4709                                                 $token->{attributes}->{content}
4710                                                       ->{has_reference});
4711                    } else {                    } else {
4712                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      !!!cp ('t108');
                     $prev_sibling = $foster_parent_element->last_child;  
4713                    }                    }
                   last OE;  
4714                  }                  }
               } # 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});  
4715                } else {                } else {
4716                  $foster_parent_element->insert_before                  if ($token->{attributes}->{charset}) {
4717                    ($self->{document}->create_text_node ($token->{data}),                    !!!cp ('t109');
4718                     $next_sibling);                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4719                }                        ->set_user_data (manakai_has_reference =>
4720              } else {                                             $token->{attributes}->{charset}
4721                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                                                 ->{has_reference});
4722              }                  }
4723                                if ($token->{attributes}->{content}) {
4724              !!!next-token;                    !!!cp ('t110');
4725              redo B;                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4726            } elsif ($token->{type} eq 'comment') {                        ->set_user_data (manakai_has_reference =>
4727              my $comment = $self->{document}->create_comment ($token->{data});                                             $token->{attributes}->{content}
4728              $self->{open_elements}->[-1]->[0]->append_child ($comment);                                                 ->{has_reference});
4729              !!!next-token;                  }
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
4730                }                }
4731    
4732                push @$active_formatting_elements, ['#marker', '']                pop @{$self->{open_elements}} # <head>
4733                  if $token->{tag_name} eq 'caption';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4734                  !!!ack ('t110.1');
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
4735                !!!next-token;                !!!next-token;
4736                redo B;                next B;
4737              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
4738                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4739                        td => 1, th => 1, tr => 1,                  !!!cp ('t111');
4740                       }->{$token->{tag_name}}) {                  ## As if </noscript>
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4741                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4742                    !!!parse-error (type => 'in noscript', text => 'title',
4743                                    token => $token);
4744                  
4745                    $self->{insertion_mode} = IN_HEAD_IM;
4746                    ## Reprocess in the "in head" insertion mode...
4747                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4748                    !!!cp ('t112');
4749                    !!!parse-error (type => 'after head',
4750                                    text => $token->{tag_name}, token => $token);
4751                    push @{$self->{open_elements}},
4752                        [$self->{head_element}, $el_category->{head}];
4753                  } else {
4754                    !!!cp ('t113');
4755                }                }
4756    
4757                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
4758                $self->{insertion_mode} = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
4759                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
4760                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4761                redo B;                pop @{$self->{open_elements}} # <head>
4762              } elsif ($token->{tag_name} eq 'table') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4763                ## NOTE: There are code clones for this "table in table"                next B;
4764                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              } elsif ($token->{tag_name} eq 'style' or
4765                         $token->{tag_name} eq 'noframes') {
4766                ## As if </table>                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4767                ## have a table element in table scope                ## insertion mode IN_HEAD_IM)
4768                my $i;                ## NOTE: There is a "as if in head" code clone.
4769                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4770                  my $node = $self->{open_elements}->[$_];                  !!!cp ('t114');
4771                  if ($node->[1] eq 'table') {                  !!!parse-error (type => 'after head',
4772                    $i = $_;                                  text => $token->{tag_name}, token => $token);
4773                    last INSCOPE;                  push @{$self->{open_elements}},
4774                  } elsif ({                      [$self->{head_element}, $el_category->{head}];
4775                            table => 1, html => 1,                } else {
4776                           }->{$node->[1]}) {                  !!!cp ('t115');
4777                    last INSCOPE;                }
4778                  }                $parse_rcdata->(CDATA_CONTENT_MODEL);
4779                } # INSCOPE                pop @{$self->{open_elements}} # <head>
4780                unless (defined $i) {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4781                  !!!parse-error (type => 'unmatched end tag:table');                next B;
4782                  ## Ignore tokens </table><table>              } elsif ($token->{tag_name} eq 'noscript') {
4783                  if ($self->{insertion_mode} == IN_HEAD_IM) {
4784                    !!!cp ('t116');
4785                    ## NOTE: and scripting is disalbed
4786                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4787                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4788                    !!!nack ('t116.1');
4789                    !!!next-token;
4790                    next B;
4791                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4792                    !!!cp ('t117');
4793                    !!!parse-error (type => 'in noscript', text => 'noscript',
4794                                    token => $token);
4795                    ## Ignore the token
4796                    !!!nack ('t117.1');
4797                  !!!next-token;                  !!!next-token;
4798                  redo B;                  next B;
4799                  } else {
4800                    !!!cp ('t118');
4801                    #
4802                }                }
4803                } elsif ($token->{tag_name} eq 'script') {
4804                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4805                    !!!cp ('t119');
4806                    ## As if </noscript>
4807                    pop @{$self->{open_elements}};
4808                    !!!parse-error (type => 'in noscript', text => 'script',
4809                                    token => $token);
4810                                
4811                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
4812                if ({                  ## Reprocess in the "in head" insertion mode...
4813                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4814                     td => 1, th => 1, tr => 1,                  !!!cp ('t120');
4815                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'after head',
4816                  !!!back-token; # <table>                                  text => $token->{tag_name}, token => $token);
4817                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}},
4818                  !!!back-token;                      [$self->{head_element}, $el_category->{head}];
4819                  $token = {type => 'end tag',                } else {
4820                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  !!!cp ('t121');
                 redo B;  
4821                }                }
4822    
4823                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## NOTE: There is a "as if in head" code clone.
4824                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                $script_start_tag->();
4825                  pop @{$self->{open_elements}} # <head>
4826                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4827                  next B;
4828                } elsif ($token->{tag_name} eq 'body' or
4829                         $token->{tag_name} eq 'frameset') {
4830                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4831                    !!!cp ('t122');
4832                    ## As if </noscript>
4833                    pop @{$self->{open_elements}};
4834                    !!!parse-error (type => 'in noscript',
4835                                    text => $token->{tag_name}, token => $token);
4836                    
4837                    ## Reprocess in the "in head" insertion mode...
4838                    ## As if </head>
4839                    pop @{$self->{open_elements}};
4840                    
4841                    ## Reprocess in the "after head" insertion mode...
4842                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4843                    !!!cp ('t124');
4844                    pop @{$self->{open_elements}};
4845                    
4846                    ## Reprocess in the "after head" insertion mode...
4847                  } else {
4848                    !!!cp ('t125');
4849                }                }
4850    
4851                splice @{$self->{open_elements}}, $i;                ## "after head" insertion mode
4852                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4853                  if ($token->{tag_name} eq 'body') {
4854                    !!!cp ('t126');
4855                    $self->{insertion_mode} = IN_BODY_IM;
4856                  } elsif ($token->{tag_name} eq 'frameset') {
4857                    !!!cp ('t127');
4858                    $self->{insertion_mode} = IN_FRAMESET_IM;
4859                  } else {
4860                    die "$0: tag name: $self->{tag_name}";
4861                  }
4862                  !!!nack ('t127.1');
4863                  !!!next-token;
4864                  next B;
4865                } else {
4866                  !!!cp ('t128');
4867                  #
4868                }
4869    
4870                $self->_reset_insertion_mode;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4871                  !!!cp ('t129');
4872                  ## As if </noscript>
4873                  pop @{$self->{open_elements}};
4874                  !!!parse-error (type => 'in noscript:/',
4875                                  text => $token->{tag_name}, token => $token);
4876                  
4877                  ## Reprocess in the "in head" insertion mode...
4878                  ## As if </head>
4879                  pop @{$self->{open_elements}};
4880    
4881                ## reprocess                ## Reprocess in the "after head" insertion mode...
4882                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4883                  !!!cp ('t130');
4884                  ## As if </head>
4885                  pop @{$self->{open_elements}};
4886    
4887                  ## Reprocess in the "after head" insertion mode...
4888              } else {              } else {
4889                #                !!!cp ('t131');
4890              }              }
4891            } elsif ($token->{type} eq 'end tag') {  
4892              if ($token->{tag_name} eq 'table') {              ## "after head" insertion mode
4893                ## have a table element in table scope              ## As if <body>
4894                my $i;              !!!insert-element ('body',, $token);
4895                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              $self->{insertion_mode} = IN_BODY_IM;
4896                  my $node = $self->{open_elements}->[$_];              ## reprocess
4897                  if ($node->[1] eq $token->{tag_name}) {              !!!ack-later;
4898                    $i = $_;              next B;
4899                    last INSCOPE;            } elsif ($token->{type} == END_TAG_TOKEN) {
4900                  } elsif ({              if ($token->{tag_name} eq 'head') {
4901                            table => 1, html => 1,                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4902                           }->{$node->[1]}) {                  !!!cp ('t132');
4903                    last INSCOPE;                  ## As if <head>
4904                  }                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4905                } # INSCOPE                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4906                unless (defined $i) {                  push @{$self->{open_elements}},
4907                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      [$self->{head_element}, $el_category->{head}];
4908    
4909                    ## Reprocess in the "in head" insertion mode...
4910                    pop @{$self->{open_elements}};
4911                    $self->{insertion_mode} = AFTER_HEAD_IM;
4912                    !!!next-token;
4913                    next B;
4914                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4915                    !!!cp ('t133');
4916                    ## As if </noscript>
4917                    pop @{$self->{open_elements}};
4918                    !!!parse-error (type => 'in noscript:/',
4919                                    text => 'head', token => $token);
4920                    
4921                    ## Reprocess in the "in head" insertion mode...
4922                    pop @{$self->{open_elements}};
4923                    $self->{insertion_mode} = AFTER_HEAD_IM;
4924                    !!!next-token;
4925                    next B;
4926                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4927                    !!!cp ('t134');
4928                    pop @{$self->{open_elements}};
4929                    $self->{insertion_mode} = AFTER_HEAD_IM;
4930                    !!!next-token;
4931                    next B;
4932                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4933                    !!!cp ('t134.1');
4934                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4935                                    token => $token);
4936                  ## Ignore the token                  ## Ignore the token
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  next B;
4939                  } else {
4940                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4941                }                }
4942                              } elsif ($token->{tag_name} eq 'noscript') {
4943                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4944                if ({                  !!!cp ('t136');
4945                     dd => 1, dt => 1, li => 1, p => 1,                  pop @{$self->{open_elements}};
4946                     td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
4947                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!next-token;
4948                  !!!back-token;                  next B;
4949                  $token = {type => 'end tag',                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4950                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4951                  redo B;                  !!!cp ('t137');
4952                    !!!parse-error (type => 'unmatched end tag',
4953                                    text => 'noscript', token => $token);
4954                    ## Ignore the token ## ISSUE: An issue in the spec.
4955                    !!!next-token;
4956                    next B;
4957                  } else {
4958                    !!!cp ('t138');
4959                    #
4960                }                }
4961                } elsif ({
4962                if ($self->{open_elements}->[-1]->[1] ne 'table') {                        body => 1, html => 1,
4963                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                       }->{$token->{tag_name}}) {
4964                  if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4965                      $self->{insertion_mode} == IN_HEAD_IM or
4966                      $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4967                    !!!cp ('t140');
4968                    !!!parse-error (type => 'unmatched end tag',
4969                                    text => $token->{tag_name}, token => $token);
4970                    ## Ignore the token
4971                    !!!next-token;
4972                    next B;
4973                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4974                    !!!cp ('t140.1');
4975                    !!!parse-error (type => 'unmatched end tag',
4976                                    text => $token->{tag_name}, token => $token);
4977                    ## Ignore the token
4978                    !!!next-token;
4979                    next B;
4980                  } else {
4981                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4982                }                }
4983                } elsif ($token->{tag_name} eq 'p') {
4984                  !!!cp ('t142');
4985                  !!!parse-error (type => 'unmatched end tag',
4986                                  text => $token->{tag_name}, token => $token);
4987                  ## Ignore the token
4988                  !!!next-token;
4989                  next B;
4990                } elsif ($token->{tag_name} eq 'br') {
4991                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4992                    !!!cp ('t142.2');
4993                    ## (before head) as if <head>, (in head) as if </head>
4994                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4995                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4996                    $self->{insertion_mode} = AFTER_HEAD_IM;
4997      
4998                    ## Reprocess in the "after head" insertion mode...
4999                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5000                    !!!cp ('t143.2');
5001                    ## As if </head>
5002                    pop @{$self->{open_elements}};
5003                    $self->{insertion_mode} = AFTER_HEAD_IM;
5004      
5005                    ## Reprocess in the "after head" insertion mode...
5006                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5007                    !!!cp ('t143.3');
5008                    ## ISSUE: Two parse errors for <head><noscript></br>
5009                    !!!parse-error (type => 'unmatched end tag',
5010                                    text => 'br', token => $token);
5011                    ## As if </noscript>
5012                    pop @{$self->{open_elements}};
5013                    $self->{insertion_mode} = IN_HEAD_IM;
5014    
5015                splice @{$self->{open_elements}}, $i;                  ## Reprocess in the "in head" insertion mode...
5016                    ## As if </head>
5017                    pop @{$self->{open_elements}};
5018                    $self->{insertion_mode} = AFTER_HEAD_IM;
5019    
5020                $self->_reset_insertion_mode;                  ## Reprocess in the "after head" insertion mode...
5021                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5022                    !!!cp ('t143.4');
5023                    #
5024                  } else {
5025                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5026                  }
5027    
5028                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5029                  !!!parse-error (type => 'unmatched end tag',
5030                                  text => 'br', token => $token);
5031                  ## Ignore the token
5032                !!!next-token;                !!!next-token;
5033                redo B;                next B;
5034              } elsif ({              } else {
5035                        body => 1, caption => 1, col => 1, colgroup => 1,                !!!cp ('t145');
5036                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                !!!parse-error (type => 'unmatched end tag',
5037                        thead => 1, tr => 1,                                text => $token->{tag_name}, token => $token);
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5038                ## Ignore the token                ## Ignore the token
5039                !!!next-token;                !!!next-token;
5040                redo B;                next B;
5041                }
5042    
5043                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5044                  !!!cp ('t146');
5045                  ## As if </noscript>
5046                  pop @{$self->{open_elements}};
5047                  !!!parse-error (type => 'in noscript:/',
5048                                  text => $token->{tag_name}, token => $token);
5049                  
5050                  ## Reprocess in the "in head" insertion mode...
5051                  ## As if </head>
5052                  pop @{$self->{open_elements}};
5053    
5054                  ## Reprocess in the "after head" insertion mode...
5055                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5056                  !!!cp ('t147');
5057                  ## As if </head>
5058                  pop @{$self->{open_elements}};
5059    
5060                  ## Reprocess in the "after head" insertion mode...
5061                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5062    ## ISSUE: This case cannot be reached?
5063                  !!!cp ('t148');
5064                  !!!parse-error (type => 'unmatched end tag',
5065                                  text => $token->{tag_name}, token => $token);
5066                  ## Ignore the token ## ISSUE: An issue in the spec.
5067                  !!!next-token;
5068                  next B;
5069              } else {              } else {
5070                #                !!!cp ('t149');
5071              }              }
           } else {  
             #  
           }  
5072    
5073            !!!parse-error (type => 'in table:'.$token->{tag_name});              ## "after head" insertion mode
5074            $in_body->($insert_to_foster);              ## As if <body>
5075            redo B;              !!!insert-element ('body',, $token);
5076          } elsif ($self->{insertion_mode} eq 'in caption') {              $self->{insertion_mode} = IN_BODY_IM;
5077            if ($token->{type} eq 'character') {              ## reprocess
5078              ## NOTE: This is a code clone of "character in body".              next B;
5079          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5080            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5081              !!!cp ('t149.1');
5082    
5083              ## NOTE: As if <head>
5084              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5085              $self->{open_elements}->[-1]->[0]->append_child
5086                  ($self->{head_element});
5087              #push @{$self->{open_elements}},
5088              #    [$self->{head_element}, $el_category->{head}];
5089              #$self->{insertion_mode} = IN_HEAD_IM;
5090              ## NOTE: Reprocess.
5091    
5092              ## NOTE: As if </head>
5093              #pop @{$self->{open_elements}};
5094              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5095              ## NOTE: Reprocess.
5096              
5097              #
5098            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5099              !!!cp ('t149.2');
5100    
5101              ## NOTE: As if </head>
5102              pop @{$self->{open_elements}};
5103              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5104              ## NOTE: Reprocess.
5105    
5106              #
5107            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5108              !!!cp ('t149.3');
5109    
5110              !!!parse-error (type => 'in noscript:#eof', token => $token);
5111    
5112              ## As if </noscript>
5113              pop @{$self->{open_elements}};
5114              #$self->{insertion_mode} = IN_HEAD_IM;
5115              ## NOTE: Reprocess.
5116    
5117              ## NOTE: As if </head>
5118              pop @{$self->{open_elements}};
5119              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5120              ## NOTE: Reprocess.
5121    
5122              #
5123            } else {
5124              !!!cp ('t149.4');
5125              #
5126            }
5127    
5128            ## NOTE: As if <body>
5129            !!!insert-element ('body',, $token);
5130            $self->{insertion_mode} = IN_BODY_IM;
5131            ## NOTE: Reprocess.
5132            next B;
5133          } else {
5134            die "$0: $token->{type}: Unknown token type";
5135          }
5136    
5137              ## ISSUE: An issue in the spec.
5138        } elsif ($self->{insertion_mode} & BODY_IMS) {
5139              if ($token->{type} == CHARACTER_TOKEN) {
5140                !!!cp ('t150');
5141                ## NOTE: There is a code clone of "character in body".
5142              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5143                            
5144              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5145    
5146              !!!next-token;              !!!next-token;
5147              redo B;              next B;
5148            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
5149              if ({              if ({
5150                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5151                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5152                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5153                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5154                    ## have an element in table scope
5155                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5156                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5157                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5158                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5159                  my $node = $self->{open_elements}->[$_];  
5160                  if ($node->[1] eq 'caption') {                      ## Close the cell
5161                    $i = $_;                      !!!back-token; # <x>
5162                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5163                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5164                            table => 1, html => 1,                                line => $token->{line},
5165                           }->{$node->[1]}) {                                column => $token->{column}};
5166                    last INSCOPE;                      next B;
5167                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5168                        !!!cp ('t152');
5169                        ## ISSUE: This case can never be reached, maybe.
5170                        last;
5171                      }
5172                  }                  }
5173                } # INSCOPE  
5174                unless (defined $i) {                  !!!cp ('t153');
5175                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5176                        text => $token->{tag_name}, token => $token);
5177                  ## Ignore the token                  ## Ignore the token
5178                    !!!nack ('t153.1');
5179                  !!!next-token;                  !!!next-token;
5180                  redo B;                  next B;
5181                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5182                                  !!!parse-error (type => 'not closed', text => 'caption',
5183                ## generate implied end tags                                  token => $token);
5184                if ({                  
5185                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5186                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5187                    }->{$self->{open_elements}->[-1]->[1]}) {                  my $i;
5188                  !!!back-token; # <?>                  INSCOPE: {
5189                  $token = {type => 'end tag', tag_name => 'caption'};                    for (reverse 0..$#{$self->{open_elements}}) {
5190                  !!!back-token;                      my $node = $self->{open_elements}->[$_];
5191                  $token = {type => 'end tag',                      if ($node->[1] & CAPTION_EL) {
5192                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        !!!cp ('t155');
5193                  redo B;                        $i = $_;
5194                }                        last INSCOPE;
5195                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5196                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        !!!cp ('t156');
5197                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        last;
5198                }                      }
5199                      }
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5200    
5201                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5202                      !!!parse-error (type => 'start tag not allowed',
5203                                      text => $token->{tag_name}, token => $token);
5204                      ## Ignore the token
5205                      !!!nack ('t157.1');
5206                      !!!next-token;
5207                      next B;
5208                    } # INSCOPE
5209                    
5210                    ## generate implied end tags
5211                    while ($self->{open_elements}->[-1]->[1]
5212                               & END_TAG_OPTIONAL_EL) {
5213                      !!!cp ('t158');
5214                      pop @{$self->{open_elements}};
5215                    }
5216    
5217                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5218                redo B;                    !!!cp ('t159');
5219                      !!!parse-error (type => 'not closed',
5220                                      text => $self->{open_elements}->[-1]->[0]
5221                                          ->manakai_local_name,
5222                                      token => $token);
5223                    } else {
5224                      !!!cp ('t160');
5225                    }
5226                    
5227                    splice @{$self->{open_elements}}, $i;
5228                    
5229                    $clear_up_to_marker->();
5230                    
5231                    $self->{insertion_mode} = IN_TABLE_IM;
5232                    
5233                    ## reprocess
5234                    !!!ack-later;
5235                    next B;
5236                  } else {
5237                    !!!cp ('t161');
5238                    #
5239                  }
5240              } else {              } else {
5241                  !!!cp ('t162');
5242                #                #
5243              }              }
5244            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5245              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5246                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5247                my $i;                  ## have an element in table scope
5248                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5249                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5250                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5251                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5252                    last INSCOPE;                      !!!cp ('t163');
5253                  } elsif ({                      $i = $_;
5254                            table => 1, html => 1,                      last INSCOPE;
5255                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5256                    last INSCOPE;                      !!!cp ('t164');
5257                        last INSCOPE;
5258                      }
5259                    } # INSCOPE
5260                      unless (defined $i) {
5261                        !!!cp ('t165');
5262                        !!!parse-error (type => 'unmatched end tag',
5263                                        text => $token->{tag_name},
5264                                        token => $token);
5265                        ## Ignore the token
5266                        !!!next-token;
5267                        next B;
5268                      }
5269                    
5270                    ## generate implied end tags
5271                    while ($self->{open_elements}->[-1]->[1]
5272                               & END_TAG_OPTIONAL_EL) {
5273                      !!!cp ('t166');
5274                      pop @{$self->{open_elements}};
5275                  }                  }
5276                } # INSCOPE  
5277                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5278                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5279                      !!!cp ('t167');
5280                      !!!parse-error (type => 'not closed',
5281                                      text => $self->{open_elements}->[-1]->[0]
5282                                          ->manakai_local_name,
5283                                      token => $token);
5284                    } else {
5285                      !!!cp ('t168');
5286                    }
5287                    
5288                    splice @{$self->{open_elements}}, $i;
5289                    
5290                    $clear_up_to_marker->();
5291                    
5292                    $self->{insertion_mode} = IN_ROW_IM;
5293                    
5294                    !!!next-token;
5295                    next B;
5296                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5297                    !!!cp ('t169');
5298                    !!!parse-error (type => 'unmatched end tag',
5299                                    text => $token->{tag_name}, token => $token);
5300                  ## Ignore the token                  ## Ignore the token
5301                  !!!next-token;                  !!!next-token;
5302                  redo B;                  next B;
5303                }                } else {
5304                                  !!!cp ('t170');
5305                ## generate implied end tags                  #
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5306                }                }
5307                } elsif ($token->{tag_name} eq 'caption') {
5308                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5309                    ## have a table element in table scope
5310                    my $i;
5311                    INSCOPE: {
5312                      for (reverse 0..$#{$self->{open_elements}}) {
5313                        my $node = $self->{open_elements}->[$_];
5314                        if ($node->[1] & CAPTION_EL) {
5315                          !!!cp ('t171');
5316                          $i = $_;
5317                          last INSCOPE;
5318                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5319                          !!!cp ('t172');
5320                          last;
5321                        }
5322                      }
5323    
5324                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5325                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5326                                      text => $token->{tag_name}, token => $token);
5327                      ## Ignore the token
5328                      !!!next-token;
5329                      next B;
5330                    } # INSCOPE
5331                    
5332                    ## generate implied end tags
5333                    while ($self->{open_elements}->[-1]->[1]
5334                               & END_TAG_OPTIONAL_EL) {
5335                      !!!cp ('t174');
5336                      pop @{$self->{open_elements}};
5337                    }
5338                    
5339                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5340                      !!!cp ('t175');
5341                      !!!parse-error (type => 'not closed',
5342                                      text => $self->{open_elements}->[-1]->[0]
5343                                          ->manakai_local_name,
5344                                      token => $token);
5345                    } else {
5346                      !!!cp ('t176');
5347                    }
5348                    
5349                    splice @{$self->{open_elements}}, $i;
5350                    
5351                    $clear_up_to_marker->();
5352                    
5353                    $self->{insertion_mode} = IN_TABLE_IM;
5354                    
5355                    !!!next-token;
5356                    next B;
5357                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5358                    !!!cp ('t177');
5359                    !!!parse-error (type => 'unmatched end tag',
5360                                    text => $token->{tag_name}, token => $token);
5361                    ## Ignore the token
5362                    !!!next-token;
5363                    next B;
5364                  } else {
5365                    !!!cp ('t178');
5366                    #
5367                }                }
5368                } elsif ({
5369                          table => 1, tbody => 1, tfoot => 1,
5370                          thead => 1, tr => 1,
5371                         }->{$token->{tag_name}} and
5372                         $self->{insertion_mode} == IN_CELL_IM) {
5373                  ## have an element in table scope
5374                  my $i;
5375                  my $tn;
5376                  INSCOPE: {
5377                    for (reverse 0..$#{$self->{open_elements}}) {
5378                      my $node = $self->{open_elements}->[$_];
5379                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5380                        !!!cp ('t179');
5381                        $i = $_;
5382    
5383                        ## Close the cell
5384                        !!!back-token; # </x>
5385                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5386                                  line => $token->{line},
5387                                  column => $token->{column}};
5388                        next B;
5389                      } elsif ($node->[1] & TABLE_CELL_EL) {
5390                        !!!cp ('t180');
5391                        $tn = $node->[0]->manakai_local_name;
5392                        ## NOTE: There is exactly one |td| or |th| element
5393                        ## in scope in the stack of open elements by definition.
5394                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5395                        ## ISSUE: Can this be reached?
5396                        !!!cp ('t181');
5397                        last;
5398                      }
5399                    }
5400    
5401                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5402                    !!!parse-error (type => 'unmatched end tag',
5403                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5404                    ## Ignore the token
5405                $self->{insertion_mode} = 'in table';                  !!!next-token;
5406                    next B;
5407                !!!next-token;                } # INSCOPE
5408                redo B;              } elsif ($token->{tag_name} eq 'table' and
5409              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5410                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5411                                  token => $token);
5412    
5413                ## As if </caption>                ## As if </caption>
5414                ## have a table element in table scope                ## have a table element in table scope
5415                my $i;                my $i;
5416                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5417                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5418                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5419                      !!!cp ('t184');
5420                    $i = $_;                    $i = $_;
5421                    last INSCOPE;                    last INSCOPE;
5422                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5423                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5424                    last INSCOPE;                    last INSCOPE;
5425                  }                  }
5426                } # INSCOPE                } # INSCOPE
5427                unless (defined $i) {                unless (defined $i) {
5428                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5429                    !!!parse-error (type => 'unmatched end tag',
5430                                    text => 'caption', token => $token);
5431                  ## Ignore the token                  ## Ignore the token
5432                  !!!next-token;                  !!!next-token;
5433                  redo B;                  next B;
5434                }                }
5435                                
5436                ## generate implied end tags                ## generate implied end tags
5437                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5438                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5439                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                   }->{$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;  
5440                }                }
5441    
5442                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5443                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5444                    !!!parse-error (type => 'not closed',
5445                                    text => $self->{open_elements}->[-1]->[0]
5446                                        ->manakai_local_name,
5447                                    token => $token);
5448                  } else {
5449                    !!!cp ('t189');
5450                }                }
5451    
5452                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5453    
5454                $clear_up_to_marker->();                $clear_up_to_marker->();
5455    
5456                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5457    
5458                ## reprocess                ## reprocess
5459                redo B;                next B;
5460              } elsif ({              } elsif ({
5461                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
5462                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5463                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5464                ## Ignore the token                  !!!cp ('t190');
5465                redo B;                  !!!parse-error (type => 'unmatched end tag',
5466              } else {                                  text => $token->{tag_name}, token => $token);
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
5467                  ## Ignore the token                  ## Ignore the token
5468                  !!!next-token;                  !!!next-token;
5469                  redo B;                  next B;
5470                } else {                } else {
5471                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5472                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5473                }                }
5474              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5475                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5476                          thead => 1, tr => 1,
5477                         }->{$token->{tag_name}} and
5478                         $self->{insertion_mode} == IN_CAPTION_IM) {
5479                  !!!cp ('t192');
5480                  !!!parse-error (type => 'unmatched end tag',
5481                                  text => $token->{tag_name}, token => $token);
5482                ## Ignore the token                ## Ignore the token
5483                !!!next-token;                !!!next-token;
5484                redo B;                next B;
5485              } else {              } else {
5486                #                !!!cp ('t193');
5487                  #
5488              }              }
5489            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5490              #          for my $entry (@{$self->{open_elements}}) {
5491              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5492                !!!cp ('t75');
5493                !!!parse-error (type => 'in body:#eof', token => $token);
5494                last;
5495            }            }
5496            }
5497    
5498            ## As if </colgroup>          ## Stop parsing.
5499            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5500              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5501              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5502          }
5503    
5504          $insert = $insert_to_current;
5505          #
5506        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5507          if ($token->{type} == CHARACTER_TOKEN) {
5508            if (not $open_tables->[-1]->[1] and # tainted
5509                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5510              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5511                  
5512              unless (length $token->{data}) {
5513                !!!cp ('t194');
5514              !!!next-token;              !!!next-token;
5515              redo B;              next B;
5516            } else {            } else {
5517              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5518            }            }
5519          } 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;  
               }  
             }  
5520    
5521              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5522    
5523              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5524              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5525              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5526              ## result in a new Text node.              ## result in a new Text node.
5527              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5528                
5529              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]}) {  
5530                # MUST                # MUST
5531                my $foster_parent_element;                my $foster_parent_element;
5532                my $next_sibling;                my $next_sibling;
5533                my $prev_sibling;                my $prev_sibling;
5534                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5535                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5536                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5537                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5538                        !!!cp ('t196');
5539                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5540                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5541                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5542                    } else {                    } else {
5543                        !!!cp ('t197');
5544                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5545                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5546                    }                    }
# Line 3997  sub _tree_construction_main ($) { Line 5552  sub _tree_construction_main ($) {
5552                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5553                if (defined $prev_sibling and                if (defined $prev_sibling and
5554                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5555                    !!!cp ('t198');
5556                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5557                } else {                } else {
5558                    !!!cp ('t199');
5559                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5560                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5561                     $next_sibling);                     $next_sibling);
5562                }                }
5563              } else {            $open_tables->[-1]->[1] = 1; # tainted
5564                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5565              !!!cp ('t200');
5566              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5567            }
5568                
5569            !!!next-token;
5570            next B;
5571          } elsif ($token->{type} == START_TAG_TOKEN) {
5572            if ({
5573                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5574                 th => 1, td => 1,
5575                }->{$token->{tag_name}}) {
5576              if ($self->{insertion_mode} == IN_TABLE_IM) {
5577                ## Clear back to table context
5578                while (not ($self->{open_elements}->[-1]->[1]
5579                                & TABLE_SCOPING_EL)) {
5580                  !!!cp ('t201');
5581                  pop @{$self->{open_elements}};
5582              }              }
5583                            
5584              !!!next-token;              !!!insert-element ('tbody',, $token);
5585              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5586            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5587              ## Copied from 'in table'            }
5588              my $comment = $self->{document}->create_comment ($token->{data});            
5589              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5590              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5591              redo B;                !!!cp ('t202');
5592            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5593              if ({              }
5594                   tr => 1,                  
5595                   th => 1, td => 1,              ## Clear back to table body context
5596                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5597                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5598                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5599                  ## ISSUE: Can this case be reached?
5600                  pop @{$self->{open_elements}};
5601                }
5602                    
5603                    $self->{insertion_mode} = IN_ROW_IM;
5604                    if ($token->{tag_name} eq 'tr') {
5605                      !!!cp ('t204');
5606                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5607                      !!!nack ('t204');
5608                      !!!next-token;
5609                      next B;
5610                    } else {
5611                      !!!cp ('t205');
5612                      !!!insert-element ('tr',, $token);
5613                      ## reprocess in the "in row" insertion mode
5614                    }
5615                  } else {
5616                    !!!cp ('t206');
5617                }                }
5618    
5619                ## Clear back to table body context                ## Clear back to table row context
5620                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5621                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5622                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5624                }                }
5625                                
5626                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5627                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5628                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5629                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5630                } else {                
5631                  !!!insert-element ('tr');                !!!nack ('t207.1');
5632                  ## reprocess                !!!next-token;
5633                }                next B;
               redo B;  
5634              } elsif ({              } elsif ({
5635                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5636                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5637                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5638                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5639                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5640                my $i;                  ## As if </tr>
5641                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5642                  my $node = $self->{open_elements}->[$_];                  my $i;
5643                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5644                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5645                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5646                    $i = $_;                      !!!cp ('t208');
5647                    last INSCOPE;                      $i = $_;
5648                  } elsif ({                      last INSCOPE;
5649                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5650                           }->{$node->[1]}) {                      !!!cp ('t209');
5651                    last INSCOPE;                      last INSCOPE;
5652                      }
5653                    } # INSCOPE
5654                    unless (defined $i) {
5655                      !!!cp ('t210');
5656    ## TODO: This type is wrong.
5657                      !!!parse-error (type => 'unmacthed end tag',
5658                                      text => $token->{tag_name}, token => $token);
5659                      ## Ignore the token
5660                      !!!nack ('t210.1');
5661                      !!!next-token;
5662                      next B;
5663                    }
5664                    
5665                    ## Clear back to table row context
5666                    while (not ($self->{open_elements}->[-1]->[1]
5667                                    & TABLE_ROW_SCOPING_EL)) {
5668                      !!!cp ('t211');
5669                      ## ISSUE: Can this case be reached?
5670                      pop @{$self->{open_elements}};
5671                    }
5672                    
5673                    pop @{$self->{open_elements}}; # tr
5674                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5675                    if ($token->{tag_name} eq 'tr') {
5676                      !!!cp ('t212');
5677                      ## reprocess
5678                      !!!ack-later;
5679                      next B;
5680                    } else {
5681                      !!!cp ('t213');
5682                      ## reprocess in the "in table body" insertion mode...
5683                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5684                }                }
5685    
5686                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5687                while (not {                  ## have an element in table scope
5688                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5689                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5690                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5691                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5692                        !!!cp ('t214');
5693                        $i = $_;
5694                        last INSCOPE;
5695                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5696                        !!!cp ('t215');
5697                        last INSCOPE;
5698                      }
5699                    } # INSCOPE
5700                    unless (defined $i) {
5701                      !!!cp ('t216');
5702    ## TODO: This erorr type is wrong.
5703                      !!!parse-error (type => 'unmatched end tag',
5704                                      text => $token->{tag_name}, token => $token);
5705                      ## Ignore the token
5706                      !!!nack ('t216.1');
5707                      !!!next-token;
5708                      next B;
5709                    }
5710    
5711                    ## Clear back to table body context
5712                    while (not ($self->{open_elements}->[-1]->[1]
5713                                    & TABLE_ROWS_SCOPING_EL)) {
5714                      !!!cp ('t217');
5715                      ## ISSUE: Can this state be reached?
5716                      pop @{$self->{open_elements}};
5717                    }
5718                    
5719                    ## As if <{current node}>
5720                    ## have an element in table scope
5721                    ## true by definition
5722                    
5723                    ## Clear back to table body context
5724                    ## nop by definition
5725                    
5726                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5727                    $self->{insertion_mode} = IN_TABLE_IM;
5728                    ## reprocess in "in table" insertion mode...
5729                  } else {
5730                    !!!cp ('t218');
5731                }                }
5732    
5733                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5734                ## have an element in table scope                  ## Clear back to table context
5735                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5736                                    & TABLE_SCOPING_EL)) {
5737                ## Clear back to table body context                    !!!cp ('t219');
5738                ## nop by definition                    ## ISSUE: Can this state be reached?
5739                      pop @{$self->{open_elements}};
5740                pop @{$self->{open_elements}};                  }
5741                $self->{insertion_mode} = 'in table';                  
5742                ## reprocess                  !!!insert-element ('colgroup',, $token);
5743                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5744                    ## reprocess
5745                    !!!ack-later;
5746                    next B;
5747                  } elsif ({
5748                            caption => 1,
5749                            colgroup => 1,
5750                            tbody => 1, tfoot => 1, thead => 1,
5751                           }->{$token->{tag_name}}) {
5752                    ## Clear back to table context
5753                    while (not ($self->{open_elements}->[-1]->[1]
5754                                    & TABLE_SCOPING_EL)) {
5755                      !!!cp ('t220');
5756                      ## ISSUE: Can this state be reached?
5757                      pop @{$self->{open_elements}};
5758                    }
5759                    
5760                    push @$active_formatting_elements, ['#marker', '']
5761                        if $token->{tag_name} eq 'caption';
5762                    
5763                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5764                    $self->{insertion_mode} = {
5765                                               caption => IN_CAPTION_IM,
5766                                               colgroup => IN_COLUMN_GROUP_IM,
5767                                               tbody => IN_TABLE_BODY_IM,
5768                                               tfoot => IN_TABLE_BODY_IM,
5769                                               thead => IN_TABLE_BODY_IM,
5770                                              }->{$token->{tag_name}};
5771                    !!!next-token;
5772                    !!!nack ('t220.1');
5773                    next B;
5774                  } else {
5775                    die "$0: in table: <>: $token->{tag_name}";
5776                  }
5777              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5778                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5779                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5780                                      ->manakai_local_name,
5781                                  token => $token);
5782    
5783                ## As if </table>                ## As if </table>
5784                ## have a table element in table scope                ## have a table element in table scope
5785                my $i;                my $i;
5786                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5787                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5788                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5789                      !!!cp ('t221');
5790                    $i = $_;                    $i = $_;
5791                    last INSCOPE;                    last INSCOPE;
5792                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5793                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5794                    last INSCOPE;                    last INSCOPE;
5795                  }                  }
5796                } # INSCOPE                } # INSCOPE
5797                unless (defined $i) {                unless (defined $i) {
5798                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5799    ## TODO: The following is wrong, maybe.
5800                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5801                                    token => $token);
5802                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5803                    !!!nack ('t223.1');
5804                  !!!next-token;                  !!!next-token;
5805                  redo B;                  next B;
5806                }                }
5807                                
5808    ## TODO: Followings are removed from the latest spec.
5809                ## generate implied end tags                ## generate implied end tags
5810                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5811                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5812                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                   }->{$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;  
5813                }                }
5814    
5815                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5816                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5817                    ## NOTE: |<table><tr><table>|
5818                    !!!parse-error (type => 'not closed',
5819                                    text => $self->{open_elements}->[-1]->[0]
5820                                        ->manakai_local_name,
5821                                    token => $token);
5822                  } else {
5823                    !!!cp ('t226');
5824                }                }
5825    
5826                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5827                  pop @{$open_tables};
5828    
5829                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5830    
5831                ## reprocess            ## reprocess
5832                redo B;            !!!ack-later;
5833              } else {            next B;
5834                #          } elsif ($token->{tag_name} eq 'style') {
5835              }            if (not $open_tables->[-1]->[1]) { # tainted
5836            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5837              if ({              ## NOTE: This is a "as if in head" code clone.
5838                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5839                  }->{$token->{tag_name}}) {              next B;
5840                ## have an element in table scope            } else {
5841                my $i;              !!!cp ('t227.7');
5842                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5843                  my $node = $self->{open_elements}->[$_];            }
5844                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5845                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5846                    last INSCOPE;              !!!cp ('t227.6');
5847                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5848                            table => 1, html => 1,              $script_start_tag->();
5849                           }->{$node->[1]}) {              next B;
5850                    last INSCOPE;            } else {
5851                  }              !!!cp ('t227.5');
5852                } # INSCOPE              #
5853                unless (defined $i) {            }
5854                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5855                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5856                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5857                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5858                }                if ($type eq 'hidden') {
5859                    !!!cp ('t227.3');
5860                    !!!parse-error (type => 'in table',
5861                                    text => $token->{tag_name}, token => $token);
5862    
5863                ## Clear back to table body context                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
5864    
5865                pop @{$self->{open_elements}};                  ## TODO: form element pointer
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5866    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5867                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
5868    
5869                pop @{$self->{open_elements}};                  !!!next-token;
5870                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5871                ## reprocess                  next B;
5872                redo B;                } else {
5873              } elsif ({                  !!!cp ('t227.2');
5874                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5875                        html => 1, td => 1, th => 1, tr => 1,                }
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
5876              } else {              } else {
5877                  !!!cp ('t227.1');
5878                #                #
5879              }              }
5880            } else {            } else {
5881                !!!cp ('t227.4');
5882              #              #
5883            }            }
5884                      } else {
5885            ## As if in table            !!!cp ('t227');
5886            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5887            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
5888    
5889              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5890                            token => $token);
5891    
5892              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5893              ## ISSUE: Spec says that "whenever a node would be inserted          #
5894              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5895              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5896              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
5897                ## have an element in table scope                ## have an element in table scope
5898                my $i;                my $i;
5899                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5900                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5901                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5902                      !!!cp ('t228');
5903                    $i = $_;                    $i = $_;
5904                    last INSCOPE;                    last INSCOPE;
5905                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5906                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5907                    last INSCOPE;                    last INSCOPE;
5908                  }                  }
5909                } # INSCOPE                } # INSCOPE
5910                unless (defined $i) {                unless (defined $i) {
5911                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5912                    !!!parse-error (type => 'unmatched end tag',
5913                                    text => $token->{tag_name}, token => $token);
5914                  ## Ignore the token                  ## Ignore the token
5915                    !!!nack ('t230.1');
5916                  !!!next-token;                  !!!next-token;
5917                  redo B;                  next B;
5918                  } else {
5919                    !!!cp ('t232');
5920                }                }
5921    
5922                ## Clear back to table row context                ## Clear back to table row context
5923                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5924                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5925                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5926                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5927                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5928                }                }
5929    
5930                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5931                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5932                ## reprocess                !!!next-token;
5933                redo B;                !!!nack ('t231.1');
5934                  next B;
5935              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5936                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5937                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5938                    ## have an element in table scope
5939                ## As if </table>                  my $i;
5940                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5941                my $i;                    my $node = $self->{open_elements}->[$_];
5942                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5943                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5944                  if ($node->[1] eq 'table') {                      $i = $_;
5945                    $i = $_;                      last INSCOPE;
5946                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5947                  } elsif ({                      !!!cp ('t234');
5948                            table => 1, html => 1,                      last INSCOPE;
5949                           }->{$node->[1]}) {                    }
5950                    last INSCOPE;                  } # INSCOPE
5951                    unless (defined $i) {
5952                      !!!cp ('t235');
5953    ## TODO: The following is wrong.
5954                      !!!parse-error (type => 'unmatched end tag',
5955                                      text => $token->{type}, token => $token);
5956                      ## Ignore the token
5957                      !!!nack ('t236.1');
5958                      !!!next-token;
5959                      next B;
5960                  }                  }
5961                } # INSCOPE                  
5962                unless (defined $i) {                  ## Clear back to table row context
5963                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5964                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5965                  !!!next-token;                    !!!cp ('t236');
5966                  redo B;  ## ISSUE: Can this state be reached?
5967                }                    pop @{$self->{open_elements}};
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5968                  }                  }
5969                } # INSCOPE                  
5970                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5971                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5972                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5973                  !!!next-token;                }
5974                  redo B;  
5975                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5976                    ## have an element in table scope
5977                ## Clear back to table row context                  my $i;
5978                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5979                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5980                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5981                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5982                        $i = $_;
5983                        last INSCOPE;
5984                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5985                        !!!cp ('t238');
5986                        last INSCOPE;
5987                      }
5988                    } # INSCOPE
5989                    unless (defined $i) {
5990                      !!!cp ('t239');
5991                      !!!parse-error (type => 'unmatched end tag',
5992                                      text => $token->{tag_name}, token => $token);
5993                      ## Ignore the token
5994                      !!!nack ('t239.1');
5995                      !!!next-token;
5996                      next B;
5997                    }
5998                    
5999                    ## Clear back to table body context
6000                    while (not ($self->{open_elements}->[-1]->[1]
6001                                    & TABLE_ROWS_SCOPING_EL)) {
6002                      !!!cp ('t240');
6003                      pop @{$self->{open_elements}};
6004                    }
6005                    
6006                    ## As if <{current node}>
6007                    ## have an element in table scope
6008                    ## true by definition
6009                    
6010                    ## Clear back to table body context
6011                    ## nop by definition
6012                    
6013                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6014                    $self->{insertion_mode} = IN_TABLE_IM;
6015                    ## reprocess in the "in table" insertion mode...
6016                }                }
6017    
6018                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
6019                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
6020                !!!next-token;                ## the code for <table> in the "in table" insertion mode
6021                redo B;                ## is synced with it.
6022              } elsif ($token->{tag_name} eq 'table') {  
6023                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
6024                my $i;                my $i;
6025                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6026                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6027                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
6028                      !!!cp ('t241');
6029                    $i = $_;                    $i = $_;
6030                    last INSCOPE;                    last INSCOPE;
6031                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6032                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6033                    last INSCOPE;                    last INSCOPE;
6034                  }                  }
6035                } # INSCOPE                } # INSCOPE
6036                unless (defined $i) {                unless (defined $i) {
6037                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
6038                    !!!parse-error (type => 'unmatched end tag',
6039                                    text => $token->{tag_name}, token => $token);
6040                  ## Ignore the token                  ## Ignore the token
6041                    !!!nack ('t243.1');
6042                  !!!next-token;                  !!!next-token;
6043                  redo B;                  next B;
6044                }                }
6045                    
6046                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
6047                while (not {                pop @{$open_tables};
6048                  tr => 1, html => 1,                
6049                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
6050                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
6051                  pop @{$self->{open_elements}};                !!!next-token;
6052                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
6053              } elsif ({              } elsif ({
6054                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6055                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6056                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6057                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6058                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6059                  my $node = $self->{open_elements}->[$_];                  my $i;
6060                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6061                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6062                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6063                  } elsif ({                      !!!cp ('t247');
6064                            table => 1, html => 1,                      $i = $_;
6065                           }->{$node->[1]}) {                      last INSCOPE;
6066                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6067                        !!!cp ('t248');
6068                        last INSCOPE;
6069                      }
6070                    } # INSCOPE
6071                      unless (defined $i) {
6072                        !!!cp ('t249');
6073                        !!!parse-error (type => 'unmatched end tag',
6074                                        text => $token->{tag_name}, token => $token);
6075                        ## Ignore the token
6076                        !!!nack ('t249.1');
6077                        !!!next-token;
6078                        next B;
6079                      }
6080                    
6081                    ## As if </tr>
6082                    ## have an element in table scope
6083                    my $i;
6084                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6085                      my $node = $self->{open_elements}->[$_];
6086                      if ($node->[1] & TABLE_ROW_EL) {
6087                        !!!cp ('t250');
6088                        $i = $_;
6089                        last INSCOPE;
6090                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6091                        !!!cp ('t251');
6092                        last INSCOPE;
6093                      }
6094                    } # INSCOPE
6095                      unless (defined $i) {
6096                        !!!cp ('t252');
6097                        !!!parse-error (type => 'unmatched end tag',
6098                                        text => 'tr', token => $token);
6099                        ## Ignore the token
6100                        !!!nack ('t252.1');
6101                        !!!next-token;
6102                        next B;
6103                      }
6104                    
6105                    ## Clear back to table row context
6106                    while (not ($self->{open_elements}->[-1]->[1]
6107                                    & TABLE_ROW_SCOPING_EL)) {
6108                      !!!cp ('t253');
6109    ## ISSUE: Can this case be reached?
6110                      pop @{$self->{open_elements}};
6111                  }                  }
6112                } # INSCOPE                  
6113                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6114                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6115                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6116                }                }
6117    
               ## As if </tr>  
6118                ## have an element in table scope                ## have an element in table scope
6119                my $i;                my $i;
6120                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6121                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6122                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6123                      !!!cp ('t254');
6124                    $i = $_;                    $i = $_;
6125                    last INSCOPE;                    last INSCOPE;
6126                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6127                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6128                    last INSCOPE;                    last INSCOPE;
6129                  }                  }
6130                } # INSCOPE                } # INSCOPE
6131                unless (defined $i) {                unless (defined $i) {
6132                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6133                    !!!parse-error (type => 'unmatched end tag',
6134                                    text => $token->{tag_name}, token => $token);
6135                  ## Ignore the token                  ## Ignore the token
6136                    !!!nack ('t256.1');
6137                  !!!next-token;                  !!!next-token;
6138                  redo B;                  next B;
6139                }                }
6140    
6141                ## Clear back to table row context                ## Clear back to table body context
6142                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6143                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6144                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6145                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6146                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6147                }                }
6148    
6149                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6150                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6151                ## reprocess                !!!nack ('t257.1');
6152                redo B;                !!!next-token;
6153                  next B;
6154              } elsif ({              } elsif ({
6155                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6156                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6157                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6158                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6159                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6160                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6161                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6162                !!!next-token;                            text => $token->{tag_name}, token => $token);
6163                redo B;            ## Ignore the token
6164              } else {            !!!nack ('t258.1');
6165                #             !!!next-token;
6166              }            next B;
6167            } else {          } else {
6168              #            !!!cp ('t259');
6169            }            !!!parse-error (type => 'in table:/',
6170                              text => $token->{tag_name}, token => $token);
6171    
6172            ## As if in table            $insert = $insert_to_foster;
6173            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6174            $in_body->($insert_to_foster);          }
6175            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6176          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6177            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6178              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6179              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6180                          #
6181              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6182              !!!cp ('t259.2');
6183              #
6184            }
6185    
6186              !!!next-token;          ## Stop parsing
6187              redo B;          last B;
6188            } elsif ($token->{type} eq 'comment') {        } else {
6189              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6190              my $comment = $self->{document}->create_comment ($token->{data});        }
6191              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6192              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6193              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6194            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6195              if ({                unless (length $token->{data}) {
6196                   caption => 1, col => 1, colgroup => 1,                  !!!cp ('t260');
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
6197                  !!!next-token;                  !!!next-token;
6198                  redo B;                  next B;
6199                }                }
6200                }
6201                ## Close the cell              
6202                !!!back-token; # <?>              !!!cp ('t261');
6203                $token = {type => 'end tag', tag_name => $tn};              #
6204                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6205              } else {              if ($token->{tag_name} eq 'col') {
6206                  !!!cp ('t262');
6207                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6208                  pop @{$self->{open_elements}};
6209                  !!!ack ('t262.1');
6210                  !!!next-token;
6211                  next B;
6212                } else {
6213                  !!!cp ('t263');
6214                #                #
6215              }              }
6216            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6217              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6218                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6219                my $i;                  !!!cp ('t264');
6220                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6221                  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});  
6222                  ## Ignore the token                  ## Ignore the token
6223                  !!!next-token;                  !!!next-token;
6224                  redo B;                  next B;
6225                }                } else {
6226                                  !!!cp ('t265');
6227                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6228                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6229                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6230                     td => ($token->{tag_name} eq 'th'),                  next B;            
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6231                }                }
6232                } elsif ($token->{tag_name} eq 'col') {
6233                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6234                  !!!parse-error (type => 'unmatched end tag',
6235                $clear_up_to_marker->();                                text => 'col', token => $token);
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6236                ## Ignore the token                ## Ignore the token
6237                !!!next-token;                !!!next-token;
6238                redo B;                next B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
6239              } else {              } else {
6240                #                !!!cp ('t267');
6241                  #
6242              }              }
6243          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6244            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6245                @{$self->{open_elements}} == 1) { # redundant, maybe
6246              !!!cp ('t270.2');
6247              ## Stop parsing.
6248              last B;
6249            } else {
6250              ## NOTE: As if </colgroup>.
6251              !!!cp ('t270.1');
6252              pop @{$self->{open_elements}}; # colgroup
6253              $self->{insertion_mode} = IN_TABLE_IM;
6254              ## Reprocess.
6255              next B;
6256            }
6257          } else {
6258            die "$0: $token->{type}: Unknown token type";
6259          }
6260    
6261              ## As if </colgroup>
6262              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6263                !!!cp ('t269');
6264    ## TODO: Wrong error type?
6265                !!!parse-error (type => 'unmatched end tag',
6266                                text => 'colgroup', token => $token);
6267                ## Ignore the token
6268                !!!nack ('t269.1');
6269                !!!next-token;
6270                next B;
6271            } else {            } else {
6272              #              !!!cp ('t270');
6273                pop @{$self->{open_elements}}; # colgroup
6274                $self->{insertion_mode} = IN_TABLE_IM;
6275                !!!ack-later;
6276                ## reprocess
6277                next B;
6278              }
6279        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6280          if ($token->{type} == CHARACTER_TOKEN) {
6281            !!!cp ('t271');
6282            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6283            !!!next-token;
6284            next B;
6285          } elsif ($token->{type} == START_TAG_TOKEN) {
6286            if ($token->{tag_name} eq 'option') {
6287              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6288                !!!cp ('t272');
6289                ## As if </option>
6290                pop @{$self->{open_elements}};
6291              } else {
6292                !!!cp ('t273');
6293            }            }
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
6294    
6295                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6296                !!!next-token;            !!!nack ('t273.1');
6297                redo B;            !!!next-token;
6298              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6299                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6300                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6301                  pop @{$self->{open_elements}};              !!!cp ('t274');
6302                }              ## As if </option>
6303                pop @{$self->{open_elements}};
6304              } else {
6305                !!!cp ('t275');
6306              }
6307    
6308                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6309                  ## As if </optgroup>              !!!cp ('t276');
6310                  pop @{$self->{open_elements}};              ## As if </optgroup>
6311                }              pop @{$self->{open_elements}};
6312              } else {
6313                !!!cp ('t277');
6314              }
6315    
6316                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6317                !!!next-token;            !!!nack ('t277.1');
6318                redo B;            !!!next-token;
6319              } elsif ($token->{tag_name} eq 'select') {            next B;
6320                !!!parse-error (type => 'not closed:select');          } elsif ({
6321                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6322                ## have an element in table scope                   }->{$token->{tag_name}} or
6323                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6324                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6325                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6326                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6327                    $i = $_;                     tr => 1, td => 1, th => 1,
6328                    last INSCOPE;                    }->{$token->{tag_name}})) {
6329                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6330                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6331                           }->{$node->[1]}) {                            token => $token);
6332                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6333                  }            ## as if there were </select> (otherwise).
6334                } # INSCOPE            ## have an element in table scope
6335                unless (defined $i) {            my $i;
6336                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6337                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6338                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6339                  redo B;                !!!cp ('t278');
6340                }                $i = $_;
6341                  last INSCOPE;
6342                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6343                  !!!cp ('t279');
6344                  last INSCOPE;
6345                }
6346              } # INSCOPE
6347              unless (defined $i) {
6348                !!!cp ('t280');
6349                !!!parse-error (type => 'unmatched end tag',
6350                                text => 'select', token => $token);
6351                ## Ignore the token
6352                !!!nack ('t280.1');
6353                !!!next-token;
6354                next B;
6355              }
6356                                
6357                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6358              splice @{$self->{open_elements}}, $i;
6359    
6360                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6361    
6362                !!!next-token;            if ($token->{tag_name} eq 'select') {
6363                redo B;              !!!nack ('t281.2');
6364              } else {              !!!next-token;
6365                #              next B;
6366              } else {
6367                !!!cp ('t281.1');
6368                !!!ack-later;
6369                ## Reprocess the token.
6370                next B;
6371              }
6372            } else {
6373              !!!cp ('t282');
6374              !!!parse-error (type => 'in select',
6375                              text => $token->{tag_name}, token => $token);
6376              ## Ignore the token
6377              !!!nack ('t282.1');
6378              !!!next-token;
6379              next B;
6380            }
6381          } elsif ($token->{type} == END_TAG_TOKEN) {
6382            if ($token->{tag_name} eq 'optgroup') {
6383              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6384                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6385                !!!cp ('t283');
6386                ## As if </option>
6387                splice @{$self->{open_elements}}, -2;
6388              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6389                !!!cp ('t284');
6390                pop @{$self->{open_elements}};
6391              } else {
6392                !!!cp ('t285');
6393                !!!parse-error (type => 'unmatched end tag',
6394                                text => $token->{tag_name}, token => $token);
6395                ## Ignore the token
6396              }
6397              !!!nack ('t285.1');
6398              !!!next-token;
6399              next B;
6400            } elsif ($token->{tag_name} eq 'option') {
6401              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6402                !!!cp ('t286');
6403                pop @{$self->{open_elements}};
6404              } else {
6405                !!!cp ('t287');
6406                !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408                ## Ignore the token
6409              }
6410              !!!nack ('t287.1');
6411              !!!next-token;
6412              next B;
6413            } elsif ($token->{tag_name} eq 'select') {
6414              ## have an element in table scope
6415              my $i;
6416              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6417                my $node = $self->{open_elements}->[$_];
6418                if ($node->[1] & SELECT_EL) {
6419                  !!!cp ('t288');
6420                  $i = $_;
6421                  last INSCOPE;
6422                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6423                  !!!cp ('t289');
6424                  last INSCOPE;
6425              }              }
6426            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6427              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6428                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6429                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6430                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6431                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6432                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6433                  pop @{$self->{open_elements}};              !!!next-token;
6434                } else {              next B;
6435                  !!!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;  
               }  
6436                                
6437                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6438              splice @{$self->{open_elements}}, $i;
6439    
6440                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6441    
6442                !!!next-token;            !!!nack ('t291.1');
6443                redo B;            !!!next-token;
6444              } elsif ({            next B;
6445                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6446                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6447                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6448                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6449                                   }->{$token->{tag_name}}) {
6450                ## have an element in table scope  ## TODO: The following is wrong?
6451                my $i;            !!!parse-error (type => 'unmatched end tag',
6452                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;  
               }  
6453                                
6454                ## As if </select>            ## have an element in table scope
6455                ## have an element in table scope            my $i;
6456                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6457                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6458                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6459                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6460                    $i = $_;                $i = $_;
6461                    last INSCOPE;                last INSCOPE;
6462                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6463                            table => 1, html => 1,                !!!cp ('t293');
6464                           }->{$node->[1]}) {                last INSCOPE;
6465                    last INSCOPE;              }
6466                  }            } # INSCOPE
6467                } # INSCOPE            unless (defined $i) {
6468                unless (defined $i) {              !!!cp ('t294');
6469                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6470                  ## Ignore the </select> token              !!!nack ('t294.1');
6471                  !!!next-token; ## TODO: ok?              !!!next-token;
6472                  redo B;              next B;
6473                }            }
6474                                
6475                splice @{$self->{open_elements}}, $i;            ## As if </select>
6476              ## have an element in table scope
6477                $self->_reset_insertion_mode;            undef $i;
6478              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6479                ## reprocess              my $node = $self->{open_elements}->[$_];
6480                redo B;              if ($node->[1] & SELECT_EL) {
6481              } else {                !!!cp ('t295');
6482                #                $i = $_;
6483                  last INSCOPE;
6484                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6485    ## ISSUE: Can this state be reached?
6486                  !!!cp ('t296');
6487                  last INSCOPE;
6488              }              }
6489            } else {            } # INSCOPE
6490              #            unless (defined $i) {
6491                !!!cp ('t297');
6492    ## TODO: The following error type is correct?
6493                !!!parse-error (type => 'unmatched end tag',
6494                                text => 'select', token => $token);
6495                ## Ignore the </select> token
6496                !!!nack ('t297.1');
6497                !!!next-token; ## TODO: ok?
6498                next B;
6499            }            }
6500                  
6501              !!!cp ('t298');
6502              splice @{$self->{open_elements}}, $i;
6503    
6504            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6505    
6506              !!!ack-later;
6507              ## reprocess
6508              next B;
6509            } else {
6510              !!!cp ('t299');
6511              !!!parse-error (type => 'in select:/',
6512                              text => $token->{tag_name}, token => $token);
6513            ## Ignore the token            ## Ignore the token
6514              !!!nack ('t299.3');
6515            !!!next-token;            !!!next-token;
6516            redo B;            next B;
6517          } elsif ($self->{insertion_mode} eq 'after body') {          }
6518            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6519              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6520                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6521                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6522                            !!!parse-error (type => 'in body:#eof', token => $token);
6523                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6524              !!!cp ('t299.2');
6525            }
6526    
6527                unless (length $token->{data}) {          ## Stop parsing.
6528                  !!!next-token;          last B;
6529                  redo B;        } else {
6530                }          die "$0: $token->{type}: Unknown token type";
6531              }        }
6532                    } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6533              #        if ($token->{type} == CHARACTER_TOKEN) {
6534              !!!parse-error (type => 'after body:#'.$token->{type});          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6535            } elsif ($token->{type} eq 'comment') {            my $data = $1;
6536              my $comment = $self->{document}->create_comment ($token->{data});            ## As if in body
6537              $self->{open_elements}->[0]->[0]->append_child ($comment);            $reconstruct_active_formatting_elements->($insert_to_current);
6538                  
6539              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6540              
6541              unless (length $token->{data}) {
6542                !!!cp ('t300');
6543              !!!next-token;              !!!next-token;
6544              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
6545            }            }
6546            }
6547            
6548            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6549              !!!cp ('t301');
6550              !!!parse-error (type => 'after html:#text', token => $token);
6551    
6552            $self->{insertion_mode} = 'in body';            ## Reprocess in the "after body" insertion mode.
6553            ## reprocess          } else {
6554            redo B;            !!!cp ('t302');
6555          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6556            if ($token->{type} eq 'character') {          
6557              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## "after body" insertion mode
6558                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!parse-error (type => 'after body:#text', token => $token);
6559    
6560                unless (length $token->{data}) {          $self->{insertion_mode} = IN_BODY_IM;
6561                  !!!next-token;          ## reprocess
6562                  redo B;          next B;
6563                }        } elsif ($token->{type} == START_TAG_TOKEN) {
6564              }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6565              !!!cp ('t303');
6566              !!!parse-error (type => 'after html',
6567                              text => $token->{tag_name}, token => $token);
6568              
6569              ## Reprocess in the "after body" insertion mode.
6570            } else {
6571              !!!cp ('t304');
6572            }
6573    
6574              #          ## "after body" insertion mode
6575            } elsif ($token->{type} eq 'comment') {          !!!parse-error (type => 'after body',
6576              my $comment = $self->{document}->create_comment ($token->{data});                          text => $token->{tag_name}, token => $token);
6577              $self->{open_elements}->[-1]->[0]->append_child ($comment);  
6578            $self->{insertion_mode} = IN_BODY_IM;
6579            !!!ack-later;
6580            ## reprocess
6581            next B;
6582          } elsif ($token->{type} == END_TAG_TOKEN) {
6583            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6584              !!!cp ('t305');
6585              !!!parse-error (type => 'after html:/',
6586                              text => $token->{tag_name}, token => $token);
6587              
6588              $self->{insertion_mode} = AFTER_BODY_IM;
6589              ## Reprocess in the "after body" insertion mode.
6590            } else {
6591              !!!cp ('t306');
6592            }
6593    
6594            ## "after body" insertion mode
6595            if ($token->{tag_name} eq 'html') {
6596              if (defined $self->{inner_html_node}) {
6597                !!!cp ('t307');
6598                !!!parse-error (type => 'unmatched end tag',
6599                                text => 'html', token => $token);
6600                ## Ignore the token
6601              !!!next-token;              !!!next-token;
6602              redo B;              next B;
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6603            } else {            } else {
6604              #              !!!cp ('t308');
6605                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6606                !!!next-token;
6607                next B;
6608            }            }
6609            } else {
6610              !!!cp ('t309');
6611              !!!parse-error (type => 'after body:/',
6612                              text => $token->{tag_name}, token => $token);
6613    
6614              $self->{insertion_mode} = IN_BODY_IM;
6615              ## reprocess
6616              next B;
6617            }
6618          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6619            !!!cp ('t309.2');
6620            ## Stop parsing
6621            last B;
6622          } else {
6623            die "$0: $token->{type}: Unknown token type";
6624          }
6625        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6626          if ($token->{type} == CHARACTER_TOKEN) {
6627            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6628              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6629                        
6630            if (defined $token->{tag_name}) {            unless (length $token->{data}) {
6631              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t310');
6632                !!!next-token;
6633                next B;
6634              }
6635            }
6636            
6637            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6638              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6639                !!!cp ('t311');
6640                !!!parse-error (type => 'in frameset:#text', token => $token);
6641              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6642                !!!cp ('t312');
6643                !!!parse-error (type => 'after frameset:#text', token => $token);
6644              } else { # "after after frameset"
6645                !!!cp ('t313');
6646                !!!parse-error (type => 'after html:#text', token => $token);
6647              }
6648              
6649              ## Ignore the token.
6650              if (length $token->{data}) {
6651                !!!cp ('t314');
6652                ## reprocess the rest of characters
6653            } else {            } else {
6654              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t315');
6655                !!!next-token;
6656              }
6657              next B;
6658            }
6659            
6660            die qq[$0: Character "$token->{data}"];
6661          } elsif ($token->{type} == START_TAG_TOKEN) {
6662            if ($token->{tag_name} eq 'frameset' and
6663                $self->{insertion_mode} == IN_FRAMESET_IM) {
6664              !!!cp ('t318');
6665              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6666              !!!nack ('t318.1');
6667              !!!next-token;
6668              next B;
6669            } elsif ($token->{tag_name} eq 'frame' and
6670                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6671              !!!cp ('t319');
6672              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6673              pop @{$self->{open_elements}};
6674              !!!ack ('t319.1');
6675              !!!next-token;
6676              next B;
6677            } elsif ($token->{tag_name} eq 'noframes') {
6678              !!!cp ('t320');
6679              ## NOTE: As if in head.
6680              $parse_rcdata->(CDATA_CONTENT_MODEL);
6681              next B;
6682    
6683              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6684              ## has no parse error.
6685            } else {
6686              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6687                !!!cp ('t321');
6688                !!!parse-error (type => 'in frameset',
6689                                text => $token->{tag_name}, token => $token);
6690              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6691                !!!cp ('t322');
6692                !!!parse-error (type => 'after frameset',
6693                                text => $token->{tag_name}, token => $token);
6694              } else { # "after after frameset"
6695                !!!cp ('t322.2');
6696                !!!parse-error (type => 'after after frameset',
6697                                text => $token->{tag_name}, token => $token);
6698            }            }
6699            ## Ignore the token            ## Ignore the token
6700              !!!nack ('t322.1');
6701            !!!next-token;            !!!next-token;
6702            redo B;            next B;
6703          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6704            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6705              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'frameset' and
6706                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{insertion_mode} == IN_FRAMESET_IM) {
6707              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6708                  @{$self->{open_elements}} == 1) {
6709                !!!cp ('t325');
6710                !!!parse-error (type => 'unmatched end tag',
6711                                text => $token->{tag_name}, token => $token);
6712                ## Ignore the token
6713                !!!next-token;
6714              } else {
6715                !!!cp ('t326');
6716                pop @{$self->{open_elements}};
6717                !!!next-token;
6718              }
6719    
6720                unless (length $token->{data}) {            if (not defined $self->{inner_html_node} and
6721                  !!!next-token;                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6722                  redo B;              !!!cp ('t327');
6723                }              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6724              } else {
6725                !!!cp ('t328');
6726              }
6727              next B;
6728            } elsif ($token->{tag_name} eq 'html' and
6729                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6730              !!!cp ('t329');
6731              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6732              !!!next-token;
6733              next B;
6734            } else {
6735              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6736                !!!cp ('t330');
6737                !!!parse-error (type => 'in frameset:/',
6738                                text => $token->{tag_name}, token => $token);
6739              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6740                !!!cp ('t330.1');
6741                !!!parse-error (type => 'after frameset:/',
6742                                text => $token->{tag_name}, token => $token);
6743              } else { # "after after html"
6744                !!!cp ('t331');
6745                !!!parse-error (type => 'after after frameset:/',
6746                                text => $token->{tag_name}, token => $token);
6747              }
6748              ## Ignore the token
6749              !!!next-token;
6750              next B;
6751            }
6752          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6753            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6754                    @{$self->{open_elements}} == 1) { # redundant, maybe
6755              !!!cp ('t331.1');
6756              !!!parse-error (type => 'in body:#eof', token => $token);
6757            } else {
6758              !!!cp ('t331.2');
6759            }
6760            
6761            ## Stop parsing
6762            last B;
6763          } else {
6764            die "$0: $token->{type}: Unknown token type";
6765          }
6766    
6767          ## ISSUE: An issue in spec here
6768        } else {
6769          die "$0: $self->{insertion_mode}: Unknown insertion mode";
6770        }
6771    
6772        ## "in body" insertion mode
6773        if ($token->{type} == START_TAG_TOKEN) {
6774          if ($token->{tag_name} eq 'script') {
6775            !!!cp ('t332');
6776            ## NOTE: This is an "as if in head" code clone
6777            $script_start_tag->();
6778            next B;
6779          } elsif ($token->{tag_name} eq 'style') {
6780            !!!cp ('t333');
6781            ## NOTE: This is an "as if in head" code clone
6782            $parse_rcdata->(CDATA_CONTENT_MODEL);
6783            next B;
6784          } elsif ({
6785                    base => 1, link => 1,
6786                   }->{$token->{tag_name}}) {
6787            !!!cp ('t334');
6788            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6789            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6790            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6791            !!!ack ('t334.1');
6792            !!!next-token;
6793            next B;
6794          } elsif ($token->{tag_name} eq 'meta') {
6795            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6796            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6797            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6798    
6799            unless ($self->{confident}) {
6800              if ($token->{attributes}->{charset}) {
6801                !!!cp ('t335');
6802                ## NOTE: Whether the encoding is supported or not is handled
6803                ## in the {change_encoding} callback.
6804                $self->{change_encoding}
6805                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6806                
6807                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6808                    ->set_user_data (manakai_has_reference =>
6809                                         $token->{attributes}->{charset}
6810                                             ->{has_reference});
6811              } elsif ($token->{attributes}->{content}) {
6812                if ($token->{attributes}->{content}->{value}
6813                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6814                        [\x09-\x0D\x20]*=
6815                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6816                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6817                  !!!cp ('t336');
6818                  ## NOTE: Whether the encoding is supported or not is handled
6819                  ## in the {change_encoding} callback.
6820                  $self->{change_encoding}
6821                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6822                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6823                      ->set_user_data (manakai_has_reference =>
6824                                           $token->{attributes}->{content}
6825                                                 ->{has_reference});
6826              }              }
6827              }
6828            } else {
6829              if ($token->{attributes}->{charset}) {
6830                !!!cp ('t337');
6831                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6832                    ->set_user_data (manakai_has_reference =>
6833                                         $token->{attributes}->{charset}
6834                                             ->{has_reference});
6835              }
6836              if ($token->{attributes}->{content}) {
6837                !!!cp ('t338');
6838                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6839                    ->set_user_data (manakai_has_reference =>
6840                                         $token->{attributes}->{content}
6841                                             ->{has_reference});
6842              }
6843            }
6844    
6845              #          !!!ack ('t338.1');
6846            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6847              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6848              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6849              !!!next-token;          !!!cp ('t341');
6850              redo B;          ## NOTE: This is an "as if in head" code clone
6851            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6852              if ($token->{tag_name} eq 'noframes') {          next B;
6853                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6854                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6855              } else {                
6856                #          if (@{$self->{open_elements}} == 1 or
6857                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6858              !!!cp ('t342');
6859              ## Ignore the token
6860            } else {
6861              my $body_el = $self->{open_elements}->[1]->[0];
6862              for my $attr_name (keys %{$token->{attributes}}) {
6863                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6864                  !!!cp ('t343');
6865                  $body_el->set_attribute_ns
6866                    (undef, [undef, $attr_name],
6867                     $token->{attributes}->{$attr_name}->{value});
6868              }              }
6869            } elsif ($token->{type} eq 'end tag') {            }
6870              if ($token->{tag_name} eq 'html') {          }
6871                $phase = 'trailing end';          !!!nack ('t343.1');
6872            !!!next-token;
6873            next B;
6874          } elsif ({
6875                    address => 1, blockquote => 1, center => 1, dir => 1,
6876                    div => 1, dl => 1, fieldset => 1,
6877                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6878                    menu => 1, ol => 1, p => 1, ul => 1,
6879                    pre => 1, listing => 1,
6880                    form => 1,
6881                    table => 1,
6882                    hr => 1,
6883                   }->{$token->{tag_name}}) {
6884            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6885              !!!cp ('t350');
6886              !!!parse-error (type => 'in form:form', token => $token);
6887              ## Ignore the token
6888              !!!nack ('t350.1');
6889              !!!next-token;
6890              next B;
6891            }
6892    
6893            ## has a p element in scope
6894            INSCOPE: for (reverse @{$self->{open_elements}}) {
6895              if ($_->[1] & P_EL) {
6896                !!!cp ('t344');
6897                !!!back-token; # <form>
6898                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6899                          line => $token->{line}, column => $token->{column}};
6900                next B;
6901              } elsif ($_->[1] & SCOPING_EL) {
6902                !!!cp ('t345');
6903                last INSCOPE;
6904              }
6905            } # INSCOPE
6906              
6907            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6908            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6909              !!!nack ('t346.1');
6910              !!!next-token;
6911              if ($token->{type} == CHARACTER_TOKEN) {
6912                $token->{data} =~ s/^\x0A//;
6913                unless (length $token->{data}) {
6914                  !!!cp ('t346');
6915                !!!next-token;                !!!next-token;
               redo B;  
6916              } else {              } else {
6917                #                !!!cp ('t349');
6918              }              }
6919            } else {            } else {
6920              #              !!!cp ('t348');
6921            }            }
6922            } elsif ($token->{tag_name} eq 'form') {
6923              !!!cp ('t347.1');
6924              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6925    
6926              !!!nack ('t347.2');
6927              !!!next-token;
6928            } elsif ($token->{tag_name} eq 'table') {
6929              !!!cp ('t382');
6930              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6931                        
6932            if (defined $token->{tag_name}) {            $self->{insertion_mode} = IN_TABLE_IM;
6933              !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
6934              !!!nack ('t382.1');
6935              !!!next-token;
6936            } elsif ($token->{tag_name} eq 'hr') {
6937              !!!cp ('t386');
6938              pop @{$self->{open_elements}};
6939            
6940              !!!nack ('t386.1');
6941              !!!next-token;
6942            } else {
6943              !!!nack ('t347.1');
6944              !!!next-token;
6945            }
6946            next B;
6947          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6948            ## has a p element in scope
6949            INSCOPE: for (reverse @{$self->{open_elements}}) {
6950              if ($_->[1] & P_EL) {
6951                !!!cp ('t353');
6952                !!!back-token; # <x>
6953                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6954                          line => $token->{line}, column => $token->{column}};
6955                next B;
6956              } elsif ($_->[1] & SCOPING_EL) {
6957                !!!cp ('t354');
6958                last INSCOPE;
6959              }
6960            } # INSCOPE
6961              
6962            ## Step 1
6963            my $i = -1;
6964            my $node = $self->{open_elements}->[$i];
6965            my $li_or_dtdd = {li => {li => 1},
6966                              dt => {dt => 1, dd => 1},
6967                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6968            LI: {
6969              ## Step 2
6970              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6971                if ($i != -1) {
6972                  !!!cp ('t355');
6973                  !!!parse-error (type => 'not closed',
6974                                  text => $self->{open_elements}->[-1]->[0]
6975                                      ->manakai_local_name,
6976                                  token => $token);
6977                } else {
6978                  !!!cp ('t356');
6979                }
6980                splice @{$self->{open_elements}}, $i;
6981                last LI;
6982            } else {            } else {
6983              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6984            }            }
6985              
6986              ## Step 3
6987              if (not ($node->[1] & FORMATTING_EL) and
6988                  #not $phrasing_category->{$node->[1]} and
6989                  ($node->[1] & SPECIAL_EL or
6990                   $node->[1] & SCOPING_EL) and
6991                  not ($node->[1] & ADDRESS_EL) and
6992                  not ($node->[1] & DIV_EL)) {
6993                !!!cp ('t358');
6994                last LI;
6995              }
6996              
6997              !!!cp ('t359');
6998              ## Step 4
6999              $i--;
7000              $node = $self->{open_elements}->[$i];
7001              redo LI;
7002            } # LI
7003              
7004            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7005            !!!nack ('t359.1');
7006            !!!next-token;
7007            next B;
7008          } elsif ($token->{tag_name} eq 'plaintext') {
7009            ## has a p element in scope
7010            INSCOPE: for (reverse @{$self->{open_elements}}) {
7011              if ($_->[1] & P_EL) {
7012                !!!cp ('t367');
7013                !!!back-token; # <plaintext>
7014                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7015                          line => $token->{line}, column => $token->{column}};
7016                next B;
7017              } elsif ($_->[1] & SCOPING_EL) {
7018                !!!cp ('t368');
7019                last INSCOPE;
7020              }
7021            } # INSCOPE
7022              
7023            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7024              
7025            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7026              
7027            !!!nack ('t368.1');
7028            !!!next-token;
7029            next B;
7030          } elsif ($token->{tag_name} eq 'a') {
7031            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7032              my $node = $active_formatting_elements->[$i];
7033              if ($node->[1] & A_EL) {
7034                !!!cp ('t371');
7035                !!!parse-error (type => 'in a:a', token => $token);
7036                
7037                !!!back-token; # <a>
7038                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7039                          line => $token->{line}, column => $token->{column}};
7040                $formatting_end_tag->($token);
7041                
7042                AFE2: for (reverse 0..$#$active_formatting_elements) {
7043                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7044                    !!!cp ('t372');
7045                    splice @$active_formatting_elements, $_, 1;
7046                    last AFE2;
7047                  }
7048                } # AFE2
7049                OE: for (reverse 0..$#{$self->{open_elements}}) {
7050                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7051                    !!!cp ('t373');
7052                    splice @{$self->{open_elements}}, $_, 1;
7053                    last OE;
7054                  }
7055                } # OE
7056                last AFE;
7057              } elsif ($node->[0] eq '#marker') {
7058                !!!cp ('t374');
7059                last AFE;
7060              }
7061            } # AFE
7062              
7063            $reconstruct_active_formatting_elements->($insert_to_current);
7064    
7065            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7066            push @$active_formatting_elements, $self->{open_elements}->[-1];
7067    
7068            !!!nack ('t374.1');
7069            !!!next-token;
7070            next B;
7071          } elsif ($token->{tag_name} eq 'nobr') {
7072            $reconstruct_active_formatting_elements->($insert_to_current);
7073    
7074            ## has a |nobr| element in scope
7075            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7076              my $node = $self->{open_elements}->[$_];
7077              if ($node->[1] & NOBR_EL) {
7078                !!!cp ('t376');
7079                !!!parse-error (type => 'in nobr:nobr', token => $token);
7080                !!!back-token; # <nobr>
7081                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7082                          line => $token->{line}, column => $token->{column}};
7083                next B;
7084              } elsif ($node->[1] & SCOPING_EL) {
7085                !!!cp ('t377');
7086                last INSCOPE;
7087              }
7088            } # INSCOPE
7089            
7090            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7091            push @$active_formatting_elements, $self->{open_elements}->[-1];
7092            
7093            !!!nack ('t377.1');
7094            !!!next-token;
7095            next B;
7096          } elsif ($token->{tag_name} eq 'button') {
7097            ## has a button element in scope
7098            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7099              my $node = $self->{open_elements}->[$_];
7100              if ($node->[1] & BUTTON_EL) {
7101                !!!cp ('t378');
7102                !!!parse-error (type => 'in button:button', token => $token);
7103                !!!back-token; # <button>
7104                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7105                          line => $token->{line}, column => $token->{column}};
7106                next B;
7107              } elsif ($node->[1] & SCOPING_EL) {
7108                !!!cp ('t379');
7109                last INSCOPE;
7110              }
7111            } # INSCOPE
7112              
7113            $reconstruct_active_formatting_elements->($insert_to_current);
7114              
7115            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7116    
7117            ## TODO: associate with $self->{form_element} if defined
7118    
7119            push @$active_formatting_elements, ['#marker', ''];
7120    
7121            !!!nack ('t379.1');
7122            !!!next-token;
7123            next B;
7124          } elsif ({
7125                    xmp => 1,
7126                    iframe => 1,
7127                    noembed => 1,
7128                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7129                    noscript => 0, ## TODO: 1 if scripting is enabled
7130                   }->{$token->{tag_name}}) {
7131            if ($token->{tag_name} eq 'xmp') {
7132              !!!cp ('t381');
7133              $reconstruct_active_formatting_elements->($insert_to_current);
7134            } else {
7135              !!!cp ('t399');
7136            }
7137            ## NOTE: There is an "as if in body" code clone.
7138            $parse_rcdata->(CDATA_CONTENT_MODEL);
7139            next B;
7140          } elsif ($token->{tag_name} eq 'isindex') {
7141            !!!parse-error (type => 'isindex', token => $token);
7142            
7143            if (defined $self->{form_element}) {
7144              !!!cp ('t389');
7145            ## Ignore the token            ## Ignore the token
7146              !!!nack ('t389'); ## NOTE: Not acknowledged.
7147            !!!next-token;            !!!next-token;
7148            redo B;            next B;
7149            } else {
7150              !!!ack ('t391.1');
7151    
7152            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7153              my $form_attrs;
7154              $form_attrs->{action} = $at->{action} if $at->{action};
7155              my $prompt_attr = $at->{prompt};
7156              $at->{name} = {name => 'name', value => 'isindex'};
7157              delete $at->{action};
7158              delete $at->{prompt};
7159              my @tokens = (
7160                            {type => START_TAG_TOKEN, tag_name => 'form',
7161                             attributes => $form_attrs,
7162                             line => $token->{line}, column => $token->{column}},
7163                            {type => START_TAG_TOKEN, tag_name => 'hr',
7164                             line => $token->{line}, column => $token->{column}},
7165                            {type => START_TAG_TOKEN, tag_name => 'p',
7166                             line => $token->{line}, column => $token->{column}},
7167                            {type => START_TAG_TOKEN, tag_name => 'label',
7168                             line => $token->{line}, column => $token->{column}},
7169                           );
7170              if ($prompt_attr) {
7171                !!!cp ('t390');
7172                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7173                               #line => $token->{line}, column => $token->{column},
7174                              };
7175              } else {
7176                !!!cp ('t391');
7177                push @tokens, {type => CHARACTER_TOKEN,
7178                               data => 'This is a searchable index. Insert your search keywords here: ',
7179                               #line => $token->{line}, column => $token->{column},
7180                              }; # SHOULD
7181                ## TODO: make this configurable
7182              }
7183              push @tokens,
7184                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7185                             line => $token->{line}, column => $token->{column}},
7186                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7187                            {type => END_TAG_TOKEN, tag_name => 'label',
7188                             line => $token->{line}, column => $token->{column}},
7189                            {type => END_TAG_TOKEN, tag_name => 'p',
7190                             line => $token->{line}, column => $token->{column}},
7191                            {type => START_TAG_TOKEN, tag_name => 'hr',
7192                             line => $token->{line}, column => $token->{column}},
7193                            {type => END_TAG_TOKEN, tag_name => 'form',
7194                             line => $token->{line}, column => $token->{column}};
7195              !!!back-token (@tokens);
7196              !!!next-token;
7197              next B;
7198            }
7199          } elsif ($token->{tag_name} eq 'textarea') {
7200            my $tag_name = $token->{tag_name};
7201            my $el;
7202            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7203            
7204            ## TODO: $self->{form_element} if defined
7205            $self->{content_model} = RCDATA_CONTENT_MODEL;
7206            delete $self->{escape}; # MUST
7207            
7208            $insert->($el);
7209            
7210            my $text = '';
7211            !!!nack ('t392.1');
7212            !!!next-token;
7213            if ($token->{type} == CHARACTER_TOKEN) {
7214              $token->{data} =~ s/^\x0A//;
7215              unless (length $token->{data}) {
7216                !!!cp ('t392');
7217                !!!next-token;
7218              } else {
7219                !!!cp ('t393');
7220              }
7221          } else {          } else {
7222            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7223            }
7224            while ($token->{type} == CHARACTER_TOKEN) {
7225              !!!cp ('t395');
7226              $text .= $token->{data};
7227              !!!next-token;
7228            }
7229            if (length $text) {
7230              !!!cp ('t396');
7231              $el->manakai_append_text ($text);
7232            }
7233            
7234            $self->{content_model} = PCDATA_CONTENT_MODEL;
7235            
7236            if ($token->{type} == END_TAG_TOKEN and
7237                $token->{tag_name} eq $tag_name) {
7238              !!!cp ('t397');
7239              ## Ignore the token
7240            } else {
7241              !!!cp ('t398');
7242              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7243          }          }
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
7244          !!!next-token;          !!!next-token;
7245          redo B;          next B;
7246        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7247          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7248          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7249            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7250              my $node = $self->{open_elements}->[$_];
7251              if ($node->[1] & RUBY_EL) {
7252                !!!cp ('t398.1');
7253                ## generate implied end tags
7254                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7255                  !!!cp ('t398.2');
7256                  pop @{$self->{open_elements}};
7257                }
7258                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7259                  !!!cp ('t398.3');
7260                  !!!parse-error (type => 'not closed',
7261                                  text => $self->{open_elements}->[-1]->[0]
7262                                      ->manakai_local_name,
7263                                  token => $token);
7264                  pop @{$self->{open_elements}}
7265                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7266                }
7267                last INSCOPE;
7268              } elsif ($node->[1] & SCOPING_EL) {
7269                !!!cp ('t398.4');
7270                last INSCOPE;
7271              }
7272            } # INSCOPE
7273    
7274            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7275    
7276            !!!nack ('t398.5');
7277          !!!next-token;          !!!next-token;
7278          redo B;          redo B;
7279        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7280          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7281            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7282            ## As if in the main phase.  
7283            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7284            ## just before the phase has been changed to the trailing  
7285            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7286            $reconstruct_active_formatting_elements->($insert_to_current)  
7287              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7288            
7289            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7290            
7291            if ($self->{self_closing}) {
7292              pop @{$self->{open_elements}};
7293              !!!ack ('t398.1');
7294            } else {
7295              !!!cp ('t398.2');
7296              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7297              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7298              ## mode, "in body" (not "in foreign content") secondary insertion
7299              ## mode, maybe.
7300            }
7301    
7302            !!!next-token;
7303            next B;
7304          } elsif ({
7305                    caption => 1, col => 1, colgroup => 1, frame => 1,
7306                    frameset => 1, head => 1, option => 1, optgroup => 1,
7307                    tbody => 1, td => 1, tfoot => 1, th => 1,
7308                    thead => 1, tr => 1,
7309                   }->{$token->{tag_name}}) {
7310            !!!cp ('t401');
7311            !!!parse-error (type => 'in body',
7312                            text => $token->{tag_name}, token => $token);
7313            ## Ignore the token
7314            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7315            !!!next-token;
7316            next B;
7317            
7318            ## ISSUE: An issue on HTML5 new elements in the spec.
7319          } else {
7320            if ($token->{tag_name} eq 'image') {
7321              !!!cp ('t384');
7322              !!!parse-error (type => 'image', token => $token);
7323              $token->{tag_name} = 'img';
7324            } else {
7325              !!!cp ('t385');
7326            }
7327    
7328            ## NOTE: There is an "as if <br>" code clone.
7329            $reconstruct_active_formatting_elements->($insert_to_current);
7330            
7331            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7332    
7333            if ({
7334                 applet => 1, marquee => 1, object => 1,
7335                }->{$token->{tag_name}}) {
7336              !!!cp ('t380');
7337              push @$active_formatting_elements, ['#marker', ''];
7338              !!!nack ('t380.1');
7339            } elsif ({
7340                      b => 1, big => 1, em => 1, font => 1, i => 1,
7341                      s => 1, small => 1, strile => 1,
7342                      strong => 1, tt => 1, u => 1,
7343                     }->{$token->{tag_name}}) {
7344              !!!cp ('t375');
7345              push @$active_formatting_elements, $self->{open_elements}->[-1];
7346              !!!nack ('t375.1');
7347            } elsif ($token->{tag_name} eq 'input') {
7348              !!!cp ('t388');
7349              ## TODO: associate with $self->{form_element} if defined
7350              pop @{$self->{open_elements}};
7351              !!!ack ('t388.2');
7352            } elsif ({
7353                      area => 1, basefont => 1, bgsound => 1, br => 1,
7354                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7355                      #image => 1,
7356                     }->{$token->{tag_name}}) {
7357              !!!cp ('t388.1');
7358              pop @{$self->{open_elements}};
7359              !!!ack ('t388.3');
7360            } elsif ($token->{tag_name} eq 'select') {
7361              ## TODO: associate with $self->{form_element} if defined
7362            
7363              if ($self->{insertion_mode} & TABLE_IMS or
7364                  $self->{insertion_mode} & BODY_TABLE_IMS or
7365                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7366                !!!cp ('t400.1');
7367                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7368              } else {
7369                !!!cp ('t400.2');
7370                $self->{insertion_mode} = IN_SELECT_IM;
7371              }
7372              !!!nack ('t400.3');
7373            } else {
7374              !!!nack ('t402');
7375            }
7376            
7377            !!!next-token;
7378            next B;
7379          }
7380        } elsif ($token->{type} == END_TAG_TOKEN) {
7381          if ($token->{tag_name} eq 'body') {
7382            ## has a |body| element in scope
7383            my $i;
7384            INSCOPE: {
7385              for (reverse @{$self->{open_elements}}) {
7386                if ($_->[1] & BODY_EL) {
7387                  !!!cp ('t405');
7388                  $i = $_;
7389                  last INSCOPE;
7390                } elsif ($_->[1] & SCOPING_EL) {
7391                  !!!cp ('t405.1');
7392                  last;
7393                }
7394              }
7395    
7396              !!!parse-error (type => 'start tag not allowed',
7397                              text => $token->{tag_name}, token => $token);
7398              ## NOTE: Ignore the token.
7399              !!!next-token;
7400              next B;
7401            } # INSCOPE
7402    
7403            for (@{$self->{open_elements}}) {
7404              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7405                !!!cp ('t403');
7406                !!!parse-error (type => 'not closed',
7407                                text => $_->[0]->manakai_local_name,
7408                                token => $token);
7409                last;
7410              } else {
7411                !!!cp ('t404');
7412              }
7413            }
7414    
7415            $self->{insertion_mode} = AFTER_BODY_IM;
7416            !!!next-token;
7417            next B;
7418          } elsif ($token->{tag_name} eq 'html') {
7419            ## TODO: Update this code.  It seems that the code below is not
7420            ## up-to-date, though it has same effect as speced.
7421            if (@{$self->{open_elements}} > 1 and
7422                $self->{open_elements}->[1]->[1] & BODY_EL) {
7423              ## ISSUE: There is an issue in the spec.
7424              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7425                !!!cp ('t406');
7426                !!!parse-error (type => 'not closed',
7427                                text => $self->{open_elements}->[1]->[0]
7428                                    ->manakai_local_name,
7429                                token => $token);
7430              } else {
7431                !!!cp ('t407');
7432              }
7433              $self->{insertion_mode} = AFTER_BODY_IM;
7434              ## reprocess
7435              next B;
7436            } else {
7437              !!!cp ('t408');
7438              !!!parse-error (type => 'unmatched end tag',
7439                              text => $token->{tag_name}, token => $token);
7440              ## Ignore the token
7441              !!!next-token;
7442              next B;
7443            }
7444          } elsif ({
7445                    address => 1, blockquote => 1, center => 1, dir => 1,
7446                    div => 1, dl => 1, fieldset => 1, listing => 1,
7447                    menu => 1, ol => 1, pre => 1, ul => 1,
7448                    dd => 1, dt => 1, li => 1,
7449                    applet => 1, button => 1, marquee => 1, object => 1,
7450                   }->{$token->{tag_name}}) {
7451            ## has an element in scope
7452            my $i;
7453            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7454              my $node = $self->{open_elements}->[$_];
7455              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7456                !!!cp ('t410');
7457                $i = $_;
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                !!!cp ('t411');
7461                last INSCOPE;
7462              }
7463            } # INSCOPE
7464    
7465            unless (defined $i) { # has an element in scope
7466              !!!cp ('t413');
7467              !!!parse-error (type => 'unmatched end tag',
7468                              text => $token->{tag_name}, token => $token);
7469              ## NOTE: Ignore the token.
7470            } else {
7471              ## Step 1. generate implied end tags
7472              while ({
7473                      ## END_TAG_OPTIONAL_EL
7474                      dd => ($token->{tag_name} ne 'dd'),
7475                      dt => ($token->{tag_name} ne 'dt'),
7476                      li => ($token->{tag_name} ne 'li'),
7477                      p => 1,
7478                      rt => 1,
7479                      rp => 1,
7480                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7481                !!!cp ('t409');
7482                pop @{$self->{open_elements}};
7483              }
7484    
7485              ## Step 2.
7486              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7487                      ne $token->{tag_name}) {
7488                !!!cp ('t412');
7489                !!!parse-error (type => 'not closed',
7490                                text => $self->{open_elements}->[-1]->[0]
7491                                    ->manakai_local_name,
7492                                token => $token);
7493              } else {
7494                !!!cp ('t414');
7495              }
7496    
7497              ## Step 3.
7498              splice @{$self->{open_elements}}, $i;
7499    
7500              ## Step 4.
7501              $clear_up_to_marker->()
7502                  if {
7503                    applet => 1, button => 1, marquee => 1, object => 1,
7504                  }->{$token->{tag_name}};
7505            }
7506            !!!next-token;
7507            next B;
7508          } elsif ($token->{tag_name} eq 'form') {
7509            undef $self->{form_element};
7510    
7511            ## has an element in scope
7512            my $i;
7513            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7514              my $node = $self->{open_elements}->[$_];
7515              if ($node->[1] & FORM_EL) {
7516                !!!cp ('t418');
7517                $i = $_;
7518                last INSCOPE;
7519              } elsif ($node->[1] & SCOPING_EL) {
7520                !!!cp ('t419');
7521                last INSCOPE;
7522              }
7523            } # INSCOPE
7524    
7525            unless (defined $i) { # has an element in scope
7526              !!!cp ('t421');
7527              !!!parse-error (type => 'unmatched end tag',
7528                              text => $token->{tag_name}, token => $token);
7529              ## NOTE: Ignore the token.
7530            } else {
7531              ## Step 1. generate implied end tags
7532              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7533                !!!cp ('t417');
7534                pop @{$self->{open_elements}};
7535              }
7536                        
7537            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7538              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7539                      ne $token->{tag_name}) {
7540                !!!cp ('t417.1');
7541                !!!parse-error (type => 'not closed',
7542                                text => $self->{open_elements}->[-1]->[0]
7543                                    ->manakai_local_name,
7544                                token => $token);
7545              } else {
7546                !!!cp ('t420');
7547              }  
7548                        
7549            unless (length $token->{data}) {            ## Step 3.
7550              !!!next-token;            splice @{$self->{open_elements}}, $i;
7551              redo B;          }
7552    
7553            !!!next-token;
7554            next B;
7555          } elsif ({
7556                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7557                   }->{$token->{tag_name}}) {
7558            ## has an element in scope
7559            my $i;
7560            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7561              my $node = $self->{open_elements}->[$_];
7562              if ($node->[1] & HEADING_EL) {
7563                !!!cp ('t423');
7564                $i = $_;
7565                last INSCOPE;
7566              } elsif ($node->[1] & SCOPING_EL) {
7567                !!!cp ('t424');
7568                last INSCOPE;
7569              }
7570            } # INSCOPE
7571    
7572            unless (defined $i) { # has an element in scope
7573              !!!cp ('t425.1');
7574              !!!parse-error (type => 'unmatched end tag',
7575                              text => $token->{tag_name}, token => $token);
7576              ## NOTE: Ignore the token.
7577            } else {
7578              ## Step 1. generate implied end tags
7579              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7580                !!!cp ('t422');
7581                pop @{$self->{open_elements}};
7582              }
7583              
7584              ## Step 2.
7585              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7586                      ne $token->{tag_name}) {
7587                !!!cp ('t425');
7588                !!!parse-error (type => 'unmatched end tag',
7589                                text => $token->{tag_name}, token => $token);
7590              } else {
7591                !!!cp ('t426');
7592              }
7593    
7594              ## Step 3.
7595              splice @{$self->{open_elements}}, $i;
7596            }
7597            
7598            !!!next-token;
7599            next B;
7600          } elsif ($token->{tag_name} eq 'p') {
7601            ## has an element in scope
7602            my $i;
7603            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7604              my $node = $self->{open_elements}->[$_];
7605              if ($node->[1] & P_EL) {
7606                !!!cp ('t410.1');
7607                $i = $_;
7608                last INSCOPE;
7609              } elsif ($node->[1] & SCOPING_EL) {
7610                !!!cp ('t411.1');
7611                last INSCOPE;
7612            }            }
7613            } # INSCOPE
7614    
7615            if (defined $i) {
7616              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7617                      ne $token->{tag_name}) {
7618                !!!cp ('t412.1');
7619                !!!parse-error (type => 'not closed',
7620                                text => $self->{open_elements}->[-1]->[0]
7621                                    ->manakai_local_name,
7622                                token => $token);
7623              } else {
7624                !!!cp ('t414.1');
7625              }
7626    
7627              splice @{$self->{open_elements}}, $i;
7628            } else {
7629              !!!cp ('t413.1');
7630              !!!parse-error (type => 'unmatched end tag',
7631                              text => $token->{tag_name}, token => $token);
7632    
7633              !!!cp ('t415.1');
7634              ## As if <p>, then reprocess the current token
7635              my $el;
7636              !!!create-element ($el, $HTML_NS, 'p',, $token);
7637              $insert->($el);
7638              ## NOTE: Not inserted into |$self->{open_elements}|.
7639          }          }
7640    
7641          !!!parse-error (type => 'after html:#character');          !!!next-token;
7642          $phase = 'main';          next B;
7643          ## reprocess        } elsif ({
7644          redo B;                  a => 1,
7645        } elsif ($token->{type} eq 'start tag' or                  b => 1, big => 1, em => 1, font => 1, i => 1,
7646                 $token->{type} eq 'end tag') {                  nobr => 1, s => 1, small => 1, strile => 1,
7647          !!!parse-error (type => 'after html:'.$token->{tag_name});                  strong => 1, tt => 1, u => 1,
7648          $phase = 'main';                 }->{$token->{tag_name}}) {
7649          ## reprocess          !!!cp ('t427');
7650          redo B;          $formatting_end_tag->($token);
7651        } elsif ($token->{type} eq 'end-of-file') {          next B;
7652          ## Stop parsing        } elsif ($token->{tag_name} eq 'br') {
7653          last B;          !!!cp ('t428');
7654            !!!parse-error (type => 'unmatched end tag',
7655                            text => 'br', token => $token);
7656    
7657            ## As if <br>
7658            $reconstruct_active_formatting_elements->($insert_to_current);
7659            
7660            my $el;
7661            !!!create-element ($el, $HTML_NS, 'br',, $token);
7662            $insert->($el);
7663            
7664            ## Ignore the token.
7665            !!!next-token;
7666            next B;
7667          } elsif ({
7668                    caption => 1, col => 1, colgroup => 1, frame => 1,
7669                    frameset => 1, head => 1, option => 1, optgroup => 1,
7670                    tbody => 1, td => 1, tfoot => 1, th => 1,
7671                    thead => 1, tr => 1,
7672                    area => 1, basefont => 1, bgsound => 1,
7673                    embed => 1, hr => 1, iframe => 1, image => 1,
7674                    img => 1, input => 1, isindex => 1, noembed => 1,
7675                    noframes => 1, param => 1, select => 1, spacer => 1,
7676                    table => 1, textarea => 1, wbr => 1,
7677                    noscript => 0, ## TODO: if scripting is enabled
7678                   }->{$token->{tag_name}}) {
7679            !!!cp ('t429');
7680            !!!parse-error (type => 'unmatched end tag',
7681                            text => $token->{tag_name}, token => $token);
7682            ## Ignore the token
7683            !!!next-token;
7684            next B;
7685            
7686            ## ISSUE: Issue on HTML5 new elements in spec
7687            
7688        } else {        } else {
7689          die "$0: $token->{type}: Unknown token";          ## Step 1
7690            my $node_i = -1;
7691            my $node = $self->{open_elements}->[$node_i];
7692    
7693            ## Step 2
7694            S2: {
7695              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7696                ## Step 1
7697                ## generate implied end tags
7698                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7699                  !!!cp ('t430');
7700                  ## NOTE: |<ruby><rt></ruby>|.
7701                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7702                  ## which seems wrong.
7703                  pop @{$self->{open_elements}};
7704                  $node_i++;
7705                }
7706            
7707                ## Step 2
7708                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7709                        ne $token->{tag_name}) {
7710                  !!!cp ('t431');
7711                  ## NOTE: <x><y></x>
7712                  !!!parse-error (type => 'not closed',
7713                                  text => $self->{open_elements}->[-1]->[0]
7714                                      ->manakai_local_name,
7715                                  token => $token);
7716                } else {
7717                  !!!cp ('t432');
7718                }
7719                
7720                ## Step 3
7721                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7722    
7723                !!!next-token;
7724                last S2;
7725              } else {
7726                ## Step 3
7727                if (not ($node->[1] & FORMATTING_EL) and
7728                    #not $phrasing_category->{$node->[1]} and
7729                    ($node->[1] & SPECIAL_EL or
7730                     $node->[1] & SCOPING_EL)) {
7731                  !!!cp ('t433');
7732                  !!!parse-error (type => 'unmatched end tag',
7733                                  text => $token->{tag_name}, token => $token);
7734                  ## Ignore the token
7735                  !!!next-token;
7736                  last S2;
7737                }
7738    
7739                !!!cp ('t434');
7740              }
7741              
7742              ## Step 4
7743              $node_i--;
7744              $node = $self->{open_elements}->[$node_i];
7745              
7746              ## Step 5;
7747              redo S2;
7748            } # S2
7749            next B;
7750        }        }
7751      }      }
7752        next B;
7753      } continue { # B
7754        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7755          ## NOTE: The code below is executed in cases where it does not have
7756          ## to be, but it it is harmless even in those cases.
7757          ## has an element in scope
7758          INSCOPE: {
7759            for (reverse 0..$#{$self->{open_elements}}) {
7760              my $node = $self->{open_elements}->[$_];
7761              if ($node->[1] & FOREIGN_EL) {
7762                last INSCOPE;
7763              } elsif ($node->[1] & SCOPING_EL) {
7764                last;
7765              }
7766            }
7767            
7768            ## NOTE: No foreign element in scope.
7769            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7770          } # INSCOPE
7771        }
7772    } # B    } # B
7773    
7774    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5088  sub _tree_construction_main ($) { Line 7776  sub _tree_construction_main ($) {
7776    ## TODO: script stuffs    ## TODO: script stuffs
7777  } # _tree_construct_main  } # _tree_construct_main
7778    
7779  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7780    my $class = shift;    my $class = shift;
7781    my $node = shift;    my $node = shift;
7782    my $s = \$_[0];    #my $s = \$_[0];
7783    my $onerror = $_[1];    my $onerror = $_[1];
7784      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7785    
7786      ## ISSUE: Should {confident} be true?
7787    
7788    my $nt = $node->node_type;    my $nt = $node->node_type;
7789    if ($nt == 9) {    if ($nt == 9) {
# Line 5109  sub set_inner_html ($$$) { Line 7800  sub set_inner_html ($$$) {
7800      }      }
7801    
7802      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7803      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7804    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7805      ## TODO: If non-html element      ## TODO: If non-html element
7806    
7807      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7808    
7809    ## TODO: Support for $get_wrapper
7810    
7811      ## Step 1 # MUST      ## Step 1 # MUST
7812      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7813      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5122  sub set_inner_html ($$$) { Line 7815  sub set_inner_html ($$$) {
7815      my $p = $class->new;      my $p = $class->new;
7816      $p->{document} = $doc;      $p->{document} = $doc;
7817    
7818      ## Step 9 # MUST      ## Step 8 # MUST
7819      my $i = 0;      my $i = 0;
7820      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7821      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7822      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7823        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7824        $input = $get_wrapper->($input);
7825        $p->{set_next_char} = sub {
7826        my $self = shift;        my $self = shift;
7827    
7828        pop @{$self->{prev_input_character}};        my $char = '';
7829        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_next_char}) {
7830            $char = $self->{next_next_char};
7831            delete $self->{next_next_char};
7832            $self->{next_char} = ord $char;
7833          } else {
7834            $self->{char_buffer} = '';
7835            $self->{char_buffer_pos} = 0;
7836            
7837            my $count = $input->manakai_read_until
7838                ($self->{char_buffer},
7839                 qr/(?![\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7840                   $self->{char_buffer_pos});
7841            if ($count) {
7842              $self->{line_prev} = $self->{line};
7843              $self->{column_prev} = $self->{column};
7844              $self->{column}++;
7845              $self->{next_char}
7846                  = ord substr ($self->{char_buffer},
7847                                $self->{char_buffer_pos}++, 1);
7848              return;
7849            }
7850            
7851            if ($input->read ($char, 1)) {
7852              $self->{next_char} = ord $char;
7853            } else {
7854              $self->{next_char} = -1;
7855              return;
7856            }
7857          }
7858    
7859        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7860        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7861        $column++;  
7862          if ($self->{next_char} == 0x000A) { # LF
7863        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7864          $line++;          $p->{column} = 0;
7865          $column = 0;          !!!cp ('i1');
7866        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7867          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7868          $self->{next_input_character} = 0x000A; # LF # MUST          my $next = '';
7869          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7870          $column = 0;            $self->{next_next_char} = $next;
7871        } elsif ($self->{next_input_character} > 0x10FFFF) {          }
7872          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0x000A; # LF # MUST
7873        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $p->{line}++;
7874            $p->{column} = 0;
7875            !!!cp ('i2');
7876          } elsif ($self->{next_char} > 0x10FFFF) {
7877            $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7878            !!!cp ('i3');
7879          } elsif ($self->{next_char} == 0x0000) { # NULL
7880            !!!cp ('i4');
7881          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7882          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7883          } elsif ($disallowed_control_chars->{$self->{next_char}}) {
7884            !!!cp ('i4.1');
7885            if ($self->{next_char} < 0x10000) {
7886              !!!parse-error (type => 'control char',
7887                              text => (sprintf 'U+%04X', $self->{next_char}));
7888            } else {
7889              !!!parse-error (type => 'control char',
7890                              text => (sprintf 'U-%08X', $self->{next_char}));
7891            }
7892        }        }
7893      };      };
7894      $p->{prev_input_character} = [-1, -1, -1];  
7895      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7896              #my ($scalar, $specials_range, $offset) = @_;
7897          return 0 if defined $p->{next_next_char};
7898    
7899          my $pattern = qr/(?![$_[1]\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/;
7900          my $offset = $_[2] || 0;
7901          
7902          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7903            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7904            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7905              substr ($_[0], $offset)
7906                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7907              my $count = $+[0] - $-[0];
7908              if ($count) {
7909                $p->{column} += $count;
7910                $p->{char_buffer_pos} += $count;
7911                $p->{line_prev} = $p->{line};
7912                $p->{column_prev} = $p->{column} - 1;
7913                $p->{prev_char} = [-1, -1, -1];
7914                $p->{next_char} = -1;
7915              }
7916              return $count;
7917            } else {
7918              return 0;
7919            }
7920          } else {
7921            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7922            if ($count) {
7923              $p->{column} += $count;
7924              $p->{column_prev} += $count;
7925              $p->{prev_char} = [-1, -1, -1];
7926              $p->{next_char} = -1;
7927            }
7928            return $count;
7929          }
7930        }; # $p->{read_until}
7931    
7932      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7933        my (%opt) = @_;        my (%opt) = @_;
7934        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7935          my $column = $opt{column};
7936          if (defined $opt{token} and defined $opt{token}->{line}) {
7937            $line = $opt{token}->{line};
7938            $column = $opt{token}->{column};
7939          }
7940          warn "Parse error ($opt{type}) at line $line column $column\n";
7941      };      };
7942      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7943        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7944      };      };
7945            
7946        my $char_onerror = sub {
7947          my (undef, $type, %opt) = @_;
7948          $ponerror->(layer => 'encode',
7949                      line => $p->{line}, column => $p->{column} + 1,
7950                      %opt, type => $type);
7951        }; # $char_onerror
7952        $input->onerror ($char_onerror);
7953    
7954      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7955      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7956    
7957      ## Step 2      ## Step 2
7958      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7959      $p->{content_model_flag} = {      $p->{content_model} = {
7960        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7961        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7962        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7963        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7964        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7965        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7966        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7967        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7968        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7969        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7970      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7971         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7972            unless defined $p->{content_model};
7973            ## ISSUE: What is "the name of the element"? local name?
7974    
7975      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7976          ## TODO: Foreign element OK?
7977    
7978      ## Step 4      ## Step 3
7979      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7980        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7981    
7982      ## Step 5 # MUST      ## Step 4 # MUST
7983      $doc->append_child ($root);      $doc->append_child ($root);
7984    
7985      ## Step 6 # MUST      ## Step 5 # MUST
7986      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7987    
7988      undef $p->{head_element};      undef $p->{head_element};
7989    
7990      ## Step 7 # MUST      ## Step 6 # MUST
7991      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7992    
7993      ## Step 8 # MUST      ## Step 7 # MUST
7994      my $anode = $node;      my $anode = $node;
7995      AN: while (defined $anode) {      AN: while (defined $anode) {
7996        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7997          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7998          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7999            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
8000                !!!cp ('i5');
8001              $p->{form_element} = $anode;              $p->{form_element} = $anode;
8002              last AN;              last AN;
8003            }            }
# Line 5213  sub set_inner_html ($$$) { Line 8006  sub set_inner_html ($$$) {
8006        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8007      } # AN      } # AN
8008            
8009      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8010      {      {
8011        my $self = $p;        my $self = $p;
8012        !!!next-token;        !!!next-token;
8013      }      }
8014      $p->_tree_construction_main;      $p->_tree_construction_main;
8015    
8016      ## Step 11 # MUST      ## Step 10 # MUST
8017      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8018      for (@cn) {      for (@cn) {
8019        $node->remove_child ($_);        $node->remove_child ($_);
8020      }      }
8021      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8022    
8023      ## Step 12 # MUST      ## Step 11 # MUST
8024      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8025      for (@cn) {      for (@cn) {
8026        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5237  sub set_inner_html ($$$) { Line 8029  sub set_inner_html ($$$) {
8029      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8030    
8031      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8032    
8033        delete $p->{parse_error}; # delete loop
8034    } else {    } else {
8035      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";
8036    }    }
# Line 5244  sub set_inner_html ($$$) { Line 8038  sub set_inner_html ($$$) {
8038    
8039  } # tree construction stage  } # tree construction stage
8040    
8041  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8042    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 = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $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};  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 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  
8043    
8044  1;  1;
8045  # $Date$  # $Date$

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.181

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24